Skip to content

Commit

Permalink
fix(linter): error on dependencies that are only in devDependencies i…
Browse files Browse the repository at this point in the history
…nstead of production dependencies (#18780)
  • Loading branch information
jaysoo authored Aug 22, 2023
1 parent 34f4148 commit 9ffea2b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
60 changes: 56 additions & 4 deletions packages/eslint-plugin/src/rules/dependency-checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe('Dependency checks (eslint)', () => {
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" project uses the following packages, but they are missing from the "dependencies":
"The "liba" project uses the following packages, but they are missing from "dependencies":
- external2"
`);
expect(failures[0].line).toEqual(3);
Expand Down Expand Up @@ -1462,11 +1462,63 @@ describe('Dependency checks (eslint)', () => {
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" project uses the following packages, but they are missing from the "dependencies":
"The "liba" project uses the following packages, but they are missing from "dependencies":
- tslib"
`);
expect(failures[0].line).toEqual(3);
});

it('should report missing package if it is in devDependencies', () => {
const packageJson = {
name: '@mycompany/liba',
dependencies: {},
devDependencies: {
external1: '^16.0.0',
},
};

const fileSys = {
'./libs/liba/package.json': JSON.stringify(packageJson, null, 2),
'./libs/liba/src/index.ts': '',
'./package.json': JSON.stringify(rootPackageJson, null, 2),
};
vol.fromJSON(fileSys, '/root');

const failures = runRule(
{},
`/root/libs/liba/package.json`,
JSON.stringify(packageJson, null, 2),
{
nodes: {
liba: {
name: 'liba',
type: 'lib',
data: {
root: 'libs/liba',
targets: {
build: {},
},
},
},
},
externalNodes,
dependencies: {
liba: [{ source: 'liba', target: 'npm:external1', type: 'static' }],
},
},
{
liba: [
createFile(`libs/liba/src/main.ts`, ['npm:external1']),
createFile(`libs/liba/package.json`),
],
}
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" project uses the following packages, but they are missing from "dependencies":
- external1"
`);
});
});

it('should require swc if @nx/js:swc executor', () => {
Expand Down Expand Up @@ -1525,8 +1577,8 @@ it('should require swc if @nx/js:swc executor', () => {
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" project uses the following packages, but they are missing from the "dependencies":
- @swc/helpers"
"The "liba" project uses the following packages, but they are missing from "dependencies":
- @swc/helpers"
`);
expect(failures[0].line).toEqual(3);
});
Expand Down
16 changes: 7 additions & 9 deletions packages/eslint-plugin/src/rules/dependency-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { findProject, getSourceFilePath } from '../utils/runtime-lint-utils';
import {
getAllDependencies,
getPackageJson,
getProductionDependencies,
} from '../utils/package-json-utils';

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

globalThis.projPackageJsonDeps ??= getAllDependencies(
globalThis.projPackageJsonDeps ??= getProductionDependencies(
getPackageJson(projPackageJsonPath)
);
const projPackageJsonDeps: Record<string, string> =
Expand Down Expand Up @@ -283,12 +284,9 @@ export default createESLintRule<Options, MessageIds>({
if (
!node.properties ||
!node.properties.some((p) =>
[
'dependencies',
'peerDependencies',
'devDependencies',
'optionalDependencies',
].includes((p.key as any).value)
['dependencies', 'peerDependencies', 'optionalDependencies'].includes(
(p.key as any).value
)
)
) {
context.report({
Expand Down Expand Up @@ -326,7 +324,7 @@ export default createESLintRule<Options, MessageIds>({
}

return {
['JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=/^(dev|peer|optional)?dependencies$/i]'](
['JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=/^(peer|optional)?dependencies$/i]'](
node: AST.JSONProperty
) {
validateMissingDependencies(node);
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/utils/package-json-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export function getAllDependencies(
};
}

export function getProductionDependencies(
packageJson: PackageJson
): Record<string, string> {
return {
...packageJson.dependencies,
...packageJson.peerDependencies,
...packageJson.optionalDependencies,
};
}

export function getPackageJson(path: string): PackageJson {
if (existsSync(path)) {
return readJsonFile(path);
Expand Down

1 comment on commit 9ffea2b

@vercel
Copy link

@vercel vercel bot commented on 9ffea2b Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.