Skip to content

Commit 5c2f253

Browse files
thePunderWomanatscott
authored andcommitted
fix(migrations): cf migration - ensure full check runs for all imports (#53637)
In cases where CommonModule was unsafe to remove but other imports were present, the symbol check would be skipped. This should run for all the possibly removed symbols for safety. PR Close #53637
1 parent c99491e commit 5c2f253

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

packages/core/schematics/ng-generate/control-flow-migration/types.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -311,26 +311,22 @@ export class AnalyzedFile {
311311
* It is only run on .ts files.
312312
*/
313313
verifyCanRemoveImports() {
314-
// if we already know it's not safe to remove the common module import
315-
// skip this check entirely
316-
if (this.removeCommonModule) {
317-
const importDeclaration = this.importRanges.find(r => r.type === 'importDeclaration');
318-
const instances = lookupIdentifiersInSourceFile(this.sourceFile, importWithCommonRemovals);
319-
let foundImportDeclaration = false;
320-
let count = 0;
321-
for (let range of this.importRanges) {
322-
for (let instance of instances) {
323-
if (instance.getStart() >= range.start && instance.getEnd() <= range.end!) {
324-
if (range === importDeclaration) {
325-
foundImportDeclaration = true;
326-
}
327-
count++;
314+
const importDeclaration = this.importRanges.find(r => r.type === 'importDeclaration');
315+
const instances = lookupIdentifiersInSourceFile(this.sourceFile, importWithCommonRemovals);
316+
let foundImportDeclaration = false;
317+
let count = 0;
318+
for (let range of this.importRanges) {
319+
for (let instance of instances) {
320+
if (instance.getStart() >= range.start && instance.getEnd() <= range.end!) {
321+
if (range === importDeclaration) {
322+
foundImportDeclaration = true;
328323
}
324+
count++;
329325
}
330326
}
331-
if (instances.size !== count && importDeclaration !== undefined && foundImportDeclaration) {
332-
importDeclaration.remove = false;
333-
}
327+
}
328+
if (instances.size !== count && importDeclaration !== undefined && foundImportDeclaration) {
329+
importDeclaration.remove = false;
334330
}
335331
}
336332
}

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,6 +5226,56 @@ describe('control flow migration', () => {
52265226

52275227
expect(actual).toBe(expected);
52285228
});
5229+
5230+
it('should not remove other imports when mismatch in counts', async () => {
5231+
writeFile('/comp.ts', [
5232+
`import {DatePipe, NgIf} from '@angular/common';`,
5233+
`import {Component, NgModule, Pipe, PipeTransform} from '@angular/core';`,
5234+
`@Component({`,
5235+
` selector: 'example',`,
5236+
` template: \`<span>{{ example | date }}</span>\`,`,
5237+
`})`,
5238+
`export class ExampleCmp {`,
5239+
` example: 'stuff',`,
5240+
`}`,
5241+
`const NG_MODULE_IMPORTS = [`,
5242+
` DatePipe,`,
5243+
` NgIf,`,
5244+
`];`,
5245+
`@NgModule({`,
5246+
` declarations: [ExampleCmp],`,
5247+
` imports: [NG_MODULE_IMPORTS],`,
5248+
` exports: [ExampleCmp],`,
5249+
`})`,
5250+
`export class ExampleModule {}`,
5251+
].join('\n'));
5252+
5253+
await runMigration();
5254+
const actual = tree.readContent('/comp.ts');
5255+
const expected = [
5256+
`import {DatePipe, NgIf} from '@angular/common';`,
5257+
`import {Component, NgModule, Pipe, PipeTransform} from '@angular/core';`,
5258+
`@Component({`,
5259+
` selector: 'example',`,
5260+
` template: \`<span>{{ example | date }}</span>\`,`,
5261+
`})`,
5262+
`export class ExampleCmp {`,
5263+
` example: 'stuff',`,
5264+
`}`,
5265+
`const NG_MODULE_IMPORTS = [`,
5266+
` DatePipe,`,
5267+
` NgIf,`,
5268+
`];`,
5269+
`@NgModule({`,
5270+
` declarations: [ExampleCmp],`,
5271+
` imports: [NG_MODULE_IMPORTS],`,
5272+
` exports: [ExampleCmp],`,
5273+
`})`,
5274+
`export class ExampleModule {}`,
5275+
].join('\n');
5276+
5277+
expect(actual).toBe(expected);
5278+
});
52295279
});
52305280

52315281
describe('no migration needed', () => {

0 commit comments

Comments
 (0)