/
index.js
149 lines (124 loc) · 4.13 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
'use strict';
const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
const blockString = require('../../utils/blockString');
const hasEmptyLine = require('../../utils/hasEmptyLine');
const isAfterComment = require('../../utils/isAfterComment');
const isAfterStandardPropertyDeclaration = require('../../utils/isAfterStandardPropertyDeclaration');
const isCustomProperty = require('../../utils/isCustomProperty');
const isFirstNested = require('../../utils/isFirstNested');
const isFirstNodeOfRoot = require('../../utils/isFirstNodeOfRoot');
const isSingleLineString = require('../../utils/isSingleLineString');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const optionsMatches = require('../../utils/optionsMatches');
const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isAtRule, isRule, isRoot } = require('../../utils/typeGuards');
const ruleName = 'declaration-empty-line-before';
const messages = ruleMessages(ruleName, {
expected: 'Expected empty line before declaration',
rejected: 'Unexpected empty line before declaration',
});
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: ['always', 'never'],
},
{
actual: secondaryOptions,
possible: {
except: ['first-nested', 'after-comment', 'after-declaration'],
ignore: [
'after-comment',
'after-declaration',
'first-nested',
'inside-single-line-block',
],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
const prop = decl.prop;
const parent = decl.parent;
if (parent == null) {
return;
}
// Ignore the first node
if (isFirstNodeOfRoot(decl)) {
return;
}
if (!isAtRule(parent) && !isRule(parent) && !isRoot(parent)) {
return;
}
if (!isStandardSyntaxDeclaration(decl)) {
return;
}
if (isCustomProperty(prop)) {
return;
}
// Optionally ignore the node if a comment precedes it
if (optionsMatches(secondaryOptions, 'ignore', 'after-comment') && isAfterComment(decl)) {
return;
}
// Optionally ignore the node if a declaration precedes it
if (
optionsMatches(secondaryOptions, 'ignore', 'after-declaration') &&
isAfterStandardPropertyDeclaration(decl)
) {
return;
}
// Optionally ignore the node if it is the first nested
if (optionsMatches(secondaryOptions, 'ignore', 'first-nested') && isFirstNested(decl)) {
return;
}
// Optionally ignore nodes inside single-line blocks
if (
optionsMatches(secondaryOptions, 'ignore', 'inside-single-line-block') &&
isSingleLineString(blockString(parent))
) {
return;
}
let expectEmptyLineBefore = primary === 'always';
// Optionally reverse the expectation if any exceptions apply
if (
(optionsMatches(secondaryOptions, 'except', 'first-nested') && isFirstNested(decl)) ||
(optionsMatches(secondaryOptions, 'except', 'after-comment') && isAfterComment(decl)) ||
(optionsMatches(secondaryOptions, 'except', 'after-declaration') &&
isAfterStandardPropertyDeclaration(decl))
) {
expectEmptyLineBefore = !expectEmptyLineBefore;
}
// Check for at least one empty line
const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
// Return if the expectation is met
if (expectEmptyLineBefore === hasEmptyLineBefore) {
return;
}
// Fix
if (context.fix) {
if (context.newline == null) return;
if (expectEmptyLineBefore) {
addEmptyLineBefore(decl, context.newline);
} else {
removeEmptyLinesBefore(decl, context.newline);
}
return;
}
const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
report({ message, node: decl, result, ruleName });
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;