Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/schematics/angular/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,21 @@ export function getDecoratorMetadata(
.filter((node) => {
return (
node.kind == ts.SyntaxKind.Decorator &&
(node as ts.Decorator).expression &&
(node as ts.Decorator).expression.kind == ts.SyntaxKind.CallExpression
);
})
.map((node) => (node as ts.Decorator).expression as ts.CallExpression)
.filter((expr) => {
if (expr.expression.kind == ts.SyntaxKind.Identifier) {
if (expr.expression && expr.expression.kind == ts.SyntaxKind.Identifier) {
const id = expr.expression as ts.Identifier;

return id.text == identifier && angularImports[id.text] === module;
} else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
// This covers foo.NgModule when importing * as foo.
const paExpr = expr.expression as ts.PropertyAccessExpression;
// If the left expression is not an identifier, just give up at that point.
if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
if (!paExpr.expression || paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
return false;
}

Expand Down
50 changes: 50 additions & 0 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
addRouteDeclarationToModule,
addSymbolToNgModuleMetadata,
findNodes,
getDecoratorMetadata,
hasTopLevelIdentifier,
insertAfterLastOccurrence,
insertImport,
Expand Down Expand Up @@ -839,4 +840,53 @@ describe('ast utils', () => {
expect(hasTopLevelIdentifier(source, 'FooInterface', '@foo/interfaces')).toBe(false);
});
});

describe('getDecoratorMetadata', () => {
const filePath = './src/app.module.ts';

it('should return decorator metadata for a valid NgModule', () => {
const fileContent = `
import { NgModule } from '@angular/core';

@NgModule({
declarations: [],
imports: []
})
export class AppModule { }
`;
const source = getTsSource(filePath, fileContent);
const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');

expect(nodes.length).toBe(1);
expect(nodes[0].kind).toBe(ts.SyntaxKind.ObjectLiteralExpression);
});

it('should not crash when processing files without decorators', () => {
const fileContent = `
export const environment = {
production: false
};
`;
const source = getTsSource(filePath, fileContent);
const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');

expect(nodes.length).toBe(0);
});

it('should handle namespace imports correctly', () => {
const fileContent = `
import * as ng from '@angular/core';

@ng.NgModule({
declarations: []
})
export class AppModule { }
`;
const source = getTsSource(filePath, fileContent);
const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');

expect(nodes.length).toBe(1);
expect(nodes[0].kind).toBe(ts.SyntaxKind.ObjectLiteralExpression);
});
});
});