From 4ac2dbfc05ac77c1119d63ddaebf1b0c20717797 Mon Sep 17 00:00:00 2001 From: Vittorio Date: Sat, 28 Dec 2019 13:48:35 +0000 Subject: [PATCH] feat(importequal): add support for export equals and remove webpack env types that were conflicting with types node (#124) --- package-lock.json | 13 +++++++------ package.json | 1 - src/transformer/descriptor/helper/helper.ts | 11 ++++------- test/frameworkContext/context.ts | 4 +++- test/frameworkContext/contextDeprecated.ts | 4 +++- test/transformer/context.ts | 4 +++- .../descriptor/import/importEqual.test.ts | 14 ++++++++++++++ .../utils/interfaces/anotherExportEqual.ts | 6 ++++++ .../descriptor/utils/interfaces/exportEqual.ts | 6 ++++++ 9 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 test/transformer/descriptor/utils/interfaces/anotherExportEqual.ts create mode 100644 test/transformer/descriptor/utils/interfaces/exportEqual.ts diff --git a/package-lock.json b/package-lock.json index b662572a4..12cab6ddd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -217,12 +217,19 @@ "integrity": "sha512-cwi0HUsDcD502HBP8huXfTkVuWmeo1Fiz3GKxNwMBBsJV4+bKa7QrtxbNpXhVuarX7QjWfNTvmW6KmFS7YK9uw==", "dev": true, "requires": { + "@types/node": "^12.0.2", "import-fresh": "^3.0.0", "lodash": "4.17.14", "resolve-from": "^5.0.0", "resolve-global": "^1.0.0" }, "dependencies": { + "@types/node": { + "version": "12.12.22", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.22.tgz", + "integrity": "sha512-r5i93jqbPWGXYXxianGATOxTelkp6ih/U0WVnvaqAvTqM+0U6J3kw6Xk6uq/dWNRkEVw/0SLcO5ORXbVNz4FMQ==", + "dev": true + }, "lodash": { "version": "4.17.14", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.14.tgz", @@ -401,12 +408,6 @@ "source-map": "^0.6.0" } }, - "@types/webpack-env": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.14.1.tgz", - "integrity": "sha512-0Ki9jAAhKDSuLDXOIMADg54Hu60SuBTEsWaJGGy5cV+SSUQ63J2a+RrYYGrErzz39fXzTibhKrAQJAb8M7PNcA==", - "dev": true - }, "@webassemblyjs/ast": { "version": "1.8.5", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.8.5.tgz", diff --git a/package.json b/package.json index 270e4ea5b..29c7ad3cc 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "@commitlint/config-angular": "^8.2.0", "@types/jasmine": "^3.5.0", "@types/node": "^13.1.0", - "@types/webpack-env": "^1.14.1", "awesome-typescript-loader": "^5.2.1", "clean-webpack-plugin": "^3.0.0", "conventional-changelog-cli": "^2.0.31", diff --git a/src/transformer/descriptor/helper/helper.ts b/src/transformer/descriptor/helper/helper.ts index ebf35bcd8..cf09c0b83 100644 --- a/src/transformer/descriptor/helper/helper.ts +++ b/src/transformer/descriptor/helper/helper.ts @@ -15,24 +15,21 @@ export namespace TypescriptHelper { export function GetDeclarationFromNode(node: ts.Node): ts.Declaration { const typeChecker: ts.TypeChecker = TypeChecker(); const symbol: ts.Symbol = typeChecker.getSymbolAtLocation(node); + return GetDeclarationFromSymbol(symbol); } export function GetDeclarationFromSymbol(symbol: ts.Symbol): ts.Declaration { const declaration: ts.Declaration = GetFirstValidDeclaration(symbol.declarations); - if (ts.isImportSpecifier(declaration)) { - return GetDeclarationForImport(declaration) as ts.Declaration; - } - - if (ts.isImportEqualsDeclaration(declaration)) { - return GetDeclarationFromNode(declaration.moduleReference); + if (ts.isImportSpecifier(declaration) || ts.isImportEqualsDeclaration(declaration)) { + return GetDeclarationForImport(declaration); } return declaration; } - export function GetDeclarationForImport(node: ts.ImportClause | ts.ImportSpecifier): ts.TypeNode | ts.Declaration { + export function GetDeclarationForImport(node: ts.ImportClause | ts.ImportSpecifier | ts.ImportEqualsDeclaration): ts.Declaration { const typeChecker: ts.TypeChecker = TypeChecker(); const symbol: ts.Symbol = typeChecker.getSymbolAtLocation(node.name); const originalSymbol: ts.Symbol = typeChecker.getAliasedSymbol(symbol); diff --git a/test/frameworkContext/context.ts b/test/frameworkContext/context.ts index c4c620d88..50996bbd8 100644 --- a/test/frameworkContext/context.ts +++ b/test/frameworkContext/context.ts @@ -5,5 +5,7 @@ Provider.instance.provideMethodWithDeferredValue((name: string, value: () => any return jasmine.createSpy(name).and.callFake(value); }); -const frameworkContext: __WebpackModuleApi.RequireContext = require.context('./', true, /\.test(\.valid)?\.ts$/); +// @ts-ignore +// tslint:disable-next-line:typedef +const frameworkContext = require.context('./', true, /\.test(\.valid)?\.ts$/); frameworkContext.keys().map(frameworkContext); diff --git a/test/frameworkContext/contextDeprecated.ts b/test/frameworkContext/contextDeprecated.ts index d336bb372..8f86e4598 100644 --- a/test/frameworkContext/contextDeprecated.ts +++ b/test/frameworkContext/contextDeprecated.ts @@ -5,5 +5,7 @@ Provider.instance.provideMethod((name: string, value: any) => { return jasmine.createSpy(name).and.returnValue(value); }); -const frameworkContext: __WebpackModuleApi.RequireContext = require.context('./', true, /\.test(\.deprecated)?\.ts$/); +// @ts-ignore +// tslint:disable-next-line:typedef +const frameworkContext = require.context('./', true, /\.test(\.deprecated)?\.ts$/); frameworkContext.keys().map(frameworkContext); diff --git a/test/transformer/context.ts b/test/transformer/context.ts index 7074c77c8..95ddbd398 100644 --- a/test/transformer/context.ts +++ b/test/transformer/context.ts @@ -1,2 +1,4 @@ -const frameworkContext: __WebpackModuleApi.RequireContext = require.context('./', true, /\.test\.ts$/); +// @ts-ignore +// tslint:disable-next-line:typedef +const frameworkContext = require.context('./', true, /\.test\.ts$/); frameworkContext.keys().map(frameworkContext); diff --git a/test/transformer/descriptor/import/importEqual.test.ts b/test/transformer/descriptor/import/importEqual.test.ts index 8442776a0..730ed7584 100644 --- a/test/transformer/descriptor/import/importEqual.test.ts +++ b/test/transformer/descriptor/import/importEqual.test.ts @@ -1,4 +1,6 @@ import { createMock } from 'ts-auto-mock'; +import IAmAnotherExportedWithEqual from '../utils/interfaces/anotherExportEqual'; +import IAmExportedWithEqual = require('../utils/interfaces/exportEqual'); import { NameSpaceInterfaceImport } from '../utils/namespace/namespace'; import Interface = NameSpaceInterfaceImport.Interface; import SubInterface = NameSpaceInterfaceImport.SubNamespace.SubInterface; @@ -22,4 +24,16 @@ describe('import equal', () => { const mock: SubInterface = createMock(); expect(mock.a).toBe(''); }); + + it('should use the correct import for an equal exported interface used with require', () => { + const mock: IAmExportedWithEqual = createMock(); + expect(mock.a).toBe(''); + expect(mock.b).toBe(0); + }); + + it('should use the correct import for an equal exported interface used with import', () => { + const mock: IAmAnotherExportedWithEqual = createMock(); + expect(mock.a).toBe(''); + expect(mock.b).toBe(0); + }); }); diff --git a/test/transformer/descriptor/utils/interfaces/anotherExportEqual.ts b/test/transformer/descriptor/utils/interfaces/anotherExportEqual.ts new file mode 100644 index 000000000..204c56af0 --- /dev/null +++ b/test/transformer/descriptor/utils/interfaces/anotherExportEqual.ts @@ -0,0 +1,6 @@ +interface IAmAnotherExportedWithEqual { + a: string; + b: number; +} + +export = IAmAnotherExportedWithEqual; diff --git a/test/transformer/descriptor/utils/interfaces/exportEqual.ts b/test/transformer/descriptor/utils/interfaces/exportEqual.ts new file mode 100644 index 000000000..e7d48b7ce --- /dev/null +++ b/test/transformer/descriptor/utils/interfaces/exportEqual.ts @@ -0,0 +1,6 @@ +interface IAmExportedWithEqual { + a: string; + b: number; +} + +export = IAmExportedWithEqual;