-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ce6113
commit fe44754
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |