-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (189 loc) · 6.14 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var postcss = require('postcss');
var Tokenizer = require('css-selector-tokenizer');
function normalizeNodeArray(nodes) {
var array = [];
nodes.forEach(function(x) {
if(Array.isArray(x)) {
normalizeNodeArray(x).forEach(function(item) {
array.push(item);
});
} else if(x) {
array.push(x);
}
});
if(array.length > 0 && array[array.length - 1].type === "spacing") {
array.pop();
}
return array;
}
function localizeNode(node, context) {
if(context.ignoreNextSpacing && node.type !== "spacing") {
throw new Error("Missing whitespace after :" + context.ignoreNextSpacing);
}
if(context.enforceNoSpacing && node.type === "spacing") {
throw new Error("Missing whitespace before :" + context.enforceNoSpacing);
}
var newNodes;
switch(node.type) {
case "selectors":
var resultingGlobal;
context.hasPureGlobals = false;
context.hasPureImplicitGlobals = false;
newNodes = node.nodes.map(function(n) {
var nContext = {
global: context.global,
lastWasSpacing: true,
hasLocals: false,
hasImplicitGlobals: false,
explicit: false
};
n = localizeNode(n, nContext);
if(typeof resultingGlobal === "undefined") {
resultingGlobal = nContext.global;
} else if(resultingGlobal !== nContext.global) {
throw new Error("Inconsistent rule global/local result in rule '" +
Tokenizer.stringify(node) + "' (multiple selectors must result in the same mode for the rule)");
}
if(!nContext.hasLocals) {
context.hasPureGlobals = true;
if(nContext.hasImplicitGlobals) {
context.hasPureImplicitGlobals = true;
}
}
return n;
});
context.global = resultingGlobal;
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
break;
case "selector":
newNodes = node.nodes.map(function(n) {
return localizeNode(n, context);
});
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
break;
case "spacing":
if(context.ignoreNextSpacing) {
context.ignoreNextSpacing = false;
context.lastWasSpacing = false;
context.enforceNoSpacing = false;
return null;
}
context.lastWasSpacing = true;
return node;
case "pseudo-class":
if(node.name === "local" || node.name === "global") {
if(context.inside) {
throw new Error("A :" + node.name + " is not allowed inside of a :" + context.inside + "(...)");
}
context.ignoreNextSpacing = context.lastWasSpacing ? node.name : false;
context.enforceNoSpacing = context.lastWasSpacing ? false : node.name;
context.global = (node.name === "global");
context.explicit = true;
return null;
}
break;
case "nested-pseudo-class":
var subContext;
if(node.name === "local" || node.name === "global") {
if(context.inside) {
throw new Error("A :" + node.name + "(...) is not allowed inside of a :" + context.inside + "(...)");
}
subContext = {
global: (node.name === "global"),
inside: node.name,
hasLocals: false,
hasImplicitGlobals: false,
explicit: true
};
node = node.nodes.map(function(n) {
return localizeNode(n, subContext);
});
// don't leak spacing
node[0].before = undefined;
node[node.length - 1].after = undefined;
} else {
subContext = {
global: context.global,
inside: context.inside,
lastWasSpacing: true,
hasLocals: false,
hasImplicitGlobals: false,
explicit: context.explicit
};
newNodes = node.nodes.map(function(n) {
return localizeNode(n, subContext);
});
node = Object.create(node);
node.nodes = normalizeNodeArray(newNodes);
}
if(subContext.hasLocals) {
context.hasLocals = true;
}
if(subContext.hasImplicitGlobals) {
context.hasImplicitGlobals = true;
}
break;
case "attribute":
case "element":
if(!context.global && !context.explicit) {
context.hasImplicitGlobals = true;
}
break;
case "id":
case "class":
if(!context.global) {
node = {
type: "nested-pseudo-class",
name: "local",
nodes: [node]
};
context.hasLocals = true;
}
break;
}
// reset context
context.lastWasSpacing = false;
context.ignoreNextSpacing = false;
context.enforceNoSpacing = false;
return node;
}
module.exports = postcss.plugin('postcss-modules-lint', function (options) {
if(options && options.mode) {
if(options.mode !== "global" && options.mode !== "local" && options.mode !== "pure") {
throw new Error("options.mode must be either 'global', 'local' or 'pure' (default 'local')");
}
}
var pureMode = options && options.mode === "pure";
var globalMode = options && options.mode === "global";
return function(css) {
css.eachAtRule(function(atrule) {
if(/keyframes$/.test(atrule.name)) {
var globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(atrule.params);
if(globalMatch) {
if(pureMode) {
throw new Error("@keyframes :global(...) is not allowed in pure mode");
}
}
}
});
css.eachRule(function(rule) {
var selector = Tokenizer.parse(rule.selector);
var context = {
global: globalMode,
hasPureGlobals: false,
hasPureImplicitGlobals: false
};
localizeNode(selector, context);
if(pureMode && context.hasPureGlobals) {
throw new Error("Selector '" + Tokenizer.stringify(selector) + "' is not pure " +
"(pure selectors must contain at least one local class or id)");
}
if(!globalMode && context.hasPureImplicitGlobals) {
throw new Error("Selector '" + Tokenizer.stringify(selector) + "' must be explicitly flagged with :global " +
"(elsewise it would leak globally)");
}
});
};
});