-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathno-empty-code-blocks.ts
73 lines (69 loc) · 2.03 KB
/
no-empty-code-blocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { makeListener, n } from "../utils";
import type { Rule } from "eslint";
import type { visitor } from "@peggyjs/eslint-parser";
function check(
context: Rule.RuleContext,
code: visitor.AST.Code,
fromOffset: number,
messageId: string
): void {
if (code.value.replace(/\r?\n/g, "").trim() === "") {
context.report({
node: n(code),
messageId,
fix(fixer: Rule.RuleFixer): Rule.Fix {
return fixer.removeRange(
[
// Get whatever interstitial whitespace was lurking.
fromOffset,
// The {} are not include in node.code. They're always the
// next characters outside, however.
code.range[1] + 1,
]
);
},
});
}
}
const rule: Rule.RuleModule = {
meta: {
type: "layout",
docs: {
description: "code blocks in actions should not be empty",
recommended: true,
url: "https://github.com/peggyjs/peggyjs-eslint-plugin/blob/main/docs/rules/no-empty-code-blocks.md",
},
messages: {
action: "Action code block must not be empty.",
predicate: "Semantic predicate code block must not be empty.",
},
fixable: "code",
schema: [
{
type: "string",
enum: ["semantic"],
},
],
},
create(context: Rule.RuleContext): Rule.RuleListener {
const semantic = context.options[0] === "semantic";
return makeListener({
action(node: visitor.AST.ActionExpression): void {
check(context, node.code, node.expression.range[1], "action");
},
// Don't need for semantic_and and semantic_not, if using
// semantic-predicate-must-return.
semantic_and(node: visitor.AST.SemanticAndExpression): void {
if (semantic) {
check(context, node.code, node.operator.range[0], "predicate");
}
},
semantic_not(node: visitor.AST.SemanticNotExpression): void {
if (semantic) {
check(context, node.code, node.operator.range[0], "predicate");
}
},
});
},
};
export = rule;