From e5055e4b1c21314ace8947bb63d272a3254c515d Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 29 May 2019 12:21:16 +0200 Subject: [PATCH] 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 --- .../migrations/update-8/drop-es6-polyfills.ts | 2 +- .../update-8/update-lazy-module-paths.ts | 2 +- .../update-8/update-lazy-module-paths_spec.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/schematics/angular/migrations/update-8/drop-es6-polyfills.ts b/packages/schematics/angular/migrations/update-8/drop-es6-polyfills.ts index d7442b4819c1..265595e1f0ba 100644 --- a/packages/schematics/angular/migrations/update-8/drop-es6-polyfills.ts +++ b/packages/schematics/angular/migrations/update-8/drop-es6-polyfills.ts @@ -106,7 +106,7 @@ function dropES2015PolyfillsFromFile(polyfillPath: string): Rule { } const sourceFile = ts.createSourceFile(polyfillPath, - content, + content.replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true, ); diff --git a/packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts b/packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts index ade0418ee39c..1c829889c5e7 100644 --- a/packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts +++ b/packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts @@ -17,7 +17,7 @@ function* visit(directory: DirEntry): IterableIterator { if (content.includes('loadChildren')) { const source = ts.createSourceFile( entry.path, - content.toString(), + content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true, ); diff --git a/packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts b/packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts index b3cc89c159ef..a8dbd30e8b20 100644 --- a/packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts +++ b/packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts @@ -73,5 +73,19 @@ describe('Migration to version 8', () => { expect(routes).toContain( `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); }); + + it('should replace the module path string when file has BOM', async () => { + tree.create(lazyRoutePath, '\uFEFF' + Buffer.from(lazyRoute).toString()); + + schematicRunner.runSchematic('migration-08', {}, tree); + await schematicRunner.engine.executePostTasks().toPromise(); + + const routes = tree.readContent(lazyRoutePath); + + expect(routes).not.toContain('./lazy/lazy.module#LazyModule'); + expect(routes).toContain( + `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); + }); + }); });