UNPKG

693 BJavaScriptView Raw
1// tooling
2import getClosestVariable from './get-closest-variable';
3
4// set a variable on a node
5export default function setVariable(node, name, value, opts) {
6 // if the value is not a default with a value already defined
7 if (!matchDefault.test(value) || getClosestVariable(name, node, opts) === undefined) {
8 // the value without a default suffix
9 const undefaultedValue = matchDefault.test(value)
10 ? value.replace(matchDefault, '')
11 : value;
12
13 // ensure the node has a variables object
14 node.variables = node.variables || {};
15
16 // set the variable
17 node.variables[name] = undefaultedValue;
18 }
19}
20
21// match anything ending with a valid !default
22const matchDefault = /\s+!default$/;