1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const _ = require('lodash');
|
6 | const isAutoprefixable = require('../../utils/isAutoprefixable');
|
7 | const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
|
8 | const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
|
9 | const optionsMatches = require('../../utils/optionsMatches');
|
10 | const postcss = require('postcss');
|
11 | const report = require('../../utils/report');
|
12 | const ruleMessages = require('../../utils/ruleMessages');
|
13 | const styleSearch = require('style-search');
|
14 | const validateOptions = require('../../utils/validateOptions');
|
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 valuePrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
|
23 |
|
24 | function rule(actual, options) {
|
25 | return (root, result) => {
|
26 | const validOptions = validateOptions(
|
27 | result,
|
28 | ruleName,
|
29 | { actual },
|
30 | {
|
31 | optional: true,
|
32 | actual: options,
|
33 | possible: {
|
34 | ignoreValues: [_.isString],
|
35 | },
|
36 | },
|
37 | );
|
38 |
|
39 | if (!validOptions) {
|
40 | return;
|
41 | }
|
42 |
|
43 | root.walkDecls((decl) => {
|
44 | if (
|
45 | !isStandardSyntaxDeclaration(decl) ||
|
46 | !isStandardSyntaxProperty(decl.prop) ||
|
47 | !decl.value.startsWith('-')
|
48 | ) {
|
49 | return;
|
50 | }
|
51 |
|
52 | const prop = decl.prop;
|
53 | const value = decl.value;
|
54 | const unprefixedValue = postcss.vendor.unprefixed(value);
|
55 |
|
56 |
|
57 | if (optionsMatches(options, 'ignoreValues', unprefixedValue)) {
|
58 | return;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 | styleSearch({ source: value.toLowerCase(), target: valuePrefixes }, (match) => {
|
64 | const fullIdentifier = /^(-[a-z-]+)\b/i.exec(value.slice(match.startIndex))[1];
|
65 |
|
66 | if (!isAutoprefixable.propertyValue(prop, fullIdentifier)) {
|
67 | return;
|
68 | }
|
69 |
|
70 | report({
|
71 | message: messages.rejected(fullIdentifier),
|
72 | node: decl,
|
73 | index: prop.length + (decl.raws.between || '').length + match.startIndex,
|
74 | result,
|
75 | ruleName,
|
76 | });
|
77 | });
|
78 | });
|
79 | };
|
80 | }
|
81 |
|
82 | rule.ruleName = ruleName;
|
83 | rule.messages = messages;
|
84 | module.exports = rule;
|