Skip to content

Commit

Permalink
fix(material/core): prevent ng update schematic from checking node_mo…
Browse files Browse the repository at this point in the history
…dules (#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.
  • Loading branch information
crisbeto authored Nov 20, 2023
1 parent f2d5644 commit da692b1
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,33 @@ export function legacyImportsError(onSuccess: Rule): Rule {
const filesUsingLegacyImports = new Set<string>();

tree.visit(path => {
if (!path.endsWith('.ts')) {
if (path.includes('node_modules') || path.endsWith('.d.ts') || !path.endsWith('.ts')) {
return;
}

const content = tree.readText(path);

// Skip over any files that definitely cannot contain the legacy imports.
if (!content.includes(LEGACY_IMPORTS_START)) {
return;
}

const sourceFile = ts.createSourceFile(path, content, ts.ScriptTarget.Latest);

sourceFile.forEachChild(function walk(node) {
const isImportOrExport = ts.isImportDeclaration(node) || ts.isExportDeclaration(node);
// Only check top-level imports/exports.
for (const statement of sourceFile.statements) {
if (!ts.isImportDeclaration(statement) && !ts.isExportDeclaration(statement)) {
continue;
}

if (
isImportOrExport &&
node.moduleSpecifier &&
ts.isStringLiteralLike(node.moduleSpecifier) &&
node.moduleSpecifier.text.startsWith(LEGACY_IMPORTS_START)
statement.moduleSpecifier &&
ts.isStringLiteralLike(statement.moduleSpecifier) &&
statement.moduleSpecifier.text.startsWith(LEGACY_IMPORTS_START)
) {
filesUsingLegacyImports.add(path);
}

node.forEachChild(walk);
});
}
});

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

0 comments on commit da692b1

Please sign in to comment.