Skip to content

Commit

Permalink
fix(linking-tool): get module name from dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejSikorski committed Jul 11, 2020
1 parent c199b5b commit d9dfae2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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('');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Node } from 'ts-morph';

export namespace StringUtil {
export function getStringArgumentValue(node: Node): string {
return node.getText().slice(1, -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ArrowFunction,
CallExpression,
Expression,
Node,
ObjectLiteralExpression,
PropertyAccessExpression,
PropertyAssignment,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
}
});
}
Expand Down

0 comments on commit d9dfae2

Please sign in to comment.