Skip to content

Commit f6b9210

Browse files
jaysooFrozenPandaz
authored andcommittedAug 23, 2023
fix(linter): error on dependencies that are only in devDependencies instead of production dependencies (#18780)
(cherry picked from commit 9ffea2b)
1 parent a5745df commit f6b9210

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed
 

‎packages/eslint-plugin/src/rules/dependency-checks.spec.ts

+56-4
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe('Dependency checks (eslint)', () => {
446446
);
447447
expect(failures.length).toEqual(1);
448448
expect(failures[0].message).toMatchInlineSnapshot(`
449-
"The "liba" project uses the following packages, but they are missing from the "dependencies":
449+
"The "liba" project uses the following packages, but they are missing from "dependencies":
450450
- external2"
451451
`);
452452
expect(failures[0].line).toEqual(3);
@@ -1462,11 +1462,63 @@ describe('Dependency checks (eslint)', () => {
14621462
);
14631463
expect(failures.length).toEqual(1);
14641464
expect(failures[0].message).toMatchInlineSnapshot(`
1465-
"The "liba" project uses the following packages, but they are missing from the "dependencies":
1465+
"The "liba" project uses the following packages, but they are missing from "dependencies":
14661466
- tslib"
14671467
`);
14681468
expect(failures[0].line).toEqual(3);
14691469
});
1470+
1471+
it('should report missing package if it is in devDependencies', () => {
1472+
const packageJson = {
1473+
name: '@mycompany/liba',
1474+
dependencies: {},
1475+
devDependencies: {
1476+
external1: '^16.0.0',
1477+
},
1478+
};
1479+
1480+
const fileSys = {
1481+
'./libs/liba/package.json': JSON.stringify(packageJson, null, 2),
1482+
'./libs/liba/src/index.ts': '',
1483+
'./package.json': JSON.stringify(rootPackageJson, null, 2),
1484+
};
1485+
vol.fromJSON(fileSys, '/root');
1486+
1487+
const failures = runRule(
1488+
{},
1489+
`/root/libs/liba/package.json`,
1490+
JSON.stringify(packageJson, null, 2),
1491+
{
1492+
nodes: {
1493+
liba: {
1494+
name: 'liba',
1495+
type: 'lib',
1496+
data: {
1497+
root: 'libs/liba',
1498+
targets: {
1499+
build: {},
1500+
},
1501+
},
1502+
},
1503+
},
1504+
externalNodes,
1505+
dependencies: {
1506+
liba: [{ source: 'liba', target: 'npm:external1', type: 'static' }],
1507+
},
1508+
},
1509+
{
1510+
liba: [
1511+
createFile(`libs/liba/src/main.ts`, ['npm:external1']),
1512+
createFile(`libs/liba/package.json`),
1513+
],
1514+
}
1515+
);
1516+
expect(failures.length).toEqual(1);
1517+
expect(failures[0].message).toMatchInlineSnapshot(`
1518+
"The "liba" project uses the following packages, but they are missing from "dependencies":
1519+
- external1"
1520+
`);
1521+
});
14701522
});
14711523

14721524
it('should require swc if @nx/js:swc executor', () => {
@@ -1525,8 +1577,8 @@ it('should require swc if @nx/js:swc executor', () => {
15251577
);
15261578
expect(failures.length).toEqual(1);
15271579
expect(failures[0].message).toMatchInlineSnapshot(`
1528-
"The "liba" project uses the following packages, but they are missing from the "dependencies":
1529-
- @swc/helpers"
1580+
"The "liba" project uses the following packages, but they are missing from "dependencies":
1581+
- @swc/helpers"
15301582
`);
15311583
expect(failures[0].line).toEqual(3);
15321584
});

‎packages/eslint-plugin/src/rules/dependency-checks.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { findProject, getSourceFilePath } from '../utils/runtime-lint-utils';
1111
import {
1212
getAllDependencies,
1313
getPackageJson,
14+
getProductionDependencies,
1415
} from '../utils/package-json-utils';
1516

1617
export type Options = [
@@ -57,7 +58,7 @@ export default createESLintRule<Options, MessageIds>({
5758
},
5859
],
5960
messages: {
60-
missingDependency: `The "{{projectName}}" project uses the following packages, but they are missing from the "{{section}}":{{packageNames}}`,
61+
missingDependency: `The "{{projectName}}" project uses the following packages, but they are missing from "{{section}}":{{packageNames}}`,
6162
obsoleteDependency: `The "{{packageName}}" package is not used by "{{projectName}}" project.`,
6263
versionMismatch: `The version specifier does not contain the installed version of "{{packageName}}" package: {{version}}.`,
6364
missingDependencySection: `Dependency sections are missing from the "package.json" but following dependencies were detected:{{dependencies}}`,
@@ -142,7 +143,7 @@ export default createESLintRule<Options, MessageIds>({
142143
'package.json'
143144
);
144145

145-
globalThis.projPackageJsonDeps ??= getAllDependencies(
146+
globalThis.projPackageJsonDeps ??= getProductionDependencies(
146147
getPackageJson(projPackageJsonPath)
147148
);
148149
const projPackageJsonDeps: Record<string, string> =
@@ -283,12 +284,9 @@ export default createESLintRule<Options, MessageIds>({
283284
if (
284285
!node.properties ||
285286
!node.properties.some((p) =>
286-
[
287-
'dependencies',
288-
'peerDependencies',
289-
'devDependencies',
290-
'optionalDependencies',
291-
].includes((p.key as any).value)
287+
['dependencies', 'peerDependencies', 'optionalDependencies'].includes(
288+
(p.key as any).value
289+
)
292290
)
293291
) {
294292
context.report({
@@ -326,7 +324,7 @@ export default createESLintRule<Options, MessageIds>({
326324
}
327325

328326
return {
329-
['JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=/^(dev|peer|optional)?dependencies$/i]'](
327+
['JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=/^(peer|optional)?dependencies$/i]'](
330328
node: AST.JSONProperty
331329
) {
332330
validateMissingDependencies(node);

‎packages/eslint-plugin/src/utils/package-json-utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export function getAllDependencies(
1313
};
1414
}
1515

16+
export function getProductionDependencies(
17+
packageJson: PackageJson
18+
): Record<string, string> {
19+
return {
20+
...packageJson.dependencies,
21+
...packageJson.peerDependencies,
22+
...packageJson.optionalDependencies,
23+
};
24+
}
25+
1626
export function getPackageJson(path: string): PackageJson {
1727
if (existsSync(path)) {
1828
return readJsonFile(path);

0 commit comments

Comments
 (0)
Please sign in to comment.