Skip to content

Commit 91a3d11

Browse files
committed
pure selectors mark all children pure
1 parent 5eeb305 commit 91a3d11

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ module.exports = (options = {}) => {
590590
}
591591
});
592592

593+
const localRules = new Set();
593594
root.walkRules((rule) => {
594595
if (
595596
rule.parent &&
@@ -605,13 +606,23 @@ module.exports = (options = {}) => {
605606
context.options = options;
606607
context.localAliasMap = localAliasMap;
607608

608-
if (pureMode && context.hasPureGlobals) {
609+
if (
610+
pureMode &&
611+
context.hasPureGlobals &&
612+
!localRules.has(rule.parent)
613+
) {
609614
throw rule.error(
610615
'Selector "' +
611616
rule.selector +
612617
'" is not pure ' +
613618
"(pure selectors must contain at least one local class or id)"
614619
);
620+
} else {
621+
// Once a parent is pure all children are also pure
622+
// For example the span inside this .foo is also pure
623+
// although it is not local itself:
624+
// .foo { span { color: red; } }
625+
localRules.add(rule);
615626
}
616627

617628
rule.selector = context.selector;

test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,24 @@ const tests = [
868868
options: { mode: "pure" },
869869
error: /is not pure/,
870870
},
871+
{
872+
name: "consider nonlocal children of a pure parent as pure",
873+
input: ".foo { span { a_value: some-value; } }",
874+
options: { mode: "pure" },
875+
expected: ":local(.foo) { span { a_value: some-value; } }",
876+
},
877+
{
878+
name: "consider nested nonlocal children of a pure parent as pure",
879+
input: ".foo { span { a { a_value: some-value; } } }",
880+
options: { mode: "pure" },
881+
expected: ":local(.foo) { span { a { a_value: some-value; } } }",
882+
},
883+
{
884+
name: "throw on mixed parents",
885+
input: ".foo, html { span { a_value: some-value; } }",
886+
options: { mode: "pure" },
887+
error: /is not pure/,
888+
},
871889
{
872890
name: "css nesting",
873891
input: `

0 commit comments

Comments
 (0)