From 73e1c624313a711394bf35c227841d7eb44d95f9 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Fri, 1 Nov 2024 13:02:01 +0100 Subject: [PATCH] remove comments --- src/index.js | 32 ++++++++++++++++++-------------- test/index.test.js | 37 +++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/index.js b/src/index.js index 899d6ae..4c0d48b 100644 --- a/src/index.js +++ b/src/index.js @@ -8,20 +8,18 @@ const IGNORE_MARKER = "cssmodules-pure-ignore"; const isSpacing = (node) => node.type === "combinator" && node.value === " "; -function hasIgnoreComment(node) { - if (!node.parent) { - return false; - } - const indexInParent = node.parent.index(node); +function getIgnoreComment(node) { + const indexInParent = node.parent ? node.parent.index(node) : -1; for (let i = indexInParent - 1; i >= 0; i--) { const prevNode = node.parent.nodes[i]; if (prevNode.type === "comment") { - return prevNode.text.trimStart().startsWith(IGNORE_MARKER); + if (prevNode.text.trimStart().startsWith(IGNORE_MARKER)) { + return prevNode; + } } else { break; } } - return false; } function normalizeNodeArray(nodes) { @@ -531,6 +529,7 @@ module.exports = (options = {}) => { }); root.walkAtRules((atRule) => { + const ignoreComment = getIgnoreComment(atRule); if (/keyframes$/i.test(atRule.name)) { const globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec( atRule.params @@ -542,7 +541,7 @@ module.exports = (options = {}) => { let globalKeyframes = globalMode; if (globalMatch) { - if (pureMode && !hasIgnoreComment(atRule)) { + if (pureMode && !ignoreComment) { throw atRule.error( "@keyframes :global(...) is not allowed in pure mode" ); @@ -582,11 +581,7 @@ module.exports = (options = {}) => { context.options = options; context.localAliasMap = localAliasMap; - if ( - pureMode && - context.hasPureGlobals && - !hasIgnoreComment(atRule) - ) { + if (pureMode && context.hasPureGlobals && ignoreComment) { throw atRule.error( 'Selector in at-rule"' + selector + @@ -620,9 +615,14 @@ module.exports = (options = {}) => { } }); } + + if (ignoreComment) { + ignoreComment.remove(); + } }); root.walkRules((rule) => { + const ignoreComment = getIgnoreComment(rule); if ( rule.parent && rule.parent.type === "atrule" && @@ -637,7 +637,7 @@ module.exports = (options = {}) => { context.options = options; context.localAliasMap = localAliasMap; - if (pureMode && context.hasPureGlobals && !hasIgnoreComment(rule)) { + if (pureMode && context.hasPureGlobals && !ignoreComment) { throw rule.error( 'Selector "' + rule.selector + @@ -654,6 +654,10 @@ module.exports = (options = {}) => { localizeDeclaration(declaration, context) ); } + + if (ignoreComment) { + ignoreComment.remove(); + } }); }, }; diff --git a/test/index.test.js b/test/index.test.js index 2e9bd68..02daad4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -949,7 +949,15 @@ const tests = [ options: { mode: "pure" }, input: `/* cssmodules-pure-ignore */ :global(.foo) { color: blue; }`, - expected: `/* cssmodules-pure-ignore */ + expected: `.foo { color: blue; }`, + }, + { + name: "should suppress errors for global selectors after ignore comment #2", + options: { mode: "pure" }, + input: `/* cssmodules-pure-ignore */ + /* another comment */ + :global(.foo) { color: blue; }`, + expected: `/* another comment */ .foo { color: blue; }`, }, { @@ -957,8 +965,7 @@ const tests = [ options: { mode: "pure" }, input: `/* cssmodules-pure-ignore - needed for third party integration */ :global(#foo) { color: blue; }`, - expected: `/* cssmodules-pure-ignore - needed for third party integration */ - #foo { color: blue; }`, + expected: `#foo { color: blue; }`, }, { name: "should not affect rules after the ignored block", @@ -985,9 +992,7 @@ const tests = [ /* cssmodules-pure-ignore */ :global(.bar) { color: blue; } }`, - expected: `/* cssmodules-pure-ignore */ - .foo { - /* cssmodules-pure-ignore */ + expected: `.foo { .bar { color: blue; } }`, }, @@ -998,8 +1003,7 @@ const tests = [ ::view-transition-group(modal) { animation-duration: 300ms; }`, - expected: `/* cssmodules-pure-ignore */ - ::view-transition-group(modal) { + expected: `::view-transition-group(modal) { animation-duration: 300ms; }`, }, @@ -1011,8 +1015,7 @@ const tests = [ from { opacity: 1; } to { opacity: 0; } }`, - expected: `/* cssmodules-pure-ignore */ - @keyframes fadeOut { + expected: `@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }`, @@ -1025,7 +1028,6 @@ const tests = [ :global(.foo) { color: blue; } }`, expected: `@media (min-width: 768px) { - /* cssmodules-pure-ignore */ .foo { color: blue; } }`, }, @@ -1037,10 +1039,8 @@ const tests = [ .local { color: green; } /* cssmodules-pure-ignore */ :global(.bar) { color: red; }`, - expected: `/* cssmodules-pure-ignore */ - .foo { color: blue; } + expected: `.foo { color: blue; } :local(.local) { color: green; } - /* cssmodules-pure-ignore */ .bar { color: red; }`, }, { @@ -1050,8 +1050,7 @@ const tests = [ :global(.foo):hover > :global(.bar) + :global(.baz) { color: blue; }`, - expected: `/* cssmodules-pure-ignore */ - .foo:hover > .bar + .baz { + expected: `.foo:hover > .bar + .baz { color: blue; }`, }, @@ -1064,8 +1063,7 @@ const tests = [ :global(.baz) { color: blue; }`, - expected: `/* cssmodules-pure-ignore */ - .foo, + expected: `.foo, .bar, .baz { color: blue; @@ -1079,8 +1077,7 @@ const tests = [ :global(.foo)::after { content: ''; }`, - expected: `/* cssmodules-pure-ignore */ - .foo::before, + expected: `.foo::before, .foo::after { content: ''; }`,