-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
99 lines (80 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// @ts-nocheck
'use strict';
const _ = require('lodash');
const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const shorthandData = require('../../reference/shorthandData');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const ruleName = 'declaration-block-no-redundant-longhand-properties';
const messages = ruleMessages(ruleName, {
expected: (props) => `Expected shorthand property "${props}"`,
});
function rule(actual, options) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignoreShorthands: [_.isString, _.isRegExp],
},
optional: true,
},
);
if (!validOptions) {
return;
}
const longhandProperties = _.transform(shorthandData, (result, values, key) => {
if (optionsMatches(options, 'ignoreShorthands', key)) {
return;
}
values.forEach((value) => {
(result[value] || (result[value] = [])).push(key);
});
});
eachDeclarationBlock(root, (eachDecl) => {
const longhandDeclarations = {};
eachDecl((decl) => {
const prop = decl.prop.toLowerCase();
const unprefixedProp = vendor.unprefixed(prop);
const prefix = vendor.prefix(prop);
const shorthandProperties = longhandProperties[unprefixedProp];
if (!shorthandProperties) {
return;
}
shorthandProperties.forEach((shorthandProperty) => {
const prefixedShorthandProperty = prefix + shorthandProperty;
if (!longhandDeclarations[prefixedShorthandProperty]) {
longhandDeclarations[prefixedShorthandProperty] = [];
}
longhandDeclarations[prefixedShorthandProperty].push(prop);
const prefixedShorthandData = shorthandData[shorthandProperty].map((item) => {
return prefix + item;
});
if (
!_.isEqual(
prefixedShorthandData.sort(),
longhandDeclarations[prefixedShorthandProperty].sort(),
)
) {
return;
}
report({
ruleName,
result,
node: decl,
message: messages.expected(prefixedShorthandProperty),
});
});
});
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;