UNPKG

2.27 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 report = require('../../utils/report');
11const ruleMessages = require('../../utils/ruleMessages');
12const styleSearch = require('style-search');
13const validateOptions = require('../../utils/validateOptions');
14const vendor = require('../../utils/vendor');
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, context) {
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 = 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 if (context.fix) {
71 decl.value = isAutoprefixable.unprefix(decl.value);
72
73 return;
74 }
75
76 report({
77 message: messages.rejected(fullIdentifier),
78 node: decl,
79 index: prop.length + (decl.raws.between || '').length + match.startIndex,
80 result,
81 ruleName,
82 });
83 });
84 });
85 };
86}
87
88rule.ruleName = ruleName;
89rule.messages = messages;
90module.exports = rule;