Skip to content

Commit ce01c42

Browse files
Alan Agiusalexeagle
Alan Agius
authored andcommitted
fix(@schematics/angular): TypeScript related migrations should cater for BOM
In the CLI `UpdateRecorder` methods such as `insertLeft`, `remove` etc.. accepts positions which are not offset by a BOM. This is because when a file has a BOM a different recorder will be used https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/schematics/src/tree/recorder.ts#L72 which caters for an addition offset/delta. The main reason for this is that when a developer is writing a schematic they shouldn't need to compute the offset based if a file has a BOM or not and is handled out of the box. Example ```ts recorder.insertLeft(5, 'true'); ``` However this is unfortunate in the case if a ts SourceFile is used and one uses `getWidth` and `getStart` method they will already be offset by 1, which at the end it results in a double offset and hence the problem. Fixes #14551
1 parent dc56480 commit ce01c42

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

packages/schematics/angular/migrations/update-8/drop-es6-polyfills.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function dropES2015PolyfillsFromFile(polyfillPath: string): Rule {
106106
}
107107

108108
const sourceFile = ts.createSourceFile(polyfillPath,
109-
content,
109+
content.replace(/^\uFEFF/, ''),
110110
ts.ScriptTarget.Latest,
111111
true,
112112
);

packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
1717
if (content.includes('loadChildren')) {
1818
const source = ts.createSourceFile(
1919
entry.path,
20-
content.toString(),
20+
content.toString().replace(/^\uFEFF/, ''),
2121
ts.ScriptTarget.Latest,
2222
true,
2323
);

packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,19 @@ describe('Migration to version 8', () => {
7373
expect(routes).toContain(
7474
`loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`);
7575
});
76+
77+
it('should replace the module path string when file has BOM', async () => {
78+
tree.create(lazyRoutePath, '\uFEFF' + Buffer.from(lazyRoute).toString());
79+
80+
schematicRunner.runSchematic('migration-08', {}, tree);
81+
await schematicRunner.engine.executePostTasks().toPromise();
82+
83+
const routes = tree.readContent(lazyRoutePath);
84+
85+
expect(routes).not.toContain('./lazy/lazy.module#LazyModule');
86+
expect(routes).toContain(
87+
`loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`);
88+
});
89+
7690
});
7791
});

0 commit comments

Comments
 (0)