Skip to content

Commit 52b4461

Browse files
authored
Merge pull request #66 from vitonsky/65-absolute-paths-does-not-fixed
Absolute paths does not fixed
2 parents af43e6a + 1ea3b19 commit 52b4461

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

src/rules/alias.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ tester.run('paths-alias', rule, {
5757
errors: ['Update import to @foo/x/y/z'],
5858
output: `import z from '@foo/x/y/z';`,
5959
},
60+
{
61+
name: 'absolute import from alias must be fixed',
62+
filename: path.resolve('./src/index.ts'),
63+
code: `import z from '${path.resolve('./src/foo/x/y/z')}';`,
64+
errors: ['Update import to @foo/x/y/z'],
65+
output: `import z from '@foo/x/y/z';`,
66+
},
6067
{
6168
name: 'relative import from alias used in another alias must be fixed',
6269
filename: path.resolve('./src/foo/index.ts'),

src/rules/alias.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,49 +87,59 @@ const rule: Rule.RuleModule = {
8787
);
8888
if (!compilerOptions) throw new Error('Compiler options did not found');
8989

90+
const pathTrailers = ['.', '/', '~'];
9091
return {
9192
ImportDeclaration(node) {
9293
const importPath = node.source.value;
93-
if (typeof importPath === 'string' && importPath.startsWith('.')) {
94-
const filename = context.getFilename();
95-
96-
const resolvedIgnoredPaths = ignoredPaths.map((ignoredPath) =>
97-
path.normalize(path.join(path.dirname(filename), ignoredPath)),
98-
);
99-
100-
const absolutePath = path.normalize(
101-
path.join(path.dirname(filename), importPath),
102-
);
103-
104-
const replacement = findAlias(
105-
compilerOptions,
106-
baseDir,
107-
absolutePath,
108-
filename,
109-
resolvedIgnoredPaths,
110-
);
111-
112-
if (!replacement) return;
113-
114-
context.report({
115-
node,
116-
message: `Update import to ${replacement}`,
117-
fix(fixer) {
118-
const acceptableQuoteSymbols = [`'`, `"`];
119-
const originalStringQuote = node.source.raw?.slice(0, 1);
120-
const quote =
121-
originalStringQuote &&
122-
acceptableQuoteSymbols.includes(originalStringQuote)
123-
? originalStringQuote
124-
: acceptableQuoteSymbols[0];
125-
126-
return fixer.replaceText(
127-
node.source,
128-
quote + replacement + quote,
129-
);
130-
},
131-
});
132-
}
94+
if (typeof importPath !== 'string') return;
95+
96+
const isPathInImport = pathTrailers.some((pathTrailer) =>
97+
importPath.startsWith(pathTrailer),
98+
);
99+
if (!isPathInImport) return;
100+
101+
const filename = context.filename;
102+
103+
const resolvedIgnoredPaths = ignoredPaths.map((ignoredPath) =>
104+
path.normalize(path.join(path.dirname(filename), ignoredPath)),
105+
);
106+
107+
const absolutePath = path.normalize(
108+
path.resolve(
109+
importPath.startsWith('.')
110+
? path.join(path.dirname(filename), importPath)
111+
: importPath,
112+
),
113+
);
114+
115+
const replacement = findAlias(
116+
compilerOptions,
117+
baseDir,
118+
absolutePath,
119+
filename,
120+
resolvedIgnoredPaths,
121+
);
122+
123+
if (!replacement) return;
124+
125+
context.report({
126+
node,
127+
message: `Update import to ${replacement}`,
128+
fix(fixer) {
129+
const acceptableQuoteSymbols = [`'`, `"`];
130+
const originalStringQuote = node.source.raw?.slice(0, 1);
131+
const quote =
132+
originalStringQuote &&
133+
acceptableQuoteSymbols.includes(originalStringQuote)
134+
? originalStringQuote
135+
: acceptableQuoteSymbols[0];
136+
137+
return fixer.replaceText(
138+
node.source,
139+
quote + replacement + quote,
140+
);
141+
},
142+
});
133143
},
134144
};
135145
},

0 commit comments

Comments
 (0)