Skip to content

Commit

Permalink
chore: use eslint-fix-utils for element and property removals (#750)
Browse files Browse the repository at this point in the history
## PR Checklist

-   [x] Addresses an existing open issue: fixes #747
- [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

Uses the exported APIs added in
JoshuaKGoldberg/eslint-fix-utils@6bf5c33.

@michaelfaith I'd love to get your input on this: how does the API feel
to you? If you think they should be changed drastically -here and/or in
the other repo- I'm definitely up for iterating on them. 🙂

~~The build failures can be ignored, they're some problem in
`eslint-fix-utils` that I haven't dug into yet.~~ ~~Blocked on:
JoshuaKGoldberg/eslint-fix-utils#7 ✅
`[email protected]` should work now.

💖
  • Loading branch information
JoshuaKGoldberg authored Jan 23, 2025
1 parent 5e7f675 commit 6a02598
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 79 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@altano/repository-tools": "^0.1.1",
"detect-indent": "6.1.0",
"detect-newline": "3.1.0",
"eslint-fix-utils": "^0.2.0",
"package-json-validator": "^0.8.0",
"semver": "^7.5.4",
"sort-object-keys": "^1.1.3",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 15 additions & 23 deletions src/rules/no-empty-fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { AST as JsonAST } from "jsonc-eslint-parser";

import {
fixRemoveArrayElement,
fixRemoveObjectProperty,
} from "eslint-fix-utils";
import * as ESTree from "estree";

import { createRule, PackageJsonRuleContext } from "../createRule";
Expand Down Expand Up @@ -52,29 +56,17 @@ const report = (
node: node as unknown as ESTree.Node,
suggest: [
{
*fix(fixer) {
yield fixer.remove(node as unknown as ESTree.Node);

const tokenFromCurrentLine =
context.sourceCode.getTokenAfter(
node as unknown as ESTree.Node,
);
const tokenFromPreviousLine =
context.sourceCode.getTokenBefore(
node as unknown as ESTree.Node,
);

if (
tokenFromPreviousLine?.value === "," &&
tokenFromCurrentLine?.value !== ","
) {
yield fixer.remove(tokenFromPreviousLine);
}

if (tokenFromCurrentLine?.value === ",") {
yield fixer.remove(tokenFromCurrentLine);
}
},
fix:
node.type === "JSONProperty"
? fixRemoveObjectProperty(
context,
node as unknown as ESTree.Property,
)
: fixRemoveArrayElement(
context,
node as unknown as ESTree.Expression,
node.parent as unknown as ESTree.ArrayExpression,
),
messageId: "remove",
},
],
Expand Down
39 changes: 6 additions & 33 deletions src/rules/no-redundant-files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AST as JsonAST } from "jsonc-eslint-parser";

import { fixRemoveArrayElement } from "eslint-fix-utils";
import * as ESTree from "estree";

import { createRule } from "../createRule.js";
Expand Down Expand Up @@ -58,39 +59,11 @@ export const rule = createRule({
node: element as unknown as ESTree.Node,
suggest: [
{
*fix(fixer) {
yield fixer.remove(
element as unknown as ESTree.Node,
);

// If this is not the last entry, then we need to remove the comma from this line.
const tokenFromCurrentLine =
context.sourceCode.getTokenAfter(
element as unknown as ESTree.Node,
);
if (tokenFromCurrentLine?.value === ",") {
yield fixer.remove(tokenFromCurrentLine);
}

// If this is the last line and it's not the only entry, then the line above this one
// will become the last line, and should not have a trailing comma.
if (
index > 0 &&
tokenFromCurrentLine?.value !== ","
) {
const tokenFromPreviousLine =
context.sourceCode.getTokenAfter(
elements[
index - 1
] as unknown as ESTree.Node,
);
if (tokenFromPreviousLine?.value === ",") {
yield fixer.remove(
tokenFromPreviousLine,
);
}
}
},
fix: fixRemoveArrayElement(
context,
index,
elements as unknown as ESTree.Expression[],
),
messageId: "remove",
},
],
Expand Down
48 changes: 25 additions & 23 deletions src/rules/unique-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { AST as JsonAST } from "jsonc-eslint-parser";

import {
fixRemoveArrayElement,
fixRemoveObjectProperty,
} from "eslint-fix-utils";
import * as ESTree from "estree";

import { createRule } from "../createRule.js";
Expand All @@ -19,7 +23,7 @@ export const rule = createRule({
create(context) {
function check(
elements: (JsonAST.JSONNode | null)[],
getNodeToRemove: (element: JsonAST.JSONNode) => ESTree.Node,
getNodeToRemove: (element: JsonAST.JSONNode) => JsonAST.JSONNode,
) {
const seen = new Set();

Expand All @@ -28,32 +32,33 @@ export const rule = createRule({
.filter(isJSONStringLiteral)
.reverse()) {
if (seen.has(element.value)) {
report(element);
report(element, elements);
} else {
seen.add(element.value);
}
}

function report(node: JsonAST.JSONNode) {
function report(
node: JsonAST.JSONNode,
elements: (JsonAST.JSONNode | null)[],
) {
const removal = getNodeToRemove(node);
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,
)!,
),
];
},
fix:
removal.type === "JSONProperty"
? fixRemoveObjectProperty(
context,
removal as unknown as ESTree.Property,
)
: fixRemoveArrayElement(
context,
removal as unknown as ESTree.Expression,
elements as unknown as ESTree.ArrayExpression,
),
messageId: "remove",
},
],
Expand All @@ -73,18 +78,15 @@ export const rule = createRule({

switch (node.value.type) {
case "JSONArrayExpression":
check(
node.value.elements,
(element) => element as unknown as ESTree.Node,
);
check(node.value.elements, (element) => element);
break;
case "JSONObjectExpression":
check(
node.value.properties.map(
(property) => property.key,
),
(property) =>
property.parent as unknown as ESTree.Node,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(property) => property.parent!,
);
break;
}
Expand Down

0 comments on commit 6a02598

Please sign in to comment.