Skip to content

Commit 7b3006f

Browse files
authored
build: enable more strictness flags in non-library code (#23077)
Updates the compiler options for the schematics and other Node-based code, and resolves any compilation errors that showed up as a result. Also fixes a bunch of references to non-existent types from `parse5`.
1 parent 49d0ccc commit 7b3006f

37 files changed

+104
-82
lines changed

src/cdk/schematics/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ts_library(
3737
# TODO(devversion): Only include jasmine for test sources (See: tsconfig types).
3838
"@npm//@types/jasmine",
3939
"@npm//@types/glob",
40+
"@npm//@types/parse5",
4041
"@npm//@types/node",
4142
"@npm//glob",
4243
"@npm//parse5",

src/cdk/schematics/ng-add/package-config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ interface PackageJson {
1616
* Sorts the keys of the given object.
1717
* @returns A new object instance with sorted keys
1818
*/
19-
function sortObjectByKeys(obj: object) {
20-
return Object.keys(obj).sort().reduce((result, key) => (result[key] = obj[key]) && result, {});
19+
function sortObjectByKeys(obj: Record<string, string>) {
20+
return Object.keys(obj).sort().reduce((result, key) => {
21+
result[key] = obj[key];
22+
return result;
23+
}, {} as Record<string, string>);
2124
}
2225

2326
/** Adds a package to the package.json in the given host tree. */

src/cdk/schematics/ng-update/find-stylesheets.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {join} from '@angular-devkit/core';
9+
import {join, Path} from '@angular-devkit/core';
1010
import {Tree} from '@angular-devkit/schematics';
1111

1212
/** Regular expression that matches stylesheet paths */
@@ -21,7 +21,7 @@ const STYLESHEET_REGEX = /.*\.(css|scss)/;
2121
*/
2222
export function findStylesheetFiles(tree: Tree, startDirectory: string = '/'): string[] {
2323
const result: string[] = [];
24-
const visitDir = dirPath => {
24+
const visitDir = (dirPath: Path) => {
2525
const {subfiles, subdirs} = tree.getDir(dirPath);
2626

2727
subfiles.forEach(fileName => {
@@ -38,6 +38,6 @@ export function findStylesheetFiles(tree: Tree, startDirectory: string = '/'): s
3838
}
3939
});
4040
};
41-
visitDir(startDirectory);
41+
visitDir(startDirectory as Path);
4242
return result;
4343
}

src/cdk/schematics/ng-update/html-parsing/elements.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {DefaultTreeDocument, DefaultTreeElement, parseFragment} from 'parse5';
9+
import {ChildNode, Element, parseFragment} from 'parse5';
1010

1111
/**
1212
* Parses a HTML fragment and traverses all AST nodes in order find elements that
1313
* include the specified attribute.
1414
*/
1515
export function findElementsWithAttribute(html: string, attributeName: string) {
16-
const document = parseFragment(html, {sourceCodeLocationInfo: true}) as DefaultTreeDocument;
17-
const elements: DefaultTreeElement[] = [];
16+
const document = parseFragment(html, {sourceCodeLocationInfo: true});
17+
const elements: Element[] = [];
1818

19-
const visitNodes = nodes => {
20-
nodes.forEach(node => {
19+
const visitNodes = (nodes: ChildNode[]) => {
20+
nodes.forEach((node: Element) => {
2121
if (node.childNodes) {
2222
visitNodes(node.childNodes);
2323
}
2424

25-
if (node.attrs && node.attrs.some(attr => attr.name === attributeName.toLowerCase())) {
25+
if (node.attrs?.some(attr => attr.name === attributeName.toLowerCase())) {
2626
elements.push(node);
2727
}
2828
});
@@ -54,7 +54,7 @@ export function findAttributeOnElementWithAttrs(html: string, name: string, attr
5454
}
5555

5656
/** Shorthand function that checks if the specified element contains the given attribute. */
57-
function hasElementAttribute(element: DefaultTreeElement, attributeName: string): boolean {
57+
function hasElementAttribute(element: Element, attributeName: string): boolean {
5858
return element.attrs && element.attrs.some(attr => attr.name === attributeName.toLowerCase());
5959
}
6060

src/cdk/schematics/ng-update/migrations/attribute-selectors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ export class AttributeSelectorsMigration extends Migration<UpgradeData> {
2323
data = getVersionUpgradeData(this, 'attributeSelectors');
2424

2525
// Only enable the migration rule if there is upgrade data.
26-
enabled = this.data.length !== 0;
26+
enabled: boolean = this.data.length !== 0;
2727

28-
visitNode(node: ts.Node) {
28+
override visitNode(node: ts.Node) {
2929
if (ts.isStringLiteralLike(node)) {
3030
this._visitStringLiteralLike(node);
3131
}
3232
}
3333

34-
visitTemplate(template: ResolvedResource) {
34+
override visitTemplate(template: ResolvedResource) {
3535
this.data.forEach(selector => {
3636
findAllSubstringIndices(template.content, selector.replace)
3737
.map(offset => template.start + offset)
3838
.forEach(start => this._replaceSelector(template.filePath, start, selector));
3939
});
4040
}
4141

42-
visitStylesheet(stylesheet: ResolvedResource): void {
42+
override visitStylesheet(stylesheet: ResolvedResource): void {
4343
this.data.forEach(selector => {
4444
const currentSelector = `[${selector.replace}]`;
4545
const updatedSelector = `[${selector.replaceWith}]`;

src/cdk/schematics/ng-update/migrations/class-inheritance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export class ClassInheritanceMigration extends Migration<UpgradeData> {
2626
// Only enable the migration rule if there is upgrade data.
2727
enabled = this.propertyNames.size !== 0;
2828

29-
init(): void {
29+
override init(): void {
3030
getVersionUpgradeData(this, 'propertyNames')
3131
.filter(data => data.limitedTo && data.limitedTo.classes)
3232
.forEach(
3333
data => data.limitedTo.classes.forEach(name => this.propertyNames.set(name, data)));
3434
}
3535

36-
visitNode(node: ts.Node): void {
36+
override visitNode(node: ts.Node): void {
3737
if (ts.isClassDeclaration(node)) {
3838
this._visitClassDeclaration(node);
3939
}

src/cdk/schematics/ng-update/migrations/class-names.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ClassNamesMigration extends Migration<UpgradeData> {
4343
// Only enable the migration rule if there is upgrade data.
4444
enabled = this.data.length !== 0;
4545

46-
visitNode(node: ts.Node): void {
46+
override visitNode(node: ts.Node): void {
4747
if (ts.isIdentifier(node)) {
4848
this._visitIdentifier(node);
4949
}

src/cdk/schematics/ng-update/migrations/constructor-signature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ConstructorSignatureMigration extends Migration<UpgradeData> {
4040
// Only enable the migration rule if there is upgrade data.
4141
enabled = this.data.length !== 0;
4242

43-
visitNode(node: ts.Node): void {
43+
override visitNode(node: ts.Node): void {
4444
if (ts.isSourceFile(node)) {
4545
this._visitSourceFile(node);
4646
}

src/cdk/schematics/ng-update/migrations/css-selectors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export class CssSelectorsMigration extends Migration<UpgradeData> {
2525
// Only enable the migration rule if there is upgrade data.
2626
enabled = this.data.length !== 0;
2727

28-
visitNode(node: ts.Node): void {
28+
override visitNode(node: ts.Node): void {
2929
if (ts.isStringLiteralLike(node)) {
3030
this._visitStringLiteralLike(node);
3131
}
3232
}
3333

34-
visitTemplate(template: ResolvedResource): void {
34+
override visitTemplate(template: ResolvedResource): void {
3535
this.data.forEach(data => {
3636
if (data.replaceIn && !data.replaceIn.html) {
3737
return;
@@ -43,7 +43,7 @@ export class CssSelectorsMigration extends Migration<UpgradeData> {
4343
});
4444
}
4545

46-
visitStylesheet(stylesheet: ResolvedResource): void {
46+
override visitStylesheet(stylesheet: ResolvedResource): void {
4747
this.data.forEach(data => {
4848
if (data.replaceIn && !data.replaceIn.stylesheet) {
4949
return;

src/cdk/schematics/ng-update/migrations/element-selectors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ export class ElementSelectorsMigration extends Migration<UpgradeData> {
2323
data = getVersionUpgradeData(this, 'elementSelectors');
2424

2525
// Only enable the migration rule if there is upgrade data.
26-
enabled = this.data.length !== 0;
26+
enabled: boolean = this.data.length !== 0;
2727

28-
visitNode(node: ts.Node): void {
28+
override visitNode(node: ts.Node): void {
2929
if (ts.isStringLiteralLike(node)) {
3030
this._visitStringLiteralLike(node);
3131
}
3232
}
3333

34-
visitTemplate(template: ResolvedResource): void {
34+
override visitTemplate(template: ResolvedResource): void {
3535
this.data.forEach(selector => {
3636
findAllSubstringIndices(template.content, selector.replace)
3737
.map(offset => template.start + offset)
3838
.forEach(start => this._replaceSelector(template.filePath, start, selector));
3939
});
4040
}
4141

42-
visitStylesheet(stylesheet: ResolvedResource): void {
42+
override visitStylesheet(stylesheet: ResolvedResource): void {
4343
this.data.forEach(selector => {
4444
findAllSubstringIndices(stylesheet.content, selector.replace)
4545
.map(offset => stylesheet.start + offset)

src/cdk/schematics/ng-update/migrations/input-names.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class InputNamesMigration extends Migration<UpgradeData> {
2929
// Only enable the migration rule if there is upgrade data.
3030
enabled = this.data.length !== 0;
3131

32-
visitStylesheet(stylesheet: ResolvedResource): void {
32+
override visitStylesheet(stylesheet: ResolvedResource): void {
3333
this.data.forEach(name => {
3434
const currentSelector = `[${name.replace}]`;
3535
const updatedSelector = `[${name.replaceWith}]`;
@@ -42,7 +42,7 @@ export class InputNamesMigration extends Migration<UpgradeData> {
4242
});
4343
}
4444

45-
visitTemplate(template: ResolvedResource): void {
45+
override visitTemplate(template: ResolvedResource): void {
4646
this.data.forEach(name => {
4747
const limitedTo = name.limitedTo;
4848
const relativeOffsets: number[] = [];

src/cdk/schematics/ng-update/migrations/method-call-arguments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class MethodCallArgumentsMigration extends Migration<UpgradeData> {
2323
// Only enable the migration rule if there is upgrade data.
2424
enabled = this.data.length !== 0;
2525

26-
visitNode(node: ts.Node): void {
26+
override visitNode(node: ts.Node): void {
2727
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
2828
this._checkPropertyAccessMethodCall(node);
2929
}

src/cdk/schematics/ng-update/migrations/misc-template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class MiscTemplateMigration extends Migration<UpgradeData> {
2222
// currently only includes migrations for V6 deprecations.
2323
enabled = this.targetVersion === TargetVersion.V6;
2424

25-
visitTemplate(template: ResolvedResource): void {
25+
override visitTemplate(template: ResolvedResource): void {
2626
// Migration for https://github.com/angular/components/pull/10325 (v6)
2727
findAllSubstringIndices(template.content, 'cdk-focus-trap').forEach(offset => {
2828
this.failures.push({

src/cdk/schematics/ng-update/migrations/output-names.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class OutputNamesMigration extends Migration<UpgradeData> {
2828
// Only enable the migration rule if there is upgrade data.
2929
enabled = this.data.length !== 0;
3030

31-
visitTemplate(template: ResolvedResource): void {
31+
override visitTemplate(template: ResolvedResource): void {
3232
this.data.forEach(name => {
3333
const limitedTo = name.limitedTo;
3434
const relativeOffsets: number[] = [];

src/cdk/schematics/ng-update/migrations/property-names.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class PropertyNamesMigration extends Migration<UpgradeData> {
2323
// Only enable the migration rule if there is upgrade data.
2424
enabled = this.data.length !== 0;
2525

26-
visitNode(node: ts.Node): void {
26+
override visitNode(node: ts.Node): void {
2727
if (ts.isPropertyAccessExpression(node)) {
2828
this._visitPropertyAccessExpression(node);
2929
}

src/cdk/schematics/tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
"outDir": "../../../dist/packages/cdk/schematics",
88
"noEmitOnError": false,
99
"strictNullChecks": true,
10+
"noImplicitOverride": true,
1011
"noImplicitReturns": true,
12+
"noImplicitAny": true,
1113
"skipDefaultLibCheck": true,
14+
"noFallthroughCasesInSwitch": true,
15+
"noUnusedLocals": false,
16+
"noImplicitThis": true,
1217
"skipLibCheck": true,
1318
"sourceMap": true,
1419
"target": "es2015",

src/cdk/schematics/utils/build-component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ export function buildComponent(options: ComponentOptions,
175175
// Add the default component option values to the options if an option is not explicitly
176176
// specified but a default component option is available.
177177
Object.keys(options)
178-
.filter(optionName => options[optionName] == null && defaultComponentOptions[optionName])
179-
.forEach(optionName => options[optionName] = defaultComponentOptions[optionName]);
178+
.filter((optionName: keyof ComponentOptions) => {
179+
return options[optionName] == null && defaultComponentOptions[optionName];
180+
})
181+
.forEach((optionName: keyof ComponentOptions) => {
182+
(options as any)[optionName] = (defaultComponentOptions as ComponentOptions)[optionName];
183+
});
180184

181185
if (options.path === undefined) {
182186
// TODO(jelbourn): figure out if the need for this `as any` is a bug due to two different
@@ -214,7 +218,7 @@ export function buildComponent(options: ComponentOptions,
214218

215219
// Key-value object that includes the specified additional files with their loaded content.
216220
// The resolved contents can be used inside EJS templates.
217-
const resolvedFiles = {};
221+
const resolvedFiles: Record<string, string> = {};
218222

219223
for (let key in additionalFiles) {
220224
if (additionalFiles[key]) {

src/cdk/schematics/utils/html-manipulation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {SchematicsException, Tree} from '@angular-devkit/schematics';
1010
import {getChildElementIndentation} from './parse5-element';
11-
import {DefaultTreeDocument, DefaultTreeElement, parse as parseHtml} from 'parse5';
11+
import {Element, parse as parseHtml} from 'parse5';
1212

1313
/** Appends the given element HTML fragment to the `<head>` element of the specified HTML file. */
1414
export function appendHtmlElementToHead(host: Tree, htmlFilePath: string, elementHtml: string) {
@@ -44,7 +44,7 @@ export function appendHtmlElementToHead(host: Tree, htmlFilePath: string, elemen
4444
}
4545

4646
/** Parses the given HTML file and returns the head element if available. */
47-
export function getHtmlHeadTagElement(htmlContent: string): DefaultTreeElement | null {
47+
export function getHtmlHeadTagElement(htmlContent: string): Element | null {
4848
return getElementByTagName('head', htmlContent);
4949
}
5050

@@ -85,12 +85,12 @@ export function addBodyClass(host: Tree, htmlFilePath: string, className: string
8585

8686
/** Finds an element by its tag name. */
8787
function getElementByTagName(tagName: string, htmlContent: string):
88-
DefaultTreeElement | null {
89-
const document = parseHtml(htmlContent, {sourceCodeLocationInfo: true}) as DefaultTreeDocument;
88+
Element | null {
89+
const document = parseHtml(htmlContent, {sourceCodeLocationInfo: true});
9090
const nodeQueue = [...document.childNodes];
9191

9292
while (nodeQueue.length) {
93-
const node = nodeQueue.shift() as DefaultTreeElement;
93+
const node = nodeQueue.shift() as Element;
9494

9595
if (node.nodeName.toLowerCase() === tagName) {
9696
return node;

src/cdk/schematics/utils/parse5-element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88

99
import {SchematicsException} from '@angular-devkit/schematics';
10-
import {DefaultTreeElement} from 'parse5';
10+
import {Element} from 'parse5';
1111

1212
/** Determines the indentation of child elements for the given Parse5 element. */
13-
export function getChildElementIndentation(element: DefaultTreeElement) {
13+
export function getChildElementIndentation(element: Element) {
1414
const childElement = element.childNodes
15-
.find(node => node['tagName']) as DefaultTreeElement | null;
15+
.find(node => (node as Element).tagName) as Element | null;
1616

1717
if ((childElement && !childElement.sourceCodeLocation) || !element.sourceCodeLocation) {
1818
throw new SchematicsException('Cannot determine child element indentation because the ' +

src/cdk/schematics/utils/schematic-options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
1010
import {isJsonObject, JsonObject} from '@angular-devkit/core';
11+
import {Schema, Style} from '@schematics/angular/component/schema';
1112

1213
/**
1314
* Returns the default options for the `@schematics/angular:component` schematic which would
@@ -16,7 +17,7 @@ import {isJsonObject, JsonObject} from '@angular-devkit/core';
1617
* This is necessary because the Angular CLI only exposes the default values for the "--style",
1718
* "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic.
1819
*/
19-
export function getDefaultComponentOptions(project: ProjectDefinition) {
20+
export function getDefaultComponentOptions(project: ProjectDefinition): Partial<Schema> {
2021
// Note: Not all options which are available when running "ng new" will be stored in the
2122
// workspace config. List of options which will be available in the configuration:
2223
// angular/angular-cli/blob/master/packages/schematics/angular/application/index.ts#L109-L131
@@ -30,7 +31,7 @@ export function getDefaultComponentOptions(project: ProjectDefinition) {
3031
}
3132

3233
return {
33-
style: getDefaultComponentOption(project, ['style', 'styleext'], 'css'),
34+
style: getDefaultComponentOption<Style>(project, ['style', 'styleext'], Style.Css),
3435
inlineStyle: getDefaultComponentOption(project, ['inlineStyle'], false),
3536
inlineTemplate: getDefaultComponentOption(project, ['inlineTemplate'], false),
3637
skipTests: skipTests,

0 commit comments

Comments
 (0)