/
index.js
121 lines (93 loc) · 3.19 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { utils } from "stylelint";
import { moduleNamespace, namespace, ruleUrl } from "../../utils";
export const ruleName = namespace("at-each-key-value-single-line");
export const messages = utils.ruleMessages(ruleName, {
expected:
"Use @each $key, $value in $map syntax instead of $value: map-get($map, $key)"
});
export const meta = {
url: ruleUrl(ruleName)
};
export default function rule(primary) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
actual: primary
});
if (!validOptions) {
return;
}
const mapNamespace = moduleNamespace(root, "sass:map");
root.walkAtRules("each", rule => {
const parts = separateEachParams(rule.params);
// If loop is fetching both key + value, return
if (parts[0].length === 2) {
return;
}
// If didn't call map-keys, return.
if (!didCallMapKeys(parts[1], mapNamespace)) {
return;
}
// Loop over decls inside of each statement and loop for variable assignments.
rule.walkDecls(innerDecl => {
// Check that this decl is a map-get call
if (innerDecl.prop[0] !== "$") {
return;
}
if (!didCallMapGet(innerDecl.value, mapNamespace)) {
return;
}
// Check map_name + key_name match.
const map_get_parts = mapGetParameters(innerDecl.value, mapNamespace);
// Check map names match.
if (map_get_parts[0] !== mapName(parts[1], mapNamespace)) {
return;
}
// Match key names match.
if (map_get_parts[1] !== parts[0][0]) {
return;
}
utils.report({
message: messages.expected,
node: rule,
result,
ruleName
});
});
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
// Takes in a param string from node.params
// Returns: [[key variable, value variable], map_decl] (all Strings)
function separateEachParams(paramString) {
const parts = paramString.split("in");
return [parts[0].split(",").map(s => s.trim()), parts[1].trim()];
}
function didCallMapKeys(mapDecl, mapNamespace) {
const pattern = getNamespacedPattern("keys\\(.*\\)", mapNamespace);
return new RegExp(pattern).test(mapDecl);
}
function didCallMapGet(mapDecl, mapNamespace) {
const pattern = getNamespacedPattern("get\\((.*),(.*)\\)", mapNamespace);
return new RegExp(pattern).test(mapDecl);
}
// Fetch the name of the map from a map-keys() or map.keys() call.
function mapName(mapDecl, mapNamespace) {
if (didCallMapKeys(mapDecl, mapNamespace)) {
const pattern = getNamespacedPattern("keys\\((.*)\\)", mapNamespace);
return mapDecl.match(new RegExp(pattern))[1];
}
return mapDecl;
}
// Returns the parameters of a map-get or map.get call
// Returns [map variable, key_variable]
function mapGetParameters(mapGetDecl, mapNamespace) {
const pattern = getNamespacedPattern("get\\((.*), ?(.*)\\)", mapNamespace);
const parts = mapGetDecl.match(new RegExp(pattern));
return [parts[1], parts[2]];
}
function getNamespacedPattern(pattern, namespace) {
return namespace !== null ? `(?:${namespace}\\.|map-)${pattern}` : pattern;
}