From d9dfae2d289b6995c2d5772e694855c44878f903 Mon Sep 17 00:00:00 2001 From: Maciej Sikorski Date: Tue, 7 Jul 2020 14:03:06 +0200 Subject: [PATCH] fix(linking-tool): get module name from dynamic import --- .../resources/data-providers/data-provider.ts | 6 ++-- .../resources/utils/angular.util.ts | 13 ++++++++ .../resources/utils/array.util.ts | 5 ++++ .../resources/utils/string.util.ts | 7 +++++ .../resources/utils/typescript-api.util.ts | 30 +------------------ .../services/generate-links.service.ts | 13 ++++---- 6 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 libs/linking-tool/src/lib/schematics/generate-links/resources/utils/angular.util.ts create mode 100644 libs/linking-tool/src/lib/schematics/generate-links/resources/utils/array.util.ts create mode 100644 libs/linking-tool/src/lib/schematics/generate-links/resources/utils/string.util.ts diff --git a/libs/linking-tool/src/lib/schematics/generate-links/resources/data-providers/data-provider.ts b/libs/linking-tool/src/lib/schematics/generate-links/resources/data-providers/data-provider.ts index 28f7060..b3fa1dd 100644 --- a/libs/linking-tool/src/lib/schematics/generate-links/resources/data-providers/data-provider.ts +++ b/libs/linking-tool/src/lib/schematics/generate-links/resources/data-providers/data-provider.ts @@ -2,6 +2,7 @@ import { ArrowFunction, ObjectLiteralExpression, SourceFile } from 'ts-morph'; import { GenerateLinksProperty } from '../enums/generate-links-property.enum'; import { RouteDeclaration } from '../interfaces/route-declaration.interface'; import { TypescriptApiUtil } from '../utils/typescript-api.util'; +import { AngularUtil } from '../utils/angular.util'; export class DataProvider { static getRouteDeclarations(source: SourceFile): RouteDeclaration[] { @@ -64,10 +65,9 @@ export class DataProvider { if (TypescriptApiUtil.isArrowFunction(loadChildrenAssignment)) { // lazy loaded module import from v8 and Ivy - lazyLoadedModulePath = TypescriptApiUtil.getImportValueFromArrowFunction( + lazyLoadedModuleName = AngularUtil.getLazyLoadedModuleName( loadChildrenAssignment.getInitializer() as ArrowFunction - ).split('/'); - lazyLoadedModuleName = lazyLoadedModulePath[lazyLoadedModulePath.length - 1]; + ); } if (!lazyLoadedModuleName) { diff --git a/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/angular.util.ts b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/angular.util.ts new file mode 100644 index 0000000..aefb0b7 --- /dev/null +++ b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/angular.util.ts @@ -0,0 +1,13 @@ +import { ArrowFunction, PropertyAccessExpression, SyntaxKind } from 'ts-morph'; +import { TypescriptApiUtil } from './typescript-api.util'; + +export namespace AngularUtil { + export function getLazyLoadedModuleName(loader: ArrowFunction) { + const loaderReturns = TypescriptApiUtil.getCallExpressionFromArrowFunction(loader); + const moduleExtractionExpression = loaderReturns + .getChildrenOfKind(SyntaxKind.ArrowFunction)[0] + .getBody() as PropertyAccessExpression; + + return moduleExtractionExpression.getName(); + } +} diff --git a/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/array.util.ts b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/array.util.ts new file mode 100644 index 0000000..a99085e --- /dev/null +++ b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/array.util.ts @@ -0,0 +1,5 @@ +export namespace ArrayUtil { + export function joinToTitleCase(arr: string[]): string { + return arr.map(w => w[0].toUpperCase() + w.substr(1).toLowerCase()).join(''); + } +} diff --git a/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/string.util.ts b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/string.util.ts new file mode 100644 index 0000000..5d4edd2 --- /dev/null +++ b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/string.util.ts @@ -0,0 +1,7 @@ +import { Node } from 'ts-morph'; + +export namespace StringUtil { + export function getStringArgumentValue(node: Node): string { + return node.getText().slice(1, -1); + } +} diff --git a/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/typescript-api.util.ts b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/typescript-api.util.ts index 7b17ac9..d8971ef 100644 --- a/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/typescript-api.util.ts +++ b/libs/linking-tool/src/lib/schematics/generate-links/resources/utils/typescript-api.util.ts @@ -3,7 +3,6 @@ import { ArrowFunction, CallExpression, Expression, - Node, ObjectLiteralExpression, PropertyAccessExpression, PropertyAssignment, @@ -26,17 +25,6 @@ export class TypescriptApiUtil { ).getElements(); } - static getImportValueFromArrowFunction(arrowFunction: ArrowFunction): string { - const callExpression = TypescriptApiUtil.getCallExpressionFromArrowFunction(arrowFunction); - const importThen = TypescriptApiUtil.getPropertyAccessExpressionFromCallExpression( - callExpression - ); - const importCall = TypescriptApiUtil.getCallExpressionFromPropertyAccessExpression(importThen); - return TypescriptApiUtil.getCallExpressionArguments(importCall).length - ? TypescriptApiUtil.getCallExpressionArguments(importCall)[0].getText() - : ''; - } - static getObjectLiteralExpression( property: GenerateLinksProperty, object: ObjectLiteralExpression @@ -121,23 +109,7 @@ export class TypescriptApiUtil { return propertyAssignment.getInitializer() as ArrayLiteralExpression; } - private static getCallExpressionArguments(callExpression: CallExpression): Node[] { - return callExpression.getArguments(); - } - - private static getCallExpressionFromArrowFunction(arrowFunction: ArrowFunction): CallExpression { + static getCallExpressionFromArrowFunction(arrowFunction: ArrowFunction): CallExpression { return arrowFunction.getBody() as CallExpression; } - - private static getCallExpressionFromPropertyAccessExpression( - propertyAccessExpression: PropertyAccessExpression - ): CallExpression { - return propertyAccessExpression.getExpression() as CallExpression; - } - - private static getPropertyAccessExpressionFromCallExpression( - callExpression: CallExpression - ): PropertyAccessExpression { - return callExpression.getExpression() as PropertyAccessExpression; - } } diff --git a/libs/linking-tool/src/lib/schematics/generate-links/services/generate-links.service.ts b/libs/linking-tool/src/lib/schematics/generate-links/services/generate-links.service.ts index 5f3a899..9d21100 100644 --- a/libs/linking-tool/src/lib/schematics/generate-links/services/generate-links.service.ts +++ b/libs/linking-tool/src/lib/schematics/generate-links/services/generate-links.service.ts @@ -1,4 +1,4 @@ -import { Project, SourceFile } from 'ts-morph'; +import { Project } from 'ts-morph'; import { DataProvider } from '../resources/data-providers/data-provider'; import { LinkTypeMap } from '../resources/interfaces/link-type-map.interface'; import { RouteDeclaration } from '../resources/interfaces/route-declaration.interface'; @@ -47,12 +47,11 @@ export class GenerateLinksService { } if (route.loadChildren) { - this.resolveLink( - pathFromRoot.concat(path), - DataProvider.getRouteDeclarations( - this.project.getSourceFile(route.loadChildren) as SourceFile - ) - ); + const moduleFile = this.project.getSourceFile(route.loadChildren); + if (!moduleFile) { + throw Error(`Can't find module ${route.loadChildren}`); + } + this.resolveLink(pathFromRoot.concat(path), DataProvider.getRouteDeclarations(moduleFile)); } }); }