-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequal-next-line.ts
151 lines (143 loc) · 4.28 KB
/
equal-next-line.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { makeListener, n } from "../utils";
import type { Rule } from "eslint";
import { Settings } from "../settings";
import type { visitor } from "@peggyjs/eslint-parser";
function fixEquals(
node: visitor.AST.Rule,
indent: string,
newline: string
): Rule.ReportFixer {
return function fix(fixer: Rule.RuleFixer): Rule.Fix {
const first = (node.expression.type === "named")
? node.expression.name.range[1]
: node.name.range[1];
return fixer.replaceTextRange(
[first, node.equals.range[0]],
`${newline}${indent}`
);
};
}
function removeWhitespace(node: visitor.AST.Rule): Rule.ReportFixer {
return function fix(fixer: Rule.RuleFixer): Rule.Fix {
const first = (node.expression.type === "named")
? node.expression.name.range[1]
: node.name.range[1];
return fixer.replaceTextRange(
[first, node.equals.range[0]],
" "
);
};
}
const rule: Rule.RuleModule = {
meta: {
type: "layout",
docs: {
description: "require rule equal signs to be on the next line from the rule name",
recommended: true,
url: "https://github.com/peggyjs/peggyjs-eslint-plugin/blob/main/docs/rules/equal-next-line.md",
},
messages: {
next: "Equal sign must be on next line from the rule name.",
same: "Equal sign must be on the same line as rule name.",
},
fixable: "whitespace",
schema: [
{
oneOf: [
{
title: "style",
type: "string",
enum: ["always", "never"],
},
{
type: "object",
properties: {
style: {
type: "string",
enum: ["always", "never"],
},
exceptions: {
type: "array",
items: {
type: "string",
enum: ["choice", "named"],
},
},
},
},
],
},
],
},
create(context: Rule.RuleContext): Rule.RuleListener {
const optObj = (typeof context.options[0] === "string")
? { style: context.options[0] }
: context.options[0];
const opts = {
style: "never",
exceptions: ["choice", "named"],
...optObj,
};
const settings = new Settings(context.settings);
const indent = settings.indent;
const newline = settings.newline;
return makeListener({
rule(node: visitor.AST.Rule): void {
const ruleLine = node.name.loc.start.line;
const equalLine = node.equals.loc.start.line;
switch (opts.style) {
case "always": {
if (ruleLine !== equalLine - 1) {
if (!context.getSourceCode().commentsExistBetween(
n(node.name), n(node.equals)
)) {
context.report({
node: n(node.equals),
messageId: "next",
fix: fixEquals(node, indent, newline),
});
}
}
break;
}
case "never": {
let messageId: string | undefined = undefined;
let fix: Rule.ReportFixer | null = null;
if (ruleLine === equalLine) {
if (opts.exceptions.includes(node.expression.type)) {
// These go on the next line but they're not
messageId = "next";
fix = fixEquals(node, indent, newline);
}
} else {
if (opts.exceptions.includes(node.expression.type)) {
if (ruleLine !== equalLine - 1) {
// There's probably an extra blank line
messageId = "next";
fix = fixEquals(node, indent, newline);
}
} else {
messageId = "same";
fix = removeWhitespace(node);
}
}
if (messageId) {
context.report({
node: n(node.equals),
messageId,
fix,
});
}
break;
}
/* c8 ignore start */
// Schema prevents unknown styles.
default:
throw new Error(`Unknown style: '${opts.style}'`);
/* c8 ignore stop */
}
},
});
},
};
export = rule;