From 378e1d26d496b85c91c10ab945e63e98817916a6 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 13 Apr 2016 17:19:06 -0700 Subject: [PATCH] Simplify: we always remove all the variables. --- tools/globalize.js | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/tools/globalize.js b/tools/globalize.js index d7f99705f..001783a3c 100644 --- a/tools/globalize.js +++ b/tools/globalize.js @@ -20,29 +20,13 @@ var replaced = 0; script = script.replace( // matches declaration at the start of the function, not including helper function assignments - / *\bvar\s+[$\w]+(,\s*[$\w]+)*(,\s*|;\n)/, - - function(declaration) { - var parts = declaration.split(/([$\w]+)(?=[,;])/); - - for (var name of names) { - var i = parts.indexOf(name); - if (i < 0) { - throw new Error(`${filename}: ${name} not found`); - } else if (i !== 1) { - // not first: remove variable and separator before it - parts.splice(i - 1, 2); - } else if (!(i === parts.length - 2 && parts[parts.length - 1][0] === ';')) { - // not last: remove variable and separator after it - parts.splice(i, 2); - } else { - // removing only variable: nuke whole declaration - parts = []; - } - } + /^( *var )(.*)(,\n *|;\n)/m, + function(declaration, v, n, e) { replaced++; - return parts.join(''); + var n0 = names.sort().join(', '); + if (n0 !== n) throw new Error(`${filename}: expected variables (${n0}) found (${n})`); + return (e[0] === ',') ? v : ''; } );