Skip to content

Commit

Permalink
fix(routing): path wrongly resolved during routing analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
vogloblinsky committed Feb 28, 2022
1 parent 8d67d27 commit 1722ca3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/app/compiler/angular-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class AngularDependencies extends FrameworkDependencies {

const sourceFiles = this.program.getSourceFiles() || [];

RouterParserUtil.scannedFiles = sourceFiles;

sourceFiles.map((file: ts.SourceFile) => {
const filePath = file.fileName;

Expand Down
13 changes: 10 additions & 3 deletions src/utils/router-parser.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const traverse = require('traverse');
const ast = new Project();

export class RouterParserUtil {
public scannedFiles: any[] = [];
private routes: any[] = [];
private incompleteRoutes = [];
private modules = [];
Expand Down Expand Up @@ -595,15 +596,21 @@ export class RouterParserUtil {
if (foundWithAliasInImports) {
if (typeof searchedImport !== 'undefined') {
const routePathIsBad = path => {
return typeof ast.getSourceFile(path) == 'undefined';
let result = true;
this.scannedFiles.forEach(scannedFile => {

This comment has been minimized.

Copy link
@mhombach

mhombach Feb 28, 2022

@vogloblinsky I'm not into the very details here, but do you just want to see if at least one entry has this condition (path === scannedFile.path)? Because then I would recommend using this.scannedFiles.find((scannedFile) => path === scannedFile.path) because that's better to read and also better in performance (because the forEach will continue searching in all array entries, even if already one entry was found and so it's already clear that the result will be false :)

This comment has been minimized.

Copy link
@vogloblinsky

vogloblinsky Feb 28, 2022

Author Contributor

Yes ! You are right, i am a little bit tired today ;)
Fixed

if (path === scannedFile.path) {
result = false;
}
});
return result;
};

const getIndicesOf = (searchStr, str, caseSensitive) => {
var searchStrLen = searchStr.length;
const searchStrLen = searchStr.length;
if (searchStrLen == 0) {
return [];
}
var startIndex = 0,
let startIndex = 0,
index,
indices = [];
if (!caseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LOGIN } from './login.routes';

export const APP_ROUTES: Routes = [
...LOGIN,
{ path: 'about', loadChildren: './about/about.module#AboutModule' },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', redirectTo: 'home', pathMatch: 'full' }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const LOGIN = [
{
path: 'login',
},
];
25 changes: 25 additions & 0 deletions test/src/cli/cli-routes-graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,29 @@ describe('CLI Routes graph', () => {
expect(file).to.contain('HomeComponent');
});
});

describe('should support route in external file', () => {
before(function (done) {
tmp.create(distFolder);
const ls = shell('node', [
'./bin/index-cli.js',
'-p',
'./test/fixtures/todomvc-ng2-simple-routing/src/tsconfig.json',
'-d',
distFolder
]);

if (ls.stderr.toString() !== '') {
console.error(`shell error: ${ls.stderr.toString()}`);
done('error');
}
done();
});
after(() => tmp.clean(distFolder));

it('should correctly read external file', () => {
const file = read(`${distFolder}/js/routes/routes_index.js`);
expect(file).to.contain('login');
});
});
});

0 comments on commit 1722ca3

Please sign in to comment.