From 7395dc2b48aed971455bc3b1f7abf9fd5d242c28 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 17 Sep 2019 20:00:28 +0200 Subject: [PATCH] feat(@schematics/angular): add strict compiler option to workspace tsconfig Closes #14694 --- .../angular/workspace/files/tsconfig.json.template | 7 ++++++- packages/schematics/angular/workspace/index_spec.ts | 12 ++++++++++++ packages/schematics/angular/workspace/schema.json | 5 +++++ tests/legacy-cli/e2e/tests/build/platform-server.ts | 2 ++ tests/legacy-cli/e2e/tests/misc/browsers.ts | 2 +- 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/schematics/angular/workspace/files/tsconfig.json.template b/packages/schematics/angular/workspace/files/tsconfig.json.template index 30956ae7ea26..9eb43d31920f 100644 --- a/packages/schematics/angular/workspace/files/tsconfig.json.template +++ b/packages/schematics/angular/workspace/files/tsconfig.json.template @@ -2,7 +2,12 @@ "compileOnSave": false, "compilerOptions": { "baseUrl": "./", - "outDir": "./dist/out-tsc", + "outDir": "./dist/out-tsc",<% if (strict) { %> + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noFallthroughCasesInSwitch": true, + "strictNullChecks": true,<% } %> "sourceMap": true, "declaration": false, "downlevelIteration": true, diff --git a/packages/schematics/angular/workspace/index_spec.ts b/packages/schematics/angular/workspace/index_spec.ts index a33d1c2e7879..b91840fda6e5 100644 --- a/packages/schematics/angular/workspace/index_spec.ts +++ b/packages/schematics/angular/workspace/index_spec.ts @@ -71,4 +71,16 @@ describe('Workspace Schematic', () => { expect(files).not.toContain('/tslint.json'); expect(files).not.toContain('/.editorconfig'); }); + + it('should not add strict compiler options when false', async () => { + const tree = await schematicRunner.runSchematicAsync('workspace', { ...defaultOptions, strict: false }).toPromise(); + const { compilerOptions } = JSON.parse(tree.readContent('/tsconfig.json')); + expect(compilerOptions.strictNullChecks).not.toBeDefined(); + }); + + it('should not add strict compiler options when true', async () => { + const tree = await schematicRunner.runSchematicAsync('workspace', { ...defaultOptions, strict: true }).toPromise(); + const { compilerOptions } = JSON.parse(tree.readContent('/tsconfig.json')); + expect(compilerOptions.strictNullChecks).toBe(true); + }); }); diff --git a/packages/schematics/angular/workspace/schema.json b/packages/schematics/angular/workspace/schema.json index c398744b149f..827a8ab3f3ba 100644 --- a/packages/schematics/angular/workspace/schema.json +++ b/packages/schematics/angular/workspace/schema.json @@ -31,6 +31,11 @@ "type": "boolean", "default": false, "x-user-analytics": 14 + }, + "strict": { + "description": "Creates a workspace with stricter TypeScript compiler options.", + "type": "boolean", + "default": false } }, "required": [ diff --git a/tests/legacy-cli/e2e/tests/build/platform-server.ts b/tests/legacy-cli/e2e/tests/build/platform-server.ts index e5ccc5db1f85..d595f84360d5 100644 --- a/tests/legacy-cli/e2e/tests/build/platform-server.ts +++ b/tests/legacy-cli/e2e/tests/build/platform-server.ts @@ -10,6 +10,8 @@ export default async function () { const veEnabled = argv['ve']; await ng('add', '@nguniversal/express-engine', '--client-project', 'test-project'); + // todo remove once https://github.com/angular/universal/pull/1229 is landed + await silentNpm('install', '@types/express', '--save-dev'); await updateJsonFile('package.json', packageJson => { const dependencies = packageJson['dependencies']; diff --git a/tests/legacy-cli/e2e/tests/misc/browsers.ts b/tests/legacy-cli/e2e/tests/misc/browsers.ts index 74220d99ec20..50fa6d76edaa 100644 --- a/tests/legacy-cli/e2e/tests/misc/browsers.ts +++ b/tests/legacy-cli/e2e/tests/misc/browsers.ts @@ -39,7 +39,7 @@ export default async function () { await replaceInFile( 'e2e/src/app.e2e-spec.ts', 'await browser.manage().logs().get(logging.Type.BROWSER)', - '[]', + '[] as any', ); // Workaround defect in getText WebDriver implementation for Safari/Edge