Skip to content

Commit

Permalink
refactor: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji committed Jan 20, 2025
1 parent 7d83c1d commit 1bccbe0
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/rules/no-empty-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,16 @@ export const rule = createRule({
suggest: [
{
*fix(fixer) {
yield fixer.remove(
node as unknown as ESTree.Node,
);

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

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

const tokenFromPreviousLine =
context.sourceCode.getTokenAfter(
node as unknown as ESTree.Node,
);
const keys = Object.keys(
JSON.parse(context.sourceCode.text),
);
const index = keys.findIndex(
(key) => key === node.key.value,
);
const tokenFromPreviousLine =
context.sourceCode.getTokenAfter(
node as unknown as ESTree.Node,
);
if (
tokenFromPreviousLine?.value === "," &&
index === keys.length - 1
Expand All @@ -65,6 +50,20 @@ export const rule = createRule({
tokenFromPreviousLine,
);
}

const tokenFromCurrentLine =
context.sourceCode.getTokenAfter(
node as unknown as ESTree.Node,
);
if (tokenFromCurrentLine?.value === ",") {
yield fixer.remove(
tokenFromCurrentLine,
);
}

yield fixer.remove(
node as unknown as ESTree.Node,
);
},
messageId: "remove",
},
Expand All @@ -86,6 +85,7 @@ export const rule = createRule({
remove: "Remove this empty field.",
},
schema: [],
fixable: "code",
type: "suggestion",
},
});

0 comments on commit 1bccbe0

Please sign in to comment.