1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const declarationValueIndex = require('../../utils/declarationValueIndex');
|
5 | const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
|
6 | const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
|
7 | const postcss = require('postcss');
|
8 | const report = require('../../utils/report');
|
9 | const ruleMessages = require('../../utils/ruleMessages');
|
10 | const validateOptions = require('../../utils/validateOptions');
|
11 | const valueParser = require('postcss-value-parser');
|
12 |
|
13 | const ruleName = 'declaration-property-unit-blacklist';
|
14 |
|
15 | const messages = ruleMessages(ruleName, {
|
16 | rejected: (property, unit) => `Unexpected unit "${unit}" for property "${property}"`,
|
17 | });
|
18 |
|
19 | function rule(blacklist) {
|
20 | return (root, result) => {
|
21 | const validOptions = validateOptions(result, ruleName, {
|
22 | actual: blacklist,
|
23 | possible: [_.isObject],
|
24 | });
|
25 |
|
26 | if (!validOptions) {
|
27 | return;
|
28 | }
|
29 |
|
30 | root.walkDecls((decl) => {
|
31 | const prop = decl.prop;
|
32 | const value = decl.value;
|
33 |
|
34 | const unprefixedProp = postcss.vendor.unprefixed(prop);
|
35 |
|
36 | const propBlacklist = _.find(blacklist, (list, propIdentifier) =>
|
37 | matchesStringOrRegExp(unprefixedProp, propIdentifier),
|
38 | );
|
39 |
|
40 | if (!propBlacklist) {
|
41 | return;
|
42 | }
|
43 |
|
44 | valueParser(value).walk((node) => {
|
45 |
|
46 | if (node.type === 'function' && node.value.toLowerCase() === 'url') {
|
47 | return false;
|
48 | }
|
49 |
|
50 | if (node.type === 'string') {
|
51 | return;
|
52 | }
|
53 |
|
54 | const unit = getUnitFromValueNode(node);
|
55 |
|
56 | if (!unit || (unit && !propBlacklist.includes(unit.toLowerCase()))) {
|
57 | return;
|
58 | }
|
59 |
|
60 | report({
|
61 | message: messages.rejected(prop, unit),
|
62 | node: decl,
|
63 | index: declarationValueIndex(decl) + node.sourceIndex,
|
64 | result,
|
65 | ruleName,
|
66 | });
|
67 | });
|
68 | });
|
69 | };
|
70 | }
|
71 |
|
72 | rule.ruleName = ruleName;
|
73 | rule.messages = messages;
|
74 | module.exports = rule;
|