From 59cf140b157e37a95ac4ff554375f868054b2687 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 3 Apr 2024 13:30:26 +0300 Subject: [PATCH] fix: export @scope root and limit locals --- src/index.js | 34 ++++++++++++---------- test/test-cases/at-rule-scope/expected.css | 9 ++++++ test/test-cases/at-rule-scope/source.css | 6 ++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index 6746669..380871a 100644 --- a/src/index.js +++ b/src/index.js @@ -177,7 +177,7 @@ const plugin = (options = {}) => { ); } - function traverseNode(node, needExport = true) { + function traverseNode(node) { switch (node.type) { case "pseudo": if (node.value === ":local") { @@ -185,7 +185,7 @@ const plugin = (options = {}) => { 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; @@ -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; @@ -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 diff --git a/test/test-cases/at-rule-scope/expected.css b/test/test-cases/at-rule-scope/expected.css index 9fafefc..e1c64e8 100644 --- a/test/test-cases/at-rule-scope/expected.css +++ b/test/test-cases/at-rule-scope/expected.css @@ -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; } diff --git a/test/test-cases/at-rule-scope/source.css b/test/test-cases/at-rule-scope/source.css index 03327ff..7b74c9d 100644 --- a/test/test-cases/at-rule-scope/source.css +++ b/test/test-cases/at-rule-scope/source.css @@ -26,3 +26,9 @@ backdrop-filter: blur(2px); } } + +@scope { + :scope { + color: red; + } +}