Skip to content

Commit

Permalink
fix(linter): ensure no trailing commas on deps-check obsolete package…
Browse files Browse the repository at this point in the history
… fix (#17915)
  • Loading branch information
meeroslav authored Jul 3, 2023
1 parent 6b82a2f commit a70e1a8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
78 changes: 78 additions & 0 deletions packages/eslint-plugin/src/rules/dependency-checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,84 @@ describe('Dependency checks (eslint)', () => {
`);
});

it('should remove obsolete package at the end with fix', () => {
const packageJson = {
name: '@mycompany/liba',
peerDependencies: {
external1: '^16.0.0',
external2: '^5.2.0',
unneeded: '>= 16 < 18',
},
};

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(
{},
`${process.cwd()}/proj/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' },
{ source: 'liba', target: 'npm:external2', type: 'static' },
],
},
},
{
liba: [
createFile(`libs/liba/src/main.ts`, [
'npm:external1',
'npm:external2',
]),
createFile(`libs/liba/package.json`, [
'npm:external1',
'npm:external2',
'npm:unneeded',
]),
],
}
);
expect(failures.length).toEqual(1);
expect(failures[0].message).toMatchInlineSnapshot(
`"The "unneeded" package is not used by "liba"."`
);

// should apply fixer
const content = JSON.stringify(packageJson, null, 2);
const result =
content.slice(0, failures[0].fix.range[0]) +
failures[0].fix.text +
content.slice(failures[0].fix.range[1]);
expect(result).toMatchInlineSnapshot(`
"{
"name": "@mycompany/liba",
"peerDependencies": {
"external1": "^16.0.0",
"external2": "^5.2.0"
}
}"
`);
});

it('should not check obsolete deps if checkObsoleteDependencies=false', () => {
const packageJson = {
name: '@mycompany/liba',
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/dependency-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export default createESLintRule<Options, MessageIds>({
if (index > 0) {
const previousNode = node.parent.properties[index - 1];
return fixer.removeRange([
previousNode.range[1] + 1,
previousNode.range[1] + (isLastProperty ? 0 : 1),
node.range[1] + (isLastProperty ? 0 : 1),
]);
} else {
Expand Down

1 comment on commit a70e1a8

@vercel
Copy link

@vercel vercel bot commented on a70e1a8 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-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.