Skip to content

Commit

Permalink
Feature add type (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
heavenshell authored Apr 25, 2020
1 parent 6ce6113 commit fe44754
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/parsers/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isClassDeclaration,
isInterfaceDeclaration,
isFunctionDeclaration,
isTypeAliasDeclaration,
Node as TsNode,
ScriptKind,
ScriptTarget,
Expand All @@ -16,6 +17,7 @@ import {
import { getClassLikeDoc } from './classes'
import { getVariableDocFromExpression } from './expressions'
import { getFunctionDoc } from './functions'
import { getTypeDoc } from './types'
import { getVariableDoc } from './variables'

import { DocProps, ParseProps } from '../../types'
Expand Down Expand Up @@ -119,6 +121,17 @@ export const parse = ({
}
}
break
case SyntaxKind.TypeAliasDeclaration:
if (node.hasOwnProperty('jsDoc')) {
break
}
if (isTypeAliasDeclaration(node)) {
const typeDoc = getTypeDoc(node, source)
typeDoc.type = 'interface'
docs.push(typeDoc)
return
}
break
case SyntaxKind.InterfaceDeclaration:
if (node.hasOwnProperty('jsDoc')) {
break
Expand Down
59 changes: 59 additions & 0 deletions src/parsers/ts/types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
createSourceFile,
ScriptKind,
ScriptTarget,
TypeAliasDeclaration,
} from 'typescript'

import { getTypeDoc } from './types'

const getAcual = (code: string) => {
const source = createSourceFile(
'lehre.ts',
code,
ScriptTarget.ESNext,
false,
ScriptKind.TS
)
const actual = getTypeDoc(
source.statements[0] as TypeAliasDeclaration,
source
)
return actual
}

describe('types', () => {
it('getTypeDoc', () => {
const code = `
type Foo = {
}`
const actual = getAcual(code)

expect(actual).toEqual({
name: 'Foo',
type: 'function',
start: { line: 1, column: 4 },
end: { line: 2, column: 5 },
methods: [],
heritageClauses: [],
})
})

it('getTypeDoc - with props', () => {
const code = `
type Foo = {
foo: string
bar: () => void
}`
const actual = getAcual(code)

expect(actual).toEqual({
name: 'Foo',
type: 'function',
start: { line: 1, column: 4 },
end: { line: 4, column: 5 },
methods: [],
heritageClauses: [],
})
})
})
17 changes: 17 additions & 0 deletions src/parsers/ts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TypeAliasDeclaration, SourceFile } from 'typescript'

export const getTypeDoc = (node: TypeAliasDeclaration, source: SourceFile) => {
const start = source.getLineAndCharacterOfPosition(node.getStart(source))
const end = source.getLineAndCharacterOfPosition(node.getEnd())

const doc = {
name: node.name ? node.name.text : '',
type: 'function',
start: { line: start.line, column: start.character },
end: { line: end.line, column: end.character },
methods: [],
heritageClauses: [],
}

return doc
}

0 comments on commit fe44754

Please sign in to comment.