UNPKG

797 BJavaScriptView Raw
1// tooling
2import getReplacedString from './get-replaced-string';
3import setVariable from './set-variable';
4
5// transform declarations
6export default function transformDecl(decl, result, opts) {
7 // update the declaration value with its variables replaced by their corresponding values
8 decl.value = getReplacedString(decl.value, decl, result, opts);
9
10 // if this is a variable declaration
11 if (isVariableDeclaration(decl)) {
12 // set the variable on the parent node
13 setVariable(decl.parent, decl.prop.slice(1), decl.value, opts);
14
15 // remove the declaration node
16 decl.remove();
17 }
18}
19
20// return whether the declaration property is a variable declaration
21const isVariableDeclaration = decl => matchVariable.test(decl.prop);
22
23// match a variable ($any-name)
24const matchVariable = /^\$[\w-]+$/;