eslint.config.js 2.23 KB
// ESLint 扁平配置(ESM)。项目此前无 ESLint,本配置只承载一条自定义规则,
// 用于约束「权限模块」不得直接从 antd 引入组件,统一走 @uj/ui。
//
// 作用域:仅 src/access/**(业务/权限层)。其余代码(如主题 ConfigProvider、
// antd 静态 message、Layout/Menu 等尚未封装进 @uj/ui 的组件)暂不约束,避免误伤;
// 后续更多组件被 @uj/ui 封装后,再把 files 的 glob 放宽到 'src/**'。
import tsParser from '@typescript-eslint/parser';
import prettier from 'eslint-config-prettier';

/** 禁止从 antd 直接引入「值/组件」,类型导入(import type / 内联 type)放行。 */
const noAntdComponentImport = {
  meta: {
    type: 'problem',
    docs: {
      description: '禁止权限模块直接从 antd 引入组件,统一使用 @uj/ui(类型导入不受影响)',
    },
    messages: {
      forbidden: "不要直接从 'antd' 引入组件/值,请改用 '@uj/ui'(类型导入除外)。",
    },
    schema: [],
  },
  create(context) {
    return {
      ImportDeclaration(node) {
        if (node.source.value !== 'antd') return;
        // 整条 import type { ... } from 'antd' 放行
        if (node.importKind === 'type') return;
        const hasValue = node.specifiers.some((s) => {
          if (s.type === 'ImportDefaultSpecifier') return true;
          if (s.type === 'ImportNamespaceSpecifier') return true;
          if (s.type === 'ImportSpecifier') return s.importKind !== 'type';
          return false;
        });
        if (hasValue) {
          context.report({ node, messageId: 'forbidden' });
        }
      },
    };
  },
};

export default [
  { ignores: ['dist', 'node_modules', '**/*.d.ts', 'packages/**'] },
  {
    // 基础块:仅为所有 TS/TSX 提供解析能力,默认不启用任何规则
    files: ['**/*.{ts,tsx}'],
    languageOptions: {
      parser: tsParser,
      sourceType: 'module',
      ecmaVersion: 'latest',
    },
    rules: {},
  },
  {
    // 约束块:仅权限模块强制走 @uj/ui
    files: ['src/access/**/*.{ts,tsx}'],
    plugins: { uj: { rules: { 'no-antd-component-import': noAntdComponentImport } } },
    rules: { 'uj/no-antd-component-import': 'error' },
  },
  prettier,
];