Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"version": "7.0.0-beta.0",
"factory": "./update-7/fix-scss-imports",
"description": "Fix SCSS imports"
},
"fix-mixed-case-imports": {
"version": "7.0.0-beta.0",
"factory": "./update-7/fix-mixed-case-imports",
"description": "Fix import statements where the file case does not match the filesystem"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fix mixed case imports should change import paths that are incorrect 1`] = `
"import { A } from './a';
import { C } from './Path/C';
import { D } from './Path/d';
import { NonExistent } from './non';
export class B extends A {
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';

describe('fix mixed case imports', () => {
let tree: Tree;
const runner = new SchematicTestRunner(
'schematics',
require.resolve('../migration-collection.json')
);
const angularJson = {
version: 1,
projects: {
test: {
projectType: 'application',
root: '',
architect: {},
},
},
};

function setupTest() {
tree = Tree.empty();
tree.create('/angular.json', JSON.stringify(angularJson));
}

it('should not change import paths that are correct', async () => {
setupTest();
tree.create('/index.ts', `import { A } from './a';`);
tree.create('/a.ts', `export class A {}`);
tree.create('/empty.ts', ``);
await runner
.runSchematicAsync('fix-mixed-case-imports', {}, tree)
.toPromise();
expect(tree.readText('/index.ts')).toBe(`import { A } from './a';`);
});

it('should update import paths with backslashes', async () => {
setupTest();
tree.create('/windows.ts', "import { A } from '.\\a';");
tree.create('/a.ts', `export class A {}`);
await runner
.runSchematicAsync('fix-mixed-case-imports', {}, tree)
.toPromise();
expect(tree.readText('/windows.ts')).toBe(`import { A } from './a';\n`);
});

it('should change import paths that are incorrect', async () => {
setupTest();
tree.create(
'/index.ts',
[
`import { A } from './A';`,
`import { C } from './path/c';`,
`import { D } from './Path/D';`,
`import { NonExistent } from './non';`,
`export class B extends A {`,
`}`,
'',
].join(`\n`)
);
tree.create('/a.ts', `export class A {}`);
tree.create('/Path/C.ts', `export class C {}`);
tree.create('/Path/d.ts', `export class D {}`);
await runner
.runSchematicAsync('fix-mixed-case-imports', {}, tree)
.toPromise();
expect(tree.readText('/index.ts')).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { Path, dirname, join, split } from '@angular-devkit/core';
import { Rule, Tree } from '@angular-devkit/schematics';
import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';

import * as path from 'path';

import { getWorkspace } from '../../utility/workspace';

function updateTypescriptImports(filePath: string, tree: Tree): void {
const fileContent = tree.read(filePath)?.toString();
if (!fileContent) {
return;
}
const source = ts.createSourceFile(
filePath,
fileContent,
ts.ScriptTarget.Latest,
true
);
// Get all import declarations
const importDeclarations = source.statements.filter(
(statement) => statement.kind === ts.SyntaxKind.ImportDeclaration
) as ts.ImportDeclaration[];
// Just local import targets
const localImportDeclarations = importDeclarations.filter(
(importDeclaration) => {
const importPath = importDeclaration.moduleSpecifier.getText(source);
const importPathWithoutQuotes = importPath.substring(
1,
importPath.length - 1
);
return importPathWithoutQuotes.startsWith('.');
}
);
const transformers: ts.TransformerFactory<ts.SourceFile>[] = [];
localImportDeclarations.forEach((importDeclaration) => {
const importPath = importDeclaration.moduleSpecifier.getText(source);
const importPathWithoutQuotes = importPath.substring(
1,
importPath.length - 1
);
const absoluteImportPath = path.join(
path.dirname(filePath),
`${importPathWithoutQuotes}.ts`.replace(/\\/g, '/')
);
const fileEntry = tree.get(absoluteImportPath);
if (
fileEntry &&
fileEntry.path === absoluteImportPath &&
!importPathWithoutQuotes.includes('\\')
) {
// File exists. Do nothing.
return;
}
const pathFragments = split(absoluteImportPath as Path).slice(1);
const newPath: string[] = [];
let dir = tree.root;
for (const pathFragment of pathFragments) {
if (pathFragment.endsWith('.ts')) {
if (dir.subfiles.includes(pathFragment)) {
// File exists. We're done.
newPath.push(pathFragment.replace(/\.ts$/, ''));
break;
}
// File does not exist. Try to find a file with the same name but different casing.
const matchingFile = dir.subfiles.find((subFile) =>
subFile.toLowerCase().includes(pathFragment.toLowerCase())
);
if (matchingFile) {
// Found a matching file. Use it.
newPath.push(matchingFile.replace(/\.ts$/, ''));
break;
}
} else {
if (dir.subdirs.includes(pathFragment)) {
// Sub dir exists. Continue.
dir = dir.dir(pathFragment);
newPath.push(pathFragment);
continue;
}
// Dir does not exist. Try to find a dir with the same name but different casing.
const matchingDir = dir.subdirs.find((subdir) =>
subdir.toLowerCase().includes(pathFragment.toLowerCase())
);
if (matchingDir) {
// Found a matching dir. Use it.
dir = dir.dir(matchingDir);
newPath.push(matchingDir);
continue;
}
}
// Nothing found. Can't fix this.
return;
}
const newImportPathAbsolute = join(tree.root.path, ...newPath);
let newImportPathRelative = path.relative(
dirname(filePath as Path),
newImportPathAbsolute
);
if (!newImportPathRelative.startsWith('.')) {
newImportPathRelative = `./${newImportPathRelative}`;
}
if (newImportPathRelative !== importPathWithoutQuotes) {
transformers.push((transformationContext) => (file) => {
const visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node) && node === importDeclaration) {
return transformationContext.factory.updateImportDeclaration(
node as ts.ImportDeclaration,
node.decorators,
node.modifiers,
node.importClause,
transformationContext.factory.createStringLiteral(
newImportPathRelative,
true
),
node.assertClause
);
}
return ts.visitEachChild(node, visitor, transformationContext);
};
return ts.visitNode(file, visitor);
});
}
});
if (transformers.length > 0) {
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const result = ts.transform([source], transformers);
const newContent = printer.printNode(
ts.EmitHint.Unspecified,
result.transformed[0],
undefined as any
);
tree.overwrite(filePath, newContent);
}
}

async function visitTypescriptFiles(tree: Tree): Promise<void> {
const { workspace } = await getWorkspace(tree);
workspace.projects.forEach((project) => {
tree.getDir(project.root).visit((filePath) => {
if (filePath.endsWith('.ts')) {
updateTypescriptImports(filePath, tree);
}
});
});
}

export default function (): Rule {
return async (tree: Tree) => {
await visitTypescriptFiles(tree);
};
}