1 | 'use strict';
|
2 |
|
3 | const valueParser = require('postcss-value-parser');
|
4 |
|
5 | const isAutoprefixable = require('../../utils/isAutoprefixable');
|
6 | const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
|
7 | const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
|
8 | const optionsMatches = require('../../utils/optionsMatches');
|
9 | const report = require('../../utils/report');
|
10 | const ruleMessages = require('../../utils/ruleMessages');
|
11 | const setDeclarationValue = require('../../utils/setDeclarationValue');
|
12 | const validateOptions = require('../../utils/validateOptions');
|
13 | const vendor = require('../../utils/vendor');
|
14 | const { isString } = require('../../utils/validateTypes');
|
15 |
|
16 | const ruleName = 'value-no-vendor-prefix';
|
17 |
|
18 | const messages = ruleMessages(ruleName, {
|
19 | rejected: (value) => `Unexpected vendor-prefix "${value}"`,
|
20 | });
|
21 |
|
22 | const meta = {
|
23 | url: 'https://stylelint.io/user-guide/rules/list/value-no-vendor-prefix',
|
24 | };
|
25 |
|
26 | const valuePrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const hasPrefix = (value) => {
|
33 | const lowerValue = value.toLowerCase();
|
34 |
|
35 | return valuePrefixes.some((prefix) => lowerValue.startsWith(prefix));
|
36 | };
|
37 |
|
38 |
|
39 | const rule = (primary, secondaryOptions, context) => {
|
40 | return (root, result) => {
|
41 | const validOptions = validateOptions(
|
42 | result,
|
43 | ruleName,
|
44 | { actual: primary },
|
45 | {
|
46 | optional: true,
|
47 | actual: secondaryOptions,
|
48 | possible: {
|
49 | ignoreValues: [isString],
|
50 | },
|
51 | },
|
52 | );
|
53 |
|
54 | if (!validOptions) {
|
55 | return;
|
56 | }
|
57 |
|
58 | root.walkDecls((decl) => {
|
59 | const { value } = decl;
|
60 |
|
61 | if (
|
62 | !isStandardSyntaxDeclaration(decl) ||
|
63 | !isStandardSyntaxProperty(decl.prop) ||
|
64 | !value.startsWith('-')
|
65 | ) {
|
66 | return;
|
67 | }
|
68 |
|
69 | if (optionsMatches(secondaryOptions, 'ignoreValues', vendor.unprefixed(value))) {
|
70 | return;
|
71 | }
|
72 |
|
73 | const parsedValue = valueParser(value);
|
74 |
|
75 | parsedValue.walk((node) => {
|
76 | if (!hasPrefix(node.value)) {
|
77 | return;
|
78 | }
|
79 |
|
80 | if (!isAutoprefixable.propertyValue(node.value)) {
|
81 | return;
|
82 | }
|
83 |
|
84 | if (context.fix) {
|
85 | node.value = isAutoprefixable.unprefix(node.value);
|
86 |
|
87 | return;
|
88 | }
|
89 |
|
90 | report({
|
91 | message: messages.rejected(node.value),
|
92 | node: decl,
|
93 | index: decl.prop.length + (decl.raws.between || '').length + node.sourceIndex,
|
94 | result,
|
95 | ruleName,
|
96 | });
|
97 | });
|
98 |
|
99 | setDeclarationValue(decl, parsedValue.toString());
|
100 | });
|
101 | };
|
102 | };
|
103 |
|
104 | rule.ruleName = ruleName;
|
105 | rule.messages = messages;
|
106 | rule.meta = meta;
|
107 | module.exports = rule;
|