-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
145 lines (113 loc) · 3.84 KB
/
index.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
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
// @ts-nocheck
'use strict';
const beforeBlockString = require('../../utils/beforeBlockString');
const blockString = require('../../utils/blockString');
const hasBlock = require('../../utils/hasBlock');
const hasEmptyBlock = require('../../utils/hasEmptyBlock');
const rawNodeString = require('../../utils/rawNodeString');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const whitespaceChecker = require('../../utils/whitespaceChecker');
const ruleName = 'block-opening-brace-newline-after';
const messages = ruleMessages(ruleName, {
expectedAfter: () => 'Expected newline after "{"',
expectedAfterMultiLine: () => 'Expected newline after "{" of a multi-line block',
rejectedAfterMultiLine: () => 'Unexpected whitespace after "{" of a multi-line block',
});
function rule(expectation, options, context) {
const checker = whitespaceChecker('newline', expectation, messages);
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: ['always', 'always-multi-line', 'never-multi-line'],
});
if (!validOptions) {
return;
}
// Check both kinds of statement: rules and at-rules
root.walkRules(check);
root.walkAtRules(check);
function check(statement) {
// Return early if blockless or has an empty block
if (!hasBlock(statement) || hasEmptyBlock(statement)) {
return;
}
const backupCommentNextBefores = new Map();
// next node with checking newlines after comment
function nextNode(startNode) {
if (!startNode || !startNode.next) return null;
if (startNode.type === 'comment') {
const reNewLine = /\r?\n/;
const newLineMatch = reNewLine.test(startNode.raws.before);
const next = startNode.next();
if (next && newLineMatch && !reNewLine.test(next.raws.before)) {
backupCommentNextBefores.set(next, next.raws.before);
next.raws.before = startNode.raws.before;
}
return nextNode(next);
}
return startNode;
}
// Allow an end-of-line comment
const nodeToCheck = nextNode(statement.first);
if (!nodeToCheck) {
return;
}
checker.afterOneOnly({
source: rawNodeString(nodeToCheck),
index: -1,
lineCheckStr: blockString(statement),
err: (m) => {
if (context.fix) {
if (expectation.startsWith('always')) {
const index = nodeToCheck.raws.before.search(/\r?\n/);
if (index >= 0) {
nodeToCheck.raws.before = nodeToCheck.raws.before.slice(index);
} else {
nodeToCheck.raws.before = context.newline + nodeToCheck.raws.before;
}
backupCommentNextBefores.delete(nodeToCheck);
return;
}
if (expectation === 'never-multi-line') {
// Restore the `before` of the node next to the comment node.
backupCommentNextBefores.forEach((before, node) => {
node.raws.before = before;
});
backupCommentNextBefores.clear();
// Fix
const reNewLine = /\r?\n/;
let fixTarget = statement.first;
while (fixTarget) {
if (reNewLine.test(fixTarget.raws.before)) {
fixTarget.raws.before = fixTarget.raws.before.replace(/\r?\n/g, '');
}
if (fixTarget.type !== 'comment') {
break;
}
fixTarget = fixTarget.next();
}
nodeToCheck.raws.before = '';
return;
}
}
report({
message: m,
node: statement,
index: beforeBlockString(statement, { noRawBefore: true }).length + 1,
result,
ruleName,
});
},
});
// Restore the `before` of the node next to the comment node.
backupCommentNextBefores.forEach((before, node) => {
node.raws.before = before;
});
}
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;