1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
|
6 | const postcss = require('postcss');
|
7 | const report = require('../../utils/report');
|
8 | const ruleMessages = require('../../utils/ruleMessages');
|
9 | const shorthandData = require('../../reference/shorthandData');
|
10 | const validateOptions = require('../../utils/validateOptions');
|
11 |
|
12 | const ruleName = 'declaration-block-no-shorthand-property-overrides';
|
13 |
|
14 | const messages = ruleMessages(ruleName, {
|
15 | rejected: (shorthand, original) => `Unexpected shorthand "${shorthand}" after "${original}"`,
|
16 | });
|
17 |
|
18 | function rule(actual) {
|
19 | return (root, result) => {
|
20 | const validOptions = validateOptions(result, ruleName, { actual });
|
21 |
|
22 | if (!validOptions) {
|
23 | return;
|
24 | }
|
25 |
|
26 | eachDeclarationBlock(root, (eachDecl) => {
|
27 | const declarations = {};
|
28 |
|
29 | eachDecl((decl) => {
|
30 | const prop = decl.prop;
|
31 | const unprefixedProp = postcss.vendor.unprefixed(prop);
|
32 | const prefix = postcss.vendor.prefix(prop).toLowerCase();
|
33 |
|
34 | const overrideables = shorthandData[unprefixedProp.toLowerCase()];
|
35 |
|
36 | if (!overrideables) {
|
37 | declarations[prop.toLowerCase()] = prop;
|
38 |
|
39 | return;
|
40 | }
|
41 |
|
42 | overrideables.forEach((longhandProp) => {
|
43 | if (!Object.prototype.hasOwnProperty.call(declarations, prefix + longhandProp)) {
|
44 | return;
|
45 | }
|
46 |
|
47 | report({
|
48 | ruleName,
|
49 | result,
|
50 | node: decl,
|
51 | message: messages.rejected(prop, declarations[prefix + longhandProp]),
|
52 | });
|
53 | });
|
54 | });
|
55 | });
|
56 | };
|
57 | }
|
58 |
|
59 | rule.ruleName = ruleName;
|
60 | rule.messages = messages;
|
61 | module.exports = rule;
|