Skip to content

Commit da692b1

Browse files
authored
fix(material/core): prevent ng update schematic from checking node_modules (#28152)
Fixes that the `ng update` schematic was cheking `node_modules` and .d.ts files when looking for legacy imports. Also changes the logic to only look at top-level imports and exports, as well as any files that don't include `@angular/material` at all. Fixes #28150.
1 parent f2d5644 commit da692b1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/material/schematics/ng-update/migrations/legacy-imports-error.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,33 @@ export function legacyImportsError(onSuccess: Rule): Rule {
2626
const filesUsingLegacyImports = new Set<string>();
2727

2828
tree.visit(path => {
29-
if (!path.endsWith('.ts')) {
29+
if (path.includes('node_modules') || path.endsWith('.d.ts') || !path.endsWith('.ts')) {
3030
return;
3131
}
3232

3333
const content = tree.readText(path);
34+
35+
// Skip over any files that definitely cannot contain the legacy imports.
36+
if (!content.includes(LEGACY_IMPORTS_START)) {
37+
return;
38+
}
39+
3440
const sourceFile = ts.createSourceFile(path, content, ts.ScriptTarget.Latest);
3541

36-
sourceFile.forEachChild(function walk(node) {
37-
const isImportOrExport = ts.isImportDeclaration(node) || ts.isExportDeclaration(node);
42+
// Only check top-level imports/exports.
43+
for (const statement of sourceFile.statements) {
44+
if (!ts.isImportDeclaration(statement) && !ts.isExportDeclaration(statement)) {
45+
continue;
46+
}
3847

3948
if (
40-
isImportOrExport &&
41-
node.moduleSpecifier &&
42-
ts.isStringLiteralLike(node.moduleSpecifier) &&
43-
node.moduleSpecifier.text.startsWith(LEGACY_IMPORTS_START)
49+
statement.moduleSpecifier &&
50+
ts.isStringLiteralLike(statement.moduleSpecifier) &&
51+
statement.moduleSpecifier.text.startsWith(LEGACY_IMPORTS_START)
4452
) {
4553
filesUsingLegacyImports.add(path);
4654
}
47-
48-
node.forEachChild(walk);
49-
});
55+
}
5056
});
5157

5258
// If there are no legacy imports left, we can continue with the migrations.

0 commit comments

Comments
 (0)