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 22, 2025
1 parent 6e362f6 commit bdd58dc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
43 changes: 41 additions & 2 deletions src/rules/no-empty-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,51 @@ import * as ESTree from "estree";

import { createRule, PackageJsonRuleContext } from "../createRule";

const getDataAndMessageId = (
node:
| JsonAST.JSONArrayExpression
| JsonAST.JSONObjectExpression
| JsonAST.JSONProperty,
): {
data: Record<string, string>;
messageId: "emptyFields" | "emptyExpression";

Check failure on line 14 in src/rules/no-empty-fields.ts

View workflow job for this annotation

GitHub Actions / lint

Expected ""emptyExpression"" to come before ""emptyFields""
} => {
switch (node.type) {
case "JSONProperty":
return {
data: {
field: (node.key as JsonAST.JSONStringLiteral).value,
},
messageId: "emptyFields",
};
case "JSONArrayExpression":

Check failure on line 24 in src/rules/no-empty-fields.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "JSONArrayExpression" to come before "JSONProperty"
return {
data: {
expressionType: "array",
},
messageId: "emptyExpression",
};
case "JSONObjectExpression":
return {
data: {
expressionType: "object",
},
messageId: "emptyExpression",
};
}
};

const report = (
context: PackageJsonRuleContext,
node:
| JsonAST.JSONArrayExpression
| JsonAST.JSONObjectExpression
| JsonAST.JSONProperty,
) => {
const { data, messageId } = getDataAndMessageId(node);
context.report({
messageId: "emptyFields",
data,
messageId,
node: node as unknown as ESTree.Node,
suggest: [
{
Expand Down Expand Up @@ -74,7 +110,10 @@ export const rule = createRule({
},
hasSuggestions: true,
messages: {
emptyFields: "This empty field does nothing and can be removed.",
emptyFields:
"The field '{{field}}' does nothing and can be removed.",
emptyExpression:

Check failure on line 115 in src/rules/no-empty-fields.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "emptyExpression" to come before "emptyFields"
"This {{expressionType}} does nothing and can be removed.",
remove: "Remove this empty field.",
},
schema: [],
Expand Down
6 changes: 3 additions & 3 deletions src/tests/rules/no-empty-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ruleTester.run("no-empty-fields", rule, {
`,
errors: [
{
messageId: "emptyFields",
messageId: "emptyExpression",
suggestions: [
{
messageId: "remove",
Expand All @@ -61,7 +61,7 @@ ruleTester.run("no-empty-fields", rule, {
`,
errors: [
{
messageId: "emptyFields",
messageId: "emptyExpression",
suggestions: [
{
messageId: "remove",
Expand All @@ -74,7 +74,7 @@ ruleTester.run("no-empty-fields", rule, {
],
},
{
messageId: "emptyFields",
messageId: "emptyExpression",
suggestions: [
{
messageId: "remove",
Expand Down

0 comments on commit bdd58dc

Please sign in to comment.