-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheslint-local-rules.js
75 lines (73 loc) · 1.98 KB
/
eslint-local-rules.js
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
module.exports = {
rules: {
"ascii-comments": {
meta: {
type: "layout",
docs: {
description: "Comments should use ASCII characters only.",
},
},
create(context) {
const sourceCode = context.getSourceCode();
return {
Program() {
for(const comment of sourceCode.getAllComments()) {
const match = (/[^\s!-~]+/m).exec(comment.value);
if(match) {
const start = comment.range[0];
context.report({
loc: {
start: sourceCode.getLocFromIndex(start + 2 + match.index),
end: sourceCode.getLocFromIndex(start + 2 + match.index + match[0].length),
},
message: "Comments should use ASCII characters only.",
});
}
}
},
};
},
},
"single-line-control-statement-spacing": {
meta: {
type: "layout",
docs: {
description: "Enforces consistent spacing in single-line control statements.",
},
fixable: "whitespace",
},
create(context) {
const sourceCode = context.getSourceCode();
function checkSpaceAfter(node) {
if(!node) return;
if(node.loc.start.line !== node.loc.end.line) return; // Not single-line
const body = node.consequent || node.body;
const token = sourceCode.getFirstToken(body);
const prev = sourceCode.getTokenBefore(token);
const start = prev.range[1];
const end = token.range[0];
if(start === end) {
context.report({
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
message: "There should be a single space after control statements.",
fix(fixer) {
return fixer.replaceTextRange([start, end], " ");
},
});
}
}
return {
IfStatement: checkSpaceAfter,
DoWhileStatement: checkSpaceAfter,
ForInStatement: checkSpaceAfter,
ForOfStatement: checkSpaceAfter,
ForStatement: checkSpaceAfter,
WhileStatement: checkSpaceAfter,
};
},
},
},
};