Skip to content

Commit 995c881

Browse files
authored
fix: properly handle extension override mappings in file-extension-in-import (#572)
1 parent 3c257fd commit 995c881

2 files changed

Lines changed: 101 additions & 27 deletions

File tree

‎lib/rules/file-extension-in-import.js‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function getExistingExtensions(filePath, basename) {
2525
try {
2626
return fs
2727
.readdirSync(directory)
28-
.filter(filename => filename.startsWith(`${basename}.`))
28+
.filter(
29+
filename =>
30+
path.basename(filename, path.extname(filename)) === basename
31+
)
2932
.map(filename => path.extname(filename))
3033
} catch {
3134
return []
@@ -122,13 +125,27 @@ export default {
122125

123126
// If the file doesn't exist (or is a directory), the resolver may have returned
124127
// a fallback path. In this case, path.extname may return a "fake" extension
125-
// (e.g., ".client" for "utils.client" when the actual file is "utils.client.ts").
128+
// (e.g., ".client" for "utils.client" when the actual file is "utils.client.ts"),
129+
// or an empty extension (e.g., "" for "./d" when the actual file is "d.ts").
126130
// We need to search for the actual file using the full basename from the import.
127131
const fileExists =
128132
fs.existsSync(filePath) && fs.statSync(filePath).isFile()
129-
if (actualExt !== "" && !fileExists) {
130-
// Use the full basename (e.g., "utils.client") since what path.extname
131-
// thinks is an extension (e.g., ".client") may not be a real file extension
133+
const isDirectory =
134+
fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()
135+
136+
let isDirectoryImport = false
137+
138+
if (isDirectory) {
139+
const indexExt = getIndexExtension(filePath, tryExtensions)
140+
if (indexExt) {
141+
isDirectoryImport = true
142+
actualExt = indexExt
143+
actualFilePath = path.join(filePath, `index${indexExt}`)
144+
}
145+
} else if (!fileExists) {
146+
// Use the full basename (e.g., "utils.client" or "d") since what path.extname
147+
// thinks is an extension (e.g., ".client") may not be a real file extension,
148+
// or may be empty when importing without an extension.
132149
const importBasename = path.basename(name)
133150
const extensions = getExistingExtensions(
134151
filePath,
@@ -148,25 +165,6 @@ export default {
148165
}
149166
}
150167

151-
let isDirectoryImport = false
152-
153-
// Check for directory imports. This handles both:
154-
// 1. Normal case: "./my-folder" -> "./my-folder/index.js"
155-
// 2. Dot-in-name case: "./my-things.client" -> "./my-things.client/index.js"
156-
// where path.extname incorrectly sees ".client" as an extension
157-
const isDirectory =
158-
fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()
159-
if (isDirectory) {
160-
const indexExt = getIndexExtension(filePath, tryExtensions)
161-
if (indexExt) {
162-
isDirectoryImport = true
163-
actualExt = indexExt
164-
actualFilePath = path.join(filePath, `index${indexExt}`)
165-
}
166-
}
167-
168-
const style = overrideStyle[actualExt] || defaultStyle
169-
170168
let expectedExt = convertTsExtensionToJs(
171169
context,
172170
actualFilePath,
@@ -177,14 +175,21 @@ export default {
177175
const indexExt = getIndexExtension(filePath, tryExtensions)
178176
if (indexExt) {
179177
isDirectoryImport = true
178+
actualExt = indexExt
179+
actualFilePath = path.join(filePath, `index${indexExt}`)
180180
expectedExt = convertTsExtensionToJs(
181181
context,
182-
path.join(filePath, `index${indexExt}`),
183-
indexExt
182+
actualFilePath,
183+
actualExt
184184
)
185185
}
186186
}
187187

188+
const style =
189+
overrideStyle[actualExt] ??
190+
overrideStyle[expectedExt] ??
191+
defaultStyle
192+
188193
// Verify.
189194
if (style === "always" && currentExt !== expectedExt) {
190195
context.report({
@@ -218,7 +223,7 @@ export default {
218223
expectedExt !== "" &&
219224
currentExt === expectedExt
220225
) {
221-
const otherExtensions = getExistingExtensions(filePath)
226+
const otherExtensions = getExistingExtensions(actualFilePath)
222227
/** @type {import('eslint').Rule.ReportDescriptor} */
223228
const descriptor = {
224229
node,

‎tests/lib/rules/file-extension-in-import.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,38 @@ new RuleTester({
154154
options: ["never", { ".json": "always" }],
155155
},
156156

157+
// extension overrides with typescript files (issue #405)
158+
{
159+
filename: fixture("test.ts"),
160+
code: "import './d'",
161+
options: ["always", { ".ts": "never", ".js": "never" }],
162+
},
163+
{
164+
filename: fixture("test.ts"),
165+
code: "import './a'",
166+
options: ["always", { ".ts": "never", ".js": "never" }],
167+
},
168+
{
169+
filename: fixture("test.ts"),
170+
code: "import './d'",
171+
options: ["always", { ".ts": "never" }],
172+
},
173+
{
174+
filename: fixture("test.ts"),
175+
code: "import './d'",
176+
options: ["always", { ".js": "never" }],
177+
},
178+
{
179+
filename: fixture("test.ts"),
180+
code: "import './d.js'",
181+
options: ["never", { ".ts": "always" }],
182+
},
183+
{
184+
filename: fixture("test.ts"),
185+
code: "import './d.js'",
186+
options: ["never", { ".js": "always" }],
187+
},
188+
157189
// Ignore sub-paths of modules
158190
{
159191
filename: fixture("test.js"),
@@ -330,6 +362,43 @@ new RuleTester({
330362
errors: [{ messageId: "forbidExt", data: { ext: ".mjs" } }],
331363
},
332364

365+
// extension overrides with typescript files (issue #405)
366+
{
367+
filename: fixture("test.ts"),
368+
code: "import './d.js'",
369+
output: "import './d'",
370+
options: ["always", { ".ts": "never", ".js": "never" }],
371+
errors: [{ messageId: "forbidExt", data: { ext: ".js" } }],
372+
},
373+
{
374+
filename: fixture("test.ts"),
375+
code: "import './d.js'",
376+
output: "import './d'",
377+
options: ["always", { ".js": "never" }],
378+
errors: [{ messageId: "forbidExt", data: { ext: ".js" } }],
379+
},
380+
{
381+
filename: fixture("test.ts"),
382+
code: "import './d.js'",
383+
output: "import './d'",
384+
options: ["always", { ".ts": "never" }],
385+
errors: [{ messageId: "forbidExt", data: { ext: ".js" } }],
386+
},
387+
{
388+
filename: fixture("test.ts"),
389+
code: "import './d'",
390+
output: "import './d.js'",
391+
options: ["never", { ".ts": "always" }],
392+
errors: [{ messageId: "requireExt", data: { ext: ".js" } }],
393+
},
394+
{
395+
filename: fixture("test.ts"),
396+
code: "import './d'",
397+
output: "import './d.js'",
398+
options: ["never", { ".js": "always" }],
399+
errors: [{ messageId: "requireExt", data: { ext: ".js" } }],
400+
},
401+
333402
{
334403
// name: '.js has a higher priority than .json'
335404
filename: fixture("test.js"),

0 commit comments

Comments
 (0)