forked from antfu/eslint-plugin-antfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if-newline.ts
47 lines (45 loc) · 1.14 KB
/
if-newline.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
import { createEslintRule } from "../utils";
export const RULE_NAME = "if-newline";
export type MessageIds = "missingIfNewline";
export type Options = [];
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: "layout",
docs: {
description: "Newline after if",
recommended: "stylistic"
},
fixable: "whitespace",
schema: [],
messages: {
missingIfNewline: "Expect newline after if"
}
},
defaultOptions: [],
create: (context) => {
return {
IfStatement(node) {
if (!node.consequent) {
return;
}
if (node.consequent.type === "BlockStatement") {
return;
}
if (node.test.loc.end.line === node.consequent.loc.start.line) {
context.report({
node,
loc: {
start: node.test.loc.end,
end: node.consequent.loc.start
},
messageId: "missingIfNewline",
fix(fixer) {
return fixer.replaceTextRange([node.consequent.range[0], node.consequent.range[0]], "\n");
}
});
}
}
};
}
});