Skip to content

Commit 16fcd6b

Browse files
committed
remove comments
1 parent 0559b68 commit 16fcd6b

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/index.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ const IGNORE_MARKER = "cssmodules-pure-ignore";
88

99
const isSpacing = (node) => node.type === "combinator" && node.value === " ";
1010

11-
function hasIgnoreComment(node) {
12-
if (!node.parent) {
13-
return false;
14-
}
15-
const indexInParent = node.parent.index(node);
11+
function getIgnoreComment(node) {
12+
const indexInParent = node.parent ? node.parent.index(node) : -1;
1613
for (let i = indexInParent - 1; i >= 0; i--) {
1714
const prevNode = node.parent.nodes[i];
1815
if (prevNode.type === "comment") {
19-
return prevNode.text.trimStart().startsWith(IGNORE_MARKER);
16+
if (prevNode.text.trimStart().startsWith(IGNORE_MARKER)) {
17+
return prevNode;
18+
}
2019
} else {
2120
break;
2221
}
2322
}
24-
return false;
2523
}
2624

2725
function normalizeNodeArray(nodes) {
@@ -531,6 +529,7 @@ module.exports = (options = {}) => {
531529
});
532530

533531
root.walkAtRules((atRule) => {
532+
const ignoreComment = getIgnoreComment(atRule);
534533
if (/keyframes$/i.test(atRule.name)) {
535534
const globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(
536535
atRule.params
@@ -542,7 +541,7 @@ module.exports = (options = {}) => {
542541
let globalKeyframes = globalMode;
543542

544543
if (globalMatch) {
545-
if (pureMode && !hasIgnoreComment(atRule)) {
544+
if (pureMode && !ignoreComment) {
546545
throw atRule.error(
547546
"@keyframes :global(...) is not allowed in pure mode"
548547
);
@@ -582,11 +581,7 @@ module.exports = (options = {}) => {
582581
context.options = options;
583582
context.localAliasMap = localAliasMap;
584583

585-
if (
586-
pureMode &&
587-
context.hasPureGlobals &&
588-
!hasIgnoreComment(atRule)
589-
) {
584+
if (pureMode && context.hasPureGlobals && ignoreComment) {
590585
throw atRule.error(
591586
'Selector in at-rule"' +
592587
selector +
@@ -620,9 +615,14 @@ module.exports = (options = {}) => {
620615
}
621616
});
622617
}
618+
619+
if (ignoreComment) {
620+
ignoreComment.remove();
621+
}
623622
});
624623

625624
root.walkRules((rule) => {
625+
const ignoreComment = getIgnoreComment(rule);
626626
if (
627627
rule.parent &&
628628
rule.parent.type === "atrule" &&
@@ -637,7 +637,7 @@ module.exports = (options = {}) => {
637637
context.options = options;
638638
context.localAliasMap = localAliasMap;
639639

640-
if (pureMode && context.hasPureGlobals && !hasIgnoreComment(rule)) {
640+
if (pureMode && context.hasPureGlobals && !ignoreComment) {
641641
throw rule.error(
642642
'Selector "' +
643643
rule.selector +
@@ -654,6 +654,10 @@ module.exports = (options = {}) => {
654654
localizeDeclaration(declaration, context)
655655
);
656656
}
657+
658+
if (ignoreComment) {
659+
ignoreComment.remove();
660+
}
657661
});
658662
},
659663
};

test/index.test.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -949,16 +949,23 @@ const tests = [
949949
options: { mode: "pure" },
950950
input: `/* cssmodules-pure-ignore */
951951
:global(.foo) { color: blue; }`,
952-
expected: `/* cssmodules-pure-ignore */
952+
expected: `.foo { color: blue; }`,
953+
},
954+
{
955+
name: "should suppress errors for global selectors after ignore comment #2",
956+
options: { mode: "pure" },
957+
input: `/* cssmodules-pure-ignore */
958+
/* another comment */
959+
:global(.foo) { color: blue; }`,
960+
expected: `/* another comment */
953961
.foo { color: blue; }`,
954962
},
955963
{
956964
name: "should allow additional text in ignore comment",
957965
options: { mode: "pure" },
958966
input: `/* cssmodules-pure-ignore - needed for third party integration */
959967
:global(#foo) { color: blue; }`,
960-
expected: `/* cssmodules-pure-ignore - needed for third party integration */
961-
#foo { color: blue; }`,
968+
expected: `#foo { color: blue; }`,
962969
},
963970
{
964971
name: "should not affect rules after the ignored block",
@@ -985,9 +992,7 @@ const tests = [
985992
/* cssmodules-pure-ignore */
986993
:global(.bar) { color: blue; }
987994
}`,
988-
expected: `/* cssmodules-pure-ignore */
989-
.foo {
990-
/* cssmodules-pure-ignore */
995+
expected: `.foo {
991996
.bar { color: blue; }
992997
}`,
993998
},
@@ -998,8 +1003,7 @@ const tests = [
9981003
::view-transition-group(modal) {
9991004
animation-duration: 300ms;
10001005
}`,
1001-
expected: `/* cssmodules-pure-ignore */
1002-
::view-transition-group(modal) {
1006+
expected: `::view-transition-group(modal) {
10031007
animation-duration: 300ms;
10041008
}`,
10051009
},
@@ -1011,8 +1015,7 @@ const tests = [
10111015
from { opacity: 1; }
10121016
to { opacity: 0; }
10131017
}`,
1014-
expected: `/* cssmodules-pure-ignore */
1015-
@keyframes fadeOut {
1018+
expected: `@keyframes fadeOut {
10161019
from { opacity: 1; }
10171020
to { opacity: 0; }
10181021
}`,
@@ -1025,7 +1028,6 @@ const tests = [
10251028
:global(.foo) { color: blue; }
10261029
}`,
10271030
expected: `@media (min-width: 768px) {
1028-
/* cssmodules-pure-ignore */
10291031
.foo { color: blue; }
10301032
}`,
10311033
},
@@ -1037,10 +1039,8 @@ const tests = [
10371039
.local { color: green; }
10381040
/* cssmodules-pure-ignore */
10391041
:global(.bar) { color: red; }`,
1040-
expected: `/* cssmodules-pure-ignore */
1041-
.foo { color: blue; }
1042+
expected: `.foo { color: blue; }
10421043
:local(.local) { color: green; }
1043-
/* cssmodules-pure-ignore */
10441044
.bar { color: red; }`,
10451045
},
10461046
{
@@ -1050,8 +1050,7 @@ const tests = [
10501050
:global(.foo):hover > :global(.bar) + :global(.baz) {
10511051
color: blue;
10521052
}`,
1053-
expected: `/* cssmodules-pure-ignore */
1054-
.foo:hover > .bar + .baz {
1053+
expected: `.foo:hover > .bar + .baz {
10551054
color: blue;
10561055
}`,
10571056
},
@@ -1064,8 +1063,7 @@ const tests = [
10641063
:global(.baz) {
10651064
color: blue;
10661065
}`,
1067-
expected: `/* cssmodules-pure-ignore */
1068-
.foo,
1066+
expected: `.foo,
10691067
.bar,
10701068
.baz {
10711069
color: blue;
@@ -1079,8 +1077,7 @@ const tests = [
10791077
:global(.foo)::after {
10801078
content: '';
10811079
}`,
1082-
expected: `/* cssmodules-pure-ignore */
1083-
.foo::before,
1080+
expected: `.foo::before,
10841081
.foo::after {
10851082
content: '';
10861083
}`,

0 commit comments

Comments
 (0)