Skip to content

Commit

Permalink
fix(routing): scan also static value in class
Browse files Browse the repository at this point in the history
fix #394
  • Loading branch information
vogloblinsky committed Jan 14, 2018
1 parent 4b599b2 commit 26dd154
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
31 changes: 27 additions & 4 deletions src/utils/imports.util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as ts from 'typescript';

import Ast from 'ts-simple-ast';
import Ast, { PropertyDeclaration } from 'ts-simple-ast';

const ast = new Ast();

Expand All @@ -27,6 +27,25 @@ export class ImportsUtil {
return res;
}

/**
* Find for a sourceFile a variable value in a local static class
* @param srcFile
* @param variableName
* @param variableValue
*/
private findInClasses(srcFile, variableName: string, variableValue: string) {
let res = '';
srcFile.getClass(c => {
let staticProperty: PropertyDeclaration = c.getStaticProperty(variableValue);
if (staticProperty) {
if (staticProperty.getInitializer()) {
res = staticProperty.getInitializer().getText();
}
}
});
return res;
}

/**
* Find a value in a local variable declaration like an object
* @param variableDeclaration
Expand Down Expand Up @@ -214,9 +233,13 @@ export class ImportsUtil {
// Try find it in enums
if (variablesAttributes.length > 0) {
if (typeof fileToSearchIn !== 'undefined') {
let en = this.findInEnums(fileToSearchIn, metadataVariableName, variablesAttributes[1]);
if (en !== '') {
return en;
let val = this.findInEnums(fileToSearchIn, metadataVariableName, variablesAttributes[1]);
if (val !== '') {
return val;
}
val = this.findInClasses(fileToSearchIn, metadataVariableName, variablesAttributes[1]);
if (val !== '') {
return val;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/src/cli/cli-generation-big-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ describe('CLI simple generation - big app', () => {
expect(routesFile).to.contain('homeimported');
expect(routesFile).to.contain('homeenumimported');
expect(routesFile).to.contain('homeenuminfile');
expect(routesFile).to.contain('todomvcinstaticclass');
});

it('should support Object Literal Property Value Shorthand support for metadatas for modules', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/src/todomvc-ng2/src/app/about/about-routes.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class ABOUT_ENUMS {
static todomvc = 'todomvcinstaticclass';
}
8 changes: 5 additions & 3 deletions test/src/todomvc-ng2/src/app/about/about-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { AboutComponent } from './about.component';
import { TodoMVCComponent } from './todomvc/todomvc.component';
import { CompodocComponent } from './compodoc/compodoc.component';

import { ABOUT_ENUMS } from './about-routes.enum';

const ABOUT_ROUTES: Routes = [
{
path: '', component: AboutComponent,
{
path: ABOUT_ENUMS.todomvc, component: AboutComponent,
children: [
{ path: '', redirectTo: 'todomvc', pathMatch: 'full' },
{ path: 'todomvc', component: TodoMVCComponent },
{ path: 'compodoc', component: CompodocComponent }
],
]
}
];

Expand Down

0 comments on commit 26dd154

Please sign in to comment.