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 authored Nov 11, 2024
1 parent 582bd9e commit 3811927
Show file tree
Hide file tree
Showing 2 changed files with 398 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function normalizeNodeArray(nodes) {
return array;
}

const isPureSelectorSymbol = Symbol("is-pure-selector");

function localizeNode(rule, mode, localAliasMap) {
const transform = (node, context) => {
if (context.ignoreNextSpacing && !isSpacing(node)) {
Expand Down Expand Up @@ -253,7 +255,7 @@ function localizeNode(rule, mode, localAliasMap) {
}
case "nesting": {
if (node.value === "&") {
context.hasLocals = true;
context.hasLocals = rule.parent[isPureSelectorSymbol];
}
}
}
Expand Down Expand Up @@ -502,6 +504,30 @@ function localizeDeclaration(declaration, context) {
}
}

const isPureSelector = (context, rule) => {
if (!rule.parent || rule.type === "root") {
return !context.hasPureGlobals;
}

if (rule.type === "rule" && rule[isPureSelectorSymbol]) {
return rule[isPureSelectorSymbol] || isPureSelector(context, rule.parent);
}

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 @@ -652,8 +678,13 @@ module.exports = (options = {}) => {
context.localAliasMap = localAliasMap;

const ignoreComment = pureMode ? getIgnoreComment(rule) : undefined;
const isNotPure = pureMode && !isPureSelector(context, rule);

if (pureMode && context.hasPureGlobals && !ignoreComment) {
if (
isNotPure &&
isNodeWithoutDeclarations(rule) &&
!ignoreComment
) {
throw rule.error(
'Selector "' +
rule.selector +
Expand All @@ -664,6 +695,10 @@ module.exports = (options = {}) => {
ignoreComment.remove();
}

if (pureMode) {
rule[isPureSelectorSymbol] = !isNotPure;
}

rule.selector = context.selector;

// Less-syntax mixins parse as rules with no nodes
Expand Down
Loading

0 comments on commit 3811927

Please sign in to comment.