Skip to content

Commit

Permalink
chore: use eslint-fix-utils for element and property removals
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jan 19, 2025
1 parent 29a9722 commit c5d820c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 56 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.1.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.

39 changes: 6 additions & 33 deletions src/rules/no-redundant-files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fixRemoveArrayElement } from "eslint-fix-utils";
import type { AST as JsonAST } from "jsonc-eslint-parser";

Check failure on line 2 in src/rules/no-redundant-files.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "jsonc-eslint-parser" (type) to come before "eslint-fix-utils" (external)

import * as ESTree from "estree";
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
47 changes: 24 additions & 23 deletions src/rules/unique-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
fixRemoveArrayElement,
fixRemoveObjectProperty,
} from "eslint-fix-utils";
import type { AST as JsonAST } from "jsonc-eslint-parser";

Check failure on line 5 in src/rules/unique-dependencies.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "jsonc-eslint-parser" (type) to come before "eslint-fix-utils" (external)

import * as ESTree from "estree";
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.Expression | null)[],
),
messageId: "remove",
},
],
Expand All @@ -73,18 +78,14 @@ 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,
(property) => property.parent!,

Check failure on line 88 in src/rules/unique-dependencies.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
);
break;
}
Expand Down

0 comments on commit c5d820c

Please sign in to comment.