rules.js 2.58 KB
/**
 * `@drama/config` ESLint 预设 —— 通用 JS 规则集合 + 装配架构红线规则的 flat config 片段。
 *
 * 红线装配(白名单 / 规则集 / globals)单源于仓库根 `tools/eslint-rules/config.js`,本文件只把
 * 那份单源包装成 flat config 片段(`presetNode` / `presetVue` 消费)。规则集与白名单一旦在单源
 * 变更,这里自动跟随——历史上根配置与本预设漂移过,现已收敛到单源。
 */

import js from '@eslint/js'
import {
  CONN_WHITELIST,
  buildArchRules,
  ENFORCED_RULE_NAMES,
  BASE_GLOBALS,
  COMMONJS_GLOBALS,
} from '../../../tools/eslint-rules/config.js'
import { dramaPlugin } from '../../../tools/eslint-rules/index.js'

/** 规则 C 文件白名单默认值(composition root + vite 配置),与根配置同源。 */
export const DEFAULT_COMPOSITION_ROOTS = CONN_WHITELIST

/** 架构红线规则名(供测试与下游静态追溯「红线确实进了预设」)。 */
export { ENFORCED_RULE_NAMES }

/** 全仓通用 JS 语言层默认(ESM、Node 全局)。 */
export const NODE_LANGUAGE_OPTIONS = {
  ecmaVersion: 2023,
  sourceType: 'module',
  globals: { ...BASE_GLOBALS },
}

/** CommonJS 脚本 / table definition 语言层默认,保证架构红线覆盖 `.cjs`。 */
export const COMMONJS_LANGUAGE_OPTIONS = {
  ...NODE_LANGUAGE_OPTIONS,
  sourceType: 'commonjs',
  globals: { ...COMMONJS_GLOBALS },
}

/**
 * 装配架构红线 + 通用 JS 规则的 flat config 片段(ESM)。
 * @param {{ compositionRoots?: string[] }} [opts]
 * @returns {import('eslint').Linter.Config}
 */
export function buildArchRulesBlock(opts = {}) {
  return {
    files: ['**/*.js'],
    languageOptions: { ...NODE_LANGUAGE_OPTIONS },
    plugins: { drama: dramaPlugin },
    rules: buildArchRules(opts.compositionRoots || DEFAULT_COMPOSITION_ROOTS),
  }
}

/**
 * 装配架构红线 + CommonJS 语言层的 flat config 片段。
 * @param {{ compositionRoots?: string[] }} [opts]
 * @returns {import('eslint').Linter.Config}
 */
export function buildCommonjsArchRulesBlock(opts = {}) {
  return {
    files: ['**/*.cjs'],
    languageOptions: { ...COMMONJS_LANGUAGE_OPTIONS },
    plugins: { drama: dramaPlugin },
    rules: buildArchRules(opts.compositionRoots || DEFAULT_COMPOSITION_ROOTS),
  }
}

/** 全仓通用 ignores(构建产物 / 依赖 / 覆盖率 / 临时目录)。 */
export const COMMON_IGNORES_BLOCK = {
  ignores: ['**/node_modules/**', '**/dist/**', '**/coverage/**', 'tmp/**'],
}

/** ESLint 官方推荐 JS 规则集(基础 JS 规则的默认来源)。 */
export const JS_RECOMMENDED = js.configs.recommended