refactor(ui-deps): 统一组件依赖审计与回归规则

This commit is contained in:
zhanjunhao
2026-11-20 14:11:11 +08:00
parent 9145ed5834
commit d90319f43a
12 changed files with 217 additions and 104 deletions
+47 -63
View File
@@ -3,20 +3,28 @@ import { createHash } from 'node:crypto';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { Linter } from 'eslint';
import policy from './ui-dependency-policy.cjs';
const root = fileURLToPath(new URL('../', import.meta.url));
const forbidden = [
/^@radix-ui\//,
/^@floating-ui\//,
/^@lobehub\/(?:ui|icons)(?:\/|$)/,
/^@douyinfe\/semi-(?:ui|icons|foundation)(?:\/|$)/,
/^@headlessui\//,
/^@mui\//,
/^@chakra-ui\//,
/^(?:leva|antd-style|react-layout-kit)(?:\/|$)/,
];
export const isForbiddenUiDependency = (name) =>
forbidden.some((pattern) => pattern.test(name));
const { isForbiddenUiDependency, uiDependencyConfig } = policy;
const linter = new Linter({ cwd: root });
const sourceConfig = [uiDependencyConfig];
const sourceDirectories = new Set(['src', 'scripts', 'test', 'tests']);
const excludedDirectories = new Set([
'node_modules',
'dist',
'dist-ssr',
'build',
'coverage',
'report',
'logs',
'test-results',
'playwright-report',
'blob-report',
'kt_batches',
'kt_out',
]);
export { isForbiddenUiDependency };
export function auditManifest(manifest, lock) {
const issues = [];
@@ -26,14 +34,22 @@ export function auditManifest(manifest, lock) {
'optionalDependencies',
'peerDependencies',
]) {
for (const name of Object.keys(manifest[section] || {})) {
if (isForbiddenUiDependency(name))
for (const [name, specifier] of Object.entries(manifest[section] || {})) {
const alias = /^npm:((?:@[^/]+\/)?[^@/]+)(?:@|$)/.exec(specifier)?.[1];
if (isForbiddenUiDependency(name)) {
issues.push(`package.json ${section}: ${name}`);
} else if (isForbiddenUiDependency(alias)) {
issues.push(`package.json ${section}: ${name} -> ${alias}`);
}
}
}
for (const location of Object.keys(lock.packages || {})) {
if (isForbiddenUiDependency(location.split('node_modules/').at(-1))) {
for (const [location, entry] of Object.entries(lock.packages || {})) {
const names = location.split(/(?:^|\/)node_modules\//);
if (names.length === 1) continue;
if (isForbiddenUiDependency(names.at(-1))) {
issues.push(`package-lock.json: ${location}`);
} else if (isForbiddenUiDependency(entry.name)) {
issues.push(`package-lock.json: ${location} -> ${entry.name}`);
}
}
if (!lock.packages || lock.lockfileVersion < 2)
@@ -42,58 +58,26 @@ export function auditManifest(manifest, lock) {
}
export function auditSource(source, filename) {
const linter = new Linter({ configType: 'eslintrc' });
linter.defineRule('ui-dependencies', {
create(context) {
const inspect = (node) => {
if (
node?.type === 'Literal' &&
typeof node.value === 'string' &&
isForbiddenUiDependency(node.value)
) {
context.report({
node,
message: `Use Ant Design or the audited local brand assets: ${node.value}`,
});
}
};
return {
ImportDeclaration: (node) => inspect(node.source),
ExportNamedDeclaration: (node) => inspect(node.source),
ExportAllDeclaration: (node) => inspect(node.source),
ImportExpression: (node) => inspect(node.source),
CallExpression: (node) => {
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'require'
)
inspect(node.arguments[0]);
},
};
},
});
return linter
.verify(
source,
{
parserOptions: {
ecmaVersion: 2022,
sourceType: 'module',
ecmaFeatures: { jsx: true },
},
rules: { 'ui-dependencies': 'error' },
},
{ filename, allowInlineConfig: false },
)
.verify(source, sourceConfig, { filename, allowInlineConfig: false })
.map((issue) => `${filename}:${issue.line}: ${issue.message}`);
}
async function sourceFiles(dir) {
async function sourceFiles(dir, isRoot = true) {
const files = [];
for (const entry of await readdir(dir, { withFileTypes: true })) {
const name = path.join(dir, entry.name);
if (entry.isDirectory()) files.push(...(await sourceFiles(name)));
else if (/\.[cm]?[jt]sx?$/.test(entry.name)) files.push(name);
if (entry.isDirectory()) {
if (
isRoot
? sourceDirectories.has(entry.name)
: !entry.name.startsWith('.') && !excludedDirectories.has(entry.name)
) {
files.push(...(await sourceFiles(name, false)));
}
} else if (entry.isFile() && /\.(?:js|jsx|cjs|mjs)$/.test(entry.name)) {
files.push(name);
}
}
return files;
}
@@ -106,7 +90,7 @@ export async function checkUiDependencies(directory = root) {
await readFile(path.join(directory, 'package-lock.json'), 'utf8'),
);
const issues = auditManifest(manifest, lock);
const files = await sourceFiles(path.join(directory, 'src'));
const files = await sourceFiles(directory);
for (const filename of files) {
issues.push(
...auditSource(
@@ -143,7 +127,7 @@ export async function checkUiDependencies(directory = root) {
return {
issues,
files: files.length,
packages: Object.keys(lock.packages).length - 1,
packages: Math.max(0, Object.keys(lock.packages || {}).length - 1),
};
}