Skip to content

Commit

Permalink
fix: handle absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
vitonsky committed May 13, 2024
1 parent af43e6a commit 45f16d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/rules/alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ tester.run('paths-alias', rule, {
errors: ['Update import to @foo/x/y/z'],
output: `import z from '@foo/x/y/z';`,
},
{
name: 'absolute import from alias must be fixed',
filename: path.resolve('./src/index.ts'),
code: `import z from '${path.resolve('./src/foo/x/y/z')}';`,
errors: ['Update import to @foo/x/y/z'],
output: `import z from '@foo/x/y/z';`,
},
{
name: 'relative import from alias used in another alias must be fixed',
filename: path.resolve('./src/foo/index.ts'),
Expand Down
90 changes: 50 additions & 40 deletions src/rules/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,49 +87,59 @@ const rule: Rule.RuleModule = {
);
if (!compilerOptions) throw new Error('Compiler options did not found');

const pathTrailers = ['.', '/', '~'];
return {
ImportDeclaration(node) {
const importPath = node.source.value;
if (typeof importPath === 'string' && importPath.startsWith('.')) {
const filename = context.getFilename();

const resolvedIgnoredPaths = ignoredPaths.map((ignoredPath) =>
path.normalize(path.join(path.dirname(filename), ignoredPath)),
);

const absolutePath = path.normalize(
path.join(path.dirname(filename), importPath),
);

const replacement = findAlias(
compilerOptions,
baseDir,
absolutePath,
filename,
resolvedIgnoredPaths,
);

if (!replacement) return;

context.report({
node,
message: `Update import to ${replacement}`,
fix(fixer) {
const acceptableQuoteSymbols = [`'`, `"`];
const originalStringQuote = node.source.raw?.slice(0, 1);
const quote =
originalStringQuote &&
acceptableQuoteSymbols.includes(originalStringQuote)
? originalStringQuote
: acceptableQuoteSymbols[0];

return fixer.replaceText(
node.source,
quote + replacement + quote,
);
},
});
}
if (typeof importPath !== 'string') return;

const isPathInImport = pathTrailers.some((pathTrailer) =>
importPath.startsWith(pathTrailer),
);
if (!isPathInImport) return;

const filename = context.getFilename();

const resolvedIgnoredPaths = ignoredPaths.map((ignoredPath) =>
path.normalize(path.join(path.dirname(filename), ignoredPath)),
);

const absolutePath = path.normalize(
path.resolve(
importPath.startsWith('.')
? path.join(path.dirname(filename), importPath)
: importPath,
),
);

const replacement = findAlias(
compilerOptions,
baseDir,
absolutePath,
filename,
resolvedIgnoredPaths,
);

if (!replacement) return;

context.report({
node,
message: `Update import to ${replacement}`,
fix(fixer) {
const acceptableQuoteSymbols = [`'`, `"`];
const originalStringQuote = node.source.raw?.slice(0, 1);
const quote =
originalStringQuote &&
acceptableQuoteSymbols.includes(originalStringQuote)
? originalStringQuote
: acceptableQuoteSymbols[0];

return fixer.replaceText(
node.source,
quote + replacement + quote,
);
},
});
},
};
},
Expand Down

0 comments on commit 45f16d2

Please sign in to comment.