Skip to content

Commit

Permalink
feat(importequal): add support for export equals and remove webpack e…
Browse files Browse the repository at this point in the history
…nv types that were conflicting with types node (#124)
  • Loading branch information
uittorio committed Dec 28, 2019
1 parent 209ce7a commit 990ecf1
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 17 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 4 additions & 7 deletions src/transformer/descriptor/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion test/frameworkContext/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 3 additions & 1 deletion test/frameworkContext/contextDeprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 3 additions & 1 deletion test/transformer/context.ts
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions test/transformer/descriptor/import/importEqual.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,4 +24,16 @@ describe('import equal', () => {
const mock: SubInterface = createMock<SubInterface>();
expect(mock.a).toBe('');
});

it('should use the correct import for an equal exported interface used with require', () => {
const mock: IAmExportedWithEqual = createMock<IAmExportedWithEqual>();
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<IAmAnotherExportedWithEqual>();
expect(mock.a).toBe('');
expect(mock.b).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface IAmAnotherExportedWithEqual {
a: string;
b: number;
}

export = IAmAnotherExportedWithEqual;
6 changes: 6 additions & 0 deletions test/transformer/descriptor/utils/interfaces/exportEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface IAmExportedWithEqual {
a: string;
b: number;
}

export = IAmExportedWithEqual;

0 comments on commit 990ecf1

Please sign in to comment.