Skip to content

Commit 036a069

Browse files
fix: css nesting and pure mode
1 parent b6fbcbc commit 036a069

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,18 @@ const isPureSelector = (context, rule) => {
516516
return !context.hasPureGlobals || isPureSelector(context, rule.parent);
517517
};
518518

519+
const isNodeWithoutDeclarations = (rule) => {
520+
if (rule.nodes.length > 0) {
521+
return !rule.nodes.every(
522+
(item) =>
523+
item.type === "rule" ||
524+
(item.type === "atrule" && !isNodeWithoutDeclarations(item))
525+
);
526+
}
527+
528+
return true;
529+
};
530+
519531
module.exports = (options = {}) => {
520532
if (
521533
options &&
@@ -670,12 +682,8 @@ module.exports = (options = {}) => {
670682

671683
if (
672684
isNotPure &&
673-
!ignoreComment &&
674-
(rule.nodes.length > 0
675-
? !rule.nodes.every(
676-
(item) => item.type === "rule" || item.type === "atrule"
677-
)
678-
: true)
685+
isNodeWithoutDeclarations(rule) &&
686+
!ignoreComment
679687
) {
680688
throw rule.error(
681689
'Selector "' +

test/index.test.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,42 @@ const tests = [
15741574
{
15751575
name: "css nesting - throw #5",
15761576
input:
1577-
"html { button { div { div { div { @media screen and (min-width: 900px) { color: red } } } } } }",
1577+
"html { div { @media screen and (min-width: 900px) { color: red } } }",
1578+
options: { mode: "pure" },
1579+
error: /is not pure/,
1580+
},
1581+
{
1582+
name: "css nesting - throw #6",
1583+
input:
1584+
"html { div { @media screen and (min-width: 900px) { @media screen and (min-width: 900px) { color: red } } } }",
1585+
options: { mode: "pure" },
1586+
error: /is not pure/,
1587+
},
1588+
{
1589+
name: "css nesting - throw #7",
1590+
input:
1591+
"html { div { @media screen and (min-width: 900px) { .a { } @media screen and (min-width: 900px) { color: red } } } }",
1592+
options: { mode: "pure" },
1593+
error: /is not pure/,
1594+
},
1595+
{
1596+
name: "css nesting - throw #7",
1597+
input:
1598+
"html { div { @media screen and (min-width: 900px) { .a { a_value: some-value; } @media screen and (min-width: 900px) { color: red } } } }",
1599+
options: { mode: "pure" },
1600+
error: /is not pure/,
1601+
},
1602+
{
1603+
name: "css nesting - throw #8",
1604+
input: `
1605+
@media screen and (min-width: 900px) {
1606+
.a { a_value: some-value; }
1607+
@media screen and (min-width: 900px) {
1608+
div {
1609+
color: red
1610+
}
1611+
}
1612+
}`,
15781613
options: { mode: "pure" },
15791614
error: /is not pure/,
15801615
},

0 commit comments

Comments
 (0)