diff --git a/packages/core/schematics/migrations/block-template-entities/util.ts b/packages/core/schematics/migrations/block-template-entities/util.ts index b9675e5028fd9..f2521b953fd1a 100644 --- a/packages/core/schematics/migrations/block-template-entities/util.ts +++ b/packages/core/schematics/migrations/block-template-entities/util.ts @@ -66,11 +66,7 @@ export class AnalyzedFile { * @param analyzedFiles Map in which to store the results. */ export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map) { - 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. @@ -86,7 +82,7 @@ export function analyze(sourceFile: ts.SourceFile, analyzedFiles: Map void) { + sourceFile.forEachChild(function walk(node) { + if (ts.isClassDeclaration(node)) { + callback(node); + } + node.forEachChild(walk); + }); +} diff --git a/packages/core/schematics/test/block_template_entities_spec.ts b/packages/core/schematics/test/block_template_entities_spec.ts index cd28bc2550afb..cd9c2e15d532f 100644 --- a/packages/core/schematics/test/block_template_entities_spec.ts +++ b/packages/core/schematics/test/block_template_entities_spec.ts @@ -276,4 +276,23 @@ describe('Block template entities migration', () => { expect(content).toBe('My email is admin@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: \`
My email is admin@test.com

This is a brace }

\` + }) + class Comp {} + } + `); + + await runMigration(); + const content = tree.readContent('/comp.ts'); + + expect(content).toContain( + 'template: `
My email is admin@test.com

This is a brace }

`'); + }); });