Skip to content

Commit

Permalink
fix(migrations): handle nested classes in block entities migration (#…
Browse files Browse the repository at this point in the history
…52309)

Fixes that the block entities migration was only processing top-level classes. Nested classes could come up during unit tests.

PR Close #52309
  • Loading branch information
crisbeto authored and dylhunn committed Oct 24, 2023
1 parent bd9e91e commit 54bc384
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ export class AnalyzedFile {
* @param analyzedFiles Map in which to store the results.
*/
export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map<string, AnalyzedFile>) {
for (const node of sourceFile.statements) {
if (!ts.isClassDeclaration(node)) {
continue;
}

forEachClass(sourceFile, node => {
// Note: we have a utility to resolve the Angular decorators from a class declaration already.
// We don't use it here, because it requires access to the type checker which makes it more
// time-consuming to run internally.
Expand All @@ -86,7 +82,7 @@ export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map<string, An
null;

if (!metadata) {
continue;
return;
}

for (const prop of metadata.properties) {
Expand All @@ -112,7 +108,7 @@ export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map<string, An
break;
}
}
}
});
}

/**
Expand Down Expand Up @@ -183,3 +179,13 @@ class TextRangeCollector extends RecursiveVisitor {
super.visitText(text, null);
}
}

/** Executes a callback on each class declaration in a file. */
function forEachClass(sourceFile: ts.SourceFile, callback: (node: ts.ClassDeclaration) => void) {
sourceFile.forEachChild(function walk(node) {
if (ts.isClassDeclaration(node)) {
callback(node);
}
node.forEachChild(walk);
});
}
19 changes: 19 additions & 0 deletions packages/core/schematics/test/block_template_entities_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,23 @@ describe('Block template entities migration', () => {

expect(content).toBe('My email is admin&#64;test.com');
});

it('should migrate a component that is not at the top level', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
function foo() {
@Component({
template: \`<div><span>My email is admin@test.com</span></div><h1>This is a brace }</h1>\`
})
class Comp {}
}
`);

await runMigration();
const content = tree.readContent('/comp.ts');

expect(content).toContain(
'template: `<div><span>My email is admin&#64;test.com</span></div><h1>This is a brace &#125;</h1>`');
});
});

0 comments on commit 54bc384

Please sign in to comment.