Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ const plugin = (options = {}) => {
);
}

function traverseNode(node, needExport = true) {
function traverseNode(node) {
switch (node.type) {
case "pseudo":
if (node.value === ":local") {
if (node.nodes.length !== 1) {
throw new Error('Unexpected comma (",") in :local block');
}

const selector = localizeNode(node.first, needExport);
const selector = localizeNode(node.first);
// move the spaces that were around the pseudo selector to the first
// non-container node
selector.first.spaces = node.spaces;
Expand All @@ -208,12 +208,12 @@ const plugin = (options = {}) => {
/* falls through */
case "root":
case "selector": {
node.each((item) => traverseNode(item, needExport));
node.each((item) => traverseNode(item));
break;
}
case "id":
case "class":
if (needExport && exportGlobals) {
if (exportGlobals) {
exports[node.value] = [node.value];
}
break;
Expand Down Expand Up @@ -317,22 +317,24 @@ const plugin = (options = {}) => {
});

root.walkAtRules(/scope$/i, (atRule) => {
atRule.params = atRule.params
.split("to")
.map((item) => {
const selector = item.trim().slice(1, -1).trim();
if (atRule.params) {
atRule.params = atRule.params
.split("to")
.map((item) => {
const selector = item.trim().slice(1, -1).trim();

const localMatch = /^\s*:local\s*\((.+?)\)\s*$/.exec(selector);
const localMatch = /^\s*:local\s*\((.+?)\)\s*$/.exec(selector);

if (!localMatch) {
return `(${selector})`;
}
if (!localMatch) {
return `(${selector})`;
}

let parsedSelector = selectorParser().astSync(selector);
let parsedSelector = selectorParser().astSync(selector);

return `(${traverseNode(parsedSelector, false).toString()})`;
})
.join(" to ");
return `(${traverseNode(parsedSelector).toString()})`;
})
.join(" to ");
}
});

// If we found any :locals, insert an :export rule
Expand Down
9 changes: 9 additions & 0 deletions test/test-cases/at-rule-scope/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@
}
}

@scope {
:scope {
color: red;
}
}

:export {
d: _input__d;
c: _input__c;
e: _input__e;
f: _input__f;
a: _input__a;
b: _input__b;
g: _input__g;
}
6 changes: 6 additions & 0 deletions test/test-cases/at-rule-scope/source.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@
backdrop-filter: blur(2px);
}
}

@scope {
:scope {
color: red;
}
}