Skip to content

Add proper file extensions when importing a typescript file from a typescript file #20

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 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 @@ -85,16 +111,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
Empty file.
8 changes: 8 additions & 0 deletions tests/lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ new RuleTester({
filename: fixture("test.js"),
code: "import './c.mjs'",
},
{
filename: fixture("test.js"),
code: "import './d.js'",
},
{
filename: fixture("test.ts"),
code: "import './d.js'",
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a few invalid tests to verify the fixer(output)?

{
filename: fixture("test.js"),
code: "import './a.js'",
Expand Down