From 223110e2f804c58c3a58aba22865be4d14be3906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Mon, 30 Oct 2017 14:37:55 +0100 Subject: [PATCH] fix: recognize default exports and indexed properties (#41) Fixes #32. --- package.json | 2 +- src/TypescriptParser.ts | 26 ++++++++------- src/node-parser/identifier-parser.ts | 7 ++-- src/run.ts | 32 ------------------- test/TypescriptParser.spec.ts | 31 +++++++++++++++--- .../TypescriptParser.spec.ts.snap | 16 +++++----- .../typescript-parser/usagesOnly.ts | 6 +++- tslint.json | 2 ++ 8 files changed, 59 insertions(+), 63 deletions(-) delete mode 100644 src/run.ts diff --git a/package.json b/package.json index 52c948c..cd7e643 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "npm run clean && tsc -p ./config/tsconfig.build.json", "clean": "del-cli ./build ./coverage", "develop": "npm run clean && tsc -p .", - "lint": "tslint -c tslint.json 'src/**/*.ts'", + "lint": "tslint -c ./tslint.json -p ./config/tsconfig.build.json 'src/**/*.ts'", "test": "npm run lint && npm run clean && jest -c ./jest.json", "test:watch": "npm run clean && jest -c ./jest.json --watch --no-coverage", "typedoc": "del-cli ./docs && typedoc --ignoreCompilerErrors --out ./docs --mode file --tsconfig ./config/tsconfig.build.json ./src/", diff --git a/src/TypescriptParser.ts b/src/TypescriptParser.ts index 0ff0a51..05c4282 100644 --- a/src/TypescriptParser.ts +++ b/src/TypescriptParser.ts @@ -56,13 +56,13 @@ export class TypescriptParser { */ public async parseSource(source: string, scriptKind: ScriptKind = ScriptKind.TS): Promise { return await this.parseTypescript( - createSourceFile( - 'inline.tsx', - source, - ScriptTarget.ES2015, - true, - scriptKind), - '/'); + createSourceFile( + 'inline.tsx', + source, + ScriptTarget.ES2015, + true, + scriptKind), + '/'); } /** @@ -109,11 +109,13 @@ export class TypescriptParser { scriptKind = ScriptKind.TSX; break; } - return createSourceFile(o, - readFileSync(o).toString(), - ScriptTarget.ES2015, - true, - scriptKind); + return createSourceFile( + o, + readFileSync(o).toString(), + ScriptTarget.ES2015, + true, + scriptKind, + ); }) .map(o => this.parseTypescript(o, rootPath)); } diff --git a/src/node-parser/identifier-parser.ts b/src/node-parser/identifier-parser.ts index c0925ea..f5090f4 100644 --- a/src/node-parser/identifier-parser.ts +++ b/src/node-parser/identifier-parser.ts @@ -24,7 +24,6 @@ const usageAllowedIfLast = [ SyntaxKind.Parameter, SyntaxKind.PropertyDeclaration, SyntaxKind.VariableDeclaration, - SyntaxKind.ElementAccessExpression, SyntaxKind.BinaryExpression, ]; @@ -37,7 +36,7 @@ const usagePredicates: any = [ /** * Predicate function to determine if the node is possible as a "usage". * Checks for the node identifier to be the last of the identifier list. - * + * * @param {Node} node * @returns {boolean} */ @@ -57,7 +56,7 @@ function allowedIfLastIdentifier(node: Node): boolean { /** * Predicate function to determine if the node is possible as a "usage". * Checks if the identifier is on the lefthand side of a property access. - * + * * @param {Node} node * @returns {boolean} */ @@ -76,7 +75,7 @@ function allowedIfPropertyAccessFirst(node: Node): boolean { /** * Parses an identifier into a usage of a resource if the predicates are true. - * + * * @export * @param {Resource} resource * @param {Identifier} node diff --git a/src/run.ts b/src/run.ts deleted file mode 100644 index 118a3e5..0000000 --- a/src/run.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { TypescriptParser } from './'; - -const parser = new TypescriptParser(); - -const code = ` -class Foo { - private _calc: string; - - public get calc(): string { - return this._calc; - } - - public set calc(value: string) { - this._calc = value; - } - - public foo(): void { - - } -} -`; - -async function parse(): Promise { - // const parsed = await parser.parseSource(`import Foo, { Foobar } from 'myLib';`); - // const parsed = await parser.parseSource(`import Foo from 'myLib';`); - // const parsed = await parser.parseSource(`import { Foo } from 'myLib';`); - // const parsed = await parser.parseSource(`import { Foo, default as Bar } from 'myLib';`); - const parsed = await parser.parseSource(code); - console.log(parsed); -} - -parse().then(() => process.exit(0)).catch(() => process.exit(1)); diff --git a/test/TypescriptParser.spec.ts b/test/TypescriptParser.spec.ts index 5a67292..4d29f28 100644 --- a/test/TypescriptParser.spec.ts +++ b/test/TypescriptParser.spec.ts @@ -573,6 +573,24 @@ describe('TypescriptParser', () => { expect(usages).toContain('GenericType'); }); + it('should parse a default exported element', () => { + const usages = parsed.usages; + + expect(usages).toContain('defaultExportUsage'); + }); + + it('should parse an indexer property', () => { + const usages = parsed.usages; + + expect(usages).toContain('indexedUsage'); + }); + + it('should parse an indexer property access', () => { + const usages = parsed.usages; + + expect(usages).toContain('indexingUsage'); + }); + }); describe('TSX Usage parsing', () => { @@ -754,11 +772,14 @@ describe('TypescriptParser', () => { describe('Specific sources', () => { it('should parse generics in functions in classes correctly', async () => { - const parsed = await parser.parseSource(`export class TestClass { - public test() { - let a = () => { let b = null; }; - } - }`, ScriptKind.TS); + const parsed = await parser.parseSource( + `export class TestClass { + public test() { + let a = () => { let b = null; }; + } + }`, + ScriptKind.TS, + ); expect(parsed).toMatchSnapshot(); }); diff --git a/test/__snapshots__/TypescriptParser.spec.ts.snap b/test/__snapshots__/TypescriptParser.spec.ts.snap index c568c07..562e86f 100644 --- a/test/__snapshots__/TypescriptParser.spec.ts.snap +++ b/test/__snapshots__/TypescriptParser.spec.ts.snap @@ -1518,31 +1518,31 @@ File { "declarations": Array [ ClassDeclaration { "accessors": Array [], - "end": 144, + "end": 160, "isExported": true, "methods": Array [ MethodDeclaration { - "end": 130, + "end": 142, "isAbstract": false, "name": "test", "parameters": Array [], - "start": 41, + "start": 45, "type": undefined, "variables": Array [ VariableDeclaration { - "end": 112, + "end": 120, "isConst": false, "isExported": false, "name": "a", - "start": 77, + "start": 85, "type": undefined, }, VariableDeclaration { - "end": 109, + "end": 117, "isConst": false, "isExported": false, "name": "b", - "start": 96, + "start": 104, "type": undefined, }, ], @@ -1554,7 +1554,7 @@ File { "start": 0, }, ], - "end": 144, + "end": 160, "exports": Array [], "filePath": "inline.tsx", "imports": Array [], diff --git a/test/_workspace/typescript-parser/usagesOnly.ts b/test/_workspace/typescript-parser/usagesOnly.ts index c2378a2..f4e01ea 100644 --- a/test/_workspace/typescript-parser/usagesOnly.ts +++ b/test/_workspace/typescript-parser/usagesOnly.ts @@ -8,7 +8,7 @@ class Class { public assignedProperty = AssignedProperty; @FunctionDecorator() - public func(@ParamDecorator() param: TypedParam, defaultParam = DefaultParam) { + public func( @ParamDecorator() param: TypedParam, defaultParam = DefaultParam) { } private prv(param): ReturnValue { @@ -39,3 +39,7 @@ class Class extends DefaultClass { class Class extends GenericClass { } + +indexedUsage[indexingUsage]; + +export default defaultExportUsage; diff --git a/tslint.json b/tslint.json index e7c3066..15aade3 100644 --- a/tslint.json +++ b/tslint.json @@ -27,6 +27,8 @@ ] } ], + "no-boolean-literal-compare": false, + "strict-boolean-expressions": false, "ter-indent": [ true, 4,