Skip to content

Commit 8a98236

Browse files
crisbetoamysorto
authored andcommitted
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`. (cherry picked from commit 7b3006f)
1 parent e14b93c commit 8a98236

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)

0 commit comments

Comments
 (0)