@@ -15,14 +15,14 @@ const visitImport = require("../util/visit-import")
1515 * @returns {string[] } File extensions.
1616 */
1717function getExistingExtensions ( filePath ) {
18- const basename = path . basename ( filePath , path . extname ( filePath ) )
18+ const directory = path . dirname ( filePath )
19+ const extension = path . extname ( filePath )
20+ const basename = path . basename ( filePath , extension )
21+
1922 try {
2023 return fs
21- . readdirSync ( path . dirname ( filePath ) )
22- . filter (
23- filename =>
24- path . basename ( filename , path . extname ( filename ) ) === basename
25- )
24+ . readdirSync ( directory )
25+ . filter ( filename => filename . startsWith ( `${ basename } .` ) )
2626 . map ( filename => path . extname ( filename ) )
2727 } catch ( _error ) {
2828 return [ ]
@@ -74,47 +74,56 @@ module.exports = {
7474 }
7575
7676 // Get extension.
77- const originalExt = path . extname ( name )
78- const existingExts = getExistingExtensions ( filePath )
79- const ext = path . extname ( filePath ) || existingExts . join ( " or " )
80- const style = overrideStyle [ ext ] || defaultStyle
77+ const currentExt = path . extname ( name )
78+ const actualExt = path . extname ( filePath )
79+ const style = overrideStyle [ actualExt ] || defaultStyle
80+
81+ const expectedExt = mapTypescriptExtension (
82+ context ,
83+ filePath ,
84+ actualExt
85+ )
8186
8287 // Verify.
83- if ( style === "always" && ext !== originalExt ) {
84- const fileExtensionToAdd = mapTypescriptExtension (
85- context ,
86- filePath ,
87- ext
88- )
88+ if ( style === "always" && currentExt !== expectedExt ) {
8989 context . report ( {
9090 node,
9191 messageId : "requireExt" ,
92- data : { ext : fileExtensionToAdd } ,
92+ data : { ext : expectedExt } ,
9393 fix ( fixer ) {
94- if ( existingExts . length !== 1 ) {
95- return null
96- }
9794 const index = node . range [ 1 ] - 1
9895 return fixer . insertTextBeforeRange (
9996 [ index , index ] ,
100- fileExtensionToAdd
97+ expectedExt
10198 )
10299 } ,
103100 } )
104- } else if ( style === "never" && ext === originalExt ) {
101+ }
102+
103+ if (
104+ style === "never" &&
105+ currentExt !== "" &&
106+ expectedExt !== "" &&
107+ currentExt === expectedExt
108+ ) {
109+ const otherExtensions = getExistingExtensions ( filePath )
110+
111+ let fix = fixer => {
112+ const index = name . lastIndexOf ( currentExt )
113+ const start = node . range [ 0 ] + 1 + index
114+ const end = start + currentExt . length
115+ return fixer . removeRange ( [ start , end ] )
116+ }
117+
118+ if ( otherExtensions . length > 1 ) {
119+ fix = undefined
120+ }
121+
105122 context . report ( {
106123 node,
107124 messageId : "forbidExt" ,
108- data : { ext } ,
109- fix ( fixer ) {
110- if ( existingExts . length !== 1 ) {
111- return null
112- }
113- const index = name . lastIndexOf ( ext )
114- const start = node . range [ 0 ] + 1 + index
115- const end = start + ext . length
116- return fixer . removeRange ( [ start , end ] )
117- } ,
125+ data : { ext : currentExt } ,
126+ fix,
118127 } )
119128 }
120129 }
0 commit comments