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

Use explicit extensions for imports within src #58421

Merged
merged 13 commits into from
May 7, 2024
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
"local/no-in-operator": "error",
"local/debug-assert": "error",
"local/no-keywords": "error",
"local/jsdoc-format": "error"
"local/jsdoc-format": "error",
"local/js-extensions": "error"
},
"overrides": [
// By default, the ESLint CLI only looks at .js files. But, it will also look at
Expand Down
55 changes: 55 additions & 0 deletions scripts/eslint/rules/js-extensions.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { createRule } = require("./utils.cjs");

/** @typedef {import("@typescript-eslint/utils").TSESTree.ImportDeclaration | import("@typescript-eslint/utils").TSESTree.ExportAllDeclaration | import("@typescript-eslint/utils").TSESTree.ExportNamedDeclaration | import("@typescript-eslint/utils").TSESTree.TSImportEqualsDeclaration} ImportOrExport */

module.exports = createRule({
name: "js-extensions",
meta: {
docs: {
description: ``,
},
messages: {
missingJsExtension: `This relative import is missing a '.js' extension`,
},
schema: [],
type: "suggestion",
fixable: "code",
},
defaultOptions: [],

create(context) {
/** @type {(node: ImportOrExport) => 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 {
source = node.source;
}

if (source?.value.startsWith(".") && !/\.[cm]?js$/.test(source.value)) {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
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,
};
},
});
149 changes: 149 additions & 0 deletions scripts/eslint/tests/js-extensions.cjs
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");
`,
},
],
});