Skip to content

Commit

Permalink
Add proper file extensions when importing a typescript file from a ty…
Browse files Browse the repository at this point in the history
  • Loading branch information
rosskevin committed Jun 1, 2022
1 parent 88b4d95 commit c95d9cd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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'",
},
{
filename: fixture("test.js"),
code: "import './a.js'",
Expand Down

0 comments on commit c95d9cd

Please sign in to comment.