Skip to content

Commit b3b3fb0

Browse files
Changed match strategy for variables names, to make better use of balanced npm library
1 parent 5a75deb commit b3b3fb0

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

lib/resolve-value.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,34 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
3434
var resultantValue = toString(decl.value);
3535
var warnings = [];
3636

37-
3837
// Match all variables first so we can later on if there are circular dependencies
3938
var variablesUsedInValue = [];
40-
while ((matchingVarDecl = RE_VAR_FUNC_G.exec(resultantValue))) {
41-
variablesUsedInValue.push(matchingVarDecl[1]);
39+
// Create a temporary variable, storing resultantValue variable value
40+
var resultantValueTemp = resultantValue;
41+
// Use balanced lib to find var() declarations and store variable names
42+
while ((matchingVarDecl = balanced('var(', ')', resultantValueTemp))) {
43+
// Split at the comma to find variable name and fallback value
44+
// There may be other commas in the values so this isn't necessarily just 2 pieces
45+
var variableFallbackSplitPieces = matchingVarDecl.body.split(',');
46+
47+
// Get variable name and fallback, filtering empty items
48+
var variableName = variableFallbackSplitPieces[0].trim();
49+
50+
// Push found variable to variables used array
51+
variablesUsedInValue.push(variableName);
52+
53+
// Replace variable name (first occurence only) from result, to avoid circular loop
54+
resultantValueTemp = (matchingVarDecl.pre || '') + matchingVarDecl.body.replace(variableName, '') + (matchingVarDecl.post || '');
4255
}
56+
// clear temporary variable
57+
resultantValueTemp = undefined;
58+
59+
// Old strategy to find used variable names in declaration value, using RegExp
60+
// TODO: remove unused block
61+
// while ((matchingVarDecl = RE_VAR_FUNC_G.exec(resultantValue))) {
62+
// variablesUsedInValue.push(matchingVarDecl[1]);
63+
// }
64+
4365
// Remove duplicates from array
4466
variablesUsedInValue = variablesUsedInValue.filter(filterDistinct);
4567

0 commit comments

Comments
 (0)