index.js 5.69 KB
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import prettier from 'eslint-config-prettier';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';
import globals from 'globals';

/**
 * 共享 ESLint(flat) 配置。
 * boundary 规则是 wujie 架构的静态护栏:子应用绝不能 import host 独占运行时。
 */
export const app = (options = {}) =>
  tseslint.config(
    { ignores: ['dist/**', 'node_modules/**'] },
    js.configs.recommended,
    ...tseslint.configs.recommended,
    {
      // JS/MJS 脚本(scripts/*.mjs、vite.config 工厂)同样跑在 Node 里。
      // 只给 TS/TSX 注入 globals 会导致「从仓库根跑 eslint」时满屏 no-undef。
      files: ['**/*.{ts,tsx,js,mjs}'],
      languageOptions: { globals: { ...globals.browser, ...globals.node } },
    },
    {
      files: ['**/*.{ts,tsx}'],
      plugins: { 'react-hooks': reactHooks, 'react-refresh': reactRefresh },
      rules: {
        ...reactHooks.configs.recommended.rules,
        // Context 的 Provider 与配套 hook 同文件是 React 标准组织方式;
        // useAccess 是唯一允许的例外,避免为此把 Context 实例再拆成第三个模块。
        'react-refresh/only-export-components': [
          'warn',
          { allowConstantExport: true, allowExportNames: ['useAccess'] },
        ],
        // `_` 前缀是「刻意忽略」的社区约定:rest 解构剔除字段、占位参数都靠它表达。
        // 不配这三条会把有意为之的代码误判成死变量。
        '@typescript-eslint/no-unused-vars': [
          'error',
          { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_' },
        ],
        ...(options.noRestrictedImports
          ? {
              // 用 TS 版规则而不是 core 版:只有它支持 allowTypeImports,
              // 而 `import type { TableProps } from 'antd'` 是合法的(类型不产生运行时耦合)。
              '@typescript-eslint/no-restricted-imports': [
                'error',
                {
                  patterns: [
                    {
                      group: ['@uj/platform-core', '@uj/platform-core/*'],
                      message:
                        '子应用禁止引用 host 独占运行时(签名/http/Context/queryClient)。请改用 @uj/platform-sdk(props.request 代理)。',
                    },
                    {
                      group: ['@/api/core/*', 'virtual:*'],
                      message:
                        '子应用禁止直接引用 api/core 或 PEM 虚拟模块:请求必须经 props.request 由主壳代理。',
                    },
                    {
                      group: ['antd', 'antd/*'],
                      allowTypeImports: true,
                      message:
                        '子应用必须经 @uj/ui 消费组件(UAlert / UTable / useUMessage …)。缺组件就往 packages/ui 补一个 U** 再引用,不要绕过共享层直接用 antd。',
                    },
                  ],
                },
              ],
            }
          : {}),
      },
    },
    prettier,
  );

/**
 * 小程序 lane 的 ESLint(flat) 配置(原生微信小程序,非 web、非 React DOM)。
 *
 * 与 app() 的区别:
 *  - 不加载 react-hooks / react-refresh(原生 mp 用 wxml,不写 JSX 组件树);
 *  - 注入 wx / uni / App / Page / Component / getApp 等小程序全局;
 *  - 硬性禁止引用 web/host 独占运行时(platform-core / platform-sdk / vite-config / @uj/web-* / wujie),
 *    因为小程序自带 wx.request,不经主壳 props.request 代理,也不跑 vite 构建。
 * 小程序复用 @uj/api-core(createWxSender + 各类 Signer),但不得反向耦合 web lane。
 */
export const mp = () =>
  tseslint.config(
    { ignores: ['dist/**', 'node_modules/**'] },
    js.configs.recommended,
    ...tseslint.configs.recommended,
    {
      files: ['**/*.{ts,tsx,js,mjs}'],
      languageOptions: {
        globals: {
          ...globals.node,
          wx: 'readonly',
          uni: 'readonly',
          App: 'readonly',
          Page: 'readonly',
          Component: 'readonly',
          getApp: 'readonly',
          getCurrentPages: 'readonly',
        },
      },
    },
    {
      files: ['**/*.{ts,tsx}'],
      rules: {
        '@typescript-eslint/no-unused-vars': [
          'error',
          { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_' },
        ],
        'no-restricted-imports': [
          'error',
          {
            patterns: [
              {
                group: ['@uj/platform-core', '@uj/platform-core/*'],
                message:
                  '小程序禁止引用 host 独占运行时(axios/RSA/PEM)。请改用 @uj/api-core(createWxSender)。',
              },
              {
                group: ['@uj/platform-sdk', '@uj/platform-sdk/*'],
                message:
                  '小程序不经由主壳 props.request,请勿引用 web 子应用 SDK @uj/platform-sdk。',
              },
              {
                group: ['@uj/vite-config', '@uj/vite-config/*'],
                message: '小程序不跑 vite 构建,请勿引用 @uj/vite-config。',
              },
              {
                group: ['@uj/web-*'],
                message: '小程序不得直接依赖 web 子应用 / 主壳包(@uj/web-*)。',
              },
              {
                group: ['wujie'],
                message: '小程序无 wujie 微前端容器,请勿直接 import wujie。',
              },
            ],
          },
        ],
      },
    },
    prettier,
  );

export default app();