Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute paths does not fixed #66

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.filename;

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
Loading