Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

feat(@schematics/angular): strict option #705

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/schematics/angular/ng-new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function (options: NgNewOptions): Rule {
name: options.name,
version: options.version,
newProjectRoot: options.newProjectRoot || 'projects',
strict: options.strict,
};
const applicationOptions: ApplicationOptions = {
projectRoot: '',
Expand Down
12 changes: 12 additions & 0 deletions packages/schematics/angular/ng-new/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe('Ng New Schematic', () => {
const content = tree.readContent('/bar/angular.json');
expect(content).toMatch(/"prefix": "pre"/);
});

it('should set strict options in tsconfig.json', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should live in the workspace schematic, because that is where the logic exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I put it there on purpose, because even if the logic is in the workspace, the user will use this option from ng new, so putting it there also verify that the option is passed well (to avoid problems like in my initial PR where I missed the passing logic). Or maybe do the test twice?

const options = { ...defaultOptions, strict: true };
const tree = schematicRunner.runSchematic('ng-new', options);
const tsconfig = JSON.parse(tree.readContent('/bar/tsconfig.json'));

expect(tsconfig.compilerOptions.noImplicitAny).toBe(true);
expect(tsconfig.compilerOptions.strictNullChecks).toBe(true);
expect(tsconfig.compilerOptions.noImplicitThis).toBe(true);
expect(tsconfig.compilerOptions.alwaysStrict).toBe(true);
expect(tsconfig.compilerOptions.strictFunctionTypes).toBe(true);
});
});
4 changes: 4 additions & 0 deletions packages/schematics/angular/ng-new/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ export interface Schema {
* Skip creating spec files.
*/
skipTests?: boolean;
/**
* Configure TypeScript in strict mode.
*/
strict?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/ng-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
"type": "boolean",
"default": false,
"alias": "S"
},
"strict": {
"type": "boolean",
"description": "Configure TypeScript in strict mode.",
"default": false
}
},
"required": [
Expand Down
7 changes: 6 additions & 1 deletion packages/schematics/angular/workspace/files/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"lib": [
"es2017",
"dom"
]
]<% if (strict) { %>,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictFunctionTypes": true<% } %>
}
}
12 changes: 12 additions & 0 deletions packages/schematics/angular/workspace/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ describe('Workspace Schematic', () => {
expect(pkg.dependencies['zone.js']).toEqual(latestVersions.ZoneJs);
expect(pkg.devDependencies['typescript']).toEqual(latestVersions.TypeScript);
});

it('should set strict options in tsconfig.json', () => {
const options = { ...defaultOptions, strict: true };
const tree = schematicRunner.runSchematic('workspace', options);
const tsconfig = JSON.parse(tree.readContent('/tsconfig.json'));

expect(tsconfig.compilerOptions.noImplicitAny).toBe(true);
expect(tsconfig.compilerOptions.strictNullChecks).toBe(true);
expect(tsconfig.compilerOptions.noImplicitThis).toBe(true);
expect(tsconfig.compilerOptions.alwaysStrict).toBe(true);
expect(tsconfig.compilerOptions.strictFunctionTypes).toBe(true);
});
});
4 changes: 4 additions & 0 deletions packages/schematics/angular/workspace/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ export interface Schema {
* The version of the Angular CLI to use.
*/
version?: string;
/**
* Configure TypeScript in strict mode.
*/
strict?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/workspace/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"type": "string",
"description": "The version of the Angular CLI to use.",
"visible": false
},
"strict": {
"type": "boolean",
"description": "Configure TypeScript in strict mode.",
"default": false
}
},
"required": [
Expand Down