-
Notifications
You must be signed in to change notification settings - Fork 13k
Use explicit extensions for imports within src #58421
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
+2,583
−2,360
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62d6e1f
Extensions
jakebailey c89b866
Make paths for tests.ts checked
jakebailey 9461e1d
Revert package.json
jakebailey d8c8a86
Revert cancellationToken
jakebailey 10806b9
Undo other changes from ESM PR
jakebailey e83f395
newline
jakebailey c4e88da
Some other missing extensions
jakebailey aab3ba9
lint
jakebailey 3078b09
settings.json
jakebailey dc4a340
Disclaimer
jakebailey ac9698c
Also mention relative module extensions
jakebailey 9228163
Merge branch 'main' into js-extensions
jakebailey 0ff3490
Merge branch 'main' into js-extensions
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const { createRule } = require("./utils.cjs"); | ||
|
||
module.exports = createRule({ | ||
name: "js-extensions", | ||
meta: { | ||
docs: { | ||
description: ``, | ||
}, | ||
messages: { | ||
missingJsExtension: `This relative module reference is missing a '.js' extension`, | ||
}, | ||
schema: [], | ||
type: "suggestion", | ||
fixable: "code", | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
/** @type {( | ||
* node: | ||
* | import("@typescript-eslint/utils").TSESTree.ImportDeclaration | ||
* | import("@typescript-eslint/utils").TSESTree.ExportAllDeclaration | ||
* | import("@typescript-eslint/utils").TSESTree.ExportNamedDeclaration | ||
* | import("@typescript-eslint/utils").TSESTree.TSImportEqualsDeclaration | ||
* | import("@typescript-eslint/utils").TSESTree.TSModuleDeclaration | ||
* ) => void} | ||
*/ | ||
const check = node => { | ||
let source; | ||
if (node.type === "TSImportEqualsDeclaration") { | ||
const moduleReference = node.moduleReference; | ||
if ( | ||
moduleReference.type === "TSExternalModuleReference" | ||
&& moduleReference.expression.type === "Literal" | ||
&& typeof moduleReference.expression.value === "string" | ||
) { | ||
source = moduleReference.expression; | ||
} | ||
} | ||
else if (node.type === "TSModuleDeclaration") { | ||
if (node.kind === "module" && node.id.type === "Literal") { | ||
source = node.id; | ||
} | ||
} | ||
else { | ||
source = node.source; | ||
} | ||
|
||
// This is not 100% accurate; this could point to a nested package, or to a directory | ||
// containing an index.js file. But we don't have anything like that in our repo, | ||
// so this check is good enough. Replicate this logic at your own risk. | ||
if (source?.value.startsWith(".") && !/\.[cm]?js$/.test(source.value)) { | ||
const quote = source.raw[0]; | ||
context.report({ | ||
messageId: "missingJsExtension", | ||
node: source, | ||
fix: fixer => fixer.replaceText(source, `${quote}${source.value}.js${quote}`), | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
ImportDeclaration: check, | ||
ExportAllDeclaration: check, | ||
ExportNamedDeclaration: check, | ||
TSImportEqualsDeclaration: check, | ||
TSModuleDeclaration: check, | ||
}; | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
const { RuleTester } = require("./support/RuleTester.cjs"); | ||
const rule = require("../rules/js-extensions.cjs"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
warnOnUnsupportedTypeScriptVersion: false, | ||
}, | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
}); | ||
|
||
ruleTester.run("js-extensions", rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
import "some-library"; | ||
import "./a.js"; | ||
import "./a.mjs"; | ||
import "./a.cjs"; | ||
import "../foo/a.js"; | ||
import "../foo/a.mjs"; | ||
import "../foo/a.cjs"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import * as blah from "some-library"; | ||
import * as blah from "./a.js"; | ||
import * as blah from "./a.mjs"; | ||
import * as blah from "./a.cjs"; | ||
import * as blah from "../foo/a.js"; | ||
import * as blah from "../foo/a.mjs"; | ||
import * as blah from "../foo/a.cjs"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export * from "some-library"; | ||
export * from "./a.js"; | ||
export * from "./a.mjs"; | ||
export * from "./a.cjs"; | ||
export * from "../foo/a.js"; | ||
export * from "../foo/a.mjs"; | ||
export * from "../foo/a.cjs"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import blah = require("some-library"); | ||
import blah = require("./a.js"); | ||
import blah = require("./a.mjs"); | ||
import blah = require("./a.cjs"); | ||
import blah = require("../foo/a.js"); | ||
import blah = require("../foo/a.mjs"); | ||
import blah = require("../foo/a.cjs"); | ||
`, | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
import "./a"; | ||
import "../foo/a"; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 2, | ||
column: 8, | ||
}, | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 3, | ||
column: 8, | ||
}, | ||
], | ||
output: ` | ||
import "./a.js"; | ||
import "../foo/a.js"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import * as blah from "./a"; | ||
import * as blah from "../foo/a"; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 2, | ||
column: 23, | ||
}, | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 3, | ||
column: 23, | ||
}, | ||
], | ||
output: ` | ||
import * as blah from "./a.js"; | ||
import * as blah from "../foo/a.js"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export * from "./a"; | ||
export * from "../foo/a"; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 2, | ||
column: 15, | ||
}, | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 3, | ||
column: 15, | ||
}, | ||
], | ||
output: ` | ||
export * from "./a.js"; | ||
export * from "../foo/a.js"; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import blah = require("./a"); | ||
import blah = require("../foo/a"); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 2, | ||
column: 23, | ||
}, | ||
{ | ||
messageId: "missingJsExtension", | ||
line: 3, | ||
column: 23, | ||
}, | ||
], | ||
output: ` | ||
import blah = require("./a.js"); | ||
import blah = require("../foo/a.js"); | ||
`, | ||
}, | ||
], | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/* Generated file to emulate the ts.moduleSpecifiers namespace. */ | ||
|
||
export * from "../moduleSpecifiers"; | ||
export * from "../moduleSpecifiers.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/* Generated file to emulate the ts.performance namespace. */ | ||
|
||
export * from "../performance"; | ||
export * from "../performance.js"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.