UNPKG

2.04 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const isCustomProperty = require('../../utils/isCustomProperty');
5const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
6const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
7const optionsMatches = require('../../utils/optionsMatches');
8const postcss = require('postcss');
9const properties = require('known-css-properties').all;
10const report = require('../../utils/report');
11const ruleMessages = require('../../utils/ruleMessages');
12const validateOptions = require('../../utils/validateOptions');
13
14const ruleName = 'property-no-unknown';
15
16const messages = ruleMessages(ruleName, {
17 rejected: (property) => `Unexpected unknown property "${property}"`,
18});
19
20function rule(actual, options) {
21 const allValidProperties = new Set(properties);
22
23 return (root, result) => {
24 const validOptions = validateOptions(
25 result,
26 ruleName,
27 { actual },
28 {
29 actual: options,
30 possible: {
31 ignoreProperties: [_.isString, _.isRegExp],
32 checkPrefixed: _.isBoolean,
33 ignoreSelectors: [_.isString, _.isRegExp],
34 },
35 optional: true,
36 },
37 );
38
39 if (!validOptions) {
40 return;
41 }
42
43 const shouldCheckPrefixed = _.get(options, 'checkPrefixed');
44
45 root.walkDecls((decl) => {
46 const prop = decl.prop;
47
48 if (!isStandardSyntaxProperty(prop)) {
49 return;
50 }
51
52 if (!isStandardSyntaxDeclaration(decl)) {
53 return;
54 }
55
56 if (isCustomProperty(prop)) {
57 return;
58 }
59
60 if (!shouldCheckPrefixed && postcss.vendor.prefix(prop)) {
61 return;
62 }
63
64 if (optionsMatches(options, 'ignoreProperties', prop)) {
65 return;
66 }
67
68 const { selector } = decl.parent;
69
70 if (selector && optionsMatches(options, 'ignoreSelectors', selector)) {
71 return;
72 }
73
74 if (allValidProperties.has(prop.toLowerCase())) {
75 return;
76 }
77
78 report({
79 message: messages.rejected(prop),
80 node: decl,
81 result,
82 ruleName,
83 });
84 });
85 };
86}
87
88rule.ruleName = ruleName;
89rule.messages = messages;
90module.exports = rule;