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-10-28 02:51:50 +08:00
|
|
|
import buildConfig from "./build/config.js";
|
|
|
|
|
import { DIST_DIR } from "./utils/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-10-28 02:51:50 +08:00
|
|
|
function getProductionFiles(platform) {
|
|
|
|
|
return buildConfig
|
|
|
|
|
.flatMap((project) =>
|
|
|
|
|
project.files
|
|
|
|
|
.filter(
|
|
|
|
|
(file) => file.kind === "javascript" && file.platform === platform,
|
|
|
|
|
)
|
|
|
|
|
.map((file) => path.join(project.distDirectory, file.output.file)),
|
|
|
|
|
)
|
|
|
|
|
.map((file) => path.relative(DIST_DIR, file).replaceAll("\\", "/"));
|
|
|
|
|
}
|
2024-06-14 18:07:10 +08:00
|
|
|
|
2025-10-28 02:51:50 +08:00
|
|
|
const browserFiles = getProductionFiles("universal");
|
|
|
|
|
const nodejsFiles = getProductionFiles("node");
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/* TODO[@fisker]: Fix `no-restricted-syntax` */
|
|
|
|
|
|
|
|
|
|
export default [
|
|
|
|
|
{
|
|
|
|
|
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],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
|
files: ["bin/prettier.cjs"],
|
|
|
|
|
languageOptions: {
|
|
|
|
|
ecmaVersion: 5,
|
|
|
|
|
},
|
|
|
|
|
rules: {
|
|
|
|
|
"compat/compat": "error",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|