From c520a4023c49a6c4393e156edfccb10cb855046f Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Tue, 18 Apr 2023 08:14:21 -0600 Subject: [PATCH] feat(nest): Add strict option for nest applications and default strict option for libraries closes: #12407 --- .../packages/nest/generators/application.json | 5 +++++ .../packages/nest/generators/library.json | 2 +- .../generators/application/application.spec.ts | 16 ++++++++++++++++ .../application/lib/normalize-options.ts | 1 + .../application/lib/update-tsconfig.ts | 10 ++++++++++ .../nest/src/generators/application/schema.d.ts | 1 + .../nest/src/generators/application/schema.json | 5 +++++ .../generators/library/lib/normalize-options.ts | 1 + packages/nest/src/generators/library/schema.json | 2 +- 9 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/generated/packages/nest/generators/application.json b/docs/generated/packages/nest/generators/application.json index 6350158831da0c..b14777ae8e8e00 100644 --- a/docs/generated/packages/nest/generators/application.json +++ b/docs/generated/packages/nest/generators/application.json @@ -68,6 +68,11 @@ "type": "boolean", "description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.", "default": false + }, + "strict": { + "type": "boolean", + "description": "Adds strictNullChecks, noImplicitAny, strictBindCallApply, forceConsistentCasingInFileNames and noFallthroughCasesInSwitch to tsconfig.", + "default": false } }, "additionalProperties": false, diff --git a/docs/generated/packages/nest/generators/library.json b/docs/generated/packages/nest/generators/library.json index 2fe5b804d4fa71..6493e416761822 100644 --- a/docs/generated/packages/nest/generators/library.json +++ b/docs/generated/packages/nest/generators/library.json @@ -110,7 +110,7 @@ "strict": { "description": "Whether to enable tsconfig strict mode or not.", "type": "boolean", - "default": false + "default": true }, "standaloneConfig": { "description": "Split the project configuration into /project.json rather than including it inside workspace.json", diff --git a/packages/nest/src/generators/application/application.spec.ts b/packages/nest/src/generators/application/application.spec.ts index cbd50dc5e1dbbf..c3a722bcb7e9f3 100644 --- a/packages/nest/src/generators/application/application.spec.ts +++ b/packages/nest/src/generators/application/application.spec.ts @@ -59,6 +59,22 @@ describe('application generator', () => { ]); }); + it('should add strict checks with --strict', async () => { + await applicationGenerator(tree, { name: appName, strict: true }); + const tsConfig = devkit.readJson( + tree, + `apps/${appDirectory}/tsconfig.app.json` + ); + + expect(tsConfig.compilerOptions.strictNullChecks).toBeTruthy(); + expect(tsConfig.compilerOptions.noImplicitAny).toBeTruthy(); + expect(tsConfig.compilerOptions.strictBindCallApply).toBeTruthy(); + expect( + tsConfig.compilerOptions.forceConsistentCasingInFileNames + ).toBeTruthy(); + expect(tsConfig.compilerOptions.noFallthroughCasesInSwitch).toBeTruthy(); + }); + describe('--skipFormat', () => { it('should format files', async () => { jest.spyOn(devkit, 'formatFiles'); diff --git a/packages/nest/src/generators/application/lib/normalize-options.ts b/packages/nest/src/generators/application/lib/normalize-options.ts index dd7dc07483906d..4e358a0c2f9f5c 100644 --- a/packages/nest/src/generators/application/lib/normalize-options.ts +++ b/packages/nest/src/generators/application/lib/normalize-options.ts @@ -25,6 +25,7 @@ export function normalizeOptions( return { ...options, + strict: options.strict ?? false, appProjectRoot, linter: options.linter ?? Linter.EsLint, unitTestRunner: options.unitTestRunner ?? 'jest', diff --git a/packages/nest/src/generators/application/lib/update-tsconfig.ts b/packages/nest/src/generators/application/lib/update-tsconfig.ts index 748b82b02149cf..232f605f6b0486 100644 --- a/packages/nest/src/generators/application/lib/update-tsconfig.ts +++ b/packages/nest/src/generators/application/lib/update-tsconfig.ts @@ -9,6 +9,16 @@ export function updateTsConfig(tree: Tree, options: NormalizedOptions): void { (json) => { json.compilerOptions.emitDecoratorMetadata = true; json.compilerOptions.target = 'es2015'; + if (options.strict) { + json.compilerOptions = { + ...json.compilerOptions, + strictNullChecks: true, + noImplicitAny: true, + strictBindCallApply: true, + forceConsistentCasingInFileNames: true, + noFallthroughCasesInSwitch: true, + }; + } return json; } ); diff --git a/packages/nest/src/generators/application/schema.d.ts b/packages/nest/src/generators/application/schema.d.ts index e5b9d3d54b74d9..42a2b6ad3bd2e6 100644 --- a/packages/nest/src/generators/application/schema.d.ts +++ b/packages/nest/src/generators/application/schema.d.ts @@ -13,6 +13,7 @@ export interface ApplicationGeneratorOptions { e2eTestRunner?: 'jest' | 'none'; setParserOptionsProject?: boolean; rootProject?: boolean; + strict?: boolean; } interface NormalizedOptions extends ApplicationGeneratorOptions { diff --git a/packages/nest/src/generators/application/schema.json b/packages/nest/src/generators/application/schema.json index 3acc26f63f81dc..55c69037e4a10e 100644 --- a/packages/nest/src/generators/application/schema.json +++ b/packages/nest/src/generators/application/schema.json @@ -68,6 +68,11 @@ "type": "boolean", "description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.", "default": false + }, + "strict": { + "type": "boolean", + "description": "Adds strictNullChecks, noImplicitAny, strictBindCallApply, forceConsistentCasingInFileNames and noFallthroughCasesInSwitch to tsconfig.", + "default": false } }, "additionalProperties": false, diff --git a/packages/nest/src/generators/library/lib/normalize-options.ts b/packages/nest/src/generators/library/lib/normalize-options.ts index 98921c54bbbf12..8aaeb0a7c31d7e 100644 --- a/packages/nest/src/generators/library/lib/normalize-options.ts +++ b/packages/nest/src/generators/library/lib/normalize-options.ts @@ -28,6 +28,7 @@ export function normalizeOptions( const normalized: NormalizedOptions = { ...options, + strict: options.strict ?? true, controller: options.controller ?? false, fileName, global: options.global ?? false, diff --git a/packages/nest/src/generators/library/schema.json b/packages/nest/src/generators/library/schema.json index df3e8967e2e9c0..2f96990c52b635 100644 --- a/packages/nest/src/generators/library/schema.json +++ b/packages/nest/src/generators/library/schema.json @@ -110,7 +110,7 @@ "strict": { "description": "Whether to enable tsconfig strict mode or not.", "type": "boolean", - "default": false + "default": true }, "standaloneConfig": { "description": "Split the project configuration into /project.json rather than including it inside workspace.json",