Skip to content

Commit

Permalink
fix(angular): support routes as route array type for ast parsing #12707
Browse files Browse the repository at this point in the history
… (#12829)
  • Loading branch information
Coly010 committed Oct 26, 2022
1 parent 1992ada commit d34dff1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions packages/angular/src/utils/nx-devkit/route-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { addRoute } from './route-utils';

describe('standalone component utils', () => {
describe.each([
['Route[]', 'Route'],
['Routes', 'Routes'],
])('standalone component utils - %s', (routes, routeType) => {
it('should add a static route to the routes file', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
tree.write(
'routes-file.ts',
`import { Route } from '@angular/router';
export const ROUTES: Route[] = [];`
`import { ${routeType} } from '@angular/router';
export const ROUTES: ${routes} = [];`
);

// ACT
Expand All @@ -23,9 +26,9 @@ describe('standalone component utils', () => {

// ASSERT
expect(tree.read('routes-file.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { Route } from '@angular/router';
"import { ${routeType} } from '@angular/router';
import { ROUTES } from '@proj/lib';
export const ROUTES: Route[] = [
export const ROUTES: ${routes} = [
{path: 'test', children: ROUTES },]"
`);
});
Expand All @@ -35,8 +38,8 @@ describe('standalone component utils', () => {
const tree = createTreeWithEmptyWorkspace();
tree.write(
'routes-file.ts',
`import { Route } from '@angular/router';
export const ROUTES: Route[] = [];`
`import { ${routeType} } from '@angular/router';
export const ROUTES: ${routes} = [];`
);

// ACT
Expand All @@ -48,8 +51,8 @@ describe('standalone component utils', () => {

// ASSERT
expect(tree.read('routes-file.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { Route } from '@angular/router';
export const ROUTES: Route[] = [
"import { ${routeType} } from '@angular/router';
export const ROUTES: ${routes} = [
{path: 'test', , loadChildren: () => import('@proj/lib').then(m => m.ROUTES) },]"
`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/nx-devkit/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function addRoute(
const ast = tsquery.ast(routesFileContents);

const ROUTES_ARRAY_SELECTOR =
'VariableDeclaration:has(ArrayType > TypeReference > Identifier[name=Route]) > ArrayLiteralExpression';
'VariableDeclaration:has(ArrayType > TypeReference > Identifier[name=Route], Identifier[name=Routes]) > ArrayLiteralExpression';

const routesArrayNodes = tsquery(ast, ROUTES_ARRAY_SELECTOR, {
visitAllChildren: true,
Expand Down

0 comments on commit d34dff1

Please sign in to comment.