Skip to content

Commit a685559

Browse files
crisbetowagnermaciel
authored andcommitted
build: enable strictFunctionTypes in schematics (#23111)
Makes the schematics code compliant with the `strictFunctionTypes` compiler option. (cherry picked from commit 2628d27)
1 parent 9b8575f commit a685559

File tree

8 files changed

+31
-22
lines changed

8 files changed

+31
-22
lines changed

src/cdk/schematics/ng-update/devkit-migration-rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function createMigrationSchematicRule(
7575
const analyzedFiles = new Set<WorkspacePath>();
7676
const fileSystem = new DevkitFileSystem(tree);
7777
const projectNames = workspace.projects.keys();
78-
const migrations: NullableDevkitMigration[] = [...cdkMigrations, ...extraMigrations];
78+
const migrations = [...cdkMigrations, ...extraMigrations] as NullableDevkitMigration[];
7979
let hasFailures = false;
8080

8181
for (const projectName of projectNames) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export function findElementsWithAttribute(html: string, attributeName: string) {
1717
const elements: Element[] = [];
1818

1919
const visitNodes = (nodes: ChildNode[]) => {
20-
nodes.forEach((node: Element) => {
20+
nodes.forEach(n => {
21+
const node = n as Element;
22+
2123
if (node.childNodes) {
2224
visitNodes(node.childNodes);
2325
}

src/cdk/schematics/ng-update/typescript/base-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export function determineBaseTypes(node: ts.ClassDeclaration): string[]|null {
1818
.reduce((types, clause) => types.concat(clause.types), [] as ts.ExpressionWithTypeArguments[])
1919
.map(typeExpression => typeExpression.expression)
2020
.filter(expression => expression && ts.isIdentifier(expression))
21-
.map((identifier: ts.Identifier) => identifier.text);
21+
.map(identifier => (identifier as ts.Identifier).text);
2222
}

src/cdk/schematics/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"noUnusedLocals": false,
1616
"noImplicitThis": true,
1717
"skipLibCheck": true,
18+
"strictFunctionTypes": true,
1819
"sourceMap": true,
1920
"target": "es2015",
2021
"types": [

src/cdk/schematics/update-tool/migration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export type PostMigrationAction = void | {
2727
/** Creates a constructor type for the specified type. */
2828
export type Constructor<T> = (new (...args: any[]) => T);
2929
/** Gets a constructor type for the passed migration data. */
30-
export type MigrationCtor<Data, Context = never> = Constructor<Migration<Data, Context>>;
30+
export type MigrationCtor<Data, Context = any> = Constructor<Migration<Data, Context>>;
3131

32-
export abstract class Migration<Data, Context = never> {
32+
export abstract class Migration<Data, Context = any> {
3333
/** List of migration failures that need to be reported. */
3434
failures: MigrationFailure[] = [];
3535

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ function indentTextContent(text: string, numSpaces: number): string {
156156
export function buildComponent(options: ComponentOptions,
157157
additionalFiles: {[key: string]: string} = {}): Rule {
158158

159-
return async (host: Tree, context: FileSystemSchematicContext) => {
159+
return async (host, ctx) => {
160+
const context = ctx as FileSystemSchematicContext;
160161
const workspace = await getWorkspace(host);
161162
const project = getProjectFromWorkspace(workspace, options.project);
162163
const defaultComponentOptions = getDefaultComponentOptions(project);
@@ -175,12 +176,10 @@ export function buildComponent(options: ComponentOptions,
175176
// Add the default component option values to the options if an option is not explicitly
176177
// specified but a default component option is available.
177178
Object.keys(options)
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-
});
179+
.filter(key => options[key as keyof ComponentOptions] == null &&
180+
defaultComponentOptions[key as keyof ComponentOptions])
181+
.forEach(key => (options as any)[key] =
182+
(defaultComponentOptions as ComponentOptions)[key as keyof ComponentOptions]);
184183

185184
if (options.path === undefined) {
186185
// TODO(jelbourn): figure out if the need for this `as any` is a bug due to two different

src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-template-check.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,31 @@ export function isHammerJsUsedInTemplate(html: string):
3434
let customEvents = false;
3535
let standardEvents = false;
3636
const visitNodes = (nodes: parse5.ChildNode[]) => {
37-
nodes.forEach((node: parse5.Element) => {
38-
if (node.attrs) {
39-
for (let attr of node.attrs) {
40-
if (!customEvents && CUSTOM_MATERIAL_HAMMERJS_EVENS.some(e => `(${e})` === attr.name)) {
41-
customEvents = true;
42-
}
43-
if (!standardEvents && STANDARD_HAMMERJS_EVENTS.some(e => `(${e})` === attr.name)) {
44-
standardEvents = true;
45-
}
37+
nodes.forEach(node => {
38+
if (!isElement(node)) {
39+
return;
40+
}
41+
42+
for (let attr of node.attrs) {
43+
if (!customEvents && CUSTOM_MATERIAL_HAMMERJS_EVENS.some(e => `(${e})` === attr.name)) {
44+
customEvents = true;
45+
}
46+
if (!standardEvents && STANDARD_HAMMERJS_EVENTS.some(e => `(${e})` === attr.name)) {
47+
standardEvents = true;
4648
}
4749
}
4850

4951
// Do not continue traversing the AST if both type of HammerJS
5052
// usages have been detected already.
51-
if (node.childNodes && (!customEvents || !standardEvents)) {
53+
if (!customEvents || !standardEvents) {
5254
visitNodes(node.childNodes);
5355
}
5456
});
5557
};
5658
visitNodes(document.childNodes);
5759
return {customEvents, standardEvents};
5860
}
61+
62+
function isElement(node: any): node is parse5.Element {
63+
return !!node.attrs;
64+
}

src/material/schematics/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"skipDefaultLibCheck": true,
1515
"noUnusedLocals": false,
1616
"noUnusedParameters": false,
17+
"strictFunctionTypes": true,
1718
"skipLibCheck": true,
1819
"sourceMap": true,
1920
"declaration": true,

0 commit comments

Comments
 (0)