Skip to content

Commit

Permalink
feat(flags): add isAsync flag for methods and functions (#92)
Browse files Browse the repository at this point in the history
Adds a flag to methods and functions that declares
if the object is modified with `async`. Closes #66.
  • Loading branch information
buehler authored Aug 31, 2018
1 parent 617764d commit b21136a
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 30 deletions.
17 changes: 17 additions & 0 deletions src/declarations/Declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions src/declarations/FunctionDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CallableDeclaration, ExportableDeclaration } from './Declaration';
import { AsyncDeclaration, CallableDeclaration, ExportableDeclaration } from './Declaration';
import { ParameterDeclaration } from './ParameterDeclaration';
import { VariableDeclaration } from './VariableDeclaration';

Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/declarations/MethodDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AbstractDeclaration,
AsyncDeclaration,
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
Expand All @@ -22,6 +23,7 @@ import { VariableDeclaration } from './VariableDeclaration';
*/
export class MethodDeclaration implements
AbstractDeclaration,
AsyncDeclaration,
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
Expand All @@ -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,
) { }
Expand Down
1 change: 1 addition & 0 deletions src/node-parser/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
15 changes: 13 additions & 2 deletions src/node-parser/function-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/node-parser/interface-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
27 changes: 27 additions & 0 deletions test/__snapshots__/TypescriptParser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 203,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method1",
Expand All @@ -35,6 +36,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 237,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method2",
Expand All @@ -47,6 +49,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 300,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method3",
Expand Down Expand Up @@ -132,6 +135,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 55,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method1",
Expand All @@ -144,6 +148,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 100,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "abstractMethod",
Expand Down Expand Up @@ -259,6 +264,7 @@ Array [
MethodDeclaration {
"end": 1251,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "objMethod",
Expand Down Expand Up @@ -298,6 +304,7 @@ Array [
MethodDeclaration {
"end": 1307,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "arrMethod",
Expand Down Expand Up @@ -337,6 +344,7 @@ Array [
MethodDeclaration {
"end": 1386,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "objAndArrMethod",
Expand Down Expand Up @@ -478,6 +486,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 1621,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": true,
"name": "method",
Expand All @@ -490,6 +499,7 @@ ClassDeclaration {
MethodDeclaration {
"end": 1660,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "methodNonStatic",
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -736,6 +748,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 93,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method1",
Expand All @@ -748,6 +761,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 128,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method2",
Expand Down Expand Up @@ -799,6 +813,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 247,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method1",
Expand Down Expand Up @@ -832,6 +847,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 284,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "method2",
Expand Down Expand Up @@ -897,6 +913,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 680,
"isAbstract": true,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "nonOptionalFunction2",
Expand All @@ -909,6 +926,7 @@ InterfaceDeclaration {
MethodDeclaration {
"end": 712,
"isAbstract": true,
"isAsync": false,
"isOptional": true,
"isStatic": false,
"name": "optionalFunction3",
Expand Down Expand Up @@ -990,6 +1008,7 @@ Module {
"declarations": Array [
FunctionDeclaration {
"end": 62,
"isAsync": false,
"isExported": true,
"name": "modFunc",
"parameters": Array [],
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -1846,6 +1866,7 @@ File {
MethodDeclaration {
"end": 304,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "render",
Expand Down Expand Up @@ -1912,6 +1933,7 @@ Array [
MethodDeclaration {
"end": 304,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "render",
Expand Down Expand Up @@ -1978,6 +2000,7 @@ File {
MethodDeclaration {
"end": 304,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "render",
Expand Down Expand Up @@ -2052,6 +2075,7 @@ File {
MethodDeclaration {
"end": 93,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "doSomething",
Expand Down Expand Up @@ -2112,6 +2136,7 @@ Array [
MethodDeclaration {
"end": 93,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "doSomething",
Expand Down Expand Up @@ -2172,6 +2197,7 @@ File {
MethodDeclaration {
"end": 93,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "doSomething",
Expand Down Expand Up @@ -2273,6 +2299,7 @@ File {
MethodDeclaration {
"end": 142,
"isAbstract": false,
"isAsync": false,
"isOptional": false,
"isStatic": false,
"name": "test",
Expand Down
11 changes: 11 additions & 0 deletions test/_workspace/typescript-parser/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Class1 {
public static async staticAsync() { }
public func(): void { }
public async asyncFunc(): Promise<void> { }
public async asyncFuncWithoutType() { }
}

function func(): void { }
async function asyncFunc(): Promise<void> { }
async function asyncFuncWithoutType() { }
function promiseFunc(): Promise<void> { }
Loading

0 comments on commit b21136a

Please sign in to comment.