Skip to content

Commit 55f1c71

Browse files
committed
More tests + bundled fixes
1 parent ed9e81d commit 55f1c71

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

src/rules/no-extraneous-dependencies.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import fs from 'fs';
33
import minimatch from 'minimatch';
44
import resolve from 'eslint-module-utils/resolve';
55
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
6-
import includes from 'array-includes';
76
import importType from '../core/importType';
87
import { getFilePackageName } from '../core/packagePath';
98
import docsUrl from '../docsUrl';
@@ -60,11 +59,13 @@ function getPackageDepFields(packageDir, considerInParents, requireInDir) {
6059
(depsKey === 'dependencies' && considerInParents.has('prod')) ||
6160
(depsKey === 'devDependencies' && considerInParents.has('dev')) ||
6261
(depsKey === 'peerDependencies' && considerInParents.has('peer')) ||
63-
(depsKey === 'optionalDependencies' && considerInParents.has('optional')) ||
64-
(depsKey === 'bundledDependencies' && considerInParents.has('bundled'))
62+
(depsKey === 'optionalDependencies' && considerInParents.has('optional'))
6563
) {
6664
Object.assign(depFields[depsKey], parentDepFields[depsKey]);
6765
}
66+
if (depsKey === 'bundledDependencies' && considerInParents.has('bundled')) {
67+
depFields[depsKey] = depFields[depsKey].concat(parentDepFields[depsKey]);
68+
}
6869
});
6970
}
7071

@@ -97,9 +98,13 @@ function getDependencies(context, packageDir, considerInParents) {
9798
// use rule config to find package.json
9899
paths.forEach(dir => {
99100
const _packageContent = getPackageDepFields(dir, considerInParents, true);
100-
Object.keys(packageContent).forEach(depsKey =>
101-
Object.assign(packageContent[depsKey], _packageContent[depsKey]),
102-
);
101+
Object.keys(packageContent).forEach(depsKey => {
102+
if (depsKey === 'bundledDependencies') {
103+
packageContent[depsKey] = packageContent[depsKey].concat(_packageContent[depsKey]);
104+
} else {
105+
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
106+
}
107+
});
103108
});
104109
} else {
105110
// use closest package.json

tests/files/monorepo/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
},
66
"devDependencies": {
77
"left-pad": "^1.2.0"
8-
}
8+
},
9+
"peerDependencies": {
10+
"lodash": "4.17.21"
11+
},
12+
"optionalDependencies": {
13+
"chalk": "5.2.0"
14+
},
15+
"bundledDependencies": ["commander"]
916
}

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ ruleTester.run('no-extraneous-dependencies', rule, {
128128
code: 'import leftpad from "left-pad";',
129129
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['dev'] }],
130130
}),
131+
test({
132+
code: 'import lodash from "lodash";',
133+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['peer'] }],
134+
}),
135+
test({
136+
code: 'import chalk from "chalk";',
137+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['optional'] }],
138+
}),
139+
test({
140+
code: 'import commander from "commander";',
141+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['bundled'] }],
142+
}),
131143
test({ code: 'import foo from "@generated/foo"' }),
132144
test({
133145
code: 'import foo from "@generated/foo"',
@@ -331,13 +343,48 @@ ruleTester.run('no-extraneous-dependencies', rule, {
331343
message: "'left-pad' should be listed in the project's dependencies. Run 'npm i -S left-pad' to add it",
332344
}],
333345
}),
346+
test({
347+
code: 'import rightpad from "right-pad";',
348+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: [] }],
349+
errors: [{
350+
message: "'right-pad' should be listed in the project's dependencies. Run 'npm i -S right-pad' to add it",
351+
}],
352+
}),
353+
test({
354+
code: 'import rightpad from "right-pad";',
355+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['dev'] }],
356+
errors: [{
357+
message: "'right-pad' should be listed in the project's dependencies. Run 'npm i -S right-pad' to add it",
358+
}],
359+
}),
334360
test({
335361
code: 'import leftpad from "left-pad";',
336362
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['prod'] }],
337363
errors: [{
338364
message: "'left-pad' should be listed in the project's dependencies. Run 'npm i -S left-pad' to add it",
339365
}],
340366
}),
367+
test({
368+
code: 'import lodash from "lodash";',
369+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['prod'] }],
370+
errors: [{
371+
message: "'lodash' should be listed in the project's dependencies. Run 'npm i -S lodash' to add it",
372+
}],
373+
}),
374+
test({
375+
code: 'import chalk from "chalk";',
376+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['prod'] }],
377+
errors: [{
378+
message: "'chalk' should be listed in the project's dependencies. Run 'npm i -S chalk' to add it",
379+
}],
380+
}),
381+
test({
382+
code: 'import commander from "commander";',
383+
options: [{ packageDir: packageDirMonoRepoWithNested, considerInParents: ['prod'] }],
384+
errors: [{
385+
message: "'commander' should be listed in the project's dependencies. Run 'npm i -S commander' to add it",
386+
}],
387+
}),
341388
test({
342389
code: 'import react from "react";',
343390
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),

0 commit comments

Comments
 (0)