From de1f81fd40fb954559bc5771cd0b15e7eb4c92e2 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Thu, 6 Jun 2019 10:57:54 +0200 Subject: [PATCH] fix(ng-update): parse cli workspace config as json5 Currently we try to parse CLI workspace configurations gracefully by using the native `JSON.parse()` method. This means that the CLI workspace configuration needs to follow the strict JSON specification because otherwise the migrations would not be able to find TypeScript configurations in the CLI project where JSON5 workspace configurations are supported. In order to handle such workspace configurations, we leverage the JSON parsing logicfrom the `@angular-devkit/core` which is also used by the CLI. --- .../project-tsconfig-paths.spec.ts | 20 +++++++++++++++++++ .../upgrade-rules/project-tsconfig-paths.ts | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.spec.ts b/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.spec.ts index 8008dc9c64ca..cfb52dc1fc58 100644 --- a/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.spec.ts +++ b/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.spec.ts @@ -45,4 +45,24 @@ describe('ng-update project-tsconfig-paths', () => { expect(getProjectTsConfigPaths(testTree)).toEqual(['tsconfig.json']); }); + + it('should be able to read workspace configuration which is using JSON5 features', () => { + testTree.create('/my-build-config.json', ''); + testTree.create('/angular.json', `{ + // Comments, unquoted properties or trailing commas are only supported in JSON5. + projects: { + with_tests: { + targets: { + build: { + options: { + tsConfig: './my-build-config.json', + } + } + } + } + }, + }`); + + expect(getProjectTsConfigPaths(testTree)).toEqual(['my-build-config.json']); + }); }); diff --git a/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.ts b/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.ts index 0aa9164f6eda..0cc0700520a9 100644 --- a/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.ts +++ b/src/cdk/schematics/ng-update/upgrade-rules/project-tsconfig-paths.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {normalize} from '@angular-devkit/core'; +import {JsonParseMode, normalize, parseJson} from '@angular-devkit/core'; import {Tree} from '@angular-devkit/schematics'; /** Name of the default Angular CLI workspace configuration files. */ @@ -63,7 +63,9 @@ function getWorkspaceConfigGracefully(tree: Tree): any { } try { - return JSON.parse(configBuffer.toString()); + // Parse the workspace file as JSON5 which is also supported for CLI + // workspace configurations. + return parseJson(configBuffer.toString(), JsonParseMode.Json5); } catch { return null; }