Skip to content

Commit

Permalink
feat: add PREFER_PEER_DEPS doctor rule (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Apr 3, 2023
1 parent d6da2a1 commit 376407b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/doctor/rules/PREFER_PEER_DEPS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { IDoctorReport } from '..';
import type { IApi } from '../../types';

const PREFER_PEER_DEPS = ['react', 'react-dom', 'antd'];

export default (api: IApi) => {
api.addRegularCheckup(() => {
const warns: IDoctorReport = [];

if (api.pkg.dependencies) {
Object.keys(api.pkg.dependencies).forEach((pkg) => {
if (
PREFER_PEER_DEPS.includes(pkg) &&
!api.pkg.peerDependencies?.[pkg]
) {
warns.push({
type: 'warn',
problem: `The dependency \`${pkg}\` has multi-instance risk in host project`,
solution: 'Move it into `peerDependencies` from `dependencies`',
});
}
});
}

return warns;
});
};
5 changes: 5 additions & 0 deletions tests/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ test('doctor: warn checkups', async () => {
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining('CSS Modules'),
);

// PREFER_PEER_DEPS
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining('multi-instance risk'),
);
});

test('doctor: error checkups', async () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/doctor/warns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"*.css"
],
"dependencies": {
"hello": "0.0.0"
"hello": "0.0.0",
"react": "16.9.0"
},
"peerDependencies": {
"hello": "0.0.0"
Expand Down

0 comments on commit 376407b

Please sign in to comment.