Files
test-repo/scripts/production-eslint-config.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

161 lines
4.3 KiB
JavaScript
Raw Normal View History

2025-10-28 02:51:50 +08:00
import assert from "node:assert";
2024-01-08 14:48:45 +08:00
import fs from "node:fs/promises";
2025-10-28 02:51:50 +08:00
import path from "node:path";
2024-01-08 14:48:45 +08:00
import eslintPluginCompat from "eslint-plugin-compat";
2025-12-05 04:53:15 +08:00
import packageBuildConfigs from "./build/packages/index.js";
import { PROJECT_ROOT } from "./utilities/index.js";
2024-01-08 14:48:45 +08:00
const { browserslist: targets } = JSON.parse(
await fs.readFile(new URL("../package.json", import.meta.url)),
);
2025-12-05 04:53:15 +08:00
const jsFiles = packageBuildConfigs
.flatMap((packageConfig) =>
packageConfig.modules.flatMap((module) =>
module.files
.filter((file) => /\.[cm]?js$/.test(file.output))
2025-12-05 04:53:15 +08:00
.map((file) => path.join(packageConfig.distDirectory, file.output)),
),
)
.map((file) => path.relative(PROJECT_ROOT, file).replaceAll("\\", "/"));
2024-06-14 18:07:10 +08:00
2025-12-05 04:53:15 +08:00
const nodejsFiles = jsFiles.filter(
(file) =>
file.endsWith(".cjs") ||
[
"dist/prettier/index.mjs",
"dist/prettier/internal/legacy-cli.mjs",
"dist/prettier/internal/experimental-cli.mjs",
"dist/prettier/internal/experimental-cli-worker.mjs",
"dist/plugin-oxc/index.mjs",
2026-07-15 01:53:00 +08:00
"dist/plugin-yuku/index.mjs",
2025-12-05 04:53:15 +08:00
].includes(file),
);
const browserFiles = jsFiles.filter((file) => !nodejsFiles.includes(file));
2025-10-28 02:51:50 +08:00
assert.ok(browserFiles.length > 0);
assert.ok(nodejsFiles.length > 0);
2024-06-14 18:07:10 +08:00
const restrictedSyntaxes = [
{
selector: String.raw`CallExpression[callee.name="require"][arguments.0.value=/^\..*?\.mjs$/]`,
message: ".mjs file can't be `require()`d",
},
2025-10-27 23:24:02 +08:00
{
2025-10-28 02:51:50 +08:00
selector:
'TaggedTemplateExpression > MemberExpression.tag[object.name="String"][property.name="raw"]',
2025-10-27 23:24:02 +08:00
message: "`String.raw` should be transformed.",
},
2025-10-28 02:51:50 +08:00
...[
// "at", // `.at` in `acorn` didn't transform
"findLastIndex",
"findLast",
"replaceAll",
"toReversed",
].map((method) => ({
selector: `CallExpression > MemberExpression.callee[computed!=true][property.name="${method}"]`,
message: `\`.${method}()\` should be transformed`,
})),
2024-06-14 18:07:10 +08:00
];
const browserRestrictedSyntaxes = [
{
selector: 'CallExpression[callee.name="require"]',
message: "Universal bundles should not include any `require()` call.",
},
{
selector: "ImportDeclaration",
message: "Universal bundles should not include any `import` declaration.",
},
{
selector:
":matches(ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source]",
message: "Universal bundles should not `export` from other files.",
},
{
selector: "ImportExpression",
message: "Universal bundles should not include any `import()`.",
},
];
const nodejsRestrictedSyntaxes = [
// Forbid top level `require()` parsers
{
selector:
'CallExpression:not(:function *)[callee.name="require"][arguments.0.value=/plugins/]',
message: "Parsers should be inline `require()`d.",
},
// Forbid top level `import()` parsers
{
selector: "ImportExpression:not(:function *)[source.value=/plugins/]",
message: "Parsers should be inline `import()`ed.",
},
// Forbid `import`/`export` parsers
{
selector:
":matches(ImportDeclaration, ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source.value=/plugins/]",
message: "Parsers should be inline `import()`ed.",
},
];
2024-01-08 14:48:45 +08:00
export default [
{
name: "All files",
2024-01-08 14:48:45 +08:00
plugins: {
compat: eslintPluginCompat,
},
settings: {
targets,
lintAllEsApis: true,
polyfills: [
// These are not really polyfilled, but seems safe to use in target browsers
"BigInt",
"Symbol.asyncIterator",
],
},
2024-06-14 18:07:10 +08:00
linterOptions: {
noInlineConfig: true,
reportUnusedDisableDirectives: "off",
},
rules: {
"compat/compat": "error",
"no-restricted-syntax": ["error", ...restrictedSyntaxes],
},
},
{
name: "Browser files",
2024-06-14 18:07:10 +08:00
files: browserFiles,
2024-01-08 14:48:45 +08:00
rules: {
"no-restricted-syntax": [
"error",
2024-06-14 18:07:10 +08:00
...restrictedSyntaxes,
...browserRestrictedSyntaxes,
2024-01-08 14:48:45 +08:00
],
},
},
{
name: "Node.js files",
2024-06-14 18:07:10 +08:00
files: nodejsFiles,
2024-01-08 14:48:45 +08:00
rules: {
2024-06-14 18:07:10 +08:00
"no-restricted-syntax": [
"error",
...restrictedSyntaxes,
...nodejsRestrictedSyntaxes,
],
2024-01-08 14:48:45 +08:00
},
},
2024-06-14 18:07:10 +08:00
2024-01-08 14:48:45 +08:00
{
name: "Prettier bin file",
2024-01-08 14:48:45 +08:00
files: ["bin/prettier.cjs"],
languageOptions: {
ecmaVersion: 5,
},
rules: {
"compat/compat": "error",
},
},
];