diff --git a/src/declarations/Declaration.ts b/src/declarations/Declaration.ts index adc7bc1..f685513 100644 --- a/src/declarations/Declaration.ts +++ b/src/declarations/Declaration.ts @@ -209,3 +209,20 @@ export interface StaticDeclaration extends Declaration { */ isStatic: boolean; } + +/** + * Interface for possible async declarations. + * + * @export + * @interface AsyncDeclaration + * @extends {Declaration} + */ +export interface AsyncDeclaration extends Declaration { + /** + * Defines if the declaration is async or not. + * + * @type {boolean} + * @memberof AsyncDeclaration + */ + isAsync: boolean; +} diff --git a/src/declarations/FunctionDeclaration.ts b/src/declarations/FunctionDeclaration.ts index f4b6a18..99015ff 100644 --- a/src/declarations/FunctionDeclaration.ts +++ b/src/declarations/FunctionDeclaration.ts @@ -1,4 +1,4 @@ -import { CallableDeclaration, ExportableDeclaration } from './Declaration'; +import { AsyncDeclaration, CallableDeclaration, ExportableDeclaration } from './Declaration'; import { ParameterDeclaration } from './ParameterDeclaration'; import { VariableDeclaration } from './VariableDeclaration'; @@ -11,13 +11,14 @@ import { VariableDeclaration } from './VariableDeclaration'; * @implements {CallableDeclaration} * @implements {ExportableDeclaration} */ -export class FunctionDeclaration implements CallableDeclaration, ExportableDeclaration { +export class FunctionDeclaration implements AsyncDeclaration, CallableDeclaration, ExportableDeclaration { public parameters: ParameterDeclaration[] = []; public variables: VariableDeclaration[] = []; constructor( public name: string, public isExported: boolean, + public isAsync: boolean, public type?: string, public start?: number, public end?: number, diff --git a/src/declarations/MethodDeclaration.ts b/src/declarations/MethodDeclaration.ts index a5cd267..2a4bc67 100644 --- a/src/declarations/MethodDeclaration.ts +++ b/src/declarations/MethodDeclaration.ts @@ -1,5 +1,6 @@ import { AbstractDeclaration, + AsyncDeclaration, CallableDeclaration, OptionalDeclaration, ScopedDeclaration, @@ -22,6 +23,7 @@ import { VariableDeclaration } from './VariableDeclaration'; */ export class MethodDeclaration implements AbstractDeclaration, + AsyncDeclaration, CallableDeclaration, OptionalDeclaration, ScopedDeclaration, @@ -38,6 +40,7 @@ export class MethodDeclaration implements public type: string | undefined, public isOptional: boolean, public isStatic: boolean, + public isAsync: boolean, public start?: number, public end?: number, ) { } diff --git a/src/node-parser/class-parser.ts b/src/node-parser/class-parser.ts index 9c9d1cc..d0cedd3 100644 --- a/src/node-parser/class-parser.ts +++ b/src/node-parser/class-parser.ts @@ -204,6 +204,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void { getNodeType(o.type), !!o.questionToken, containsModifier(o, SyntaxKind.StaticKeyword), + containsModifier(o, SyntaxKind.AsyncKeyword), o.getStart(), o.getEnd(), ); diff --git a/src/node-parser/function-parser.ts b/src/node-parser/function-parser.ts index fb10c5d..2f6a8b2 100644 --- a/src/node-parser/function-parser.ts +++ b/src/node-parser/function-parser.ts @@ -30,7 +30,13 @@ import { isPropertySignature, } from '../type-guards/TypescriptGuards'; import { parseIdentifier } from './identifier-parser'; -import { getDefaultResourceIdentifier, getNodeType, isNodeDefaultExported, isNodeExported } from './parse-utilities'; +import { + containsModifier, + getDefaultResourceIdentifier, + getNodeType, + isNodeDefaultExported, + isNodeExported, +} from './parse-utilities'; import { parseVariable } from './variable-parser'; /** @@ -137,7 +143,12 @@ export function parseMethodParams( export function parseFunction(resource: Resource, node: FunctionDeclaration): void { const name = node.name ? node.name.text : getDefaultResourceIdentifier(resource); const func = new TshFunction( - name, isNodeExported(node), getNodeType(node.type), node.getStart(), node.getEnd(), + name, + isNodeExported(node), + containsModifier(node, SyntaxKind.AsyncKeyword), + getNodeType(node.type), + node.getStart(), + node.getEnd(), ); if (isNodeDefaultExported(node)) { func.isExported = false; diff --git a/src/node-parser/interface-parser.ts b/src/node-parser/interface-parser.ts index e4fb8f0..f5269ef 100644 --- a/src/node-parser/interface-parser.ts +++ b/src/node-parser/interface-parser.ts @@ -57,6 +57,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration): getNodeType(o.type), !!o.questionToken, containsModifier(o, SyntaxKind.StaticKeyword), + containsModifier(o, SyntaxKind.AsyncKeyword), o.getStart(), o.getEnd(), ); diff --git a/test/__snapshots__/TypescriptParser.spec.ts.snap b/test/__snapshots__/TypescriptParser.spec.ts.snap index a0a515e..e6b5526 100644 --- a/test/__snapshots__/TypescriptParser.spec.ts.snap +++ b/test/__snapshots__/TypescriptParser.spec.ts.snap @@ -23,6 +23,7 @@ ClassDeclaration { MethodDeclaration { "end": 203, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method1", @@ -35,6 +36,7 @@ ClassDeclaration { MethodDeclaration { "end": 237, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method2", @@ -47,6 +49,7 @@ ClassDeclaration { MethodDeclaration { "end": 300, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method3", @@ -132,6 +135,7 @@ ClassDeclaration { MethodDeclaration { "end": 55, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method1", @@ -144,6 +148,7 @@ ClassDeclaration { MethodDeclaration { "end": 100, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "abstractMethod", @@ -259,6 +264,7 @@ Array [ MethodDeclaration { "end": 1251, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "objMethod", @@ -298,6 +304,7 @@ Array [ MethodDeclaration { "end": 1307, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "arrMethod", @@ -337,6 +344,7 @@ Array [ MethodDeclaration { "end": 1386, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "objAndArrMethod", @@ -478,6 +486,7 @@ ClassDeclaration { MethodDeclaration { "end": 1621, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": true, "name": "method", @@ -490,6 +499,7 @@ ClassDeclaration { MethodDeclaration { "end": 1660, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "methodNonStatic", @@ -546,6 +556,7 @@ EnumDeclaration { exports[`TypescriptParser Declaration parsing Functions should parse a function correctly 1`] = ` FunctionDeclaration { "end": 84, + "isAsync": false, "isExported": false, "name": "function1", "parameters": Array [ @@ -574,6 +585,7 @@ FunctionDeclaration { exports[`TypescriptParser Declaration parsing Functions should parse an exported function correctly 1`] = ` FunctionDeclaration { "end": 219, + "isAsync": false, "isExported": true, "name": "function2", "parameters": Array [ @@ -736,6 +748,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 93, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method1", @@ -748,6 +761,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 128, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method2", @@ -799,6 +813,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 247, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method1", @@ -832,6 +847,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 284, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "method2", @@ -897,6 +913,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 680, "isAbstract": true, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "nonOptionalFunction2", @@ -909,6 +926,7 @@ InterfaceDeclaration { MethodDeclaration { "end": 712, "isAbstract": true, + "isAsync": false, "isOptional": true, "isStatic": false, "name": "optionalFunction3", @@ -990,6 +1008,7 @@ Module { "declarations": Array [ FunctionDeclaration { "end": 62, + "isAsync": false, "isExported": true, "name": "modFunc", "parameters": Array [], @@ -1273,6 +1292,7 @@ ObjectBoundParameterDeclaration { exports[`TypescriptParser Declaration parsing Parameters should parse some mixed parameters (all above) 1`] = ` FunctionDeclaration { "end": 654, + "isAsync": false, "isExported": false, "name": "mixed", "parameters": Array [ @@ -1846,6 +1866,7 @@ File { MethodDeclaration { "end": 304, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "render", @@ -1912,6 +1933,7 @@ Array [ MethodDeclaration { "end": 304, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "render", @@ -1978,6 +2000,7 @@ File { MethodDeclaration { "end": 304, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "render", @@ -2052,6 +2075,7 @@ File { MethodDeclaration { "end": 93, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2112,6 +2136,7 @@ Array [ MethodDeclaration { "end": 93, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2172,6 +2197,7 @@ File { MethodDeclaration { "end": 93, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2273,6 +2299,7 @@ File { MethodDeclaration { "end": 142, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "test", diff --git a/test/_workspace/typescript-parser/async.ts b/test/_workspace/typescript-parser/async.ts new file mode 100644 index 0000000..90aa258 --- /dev/null +++ b/test/_workspace/typescript-parser/async.ts @@ -0,0 +1,11 @@ +class Class1 { + public static async staticAsync() { } + public func(): void { } + public async asyncFunc(): Promise { } + public async asyncFuncWithoutType() { } +} + +function func(): void { } +async function asyncFunc(): Promise { } +async function asyncFuncWithoutType() { } +function promiseFunc(): Promise { } diff --git a/test/declaration-index/__snapshots__/DeclarationIndex.spec.ts.snap b/test/declaration-index/__snapshots__/DeclarationIndex.spec.ts.snap index 0d55557..71c8929 100644 --- a/test/declaration-index/__snapshots__/DeclarationIndex.spec.ts.snap +++ b/test/declaration-index/__snapshots__/DeclarationIndex.spec.ts.snap @@ -78,6 +78,7 @@ Array [ DeclarationInfo { "declaration": FunctionDeclaration { "end": 92, + "isAsync": false, "isExported": true, "name": "isString", "parameters": Array [ @@ -102,6 +103,7 @@ Array [ DeclarationInfo { "declaration": FunctionDeclaration { "end": 53, + "isAsync": false, "isExported": true, "name": "toString", "parameters": Array [], @@ -119,6 +121,7 @@ Array [ DeclarationInfo { "declaration": FunctionDeclaration { "end": 53, + "isAsync": false, "isExported": true, "name": "toString", "parameters": Array [], @@ -964,6 +967,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -991,6 +995,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1023,6 +1028,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1064,6 +1070,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1222,6 +1229,7 @@ Object { MethodDeclaration { "end": 137, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1249,6 +1257,7 @@ Object { MethodDeclaration { "end": 57, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1407,6 +1416,7 @@ Object { MethodDeclaration { "end": 137, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1434,6 +1444,7 @@ Object { MethodDeclaration { "end": 57, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1455,6 +1466,7 @@ Object { DeclarationInfo { "declaration": FunctionDeclaration { "end": 186, + "isAsync": false, "isExported": true, "name": "isNumber", "parameters": Array [ @@ -1476,6 +1488,7 @@ Object { DeclarationInfo { "declaration": FunctionDeclaration { "end": 92, + "isAsync": false, "isExported": true, "name": "isString", "parameters": Array [ @@ -1508,6 +1521,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1535,6 +1549,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1567,6 +1582,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1594,6 +1610,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1752,6 +1769,7 @@ Object { MethodDeclaration { "end": 137, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1779,6 +1797,7 @@ Object { MethodDeclaration { "end": 57, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1813,6 +1832,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1854,6 +1874,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1886,6 +1907,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1913,6 +1935,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -1945,6 +1968,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -1986,6 +2010,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2032,6 +2057,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -2073,6 +2099,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2231,6 +2258,7 @@ Object { MethodDeclaration { "end": 137, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -2258,6 +2286,7 @@ Object { MethodDeclaration { "end": 57, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", @@ -2290,6 +2319,7 @@ Object { MethodDeclaration { "end": 329, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomethingAwesome", @@ -2317,6 +2347,7 @@ Object { MethodDeclaration { "end": 105, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", diff --git a/test/declaration-index/specific-cases/body-parser/DeclarationIndex.body-parser.spec.ts b/test/declaration-index/specific-cases/body-parser/DeclarationIndex.body-parser.spec.ts deleted file mode 100644 index cb1ae08..0000000 --- a/test/declaration-index/specific-cases/body-parser/DeclarationIndex.body-parser.spec.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { join, resolve } from '../../../testUtilities'; - -import { DeclarationIndex } from '../../../../src/DeclarationIndex'; -import { TypescriptParser } from '../../../../src/TypescriptParser'; - -describe.skip('DeclarationIndex - specific case "body-parser"', () => { - - const rootPath = resolve( - __dirname, '..', '..', '..', '_workspace', 'declaration-index', 'specific-cases', 'body-parser', - ); - const files = [ - join(rootPath, 'node_modules', '@types', 'body-parser', 'index.d.ts'), - ]; - - let declarationIndex: DeclarationIndex; - - beforeEach(() => { - declarationIndex = new DeclarationIndex(new TypescriptParser(), rootPath); - }); - - it('should parse the declarations', async () => { - await declarationIndex.buildIndex(files); - console.log(declarationIndex.index['bodyParser']); - }); - -}); diff --git a/test/declaration-index/specific-cases/reindex-with-global-module/__snapshots__/DeclarationIndex.reindex-with-global-module-export.spec.ts.snap b/test/declaration-index/specific-cases/reindex-with-global-module/__snapshots__/DeclarationIndex.reindex-with-global-module-export.spec.ts.snap index a286539..374d1a3 100644 --- a/test/declaration-index/specific-cases/reindex-with-global-module/__snapshots__/DeclarationIndex.reindex-with-global-module-export.spec.ts.snap +++ b/test/declaration-index/specific-cases/reindex-with-global-module/__snapshots__/DeclarationIndex.reindex-with-global-module-export.spec.ts.snap @@ -41,6 +41,7 @@ Array [ MethodDeclaration { "end": 59, "isAbstract": false, + "isAsync": false, "isOptional": false, "isStatic": false, "name": "doSomething", diff --git a/test/parsing/Flags.spec.ts b/test/parsing/Flags.spec.ts new file mode 100644 index 0000000..3604cca --- /dev/null +++ b/test/parsing/Flags.spec.ts @@ -0,0 +1,32 @@ +import { FunctionDeclaration, TypescriptParser } from '../../src'; +import { Resource } from '../../src/resources'; +import { getWorkspaceFile, rootPath } from '../testUtilities'; + +describe('TypescriptParser', () => { + + let parser: TypescriptParser; + + beforeEach(() => { + parser = new TypescriptParser(); + }); + + describe('isAsync Flag', () => { + + const file = getWorkspaceFile('typescript-parser/async.ts'); + let parsed: Resource; + + beforeEach(async () => { + parsed = await parser.parseFile(file, rootPath); + }); + + it('should parse the given async elements in a class', () => { + expect(parsed.declarations[0]).toMatchSnapshot(); + }); + + it('should parse async functions correctly', () => { + expect(parsed.declarations.filter(d => d instanceof FunctionDeclaration)).toMatchSnapshot(); + }); + + }); + +}); diff --git a/test/parsing/__snapshots__/Flags.spec.ts.snap b/test/parsing/__snapshots__/Flags.spec.ts.snap new file mode 100644 index 0000000..70797c2 --- /dev/null +++ b/test/parsing/__snapshots__/Flags.spec.ts.snap @@ -0,0 +1,111 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`TypescriptParser isAsync Flag should parse async functions correctly 1`] = ` +Array [ + FunctionDeclaration { + "end": 205, + "isAsync": false, + "isExported": false, + "name": "func", + "parameters": Array [], + "start": 180, + "type": "void", + "variables": Array [], + }, + FunctionDeclaration { + "end": 251, + "isAsync": true, + "isExported": false, + "name": "asyncFunc", + "parameters": Array [], + "start": 206, + "type": "Promise", + "variables": Array [], + }, + FunctionDeclaration { + "end": 293, + "isAsync": true, + "isExported": false, + "name": "asyncFuncWithoutType", + "parameters": Array [], + "start": 252, + "type": undefined, + "variables": Array [], + }, + FunctionDeclaration { + "end": 335, + "isAsync": false, + "isExported": false, + "name": "promiseFunc", + "parameters": Array [], + "start": 294, + "type": "Promise", + "variables": Array [], + }, +] +`; + +exports[`TypescriptParser isAsync Flag should parse the given async elements in a class 1`] = ` +ClassDeclaration { + "accessors": Array [], + "end": 178, + "isExported": false, + "methods": Array [ + MethodDeclaration { + "end": 56, + "isAbstract": false, + "isAsync": true, + "isOptional": false, + "isStatic": true, + "name": "staticAsync", + "parameters": Array [], + "start": 19, + "type": undefined, + "variables": Array [], + "visibility": 2, + }, + MethodDeclaration { + "end": 84, + "isAbstract": false, + "isAsync": false, + "isOptional": false, + "isStatic": false, + "name": "func", + "parameters": Array [], + "start": 61, + "type": "void", + "variables": Array [], + "visibility": 2, + }, + MethodDeclaration { + "end": 132, + "isAbstract": false, + "isAsync": true, + "isOptional": false, + "isStatic": false, + "name": "asyncFunc", + "parameters": Array [], + "start": 89, + "type": "Promise", + "variables": Array [], + "visibility": 2, + }, + MethodDeclaration { + "end": 176, + "isAbstract": false, + "isAsync": true, + "isOptional": false, + "isStatic": false, + "name": "asyncFuncWithoutType", + "parameters": Array [], + "start": 137, + "type": undefined, + "variables": Array [], + "visibility": 2, + }, + ], + "name": "Class1", + "properties": Array [], + "start": 0, +} +`;