Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): error on dependencies that are only in devDependencies instead of production dependencies #18780

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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