From 1da6abc458cf4af0b4279e9041b368b54c8db175 Mon Sep 17 00:00:00 2001 From: David Majda Date: Tue, 20 Sep 2016 14:47:10 +0200 Subject: [PATCH] Use one var/let/const per variable (fixup) Fix few instances in generated code which I missed in the previous commit. See #443. --- lib/compiler/passes/generate-js.js | 31 +++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index cf35e7f..e5429ee 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -718,20 +718,15 @@ function generateJS(ast, options) { parts.push('function peg$parse' + rule.name + '() {'); + if (options.trace) { + parts.push(' var startPos = peg$currPos;'); + } + for (let i = 0; i <= stack.maxSp; i++) { stackVars[i] = s(i); } - if (options.trace) { - parts.push([ - ' var ' + stackVars.join(', ') + ',', - ' startPos = peg$currPos;' - ].join('\n')); - } else { - parts.push( - ' var ' + stackVars.join(', ') + ';' - ); - } + parts.push(' var ' + stackVars.join(', ') + ';'); parts.push(indent2(generateRuleHeader( '"' + js.stringEscape(rule.name) + '"', @@ -1248,12 +1243,6 @@ function generateJS(ast, options) { commonjs: function() { let parts = []; let dependencyVars = Object.keys(options.dependencies); - let requires = dependencyVars.map( - variable => variable - + ' = require("' - + js.stringEscape(options.dependencies[variable]) - + '")' - ); parts.push([ generateGeneratedByComment(), @@ -1262,8 +1251,14 @@ function generateJS(ast, options) { '' ].join('\n')); - if (requires.length > 0) { - parts.push('var ' + requires.join(', ') + ';'); + if (dependencyVars.length > 0) { + dependencyVars.forEach(variable => { + parts.push('var ' + variable + + ' = require("' + + js.stringEscape(options.dependencies[variable]) + + '");' + ); + }); parts.push(''); }