Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 [bugfix] Add proper file extensions when importing a typescript file from a typescript file #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const getTryExtensions = require("../util/get-try-extensions")
const visitImport = require("../util/visit-import")
const packageNamePattern = /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
const corePackageOverridePattern = /^(?:assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|worker_threads|zlib)[/\\]$/u
const typescriptFileExtensionsMapping = {
".ts": ".js",
".cts": ".cjs",
".mts": ".mjs",
}

/**
* Get all file extensions of the files which have the same basename.
Expand All @@ -31,6 +36,27 @@ function getExistingExtensions(filePath) {
}
}

/**
* Get the file extension that should be added in an import statement,
* based on the given file extension of the referenced file.
*
* For example, in typescript, when referencing another typescript from a typescript file,
* a .js extension should be used instead of the original .ts extension of the referenced file.
* @param {string} referencedFileExt The original file extension of the referenced file.
* @param {string} referencingFileExt The original file extension of the file the contains the import statement.
* @returns {string} The file extension to append to the import statement.
*/
function getFileExtensionToAdd(referencedFileExt, referencingFileExt) {
if (
referencingFileExt in typescriptFileExtensionsMapping &&
referencedFileExt in typescriptFileExtensionsMapping
) {
return typescriptFileExtensionsMapping[referencedFileExt]
}

return referencedFileExt
}

module.exports = {
meta: {
docs: {
Expand Down Expand Up @@ -92,16 +118,26 @@ module.exports = {

// Verify.
if (style === "always" && ext !== originalExt) {
const referencingFileExt = path.extname(
context.getPhysicalFilename()
)
const fileExtensionToAdd = getFileExtensionToAdd(
ext,
referencingFileExt
)
context.report({
node,
messageId: "requireExt",
data: { ext },
data: { ext: fileExtensionToAdd },
fix(fixer) {
if (existingExts.length !== 1) {
return null
}
const index = node.range[1] - 1
return fixer.insertTextBeforeRange([index, index], ext)
return fixer.insertTextBeforeRange(
[index, index],
fileExtensionToAdd
)
},
})
} else if (style === "never" && ext === originalExt) {
Expand Down