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 70b1881 commit a70c061
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/rules/no-empty-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { createRule } from "../createRule";

export const rule = createRule({
create(context) {
const report = (
node: JsonAST.JSONProperty & { key: JsonAST.JSONStringLiteral },
) => {
const report = (node: JsonAST.JSONProperty) => {
context.report({
data: { field: node.key.value },
data: { field: (node.key as JsonAST.JSONStringLiteral).value },
messageId: "emptyFields",
node: node as unknown as ESTree.Node,
suggest: [
Expand Down Expand Up @@ -45,13 +43,19 @@ export const rule = createRule({
};
return {
JSONArrayExpression(node: JsonAST.JSONArrayExpression) {
if (!node.elements.length) {
report(node.parent as any);
if (
!node.elements.length &&
node.parent.type === "JSONProperty"
) {
report(node.parent);
}
},
JSONObjectExpression(node: JsonAST.JSONObjectExpression) {
if (!node.properties.length) {
report(node.parent as any);
if (
!node.properties.length &&
node.parent.type === "JSONProperty"
) {
report(node.parent);
}
},
};
Expand All @@ -62,7 +66,6 @@ export const rule = createRule({
description: "Remove empty fields",
recommended: true,
},
fixable: "code",
hasSuggestions: true,
messages: {
emptyFields: 'Should remove empty "{{field}}"',
Expand Down

0 comments on commit a70c061

Please sign in to comment.