Skip to content

Commit 9e90b2b

Browse files
author
Sebastian Prein
committed
Explain why we changed the logic of the walk function
1 parent 16e96c7 commit 9e90b2b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ module.exports = postcss.plugin('postcss-replace', (opts = defaults) => {
3737
}];
3838

3939
css[options.commentsOnly ? 'walkComments' : 'walk']((node) => {
40+
41+
// Before we had the switch statement, we just used node.replaceValues(). This could potentially lead to
42+
// incorrect behaviour as described in https://github.com/gridonic/postcss-replace/issues/5.
43+
//
44+
// So for example if the CSS contains at-rules like @media, calling replaceValues() would replace
45+
// everything inside the @media { … } statement and since we are walking through *all* nodes, we would
46+
// encounter the nodes from the @media statement again in the next iteration/call of our walk function.
47+
//
48+
// This is why we have refactored the logic of the walk function to use a switch statement in order to do
49+
// the replacement only on the relevant nodes and use the appropriate replacement logic.
50+
//
51+
// Furthermore it also makes adding/handling new cases quite comfortable.
52+
//
53+
// @see http://api.postcss.org/
4054
switch (node.constructor.name) {
4155
case 'Comment':
4256
node.text = node.text.replace(...replacementArgs);

0 commit comments

Comments
 (0)