1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const isCustomProperty = require('../../utils/isCustomProperty');
|
5 | const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
|
6 | const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
|
7 | const optionsMatches = require('../../utils/optionsMatches');
|
8 | const postcss = require('postcss');
|
9 | const properties = require('known-css-properties').all;
|
10 | const report = require('../../utils/report');
|
11 | const ruleMessages = require('../../utils/ruleMessages');
|
12 | const validateOptions = require('../../utils/validateOptions');
|
13 |
|
14 | const ruleName = 'property-no-unknown';
|
15 |
|
16 | const messages = ruleMessages(ruleName, {
|
17 | rejected: (property) => `Unexpected unknown property "${property}"`,
|
18 | });
|
19 |
|
20 | function 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 |
|
88 | rule.ruleName = ruleName;
|
89 | rule.messages = messages;
|
90 | module.exports = rule;
|