@@ -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