Skip to content

Commit 495873e

Browse files
committed
Remove pure mode in favour of linter
1 parent c1bcce0 commit 495873e

File tree

2 files changed

+14
-83
lines changed

2 files changed

+14
-83
lines changed

src/index.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ function localizeNode(node, context) {
116116
return node;
117117
}
118118

119-
const localizeSelectors = (selectors, context) => {
119+
const localizeSelectors = (selectors, mode) => {
120120
const node = Tokenizer.parse(selectors);
121121
var resultingGlobal;
122-
context.hasPureGlobals = false;
123122
node.nodes = node.nodes.map(n => {
124123
var nContext = {
125-
global: context.global,
124+
global: mode === "global",
126125
lastWasSpacing: true,
127126
hasLocals: false,
128127
explicit: false
@@ -136,46 +135,26 @@ const localizeSelectors = (selectors, context) => {
136135
` (multiple selectors must result in the same mode for the rule)`
137136
);
138137
}
139-
if (!nContext.hasLocals) {
140-
context.hasPureGlobals = true;
141-
}
142138
return n;
143139
});
144-
context.global = resultingGlobal;
145140
return Tokenizer.stringify(node);
146141
};
147142

148-
module.exports = postcss.plugin(plugin, (options = {}) => css => {
149-
if (
150-
options.mode &&
151-
options.mode !== "global" &&
152-
options.mode !== "local" &&
153-
options.mode !== "pure"
154-
) {
155-
throw Error(
156-
'options.mode must be either "global", "local" or "pure" (default "local")'
157-
);
158-
}
159-
var pureMode = options.mode === "pure";
160-
var globalMode = options.mode === "global";
143+
const walkRules = (css, callback) => {
161144
css.walkRules(rule => {
162-
if (rule.parent.type === "atrule" && /keyframes$/.test(rule.parent.name)) {
163-
// ignore keyframe rules
164-
return;
145+
if (rule.parent.type !== "atrule" || !/keyframes$/.test(rule.parent.name)) {
146+
callback(rule);
165147
}
166-
var context = {
167-
options: options,
168-
global: globalMode,
169-
hasPureGlobals: false
170-
};
148+
});
149+
};
150+
151+
module.exports = postcss.plugin(plugin, (options = {}) => css => {
152+
walkRules(css, rule => {
171153
try {
172-
const newSelector = localizeSelectors(rule.selector, context);
173-
if (pureMode && context.hasPureGlobals) {
174-
throw Error(
175-
`Selector "${rule.selector}" is not pure (pure selectors must contain at least one local class or id)`
176-
);
177-
}
178-
rule.selector = newSelector;
154+
rule.selector = localizeSelectors(
155+
rule.selector,
156+
options.mode === "global" ? "global" : "local"
157+
);
179158
} catch (e) {
180159
throw rule.error(e.message);
181160
}

test/test.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,6 @@ test("compile explict global attribute", () => {
299299
});
300300
});
301301

302-
test("throw on invalid mode", () => {
303-
return runError({
304-
fixture: "",
305-
options: { mode: "???" },
306-
error: /"global", "local" or "pure"/
307-
});
308-
});
309-
310302
test("throw on inconsistent selector result", () => {
311303
return runError({
312304
fixture: ":global .foo, .bar {}",
@@ -356,46 +348,6 @@ test("throw on incorrect spacing with broad :local", () => {
356348
});
357349
});
358350

359-
test("throw on not pure selector (global class)", () => {
360-
return runError({
361-
fixture: ":global(.foo) {}",
362-
options: { mode: "pure" },
363-
error: /":global\(\.foo\)" is not pure/
364-
});
365-
});
366-
367-
test("throw on not pure selector (with multiple 1)", () => {
368-
return runError({
369-
fixture: ".foo, :global(.bar) {}",
370-
options: { mode: "pure" },
371-
error: /".foo, :global\(\.bar\)" is not pure/
372-
});
373-
});
374-
375-
test("throw on not pure selector (with multiple 2)", () => {
376-
return runError({
377-
fixture: ":global(.bar), .foo {}",
378-
options: { mode: "pure" },
379-
error: /":global\(\.bar\), .foo" is not pure/
380-
});
381-
});
382-
383-
test("throw on not pure selector (element)", () => {
384-
return runError({
385-
fixture: "input {}",
386-
options: { mode: "pure" },
387-
error: /"input" is not pure/
388-
});
389-
});
390-
391-
test("throw on not pure selector (attribute)", () => {
392-
return runError({
393-
fixture: '[type="radio"] {}',
394-
options: { mode: "pure" },
395-
error: /"\[type="radio"\]" is not pure/
396-
});
397-
});
398-
399351
test("pass through global element", () => {
400352
return runCSS({
401353
fixture: "input {}",

0 commit comments

Comments
 (0)