-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added unique-dependencies rule (#126)
## PR Checklist - [x] Addresses an existing open issue: fixes #50 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/blob/main/.github/CONTRIBUTING.md) were taken ## Overview I ended up naming it `unique-dependencies` because I'm not a fan of `no-` prefixes for lint rules (typescript-eslint/typescript-eslint#6022).
- Loading branch information
1 parent
ea1336e
commit a9417d1
Showing
6 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Enforce that if repository directory is specified, it matches the path to the package.json file (`package-json/unique-dependencies`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
<!-- end auto-generated rule header --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { AST as JsonAST } from "jsonc-eslint-parser"; | ||
|
||
import * as ESTree from "estree"; | ||
|
||
import { createRule } from "../createRule.js"; | ||
import { isJSONStringLiteral, isNotNullish } from "../utils/predicates.js"; | ||
|
||
const dependencyPropertyNames = new Set([ | ||
"bundleDependencies", | ||
"bundledDependencies", | ||
"dependencies", | ||
"devDependencies", | ||
"optionalDependencies", | ||
"peerDependencies", | ||
"overrides", | ||
]); | ||
|
||
export default createRule({ | ||
create(context) { | ||
function check( | ||
elements: (JsonAST.JSONNode | null)[], | ||
getNodeToRemove: (element: JsonAST.JSONNode) => ESTree.Node, | ||
) { | ||
const seen = new Set(); | ||
|
||
for (const element of elements | ||
.filter(isNotNullish) | ||
.filter(isJSONStringLiteral) | ||
.reverse()) { | ||
if (seen.has(element.value)) { | ||
report(element); | ||
} else { | ||
seen.add(element.value); | ||
} | ||
} | ||
|
||
function report(node: JsonAST.JSONNode) { | ||
context.report({ | ||
messageId: "overridden", | ||
node: node as unknown as ESTree.Node, | ||
suggest: [ | ||
{ | ||
fix(fixer) { | ||
const removal = getNodeToRemove(node); | ||
return [ | ||
fixer.remove(removal), | ||
fixer.remove( | ||
// A listing that's overridden can't be last, | ||
// so we're guaranteed there's a comma after. | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
context.sourceCode.getTokenAfter( | ||
removal, | ||
)!, | ||
), | ||
]; | ||
}, | ||
messageId: "remove", | ||
}, | ||
], | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
"Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.type=JSONLiteral]"( | ||
node: JsonAST.JSONProperty & { | ||
key: JsonAST.JSONStringLiteral; | ||
}, | ||
) { | ||
if (!dependencyPropertyNames.has(node.key.value)) { | ||
return; | ||
} | ||
|
||
switch (node.value.type) { | ||
case "JSONArrayExpression": | ||
check( | ||
node.value.elements, | ||
(element) => element as unknown as ESTree.Node, | ||
); | ||
break; | ||
case "JSONObjectExpression": | ||
check( | ||
node.value.properties.map( | ||
(property) => property.key, | ||
), | ||
(property) => | ||
property.parent as unknown as ESTree.Node, | ||
); | ||
break; | ||
} | ||
}, | ||
}; | ||
}, | ||
|
||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: | ||
"Enforce that if repository directory is specified, it matches the path to the package.json file", | ||
recommended: true, | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
overridden: | ||
"Package name is overridden by a duplicate listing later on.", | ||
remove: "Remove this redundant dependency listing.", | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.