Skip to content

Commit 71950b8

Browse files
committed
Fix not checking atrule/media query actually applying for all cases
1 parent e82198b commit 71950b8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
161161

162162
debug(`Should unroll decl? isUnderScope=${isUnderScope}`, usageSelectorBranch.selector.toString(), '|', variableSelectorBranch.selector.toString())
163163

164+
// For general cases, we can put variable usage right-below the variable definition
164165
if(isUnderScope) {
165166
usageDecl.value.replace(new RegExp(RE_VAR_FUNC.source, 'g'), (match, matchedVariableName) => {
166167
if(matchedVariableName === variableName) {
167168
variableDecl.after(usageDecl.clone());
168169
}
169170
});
170171
}
172+
// For at-rules
171173
else {
172174
// If there is a conditional like a atrule/media-query, then we should check whether
173175
// the variable can apply and put our usage within that same context
@@ -183,18 +185,20 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
183185
})
184186
if(hasAtRule) {
185187
const doesVariableApplyToUsage = isSelectorBranchUnderScope(variableSelectorBranch, usageSelectorBranch, { ignoreConditionals: true });
186-
debug(`Should expand usage? doesVariableApplyToUsage=${doesVariableApplyToUsage}`, variableSelectorBranch.selector.toString(), '|', usageSelectorBranch.selector.toString())
188+
debug(`Should expand atrule? doesVariableApplyToUsage=${doesVariableApplyToUsage}`, variableSelectorBranch.selector.toString(), '|', usageSelectorBranch.selector.toString())
187189

188-
// Create a new usage clone with only the usage decl
189-
const newUsageRule = usageDecl.parent.clone();
190-
newUsageRule.removeAll();
191-
newUsageRule.append(usageDecl.clone());
190+
if(doesVariableApplyToUsage) {
191+
// Create a new usage clone with only the usage decl
192+
const newUsageRule = usageDecl.parent.clone();
193+
newUsageRule.removeAll();
194+
newUsageRule.append(usageDecl.clone());
192195

193-
const variableAncestry = cloneParentAncestry(variableDecl.parent.parent);
194-
insertNodeIntoAncestry(variableAncestry, newUsageRule);
196+
const variableAncestry = cloneParentAncestry(variableDecl.parent.parent);
197+
insertNodeIntoAncestry(variableAncestry, newUsageRule);
195198

196-
usageDecl.parent.cloneBefore();
197-
usageDecl.parent.replaceWith(variableAncestry);
199+
usageDecl.parent.cloneBefore();
200+
usageDecl.parent.replaceWith(variableAncestry);
201+
}
198202
}
199203
}
200204

0 commit comments

Comments
 (0)