refactor(ui-deps): 统一组件依赖审计与回归规则
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { ESLint } from 'eslint';
|
||||
import {
|
||||
auditManifest,
|
||||
auditSource,
|
||||
checkUiDependencies,
|
||||
} from '../../scripts/check-ui-dependencies.mjs';
|
||||
|
||||
test('ESLint and the UI audit enforce the same rule in every JS file format', async () => {
|
||||
const eslint = new ESLint();
|
||||
const cases = [
|
||||
['src/fixture.jsx', "import { Button } from '@douyinfe/semi-ui';"],
|
||||
['src/fixture.js', "export { item } from '@lobehub/ui';"],
|
||||
['src/fixture.js', "export * from '@lobehub/other-package';"],
|
||||
['scripts/fixture.mjs', "import('@material-ui/core');"],
|
||||
['scripts/fixture.mjs', 'import(`antd-style/css?raw`);'],
|
||||
['scripts/fixture.cjs', "require('@radix-ui/react-dialog');"],
|
||||
['scripts/fixture.cjs', "require.resolve('react-layout-kit/theme');"],
|
||||
];
|
||||
for (const [filePath, source] of cases) {
|
||||
const [result] = await eslint.lintText(source, { filePath });
|
||||
const diagnostics = result.messages.filter(
|
||||
({ ruleId }) => ruleId === 'ui-dependencies/no-legacy-ui',
|
||||
);
|
||||
assert.equal(diagnostics.length, 1, `${filePath}: ${source}`);
|
||||
assert.deepEqual(auditSource(source, filePath), [
|
||||
`${filePath}:${diagnostics[0].line}: ${diagnostics[0].message}`,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test('the UI audit allows approved imports, local assets and provenance text', () => {
|
||||
const source = `
|
||||
import 'antd';
|
||||
import '@xyflow/react';
|
||||
import '@/components/ui';
|
||||
import './assets/brand-icons/lobehub.js';
|
||||
// import 'antd-style';
|
||||
const upstream = '@lobehub/icons';
|
||||
`;
|
||||
assert.deepEqual(auditSource(source, 'src/fixture.jsx'), []);
|
||||
assert.equal(auditSource('import {', 'scripts/fixture.mjs').length, 1);
|
||||
assert.equal(
|
||||
auditSource(
|
||||
"/* eslint-disable ui-dependencies/no-legacy-ui */\nimport 'leva';",
|
||||
'scripts/fixture.mjs',
|
||||
).length,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test('the UI audit checks direct, aliased and nested locked dependencies', () => {
|
||||
const manifest = {
|
||||
dependencies: { antd: '6.7.0', 'legacy-ui': 'npm:@mui/material@^7' },
|
||||
devDependencies: { '@douyinfe/semi-animation': '1.0.0' },
|
||||
optionalDependencies: { 'old-style': 'npm:antd-style' },
|
||||
peerDependencies: { '@headlessui/react': '^2' },
|
||||
};
|
||||
const lock = {
|
||||
lockfileVersion: 3,
|
||||
packages: {
|
||||
'': {},
|
||||
'node_modules/antd': {},
|
||||
'node_modules/nested/node_modules/@floating-ui/dom': {},
|
||||
'node_modules/nested/node_modules/other-name': { name: '@lobehub/ui' },
|
||||
},
|
||||
};
|
||||
assert.deepEqual(auditManifest(manifest, lock), [
|
||||
'package.json dependencies: legacy-ui -> @mui/material',
|
||||
'package.json devDependencies: @douyinfe/semi-animation',
|
||||
'package.json optionalDependencies: old-style -> antd-style',
|
||||
'package.json peerDependencies: @headlessui/react',
|
||||
'package-lock.json: node_modules/nested/node_modules/@floating-ui/dom',
|
||||
'package-lock.json: node_modules/nested/node_modules/other-name -> @lobehub/ui',
|
||||
]);
|
||||
});
|
||||
|
||||
test('the checkout passes the source, lock and brand icon audits', async () => {
|
||||
const result = await checkUiDependencies();
|
||||
assert.deepEqual(result.issues, []);
|
||||
assert.ok(result.files > 100);
|
||||
assert.ok(result.packages > 100);
|
||||
});
|
||||
Reference in New Issue
Block a user