From 06fc66a5d9ff989461c50f9d81e7dc0e5305c86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Sat, 15 Jul 2017 01:23:56 +0200 Subject: [PATCH 1/2] test: adding tests for react files --- test/TypescriptParser.spec.ts | 20 ++++++++++++++++++- .../declaration-index/reactFile.tsx | 7 +++++++ .../typescript-parser/usagesOnly.tsx | 11 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/_workspace/declaration-index/reactFile.tsx create mode 100644 test/_workspace/typescript-parser/usagesOnly.tsx diff --git a/test/TypescriptParser.spec.ts b/test/TypescriptParser.spec.ts index eef4aba..5a8fdc2 100644 --- a/test/TypescriptParser.spec.ts +++ b/test/TypescriptParser.spec.ts @@ -32,7 +32,7 @@ describe('TypescriptParser', () => { it('should parse a source code string correctly', async () => { const parsed = await parser.parseSource(`import {foo} from 'bar'; class Foobar {}; const bar = new Foobar();`); - + expect(parsed).toMatchSnapshot(); }); @@ -544,4 +544,22 @@ describe('TypescriptParser', () => { }); + describe('TSX Usage parsing', () => { + const file = getWorkspaceFile('typescript-parser/usagesOnly.tsx'); + let parsed: Resource; + + beforeEach(async () => { + parsed = await parser.parseFile(file, rootPath); + }); + + it('should parse a tsx element usage', () => { + const usages = parsed.usages; + + expect(usages).toContain('myComponent'); + expect(usages).toContain('div'); + expect(usages).toContain('complexComp'); + expect(usages).toContain('SingleComp'); + }); + }); + }); diff --git a/test/_workspace/declaration-index/reactFile.tsx b/test/_workspace/declaration-index/reactFile.tsx new file mode 100644 index 0000000..cc969dd --- /dev/null +++ b/test/_workspace/declaration-index/reactFile.tsx @@ -0,0 +1,7 @@ +import { myComponent } from './myReactTemplate'; + +export default function foobar() { + return ( + + ); +} diff --git a/test/_workspace/typescript-parser/usagesOnly.tsx b/test/_workspace/typescript-parser/usagesOnly.tsx new file mode 100644 index 0000000..0bb43a3 --- /dev/null +++ b/test/_workspace/typescript-parser/usagesOnly.tsx @@ -0,0 +1,11 @@ +export default function foobar() { + return ( + +
+ + + +
+
+ ); +} From 3cc78220b4a4ab06083d714ee8d727497d1ae88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Sat, 15 Jul 2017 13:29:59 +0200 Subject: [PATCH 2/2] fix: isNew in imports does return correct value (#8) --- package.json | 14 ++-- src/imports/DefaultImport.ts | 2 +- src/imports/ExternalModuleImport.ts | 2 +- src/imports/NamedImport.ts | 2 +- src/imports/NamespaceImport.ts | 2 +- src/imports/StringImport.ts | 2 +- test/imports/Imports.spec.ts | 100 ++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 test/imports/Imports.spec.ts diff --git a/package.json b/package.json index 601d287..f607039 100644 --- a/package.json +++ b/package.json @@ -31,17 +31,17 @@ }, "homepage": "https://github.com/TypeScript-Heroes/node-typescript-parser#readme", "devDependencies": { - "@types/jest": "^20.0.1", + "@types/jest": "^20.0.2", "@types/mock-fs": "^3.6.30", - "@types/node": "^8.0.1", - "del-cli": "^1.0.0", + "@types/node": "^8.0.13", + "del-cli": "^1.1.0", "jest": "^20.0.4", "mock-fs": "^4.4.1", "semantic-release": "^6.3.6", - "ts-jest": "^20.0.6", - "tslint": "^5.4.3", - "tslint-config-airbnb": "^5.2.0", - "tsutils": "^2.4.0", + "ts-jest": "^20.0.7", + "tslint": "^5.5.0", + "tslint-config-airbnb": "^5.2.1", + "tsutils": "^2.7.1", "typedoc": "^0.7.1" }, "dependencies": { diff --git a/src/imports/DefaultImport.ts b/src/imports/DefaultImport.ts index 931c83a..6897f8d 100644 --- a/src/imports/DefaultImport.ts +++ b/src/imports/DefaultImport.ts @@ -10,7 +10,7 @@ import { AliasedImport } from './Import'; */ export class DefaultImport implements AliasedImport { public get isNew(): boolean { - return this.start !== undefined && this.end !== undefined; + return this.start === undefined || this.end === undefined; } constructor(public libraryName: string, public alias: string, public start?: number, public end?: number) { } diff --git a/src/imports/ExternalModuleImport.ts b/src/imports/ExternalModuleImport.ts index 7702ac7..2a8dac1 100644 --- a/src/imports/ExternalModuleImport.ts +++ b/src/imports/ExternalModuleImport.ts @@ -10,7 +10,7 @@ import { AliasedImport } from './Import'; */ export class ExternalModuleImport implements AliasedImport { public get isNew(): boolean { - return this.start !== undefined && this.end !== undefined; + return this.start === undefined || this.end === undefined; } constructor(public libraryName: string, public alias: string, public start?: number, public end?: number) { } diff --git a/src/imports/NamedImport.ts b/src/imports/NamedImport.ts index ed545ca..678c4de 100644 --- a/src/imports/NamedImport.ts +++ b/src/imports/NamedImport.ts @@ -14,7 +14,7 @@ export class NamedImport implements Import { public specifiers: SymbolSpecifier[] = []; public get isNew(): boolean { - return this.start !== undefined && this.end !== undefined; + return this.start === undefined || this.end === undefined; } constructor(public libraryName: string, public start?: number, public end?: number) { } diff --git a/src/imports/NamespaceImport.ts b/src/imports/NamespaceImport.ts index 0f4e435..ba55aa7 100644 --- a/src/imports/NamespaceImport.ts +++ b/src/imports/NamespaceImport.ts @@ -9,7 +9,7 @@ import { AliasedImport } from './Import'; */ export class NamespaceImport implements AliasedImport { public get isNew(): boolean { - return this.start !== undefined && this.end !== undefined; + return this.start === undefined || this.end === undefined; } constructor(public libraryName: string, public alias: string, public start?: number, public end?: number) { } diff --git a/src/imports/StringImport.ts b/src/imports/StringImport.ts index aed63a4..cd2921e 100644 --- a/src/imports/StringImport.ts +++ b/src/imports/StringImport.ts @@ -9,7 +9,7 @@ import { Import } from './Import'; */ export class StringImport implements Import { public get isNew(): boolean { - return this.start !== undefined && this.end !== undefined; + return this.start === undefined || this.end === undefined; } constructor(public libraryName: string, public start?: number, public end?: number) { } diff --git a/test/imports/Imports.spec.ts b/test/imports/Imports.spec.ts new file mode 100644 index 0000000..efb52e5 --- /dev/null +++ b/test/imports/Imports.spec.ts @@ -0,0 +1,100 @@ +import { DefaultImport, ExternalModuleImport, NamedImport, NamespaceImport, StringImport } from '../../src/imports'; + +describe('Imports', () => { + + describe('DefaultImport', () => { + + it('should set isNew() when start is undefined', () => { + const imp = new DefaultImport('lib', 'alias', undefined, 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should set isNew() when end is undefined', () => { + const imp = new DefaultImport('lib', 'alias', 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should not set isNew() when start and end are defined', () => { + const imp = new DefaultImport('lib', 'alias', 12, 1337); + expect(imp.isNew).toBeFalsy(); + }); + + }); + + describe('ExternalModuleImport', () => { + + it('should set isNew() when start is undefined', () => { + const imp = new ExternalModuleImport('lib', 'alias', undefined, 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should set isNew() when end is undefined', () => { + const imp = new ExternalModuleImport('lib', 'alias', 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should not set isNew() when start and end are defined', () => { + const imp = new ExternalModuleImport('lib', 'alias', 12, 1337); + expect(imp.isNew).toBeFalsy(); + }); + + }); + + describe('NamedImport', () => { + + it('should set isNew() when start is undefined', () => { + const imp = new NamedImport('lib', undefined, 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should set isNew() when end is undefined', () => { + const imp = new NamedImport('lib', 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should not set isNew() when start and end are defined', () => { + const imp = new NamedImport('lib', 12, 1337); + expect(imp.isNew).toBeFalsy(); + }); + + }); + + describe('NamespaceImport', () => { + + it('should set isNew() when start is undefined', () => { + const imp = new NamespaceImport('lib', 'alias', undefined, 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should set isNew() when end is undefined', () => { + const imp = new NamespaceImport('lib', 'alias', 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should not set isNew() when start and end are defined', () => { + const imp = new NamespaceImport('lib', 'alias', 12, 1337); + expect(imp.isNew).toBeFalsy(); + }); + + }); + + describe('StringImport', () => { + + it('should set isNew() when start is undefined', () => { + const imp = new StringImport('lib', undefined, 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should set isNew() when end is undefined', () => { + const imp = new StringImport('lib', 1337); + expect(imp.isNew).toBeTruthy(); + }); + + it('should not set isNew() when start and end are defined', () => { + const imp = new StringImport('lib', 12, 1337); + expect(imp.isNew).toBeFalsy(); + }); + + }); + +});