-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into eslint-fix-utils
- Loading branch information
Showing
5 changed files
with
469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# no-empty-fields | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule flags all empty arrays and objects in a `package.json`, | ||
as such empty expressions do nothing, and are often the result of a mistake. | ||
It will report both named properties that are empty, as well as nested arrays and objects | ||
that are empty. | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```json | ||
{ | ||
"main": "lib/index.js", | ||
"scripts": {}, | ||
"files": [], | ||
"simple-git-hooks": { | ||
"pre-commit": "pnpm exec nano-staged --allow-empty", | ||
"preserveUnused": [] | ||
} | ||
} | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```json | ||
{ | ||
"main": "lib/index.js", | ||
"simple-git-hooks": { | ||
"pre-commit": "pnpm exec nano-staged --allow-empty" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import type { AST as JsonAST } from "jsonc-eslint-parser"; | ||
|
||
import * as ESTree from "estree"; | ||
|
||
import { createRule, PackageJsonRuleContext } from "../createRule"; | ||
|
||
const getDataAndMessageId = ( | ||
node: | ||
| JsonAST.JSONArrayExpression | ||
| JsonAST.JSONObjectExpression | ||
| JsonAST.JSONProperty, | ||
): { | ||
data: Record<string, string>; | ||
messageId: "emptyExpression" | "emptyFields"; | ||
} => { | ||
switch (node.type) { | ||
case "JSONArrayExpression": | ||
return { | ||
data: { | ||
expressionType: "array", | ||
}, | ||
messageId: "emptyExpression", | ||
}; | ||
case "JSONObjectExpression": | ||
return { | ||
data: { | ||
expressionType: "object", | ||
}, | ||
messageId: "emptyExpression", | ||
}; | ||
case "JSONProperty": | ||
return { | ||
data: { | ||
field: (node.key as JsonAST.JSONStringLiteral).value, | ||
}, | ||
messageId: "emptyFields", | ||
}; | ||
} | ||
}; | ||
|
||
const report = ( | ||
context: PackageJsonRuleContext, | ||
node: | ||
| JsonAST.JSONArrayExpression | ||
| JsonAST.JSONObjectExpression | ||
| JsonAST.JSONProperty, | ||
) => { | ||
const { data, messageId } = getDataAndMessageId(node); | ||
context.report({ | ||
data, | ||
messageId, | ||
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); | ||
} | ||
}, | ||
messageId: "remove", | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
const getNode = ( | ||
node: JsonAST.JSONArrayExpression | JsonAST.JSONObjectExpression, | ||
) => { | ||
return node.parent.type === "JSONProperty" ? node.parent : node; | ||
}; | ||
|
||
export const rule = createRule({ | ||
create(context) { | ||
return { | ||
JSONArrayExpression(node: JsonAST.JSONArrayExpression) { | ||
if (!node.elements.length) { | ||
report(context, getNode(node)); | ||
} | ||
}, | ||
JSONObjectExpression(node: JsonAST.JSONObjectExpression) { | ||
if (!node.properties.length) { | ||
report(context, getNode(node)); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: "Reports on unnecessary empty arrays and objects.", | ||
recommended: true, | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
emptyExpression: | ||
"This {{expressionType}} does nothing and can be removed.", | ||
emptyFields: | ||
"The field '{{field}}' does nothing and can be removed.", | ||
remove: "Remove this empty field.", | ||
}, | ||
schema: [], | ||
type: "suggestion", | ||
}, | ||
}); |
Oops, something went wrong.