Skip to content

Commit dab4426

Browse files
committed
chore: migrate off of css-selector-tokenizer
1 parent dd5bcf6 commit dab4426

File tree

2 files changed

+533
-553
lines changed

2 files changed

+533
-553
lines changed

index.js

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const postcss = require('postcss');
44
const selectorParser = require('postcss-selector-parser');
55
const valueParser = require('postcss-value-parser');
66

7-
const IS_GLOBAL = Symbol('is global');
7+
const isSpacing = node => node.type === 'combinator' && node.value === ' ';
88

99
function normalizeNodeArray(nodes) {
1010
const array = [];
@@ -25,27 +25,7 @@ function normalizeNodeArray(nodes) {
2525
return array;
2626
}
2727

28-
function checkForInconsistentRule(node, current, context) {
29-
if (context.global !== context.lastIsGlobal)
30-
throw new Error(
31-
'Inconsistent rule global/local result in rule "' +
32-
String(node) +
33-
'" (multiple selectors must result in the same mode for the rule)'
34-
);
35-
}
36-
const isSpacing = node => node.type === 'combinator' && node.value === ' ';
37-
38-
function trimSelectors(selector) {
39-
let last;
40-
while (
41-
(last = selector.last) &&
42-
(last.type === 'combinator' && last.value === ' ')
43-
) {
44-
last.remove();
45-
}
46-
}
47-
48-
function localizeNodez(rule, mode, options) {
28+
function localizeNode(rule, mode, options) {
4929
const isScopePseudo = node =>
5030
node.value === ':local' || node.value === ':global';
5131

@@ -87,27 +67,26 @@ function localizeNodez(rule, mode, options) {
8767
context.global = resultingGlobal;
8868

8969
node.nodes = normalizeNodeArray(newNodes);
90-
// console.log(node.nodes);
9170
break;
9271
}
9372
case 'selector': {
9473
newNodes = node.map(childNode => transform(childNode, context));
9574

9675
node = node.clone();
9776
node.nodes = normalizeNodeArray(newNodes);
98-
console.log('SECLE', node.toString());
9977
break;
10078
}
10179
case 'combinator': {
102-
if (!isSpacing(node)) break;
103-
104-
if (context.ignoreNextSpacing) {
105-
context.ignoreNextSpacing = false;
106-
context.lastWasSpacing = false;
107-
context.enforceNoSpacing = false;
108-
return null;
80+
if (isSpacing(node)) {
81+
if (context.ignoreNextSpacing) {
82+
context.ignoreNextSpacing = false;
83+
context.lastWasSpacing = false;
84+
context.enforceNoSpacing = false;
85+
return null;
86+
}
87+
context.lastWasSpacing = true;
88+
return node;
10989
}
110-
context.lastWasSpacing = true;
11190
break;
11291
}
11392
case 'pseudo': {
@@ -132,7 +111,6 @@ function localizeNodez(rule, mode, options) {
132111
hasLocals: false,
133112
explicit: true,
134113
};
135-
// console.log('PSUDI', node.nodes);
136114

137115
newNodes = node
138116
.map(childNode => transform(childNode, childContext))
@@ -147,12 +125,9 @@ function localizeNodez(rule, mode, options) {
147125
first.spaces = { before, after: first.spaces.after };
148126
last.spaces = { before: last.spaces.before, after };
149127
}
150-
console.log('PSUDI', node);
128+
151129
node = newNodes;
152130

153-
// // don't leak spacing
154-
// node[0].spaces.before = '';
155-
// node[node.length - 1].spaces.after = '';
156131
break;
157132
} else {
158133
childContext = {
@@ -185,36 +160,41 @@ function localizeNodez(rule, mode, options) {
185160
);
186161
}
187162

188-
const next = node.next();
189-
console.log('SPACESS', next, node.spaces);
190-
if (next) next.spaces = node.spaces;
163+
const next = node.parent;
164+
const addBackSpacing = !!node.spaces.before;
191165

192166
context.ignoreNextSpacing = context.lastWasSpacing
193167
? node.value
194168
: false;
169+
195170
context.enforceNoSpacing = context.lastWasSpacing
196171
? false
197172
: node.value;
173+
198174
context.global = node.value === ':global';
199175
context.explicit = true;
200-
return null;
176+
177+
// because this node has spacing that is lost when we remove it
178+
// we make up for it by adding an extra combinator in since adding
179+
// spacing on the parent selector doesn't work
180+
return addBackSpacing
181+
? selectorParser.combinator({ value: ' ' })
182+
: null;
201183
}
202184
break;
203185
}
204186
case 'id':
205187
case 'class': {
206188
if (!context.global) {
207-
console.log('REPLCE', node.spaces);
208189
const innerNode = node.clone();
209-
console.log(innerNode);
210190
innerNode.spaces = { before: '', after: '' };
211191

212192
node = selectorParser.pseudo({
213193
value: ':local',
214194
nodes: [innerNode],
215195
spaces: node.spaces,
216196
});
217-
// console.log('HERE');
197+
218198
context.hasLocals = true;
219199
}
220200

@@ -233,11 +213,11 @@ function localizeNodez(rule, mode, options) {
233213
hasPureGlobals: false,
234214
};
235215

236-
const updatedRule = selectorParser(root => {
216+
const selector = selectorParser(root => {
237217
transform(root, rootContext);
238-
}).processSync(rule, { updateSelector: true, lossless: true });
218+
}).processSync(rule, { updateSelector: false, lossless: true });
239219

240-
// console.log('HERE', rule.selector);
220+
rootContext.selector = selector;
241221
return rootContext;
242222
}
243223

@@ -479,7 +459,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
479459
return;
480460
}
481461

482-
const context = localizeNodez(rule, options.mode, options);
462+
const context = localizeNode(rule, options.mode);
463+
464+
context.options = options;
483465

484466
if (pureMode && context.hasPureGlobals) {
485467
throw rule.error(
@@ -489,12 +471,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
489471
'(pure selectors must contain at least one local class or id)'
490472
);
491473
}
474+
rule.selector = context.selector;
492475
// Less-syntax mixins parse as rules with no nodes
493476
if (rule.nodes) {
494-
rule.nodes.forEach(function(decl) {
495-
console.log(context);
496-
localizeDecl(decl, context);
497-
});
477+
rule.nodes.forEach(decl => localizeDecl(decl, context));
498478
}
499479
});
500480
};

0 commit comments

Comments
 (0)