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