UNPKG

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