Skip to content

Commit

Permalink
feat(nest): Add strict option for nest applications and default stric…
Browse files Browse the repository at this point in the history
…t option for libraries

closes: #12407
  • Loading branch information
ndcunningham committed Apr 18, 2023
1 parent 5769256 commit d20d548
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 15 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/nest/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/nest/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <projectRoot>/project.json rather than including it inside workspace.json",
Expand Down
16 changes: 16 additions & 0 deletions packages/nest/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function normalizeOptions(

return {
...options,
strict: options.strict ?? false,
appProjectRoot,
linter: options.linter ?? Linter.EsLint,
unitTestRunner: options.unitTestRunner ?? 'jest',
Expand Down
10 changes: 10 additions & 0 deletions packages/nest/src/generators/application/lib/update-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
Expand Down
1 change: 1 addition & 0 deletions packages/nest/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ApplicationGeneratorOptions {
e2eTestRunner?: 'jest' | 'none';
setParserOptionsProject?: boolean;
rootProject?: boolean;
strict?: boolean;
}

interface NormalizedOptions extends ApplicationGeneratorOptions {
Expand Down
5 changes: 5 additions & 0 deletions packages/nest/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function normalizeOptions(

const normalized: NormalizedOptions = {
...options,
strict: options.strict ?? true,
controller: options.controller ?? false,
fileName,
global: options.global ?? false,
Expand Down
5 changes: 3 additions & 2 deletions packages/nest/src/generators/library/lib/update-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export function updateTsConfig(tree: Tree, options: NormalizedOptions): void {
if (options.strict) {
json.compilerOptions = {
...json.compilerOptions,
strictNullChecks: true,
noImplicitAny: true,
strictBindCallApply: true,
forceConsistentCasingInFileNames: true,
strict: true,
noImplicitReturns: true,
noFallthroughCasesInSwitch: true,
};
}
Expand Down
18 changes: 7 additions & 11 deletions packages/nest/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,14 @@ describe('lib', () => {
it('should update the projects tsconfig with strict true', async () => {
await libraryGenerator(tree, { name: libName, strict: true });

const tsconfigJson = readJson(
tree,
`/libs/${libFileName}/tsconfig.lib.json`
);
expect(tsconfigJson.compilerOptions.strict).toBe(true);
const tsConfig = readJson(tree, `/libs/${libFileName}/tsconfig.lib.json`);
expect(tsConfig.compilerOptions.strictNullChecks).toBeTruthy();
expect(tsConfig.compilerOptions.noImplicitAny).toBeTruthy();
expect(tsConfig.compilerOptions.strictBindCallApply).toBeTruthy();
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).toBe(true);
expect(tsconfigJson.compilerOptions.noImplicitReturns).toBe(true);
expect(tsconfigJson.compilerOptions.noFallthroughCasesInSwitch).toBe(
true
);
tsConfig.compilerOptions.forceConsistentCasingInFileNames
).toBeTruthy();
expect(tsConfig.compilerOptions.noFallthroughCasesInSwitch).toBeTruthy();
});

it('should default to strict false', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nest/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <projectRoot>/project.json rather than including it inside workspace.json",
Expand Down

0 comments on commit d20d548

Please sign in to comment.