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

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

126 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-01-08 14:48:45 +08:00
import fs from "node:fs/promises";
import eslintPluginCompat from "eslint-plugin-compat";
const { browserslist: targets } = JSON.parse(
await fs.readFile(new URL("../package.json", import.meta.url)),
);
2024-06-14 18:07:10 +08:00
const browserFiles = [
"doc.js",
"doc.mjs",
"standalone.js",
"standalone.mjs",
"plugins/*",
];
const nodejsFiles = ["index.cjs", "index.mjs", "bin/*", "internal/*"];
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
{
selector: String.raw`TaggedTemplateExpression > MemberExpression.tag[object.name="String"][property.name="raw"]`,
message: "`String.raw` 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",
},
},
];