'use strict'; // ESLint 扁平配置。目标是发现重复定义、未定义变量和无效代码;只开启不需要改变运行行为就能满足的规则。 const js = require('@eslint/js'); const globals = require('globals'); const browserScripts = require('./eslint.browser-globals.js'); // public/index.html 按顺序加载的经典脚本共用全局作用域:每个脚本可以访问其他脚本的顶层声明 // (const 只读,let/function 等可写)。自身声明不能列入,否则 no-redeclare(builtinGlobals)会把本文件的 // 声明误报为重复定义;若两个脚本声明同一名字,则会如实报出跨文件重复定义。 const sharedScriptGlobals = Object.keys(browserScripts).map(file => ({ files: [file], languageOptions: { globals: Object.assign( {}, ...Object.entries(browserScripts) .filter(([other]) => other !== file) .map(([, names]) => names), ), }, })); module.exports = [ { ignores: ['node_modules/', 'e2e/node_modules/', 'e2e/out/', 'e2e/baseline/', 'data/', 'backups/'], }, js.configs.recommended, { linterOptions: { reportUnusedDisableDirectives: 'warn' }, rules: { 'no-redeclare': 'error', 'no-undef': 'error', 'no-unused-vars': ['warn', { args: 'none' }], 'no-empty': ['error', { allowEmptyCatch: true }], // 关闭:正则里的 \x00-\x1f 是有意过滤控制字符(文件名、单元格、配置文本清洗),不是笔误。 'no-control-regex': 'off', // 关闭:给重新抛出的错误补 cause 会改变抛出的错误对象(属于行为变化),且现有错误文案即面向用户的提示。 'preserve-caught-error': 'off', // 收窄:模板字符串、正则与注释里的全角空格等是界面/打印文本的一部分,替换会改变输出。 'no-irregular-whitespace': [ 'error', { skipStrings: true, skipTemplates: true, skipRegExps: true, skipComments: true }, ], }, }, // 后端:CommonJS 模块,运行于 Node。 { files: ['**/*.js', '**/*.cjs'], languageOptions: { ecmaVersion: 'latest', sourceType: 'commonjs', globals: { ...globals.node } }, }, // 浏览器经典脚本(含 print.js,由打印页单独加载)。 { files: ['public/*.js'], languageOptions: { sourceType: 'script', globals: { ...globals.browser } }, }, // 浏览器与服务端共用的 UMD 文件:仅在 typeof module 判断成立时使用 module/require。 { files: [ 'public/calculations.js', 'public/credit-card-fees.js', 'public/fee-estimates.js', 'public/order-copy.js', 'public/order-formula.js', 'public/order-templates.js', ], languageOptions: { globals: { module: 'readonly', require: 'readonly' } }, }, ...sharedScriptGlobals, // 单元测试:Node CommonJS。 { files: ['test/**/*.js', 'test/**/*.cjs'], languageOptions: { globals: { ...globals.node } }, }, // 端到端验证工具:Node;browser.mjs 为 ES 模块。 { files: ['e2e/**/*.js', 'e2e/**/*.cjs', 'e2e/**/*.mjs'], languageOptions: { globals: { ...globals.node } }, }, // browser.mjs 的 page.evaluate 回调在页面里执行,会用到 document/window 等浏览器全局。 { files: ['e2e/**/*.mjs'], languageOptions: { sourceType: 'module', globals: { ...globals.browser } }, }, ];