Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@schematics/angular): fix recursion in findNodes ast utils #18980

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/schematics/angular/utility/ast-utils.ts
Expand Up @@ -132,7 +132,7 @@ export function findNodes<T extends ts.Node>(
}
if (max > 0 && (recursive || !test(node))) {
for (const child of node.getChildren()) {
findNodes(child, test, max).forEach((node) => {
findNodes(child, test, max, recursive).forEach((node) => {
if (max > 0) {
arr.push(node);
}
Expand Down
41 changes: 41 additions & 0 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Expand Up @@ -609,4 +609,45 @@ describe('ast utils', () => {
);
});
});

describe('findNodes', () => {
const filePath = './src/foo.ts';
const fileContent = `
const a = {
nodeAtDepth0: {
nodeAtDepth1: {
nodeAtDepth2: { nodeAtDepth3: 'foo' },
},
},
};
`;

let recursive: boolean;

describe('when `recursive` is not set', () => {
beforeEach(() => {
recursive = false;
});

it('should return node excluding nested nodes', () => {
const source = getTsSource(filePath, fileContent);
const paNodes = findNodes(source, ts.SyntaxKind.PropertyAssignment, Infinity, recursive);

expect(paNodes.length).toEqual(1);
});
});

describe('when `recursive` is set', () => {
beforeEach(() => {
recursive = true;
});

it('should return node including all nested nodes', () => {
const source = getTsSource(filePath, fileContent);
const paNodes = findNodes(source, ts.SyntaxKind.PropertyAssignment, Infinity, recursive);

expect(paNodes.length).toEqual(4);
});
});
});
});