Skip to content

Commit

Permalink
fix: css nesting and pure mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 11, 2024
1 parent b6fbcbc commit 036a069
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,18 @@ const isPureSelector = (context, rule) => {
return !context.hasPureGlobals || isPureSelector(context, rule.parent);
};

const isNodeWithoutDeclarations = (rule) => {
if (rule.nodes.length > 0) {
return !rule.nodes.every(
(item) =>
item.type === "rule" ||
(item.type === "atrule" && !isNodeWithoutDeclarations(item))
);
}

return true;
};

module.exports = (options = {}) => {
if (
options &&
Expand Down Expand Up @@ -670,12 +682,8 @@ module.exports = (options = {}) => {

if (
isNotPure &&
!ignoreComment &&
(rule.nodes.length > 0
? !rule.nodes.every(
(item) => item.type === "rule" || item.type === "atrule"
)
: true)
isNodeWithoutDeclarations(rule) &&
!ignoreComment
) {
throw rule.error(
'Selector "' +
Expand Down
37 changes: 36 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,42 @@ const tests = [
{
name: "css nesting - throw #5",
input:
"html { button { div { div { div { @media screen and (min-width: 900px) { color: red } } } } } }",
"html { div { @media screen and (min-width: 900px) { color: red } } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "css nesting - throw #6",
input:
"html { div { @media screen and (min-width: 900px) { @media screen and (min-width: 900px) { color: red } } } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "css nesting - throw #7",
input:
"html { div { @media screen and (min-width: 900px) { .a { } @media screen and (min-width: 900px) { color: red } } } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "css nesting - throw #7",
input:
"html { div { @media screen and (min-width: 900px) { .a { a_value: some-value; } @media screen and (min-width: 900px) { color: red } } } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "css nesting - throw #8",
input: `
@media screen and (min-width: 900px) {
.a { a_value: some-value; }
@media screen and (min-width: 900px) {
div {
color: red
}
}
}`,
options: { mode: "pure" },
error: /is not pure/,
},
Expand Down

0 comments on commit 036a069

Please sign in to comment.