UNPKG

2.15 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const _ = require('lodash');
6const isAutoprefixable = require('../../utils/isAutoprefixable');
7const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
8const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
9const optionsMatches = require('../../utils/optionsMatches');
10const postcss = require('postcss');
11const report = require('../../utils/report');
12const ruleMessages = require('../../utils/ruleMessages');
13const styleSearch = require('style-search');
14const validateOptions = require('../../utils/validateOptions');
15
16const ruleName = 'value-no-vendor-prefix';
17
18const messages = ruleMessages(ruleName, {
19 rejected: (value) => `Unexpected vendor-prefix "${value}"`,
20});
21
22const valuePrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
23
24function 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 //return early if value is to be ignored
57 if (optionsMatches(options, 'ignoreValues', unprefixedValue)) {
58 return;
59 }
60
61 // Search the full declaration in order to get an accurate index
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
82rule.ruleName = ruleName;
83rule.messages = messages;
84module.exports = rule;