-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
188 lines (170 loc) · 5.78 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
'use strict';
const validateCustomProperties = require('./lib/validate-custom-properties');
const validateUtilities = require('./lib/validate-utilities');
const validateSelectors = require('./lib/validate-selectors');
const generateConfig = require('./lib/generate-config');
const toRegexp = require('./lib/to-regexp');
const path = require('path');
const checkImplicit = require('./lib/check-implicit');
const getComponentNameFromFilename = require('./lib/get-component-name-from-filename');
const DEFINE_VALUE = '([-_a-zA-Z0-9]+)\\s*(?:;\\s*(weak))?';
const DEFINE_DIRECTIVE = new RegExp(
`(?:\\*?\\s*@define ${DEFINE_VALUE})|(?:\\s*postcss-bem-linter: define ${DEFINE_VALUE})\\s*`
);
const END_DIRECTIVE = new RegExp(
'(?:\\*\\s*@end\\s*)|' + '(?:\\s*postcss-bem-linter: end)\\s*'
);
const UTILITIES_IDENT = 'utilities';
const WEAK_IDENT = 'weak';
function stripUnderscore(str) {
return str.replace(/^_/, '');
}
/**
* Set things up and call the validators.
*
* If the input CSS does not have any
* directive defining a component name according to the specified pattern,
* do nothing -- or warn, if the directive is there but the name does not match.
*
* @param {Object|String} primaryOptions
* @param {Object} [secondaryOptions]
*/
const plugin = (primaryOptions, secondaryOptions) => {
const config = generateConfig(primaryOptions, secondaryOptions);
const patterns = config.patterns;
return {
postcssPlugin: 'postcss-bem-linter',
Once(root, {result}) {
const ranges = findRanges(root);
root.walkRules(rule => {
if (rule.parent && rule.parent.name === 'keyframes') return;
if (!rule.source) return;
const ruleStartLine = rule.source.start.line;
ranges.forEach(range => {
if (ruleStartLine < range.start) return;
if (range.end && ruleStartLine > range.end) return;
checkRule(rule, range);
});
});
function checkRule(rule, range) {
if (range.defined === UTILITIES_IDENT) {
if (!patterns.utilitySelectors) {
throw new Error(
'You tried to `@define utilities` but have not provided ' +
'a `utilitySelectors` pattern'
);
}
validateUtilities({
rule,
utilityPattern: toRegexp(patterns.utilitySelectors),
ignorePattern: toRegexp(patterns.ignoreSelectors),
result,
});
return;
}
if (!patterns.componentSelectors) {
throw new Error(
'You tried to `@define` a component but have not provided ' +
'a `componentSelectors` pattern'
);
}
validateCustomProperties({
rule,
componentName: range.defined,
result,
ignorePattern: toRegexp(patterns.ignoreCustomProperties),
});
validateSelectors({
rule,
componentName: range.defined,
weakMode: range.weakMode,
selectorPattern: patterns.componentSelectors,
selectorPatternOptions: config.presetOptions,
ignorePattern: toRegexp(patterns.ignoreSelectors),
result,
});
}
function findRanges(root) {
const ranges = [];
if (root.source && root.source.input && root.source.input.file) {
const filename = root.source.input.file;
if (
checkImplicit.isImplicitUtilities(
config.implicitUtilities,
filename
)
) {
ranges.push({
defined: 'utilities',
start: 0,
weakMode: false,
});
} else if (
checkImplicit.isImplicitComponent(
config.implicitComponents,
filename
)
) {
let defined = stripUnderscore(
path.basename(filename).split('.')[0]
);
if (defined === 'index') {
defined = path.basename(path.join(filename, '..'));
} else {
defined = getComponentNameFromFilename(defined, config);
}
if (
defined !== UTILITIES_IDENT &&
!toRegexp(config.componentNamePattern).test(defined)
) {
result.warn(
`Invalid component name ${defined} from implicit conversion from filename ${filename}`
);
}
ranges.push({
defined,
start: 0,
weakMode: false,
});
}
}
root.walkComments(comment => {
const commentStartLine = comment.source
? comment.source.start.line
: null;
if (!commentStartLine) return;
if (END_DIRECTIVE.test(comment.text)) {
endCurrentRange(commentStartLine);
return;
}
const directiveMatch = comment.text.match(DEFINE_DIRECTIVE);
if (!directiveMatch) return;
const defined = (directiveMatch[1] || directiveMatch[3]).trim();
if (
defined !== UTILITIES_IDENT &&
!toRegexp(config.componentNamePattern).test(defined)
) {
result.warn(`Invalid component name in definition /*${comment}*/`, {
node: comment,
});
}
endCurrentRange(commentStartLine);
ranges.push({
defined,
start: commentStartLine,
weakMode: directiveMatch[2] === WEAK_IDENT,
});
});
return ranges;
function endCurrentRange(line) {
if (!ranges.length) return;
const lastRange = ranges[ranges.length - 1];
if (lastRange.end) return;
lastRange.end = line;
}
}
},
};
};
plugin.postcss = true;
module.exports = plugin;