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
3 changes: 3 additions & 0 deletions .vscode/settings.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
".git-blame-ignore-revs"
],

"javascript.preferences.importModuleSpecifierEnding": "js",
"typescript.preferences.importModuleSpecifierEnding": "js",

// Match dprint in organize/auto-imports.
"typescript.unstable": {
"organizeImportsCollation": "unicode",
Expand Down
70 changes: 70 additions & 0 deletions scripts/eslint/rules/js-extensions.cjs
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)) {
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,
TSModuleDeclaration: 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");
`,
},
],
});
2 changes: 1 addition & 1 deletion scripts/processDiagnosticMessages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function buildInfoFileOutput(messageTable, inputFilePathRel) {
"// <auto-generated />",
`// generated from '${inputFilePathRel}'`,
"",
'import { DiagnosticCategory, DiagnosticMessage } from "./types";',
'import { DiagnosticCategory, DiagnosticMessage } from "./types.js";',
"",
"function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {",
" return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/_namespaces/ts.moduleSpecifiers.ts
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";
2 changes: 1 addition & 1 deletion src/compiler/_namespaces/ts.performance.ts
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";
Loading