-
Notifications
You must be signed in to change notification settings - Fork 14
/
stylelint-force-app-name-prefix.cjs
115 lines (102 loc) · 3.25 KB
/
stylelint-force-app-name-prefix.cjs
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
/**
* Original code at https://github.com/SunHuawei/stylelint-force-app-name-prefix
*
* @author @SunHuawei What modified:
*
* - Use `.lr-` prefix instead of `.lr `
* - Add support for tag prefixes: `lr-`
*/
var _ = require('lodash');
var stylelint = require('stylelint');
var ruleName = 'plugin/stylelint-force-app-name-prefix';
var optionsSchema = {
appName: _.isString,
};
const messages = stylelint.utils.ruleMessages(ruleName, {
invalid: (selector, appName) => 'Selector "' + selector + '" is out of control, please wrap within .' + appName,
invalidKeyFrames: (keyframesName, appName) =>
'Keyframes name "' + keyframesName + '" is out of control, please prefix with ' + appName + '-',
invalidFontFace: (fontFamily, appName) =>
'Custom font-family "' + fontFamily + '" is out of control, please prefix with ' + appName + '-',
});
function findTopParentSelector(node) {
if (node.parent.type === 'root' || node.parent.type === 'atrule') {
return node.selector;
} else {
return findTopParentSelector(node.parent);
}
}
function isInsideAtRule(node) {
if (node.parent.type === 'atrule') {
return true;
} else if (node.parent.type === 'root') {
return false;
} else {
return findTopParentSelector(node.parent);
}
}
module.exports = stylelint.createPlugin(ruleName, function (options) {
return function (root, result) {
if (!options) return;
var validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: options,
possible: optionsSchema,
});
if (!validOptions) return;
const whiteList = ['.' + options.appName, /^:.*/];
root.walkAtRules('keyframes', (rule) => {
const keyframesName = rule.params;
if (keyframesName.indexOf(options.appName + '-') === -1) {
stylelint.utils.report({
ruleName: ruleName,
result: result,
node: rule,
message: messages.invalidKeyFrames(keyframesName, options.appName),
});
}
});
root.walkAtRules('font-face', (rule) => {
rule.walkDecls('font-family', (decl) => {
if (decl.value.indexOf(options.appName + '-') === -1) {
stylelint.utils.report({
ruleName: ruleName,
result: result,
node: rule,
message: messages.invalidFontFace(decl.value, options.appName),
});
}
});
});
root.walkRules((rule) => {
if (isInsideAtRule(rule)) return;
const topParentSelector = findTopParentSelector(rule);
if (
whiteList.find(function (whiteRule) {
if (whiteRule instanceof RegExp) {
return whiteRule.test(topParentSelector);
} else {
return whiteRule === topParentSelector;
}
})
) {
// in white list, skipped
return;
}
if (
topParentSelector.indexOf('.' + options.appName + '-') === 0 ||
topParentSelector.indexOf(options.appName + '-') === 0
) {
// good
} else {
stylelint.utils.report({
ruleName: ruleName,
result: result,
node: rule,
message: messages.invalid(rule.selector, options.appName),
});
}
});
};
});
module.exports.ruleName = ruleName;
module.exports.messages = messages;