/
index.js
202 lines (165 loc) · 4.64 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// @ts-nocheck
'use strict';
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');
const { isNumber } = require('../../utils/validateTypes');
const ruleName = 'max-empty-lines';
const messages = ruleMessages(ruleName, {
expected: (max) => `Expected no more than ${max} empty ${max === 1 ? 'line' : 'lines'}`,
});
function rule(max, options, context) {
let emptyLines = 0;
let lastIndex = -1;
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: max,
possible: isNumber,
},
{
actual: options,
possible: {
ignore: ['comments'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
const ignoreComments = optionsMatches(options, 'ignore', 'comments');
const getChars = replaceEmptyLines.bind(null, max);
/**
* 1. walk nodes & replace enterchar
* 2. deal with special case.
*/
if (context.fix) {
root.walk((node) => {
if (node.type === 'comment') {
if (!ignoreComments) {
node.raws.left = getChars(node.raws.left);
node.raws.right = getChars(node.raws.right);
}
}
if (node.raws.before) {
node.raws.before = getChars(node.raws.before);
}
});
// first node
const firstNodeRawsBefore = root.first && root.first.raws.before;
// root raws
const rootRawsAfter = root.raws.after;
// not document node
if ((root.document && root.document.constructor.name) !== 'Document') {
if (firstNodeRawsBefore) {
root.first.raws.before = getChars(firstNodeRawsBefore, true);
}
if (rootRawsAfter) {
// when max setted 0, should be treated as 1 in this situation.
root.raws.after = replaceEmptyLines(max === 0 ? 1 : max, rootRawsAfter, true);
}
} else if (rootRawsAfter) {
// `css in js` or `html`
root.raws.after = replaceEmptyLines(max === 0 ? 1 : max, rootRawsAfter);
}
return;
}
emptyLines = 0;
lastIndex = -1;
const rootString = root.toString();
styleSearch(
{
source: rootString,
target: /\r\n/.test(rootString) ? '\r\n' : '\n',
comments: ignoreComments ? 'skip' : 'check',
},
(match) => {
checkMatch(rootString, match.startIndex, match.endIndex, root);
},
);
function checkMatch(source, matchStartIndex, matchEndIndex, node) {
const eof = matchEndIndex === source.length;
let violation = false;
// Additional check for beginning of file
if (!matchStartIndex || lastIndex === matchStartIndex) {
emptyLines++;
} else {
emptyLines = 0;
}
lastIndex = matchEndIndex;
if (emptyLines > max) violation = true;
if (!eof && !violation) return;
if (violation) {
report({
message: messages.expected(max),
node,
index: matchStartIndex,
result,
ruleName,
});
}
// Additional check for end of file
if (eof && max) {
emptyLines++;
if (emptyLines > max && isEofNode(result.root, node)) {
report({
message: messages.expected(max),
node,
index: matchEndIndex,
result,
ruleName,
});
}
}
}
function replaceEmptyLines(maxLines, str, isSpecialCase = false) {
const repeatTimes = isSpecialCase ? maxLines : maxLines + 1;
if (repeatTimes === 0 || typeof str !== 'string') {
return '';
}
const emptyLFLines = '\n'.repeat(repeatTimes);
const emptyCRLFLines = '\r\n'.repeat(repeatTimes);
return /(\r\n)+/g.test(str)
? str.replace(/(\r\n)+/g, ($1) => {
if ($1.length / 2 > repeatTimes) {
return emptyCRLFLines;
}
return $1;
})
: str.replace(/(\n)+/g, ($1) => {
if ($1.length > repeatTimes) {
return emptyLFLines;
}
return $1;
});
}
};
}
/**
* Checks whether the given node is the last node of file.
* @param {Document|null} document the document node with `postcss-html` and `postcss-jsx`.
* @param {Root} root the root node of css
*/
function isEofNode(document, root) {
if (!document || document.constructor.name !== 'Document') {
return true;
}
// In the `postcss-html` and `postcss-jsx` syntax, checks that there is text after the given node.
let after;
if (root === document.last) {
after = document.raws && document.raws.afterEnd;
} else {
const rootIndex = document.index(root);
const nextNode = document.nodes[rootIndex + 1];
after = nextNode && nextNode.raws && nextNode.raws.beforeStart;
}
return !String(after).trim();
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;