diff --git a/src/rules/alias.test.ts b/src/rules/alias.test.ts index f824cf4..0f1eb26 100644 --- a/src/rules/alias.test.ts +++ b/src/rules/alias.test.ts @@ -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'), diff --git a/src/rules/alias.ts b/src/rules/alias.ts index 9e44dc4..f075279 100644 --- a/src/rules/alias.ts +++ b/src/rules/alias.ts @@ -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, + ); + }, + }); }, }; },