Skip to content

Commit d3cd217

Browse files
committed
remove comments
1 parent c812ff4 commit d3cd217

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/index.js

Lines changed: 18 additions & 14 deletions
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) {
@@ -521,6 +519,7 @@ module.exports = (options = {}) => {
521519
});
522520

523521
root.walkAtRules((atRule) => {
522+
const ignoreComment = getIgnoreComment(atRule);
524523
if (/keyframes$/i.test(atRule.name)) {
525524
const globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(
526525
atRule.params
@@ -532,7 +531,7 @@ module.exports = (options = {}) => {
532531
let globalKeyframes = globalMode;
533532

534533
if (globalMatch) {
535-
if (pureMode && !hasIgnoreComment(atRule)) {
534+
if (pureMode && !ignoreComment) {
536535
throw atRule.error(
537536
"@keyframes :global(...) is not allowed in pure mode"
538537
);
@@ -572,11 +571,7 @@ module.exports = (options = {}) => {
572571
context.options = options;
573572
context.localAliasMap = localAliasMap;
574573

575-
if (
576-
pureMode &&
577-
context.hasPureGlobals &&
578-
!hasIgnoreComment(atRule)
579-
) {
574+
if (pureMode && context.hasPureGlobals && ignoreComment) {
580575
throw atRule.error(
581576
'Selector in at-rule"' +
582577
selector +
@@ -610,9 +605,14 @@ module.exports = (options = {}) => {
610605
}
611606
});
612607
}
608+
609+
if (ignoreComment) {
610+
ignoreComment.remove();
611+
}
613612
});
614613

615614
root.walkRules((rule) => {
615+
const ignoreComment = getIgnoreComment(rule);
616616
if (
617617
rule.parent &&
618618
rule.parent.type === "atrule" &&
@@ -627,7 +627,7 @@ module.exports = (options = {}) => {
627627
context.options = options;
628628
context.localAliasMap = localAliasMap;
629629

630-
if (pureMode && context.hasPureGlobals && !hasIgnoreComment(rule)) {
630+
if (pureMode && context.hasPureGlobals && !ignoreComment) {
631631
throw rule.error(
632632
'Selector "' +
633633
rule.selector +
@@ -644,6 +644,10 @@ module.exports = (options = {}) => {
644644
localizeDeclaration(declaration, context)
645645
);
646646
}
647+
648+
if (ignoreComment) {
649+
ignoreComment.remove();
650+
}
647651
});
648652
},
649653
};

test/index.test.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -881,16 +881,23 @@ const tests = [
881881
options: { mode: "pure" },
882882
input: `/* cssmodules-pure-ignore */
883883
:global(.foo) { color: blue; }`,
884-
expected: `/* cssmodules-pure-ignore */
884+
expected: `.foo { color: blue; }`,
885+
},
886+
{
887+
name: "should suppress errors for global selectors after ignore comment #2",
888+
options: { mode: "pure" },
889+
input: `/* cssmodules-pure-ignore */
890+
/* another comment */
891+
:global(.foo) { color: blue; }`,
892+
expected: `/* another comment */
885893
.foo { color: blue; }`,
886894
},
887895
{
888896
name: "should allow additional text in ignore comment",
889897
options: { mode: "pure" },
890898
input: `/* cssmodules-pure-ignore - needed for third party integration */
891899
:global(#foo) { color: blue; }`,
892-
expected: `/* cssmodules-pure-ignore - needed for third party integration */
893-
#foo { color: blue; }`,
900+
expected: `#foo { color: blue; }`,
894901
},
895902
{
896903
name: "should not affect rules after the ignored block",
@@ -917,9 +924,7 @@ const tests = [
917924
/* cssmodules-pure-ignore */
918925
:global(.bar) { color: blue; }
919926
}`,
920-
expected: `/* cssmodules-pure-ignore */
921-
.foo {
922-
/* cssmodules-pure-ignore */
927+
expected: `.foo {
923928
.bar { color: blue; }
924929
}`,
925930
},
@@ -930,8 +935,7 @@ const tests = [
930935
::view-transition-group(modal) {
931936
animation-duration: 300ms;
932937
}`,
933-
expected: `/* cssmodules-pure-ignore */
934-
::view-transition-group(modal) {
938+
expected: `::view-transition-group(modal) {
935939
animation-duration: 300ms;
936940
}`,
937941
},
@@ -943,8 +947,7 @@ const tests = [
943947
from { opacity: 1; }
944948
to { opacity: 0; }
945949
}`,
946-
expected: `/* cssmodules-pure-ignore */
947-
@keyframes fadeOut {
950+
expected: `@keyframes fadeOut {
948951
from { opacity: 1; }
949952
to { opacity: 0; }
950953
}`,
@@ -957,7 +960,6 @@ const tests = [
957960
:global(.foo) { color: blue; }
958961
}`,
959962
expected: `@media (min-width: 768px) {
960-
/* cssmodules-pure-ignore */
961963
.foo { color: blue; }
962964
}`,
963965
},
@@ -969,10 +971,8 @@ const tests = [
969971
.local { color: green; }
970972
/* cssmodules-pure-ignore */
971973
:global(.bar) { color: red; }`,
972-
expected: `/* cssmodules-pure-ignore */
973-
.foo { color: blue; }
974+
expected: `.foo { color: blue; }
974975
:local(.local) { color: green; }
975-
/* cssmodules-pure-ignore */
976976
.bar { color: red; }`,
977977
},
978978
{
@@ -982,8 +982,7 @@ const tests = [
982982
:global(.foo):hover > :global(.bar) + :global(.baz) {
983983
color: blue;
984984
}`,
985-
expected: `/* cssmodules-pure-ignore */
986-
.foo:hover > .bar + .baz {
985+
expected: `.foo:hover > .bar + .baz {
987986
color: blue;
988987
}`,
989988
},
@@ -996,8 +995,7 @@ const tests = [
996995
:global(.baz) {
997996
color: blue;
998997
}`,
999-
expected: `/* cssmodules-pure-ignore */
1000-
.foo,
998+
expected: `.foo,
1001999
.bar,
10021000
.baz {
10031001
color: blue;
@@ -1011,8 +1009,7 @@ const tests = [
10111009
:global(.foo)::after {
10121010
content: '';
10131011
}`,
1014-
expected: `/* cssmodules-pure-ignore */
1015-
.foo::before,
1012+
expected: `.foo::before,
10161013
.foo::after {
10171014
content: '';
10181015
}`,

0 commit comments

Comments
 (0)