Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Small changes and code cleanup
  • Loading branch information
juliovedovatto committed Jun 22, 2019
commit ca24d6064e3a77f4750cc9155204c73564073836
24 changes: 8 additions & 16 deletions lib/resolve-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,32 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
var debugIndent = _debugIsInternal ? '\t' : '';

var matchingVarDecl = undefined;
var RE_VAR_FUNC_G = new RegExp(RE_VAR_FUNC.source, 'g');
var resultantValue = toString(decl.value);
var warnings = [];

// Match all variables first so we can later on if there are circular dependencies
var variablesUsedInValue = [];
var variablesUsedInValueMap = {}
// Create a temporary variable, storing resultantValue variable value
var resultantValueTemp = resultantValue;
var remainingVariableValue = resultantValue;
// Use balanced lib to find var() declarations and store variable names
while ((matchingVarDecl = balanced('var(', ')', resultantValueTemp))) {
while ((matchingVarDecl = balanced('var(', ')', remainingVariableValue))) {
// Split at the comma to find variable name and fallback value
// There may be other commas in the values so this isn't necessarily just 2 pieces
var variableFallbackSplitPieces = matchingVarDecl.body.split(',');

// Get variable name and fallback, filtering empty items
var variableName = variableFallbackSplitPieces[0].trim();

// Push found variable to variables used array
variablesUsedInValue.push(variableName);
// add variable found in the object
variablesUsedInValueMap[variableName] = true;

// Replace variable name (first occurence only) from result, to avoid circular loop
resultantValueTemp = (matchingVarDecl.pre || '') + matchingVarDecl.body.replace(variableName, '') + (matchingVarDecl.post || '');
remainingVariableValue = (matchingVarDecl.pre || '') + matchingVarDecl.body.replace(variableName, '') + (matchingVarDecl.post || '');
}
// clear temporary variable
resultantValueTemp = undefined;
remainingVariableValue = undefined;

// Old strategy to find used variable names in declaration value, using RegExp
// TODO: remove unused block
// while ((matchingVarDecl = RE_VAR_FUNC_G.exec(resultantValue))) {
// variablesUsedInValue.push(matchingVarDecl[1]);
// }

// Remove duplicates from array
variablesUsedInValue = variablesUsedInValue.filter(filterDistinct);
var variablesUsedInValue = Object.keys(variablesUsedInValueMap);

//console.log(debugIndent, (_debugIsInternal ? '' : 'Try resolving'), generateScopeList(decl.parent, true), `ignorePseudoScope=${ignorePseudoScope}`, '------------------------');

Expand Down