Skip to content

Commit

Permalink
fix(app): component template import parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vogloblinsky committed Sep 16, 2021
1 parent 7b68398 commit b31604c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 43 deletions.
38 changes: 27 additions & 11 deletions src/app/compiler/angular/deps/helpers/symbol-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,45 @@ export class SymbolHelper {
*/
private parseSymbols(
node: ts.ObjectLiteralElement,
srcFile: ts.SourceFile
srcFile: ts.SourceFile,
decoratorType: string
): Array<string | boolean> {
let localNode = node;

if (ts.isShorthandPropertyAssignment(localNode)) {
localNode = ImportsUtil.findValueInImportOrLocalVariables(node.name.text, srcFile);
if (ts.isShorthandPropertyAssignment(localNode) && decoratorType !== 'template') {
localNode = ImportsUtil.findValueInImportOrLocalVariables(
node.name.text,
srcFile,
decoratorType
);
}
if (ts.isShorthandPropertyAssignment(localNode) && decoratorType === 'template') {
const data = ImportsUtil.findValueInImportOrLocalVariables(
node.name.text,
srcFile,
decoratorType
);
return [data];
}

if (ts.isArrayLiteralExpression(localNode.initializer)) {
if (localNode.initializer && ts.isArrayLiteralExpression(localNode.initializer)) {
return localNode.initializer.elements.map(x => this.parseSymbolElements(x));
} else if (
ts.isStringLiteral(localNode.initializer) ||
ts.isTemplateLiteral(localNode.initializer) ||
(ts.isPropertyAssignment(localNode) && localNode.initializer.text)
(localNode.initializer && ts.isStringLiteral(localNode.initializer)) ||
(localNode.initializer && ts.isTemplateLiteral(localNode.initializer)) ||
(localNode.initializer &&
ts.isPropertyAssignment(localNode) &&
localNode.initializer.text)
) {
return [localNode.initializer.text];
} else if (
localNode.initializer &&
localNode.initializer.kind &&
(localNode.initializer.kind === SyntaxKind.TrueKeyword ||
localNode.initializer.kind === SyntaxKind.FalseKeyword)
) {
return [localNode.initializer.kind === SyntaxKind.TrueKeyword ? true : false];
} else if (ts.isPropertyAccessExpression(localNode.initializer)) {
} else if (localNode.initializer && ts.isPropertyAccessExpression(localNode.initializer)) {
let identifier = this.parseSymbolElements(localNode.initializer);
return [identifier];
} else if (
Expand All @@ -222,7 +238,7 @@ export class SymbolHelper {

public getSymbolDeps(
props: ReadonlyArray<ts.ObjectLiteralElementLike>,
type: string,
decoratorType: string,
srcFile: ts.SourceFile,
multiLine?: boolean
): Array<string> {
Expand All @@ -235,12 +251,12 @@ export class SymbolHelper {
filteredProps = [];

for (i; i < len; i++) {
if (props[i].name && props[i].name.text === type) {
if (props[i].name && props[i].name.text === decoratorType) {
filteredProps.push(props[i]);
}
}

return filteredProps.map(x => this.parseSymbols(x, srcFile)).pop() || [];
return filteredProps.map(x => this.parseSymbols(x, srcFile, decoratorType)).pop() || [];
}

public getSymbolDepsRaw(
Expand Down
98 changes: 68 additions & 30 deletions src/utils/imports.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';

import { Project, ts, PropertyDeclaration, SyntaxKind } from 'ts-morph';
import { Project, ts, PropertyDeclaration, SyntaxKind, VariableDeclaration } from 'ts-morph';
import FileEngine from '../app/engines/file.engine';

const ast = new Project();

Expand Down Expand Up @@ -100,10 +101,16 @@ export class ImportsUtil {
* @param {string} inputVariableName like myvar
* @return {[type]} myvar value
*/
public findValueInImportOrLocalVariables(inputVariableName: string, sourceFile: ts.SourceFile) {
public findValueInImportOrLocalVariables(
inputVariableName: string,
sourceFile: ts.SourceFile,
decoratorType?: string
) {
let metadataVariableName = inputVariableName,
searchedImport,
aliasOriginalName = '',
foundWithNamedImport = false,
foundWithDefaultImport = false,
foundWithAlias = false;

const file =
Expand All @@ -129,17 +136,37 @@ export class ImportsUtil {
importAlias = namedImports[j].getAliasNode().getText();
}
if (importName === metadataVariableName) {
foundWithNamedImport = true;
searchedImport = i;
break;
}
if (importAlias === metadataVariableName) {
foundWithNamedImport = true;
foundWithAlias = true;
aliasOriginalName = importName;
searchedImport = i;
break;
}
}
}
const namespaceImport = i.getNamespaceImport();
if (namespaceImport) {
const namespaceImportLocalName = namespaceImport.getText();
if (namespaceImportLocalName === metadataVariableName) {
searchedImport = i;
}
}

if (!foundWithNamedImport) {
const defaultImport = i.getDefaultImport();
if (defaultImport) {
const defaultImportText = defaultImport.getText();
if (defaultImportText === metadataVariableName) {
foundWithDefaultImport = true;
searchedImport = i;
}
}
}
});

function hasFoundValues(variableDeclaration) {
Expand Down Expand Up @@ -176,40 +203,41 @@ export class ImportsUtil {
return hasFoundValues(variableDeclaration);
} else {
// Try with exports
const exportDeclarations = sourceFileImport.getExportDeclarations();
if (exportDeclarations && exportDeclarations.length > 0) {
let i = 0,
len = exportDeclarations.length;
for (i; i < len; i++) {
let exportDeclaration = exportDeclarations[i];
let sourceFileExportedReference =
exportDeclaration.getModuleSpecifierSourceFile();
if (sourceFileExportedReference) {
let sourceFileExportedReferencePath =
sourceFileExportedReference.getFilePath();

const sourceFileExported =
typeof ast.getSourceFile(
sourceFileExportedReferencePath
) !== 'undefined'
? ast.getSourceFile(sourceFileExportedReferencePath)
: ast.addSourceFileAtPathIfExists(
sourceFileExportedReferencePath
);

if (sourceFileExported) {
variableDeclaration =
sourceFileExported.getVariableDeclaration(variableName);
if (variableDeclaration) {
return hasFoundValues(variableDeclaration);
}
const exportDeclarations = sourceFileImport.getExportedDeclarations();

if (exportDeclarations && exportDeclarations.size > 0) {
for (const [
exportDeclarationKey,
exportDeclarationValues
] of exportDeclarations) {
exportDeclarationValues.forEach(exportDeclarationValue => {
if (
exportDeclarationValue instanceof VariableDeclaration &&
exportDeclarationValue.getName() === variableName
) {
return hasFoundValues(exportDeclarationValue);
}
}
});
}
}
}
}
}
if (
!importPathReference &&
decoratorType === 'template' &&
searchedImport.getModuleSpecifierValue().indexOf('.html') !== -1
) {
const originalSourceFilePath = sourceFile.path;
const originalSourceFilePathFolder = originalSourceFilePath.substring(
0,
originalSourceFilePath.lastIndexOf('/')
);
const finalImportedPath =
originalSourceFilePathFolder + '/' + searchedImport.getModuleSpecifierValue();
const finalImportedPathData = FileEngine.getSync(finalImportedPath);
return finalImportedPathData;
}
} else {
// Find in local variables of the file
const variableDeclaration = file.getVariableDeclaration(metadataVariableName);
Expand All @@ -227,6 +255,16 @@ export class ImportsUtil {
let compilerNode =
initializer.compilerNode as ts.ObjectLiteralExpression;
return compilerNode.properties;
} else if (
initializerKind &&
(initializerKind === SyntaxKind.StringLiteral ||
initializerKind === SyntaxKind.NoSubstitutionTemplateLiteral)
) {
if (decoratorType === 'template') {
return initializer.getText();
} else {
return variableDeclaration.compilerNode;
}
} else if (initializerKind) {
return variableDeclaration.compilerNode;
}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/todomvc-ng2/src/app/about/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, HostListener, Input } from '@angular/core';

import template from './about.component.html';

import { Subscription } from 'rxjs/Subscription';

/**
Expand All @@ -17,7 +19,7 @@ import { Subscription } from 'rxjs/Subscription';
*/
@Component({
selector: 'about',
templateUrl: './about.component.html',
template,
providers: [EmitterService],
entryComponents: [TodoComponent, ListComponent],
preserveWhitespaces: false
Expand Down
7 changes: 7 additions & 0 deletions test/src/cli/cli-generation-big-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe('CLI simple generation - big app', () => {
expect(file).to.contain('[donothing]');
});

/**
* Import for component template
*/
it('should have metadatas - component', () => {
expect(aboutComponentFile).to.contain('example written using');
});

/**
* Routing
*/
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"lib": ["es6", "es7"]
"lib": ["es6", "es7"],
"downlevelIteration": true
},
"exclude": ["test"]
}

0 comments on commit b31604c

Please sign in to comment.