Skip to content

Commit

Permalink
fix(linter): apply missing deps fix in a single patch for deps check (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Jul 3, 2023
1 parent a70e1a8 commit 55c0a16
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
28 changes: 14 additions & 14 deletions packages/eslint-plugin/src/rules/dependency-checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ describe('Dependency checks (eslint)', () => {
}
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(
`"The "liba" uses the package "external2", but it is missing from the project's "package.json"."`
);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" uses the following packages, but they are missing from the "dependencies":
- external2"
`);
expect(failures[0].line).toEqual(3);

// apply fix
Expand Down Expand Up @@ -1407,15 +1408,13 @@ describe('Dependency checks (eslint)', () => {
],
}
);
expect(failures.length).toEqual(2);
expect(failures[0].message).toMatchInlineSnapshot(
`"The "liba" uses the package "tslib", but it is missing from the project's "package.json"."`
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" uses the following packages, but they are missing from the "dependencies":
- tslib
- external2"
`);
expect(failures[0].line).toEqual(3);
expect(failures[1].message).toMatchInlineSnapshot(
`"The "liba" uses the package "external2", but it is missing from the project's "package.json"."`
);
expect(failures[1].line).toEqual(3);
});
});

Expand Down Expand Up @@ -1491,9 +1490,10 @@ it('should require swc if @nx/js:swc executor', () => {
}
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(
`"The "liba" uses the package "@swc/helpers", but it is missing from the project's "package.json"."`
);
expect(failures[0].message).toMatchInlineSnapshot(`
"The "liba" uses the following packages, but they are missing from the "dependencies":
- @swc/helpers"
`);
expect(failures[0].line).toEqual(3);
});

Expand Down
68 changes: 34 additions & 34 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 {
getAllDependencies,
removePackageJsonFromFileMap,
} from '../utils/package-json-utils';
import { JSONLiteral } from 'jsonc-eslint-parser/lib/parser/ast';

export type Options = [
{
Expand Down Expand Up @@ -54,7 +55,7 @@ export default createESLintRule<Options, MessageIds>({
},
],
messages: {
missingDependency: `The "{{projectName}}" uses the package "{{packageName}}", but it is missing from the project's "package.json".`,
missingDependency: `The "{{projectName}}" uses the following packages, but they are missing from the "{{section}}":{{packageNames}}`,
obsoleteDependency: `The "{{packageName}}" package is not used by "{{projectName}}".`,
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 @@ -159,35 +160,43 @@ export default createESLintRule<Options, MessageIds>({
return;
}
const missingDeps = expectedDependencyNames.filter(
(d) => !projPackageJsonDeps[d]
(d) => !projPackageJsonDeps[d] && !ignoredDependencies.includes(d)
);

missingDeps.forEach((d) => {
if (!ignoredDependencies.includes(d)) {
context.report({
node: node as any,
messageId: 'missingDependency',
data: { packageName: d, projectName: sourceProject.name },
fix(fixer) {
if (missingDeps.length) {
context.report({
node: node as any,
messageId: 'missingDependency',
data: {
packageNames: missingDeps.map((d) => `\n - ${d}`).join(''),
section: (node.key as JSONLiteral).value,
projectName: sourceProject.name,
},
fix(fixer) {
missingDeps.forEach((d) => {
projPackageJsonDeps[d] =
rootPackageJsonDeps[d] || projDependencies[d];
});

const deps = (node.value as AST.JSONObjectExpression).properties;
if (deps.length) {
return fixer.insertTextAfter(
deps[deps.length - 1] as any,
`,\n "${d}": "${projPackageJsonDeps[d]}"`
);
} else {
return fixer.replaceTextRange(
[node.value.range[0] + 1, node.value.range[1] - 1],
`\n "${d}": "${projPackageJsonDeps[d]}"\n `
);
}
},
});
}
});
const deps = (node.value as AST.JSONObjectExpression).properties;
const mappedDeps = missingDeps
.map((d) => `\n "${d}": "${projPackageJsonDeps[d]}"`)
.join(',');

if (deps.length) {
return fixer.insertTextAfter(
deps[deps.length - 1] as any,
`,${mappedDeps}`
);
} else {
return fixer.insertTextAfterRange(
[node.value.range[0] + 1, node.value.range[1] - 1],
`${mappedDeps}\n `
);
}
},
});
}
}

function validateVersionMatchesInstalled(
Expand Down Expand Up @@ -261,15 +270,6 @@ export default createESLintRule<Options, MessageIds>({
]);
}
}

// remove 4 spaces, new line and potential comma from previous line
const shouldRemoveSiblingComma =
isLastProperty && node.parent.properties.length > 1;
const leadingChars = 5 + (shouldRemoveSiblingComma ? 1 : 0);
return fixer.removeRange([
node.range[0] - leadingChars,
node.range[1] + (isLastProperty ? 0 : 1),
]);
},
});
}
Expand Down

1 comment on commit 55c0a16

@vercel
Copy link

@vercel vercel bot commented on 55c0a16 Jul 3, 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-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.