1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
|
5 | const postcss = require('postcss');
|
6 | const report = require('../../utils/report');
|
7 | const ruleMessages = require('../../utils/ruleMessages');
|
8 | const validateOptions = require('../../utils/validateOptions');
|
9 |
|
10 | const ruleName = 'declaration-property-value-whitelist';
|
11 |
|
12 | const messages = ruleMessages(ruleName, {
|
13 | rejected: (property, value) => `Unexpected value "${value}" for property "${property}"`,
|
14 | });
|
15 |
|
16 | function rule(whitelist) {
|
17 | return (root, result) => {
|
18 | const validOptions = validateOptions(result, ruleName, {
|
19 | actual: whitelist,
|
20 | possible: [_.isObject],
|
21 | });
|
22 |
|
23 | if (!validOptions) {
|
24 | return;
|
25 | }
|
26 |
|
27 | root.walkDecls((decl) => {
|
28 | const prop = decl.prop;
|
29 | const value = decl.value;
|
30 |
|
31 | const unprefixedProp = postcss.vendor.unprefixed(prop);
|
32 | const propWhitelist = _.find(whitelist, (list, propIdentifier) =>
|
33 | matchesStringOrRegExp(unprefixedProp, propIdentifier),
|
34 | );
|
35 |
|
36 | if (_.isEmpty(propWhitelist)) {
|
37 | return;
|
38 | }
|
39 |
|
40 | if (matchesStringOrRegExp(value, propWhitelist)) {
|
41 | return;
|
42 | }
|
43 |
|
44 | report({
|
45 | message: messages.rejected(prop, value),
|
46 | node: decl,
|
47 | result,
|
48 | ruleName,
|
49 | });
|
50 | });
|
51 | };
|
52 | }
|
53 |
|
54 | rule.ruleName = ruleName;
|
55 | rule.messages = messages;
|
56 | module.exports = rule;
|