From d00e9526c3a234ba725334a4b7b7bc04f42f4d17 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 9 Sep 2016 11:43:06 +0200 Subject: [PATCH] Minimize variable scope Where possible, move "let" statements into inner blocks, loop headers, etc. See #442. --- benchmark/runner.js | 12 +++++------- lib/compiler/index.js | 4 +--- lib/compiler/passes/generate-bytecode.js | 14 +++++--------- lib/compiler/passes/generate-js.js | 19 +++++++------------ lib/peg.js | 4 ++-- lib/utils/arrays.js | 17 ++++++++--------- lib/utils/objects.js | 12 +++++------- .../generated-parser-behavior.spec.js | 8 ++++---- spec/unit/compiler/passes/helpers.js | 11 ++++------- spec/unit/parser.spec.js | 4 ++-- 10 files changed, 43 insertions(+), 62 deletions(-) diff --git a/benchmark/runner.js b/benchmark/runner.js index b28f93a..8eb2d9b 100644 --- a/benchmark/runner.js +++ b/benchmark/runner.js @@ -68,19 +68,17 @@ let Runner = { function testRunner(benchmark, test) { return function() { - let input, parseTime, averageParseTime, i, t; - callbacks.testStart(benchmark, test); - input = callbacks.readFile(benchmark.id + "/" + test.file); + let input = callbacks.readFile(benchmark.id + "/" + test.file); - parseTime = 0; - for (i = 0; i < runCount; i++) { - t = (new Date()).getTime(); + let parseTime = 0; + for (let i = 0; i < runCount; i++) { + let t = (new Date()).getTime(); state.parser.parse(input); parseTime += (new Date()).getTime() - t; } - averageParseTime = parseTime / runCount; + let averageParseTime = parseTime / runCount; callbacks.testFinish(benchmark, test, input.length, averageParseTime); diff --git a/lib/compiler/index.js b/lib/compiler/index.js index e4a0d57..0ee44d5 100644 --- a/lib/compiler/index.js +++ b/lib/compiler/index.js @@ -42,8 +42,6 @@ let compiler = { compile: function(ast, passes, options) { options = options !== undefined ? options : {}; - let stage; - options = objects.clone(options); objects.defaults(options, { allowedStartRules: [ast.rules[0].name], @@ -56,7 +54,7 @@ let compiler = { trace: false }); - for (stage in passes) { + for (let stage in passes) { if (passes.hasOwnProperty(stage)) { passes[stage].forEach(function(p) { p(ast, options); }); } diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 75d90c5..c54945c 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -368,10 +368,8 @@ function generateBytecode(ast) { sequence: function(node, context) { function buildElementsCode(elements, context) { - let processedCount, functionIndex; - if (elements.length > 0) { - processedCount = node.elements.length - elements.slice(1).length; + let processedCount = node.elements.length - elements.slice(1).length; return buildSequence( generate(elements[0], { @@ -395,7 +393,7 @@ function generateBytecode(ast) { ); } else { if (context.action) { - functionIndex = addFunctionConst( + let functionIndex = addFunctionConst( Object.keys(context.env), context.action.code ); @@ -531,16 +529,14 @@ function generateBytecode(ast) { }, literal: function(node) { - let stringIndex, expectedIndex; - if (node.value.length > 0) { - stringIndex = addConst('"' + let stringIndex = addConst('"' + js.stringEscape( node.ignoreCase ? node.value.toLowerCase() : node.value ) + '"' ); - expectedIndex = addConst( + let expectedIndex = addConst( 'peg$literalExpectation(' + '"' + js.stringEscape(node.value) + '", ' + node.ignoreCase @@ -562,7 +558,7 @@ function generateBytecode(ast) { [op.FAIL, expectedIndex] ); } else { - stringIndex = addConst('""'); + let stringIndex = addConst('""'); return [op.PUSH, stringIndex]; } diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 41c02f2..60df4c1 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -428,12 +428,10 @@ function generateJS(ast, options) { }, pop: function(n) { - let values; - if (n === undefined) { return s(this.sp--); } else { - values = arrays.range(this.sp - n + 1, this.sp + 1).map(s); + let values = arrays.range(this.sp - n + 1, this.sp + 1).map(s); this.sp -= n; return values; @@ -745,10 +743,7 @@ function generateJS(ast, options) { } function generateToplevel() { - let parts = [], - startRuleIndices, startRuleIndex, - startRuleFunctions, startRuleFunction, - ruleNames; + let parts = []; parts.push([ 'function peg$subclass(child, parent) {', @@ -940,24 +935,24 @@ function generateJS(ast, options) { ].join('\n')); if (options.optimize === "size") { - startRuleIndices = '{ ' + let startRuleIndices = '{ ' + options.allowedStartRules.map( function(r) { return r + ': ' + asts.indexOfRule(ast, r); } ).join(', ') + ' }'; - startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]); + let startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]); parts.push([ ' peg$startRuleIndices = ' + startRuleIndices + ',', ' peg$startRuleIndex = ' + startRuleIndex + ',' ].join('\n')); } else { - startRuleFunctions = '{ ' + let startRuleFunctions = '{ ' + options.allowedStartRules.map( function(r) { return r + ': peg$parse' + r; } ).join(', ') + ' }'; - startRuleFunction = 'peg$parse' + options.allowedStartRules[0]; + let startRuleFunction = 'peg$parse' + options.allowedStartRules[0]; parts.push([ ' peg$startRuleFunctions = ' + startRuleFunctions + ',', @@ -989,7 +984,7 @@ function generateJS(ast, options) { if (options.trace) { if (options.optimize === "size") { - ruleNames = '[' + let ruleNames = '[' + ast.rules.map( function(r) { return '"' + js.stringEscape(r.name) + '"'; } ).join(', ') diff --git a/lib/peg.js b/lib/peg.js index b95143d..5af89b8 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -25,9 +25,9 @@ let peg = { options = options !== undefined ? options : {}; function convertPasses(passes) { - let converted = {}, stage; + let converted = {}; - for (stage in passes) { + for (let stage in passes) { if (passes.hasOwnProperty(stage)) { converted[stage] = objects.values(passes[stage]); } diff --git a/lib/utils/arrays.js b/lib/utils/arrays.js index 15f8c0a..4059492 100644 --- a/lib/utils/arrays.js +++ b/lib/utils/arrays.js @@ -4,10 +4,9 @@ let arrays = { range: function(start, stop) { let length = stop - start, - result = new Array(length), - i, j; + result = new Array(length); - for (i = 0, j = start; i < length; i++, j++) { + for (let i = 0, j = start; i < length; i++, j++) { result[i] = j; } @@ -15,16 +14,16 @@ let arrays = { }, find: function(array, valueOrPredicate) { - let length = array.length, i; + let length = array.length; if (typeof valueOrPredicate === "function") { - for (i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { if (valueOrPredicate(array[i])) { return array[i]; } } } else { - for (i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { if (array[i] === valueOrPredicate) { return array[i]; } @@ -33,16 +32,16 @@ let arrays = { }, indexOf: function(array, valueOrPredicate) { - let length = array.length, i; + let length = array.length; if (typeof valueOrPredicate === "function") { - for (i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { if (valueOrPredicate(array[i])) { return i; } } } else { - for (i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { if (array[i] === valueOrPredicate) { return i; } diff --git a/lib/utils/objects.js b/lib/utils/objects.js index 9f73876..3502732 100644 --- a/lib/utils/objects.js +++ b/lib/utils/objects.js @@ -3,9 +3,9 @@ /* Object utilities. */ let objects = { values: function(object) { - let result = [], key; + let result = []; - for (key in object) { + for (let key in object) { if (object.hasOwnProperty(key)) { result.push(object[key]); } @@ -15,9 +15,9 @@ let objects = { }, clone: function(object) { - let result = {}, key; + let result = {}; - for (key in object) { + for (let key in object) { if (object.hasOwnProperty(key)) { result[key] = object[key]; } @@ -27,9 +27,7 @@ let objects = { }, defaults: function(object, defaults) { - let key; - - for (key in defaults) { + for (let key in defaults) { if (defaults.hasOwnProperty(key)) { if (!(key in object)) { object[key] = defaults[key]; diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 2f54f8a..2e06104 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -8,9 +8,9 @@ let peg = require("../../lib/peg"); describe("generated parser behavior", function() { function varyOptimizationOptions(block) { function clone(object) { - let result = {}, key; + let result = {}; - for (key in object) { + for (let key in object) { if (object.hasOwnProperty(key)) { result[key] = object[key]; } @@ -77,7 +77,7 @@ describe("generated parser behavior", function() { toFailToParse: function(input, details, options) { options = options !== undefined ? options : {}; - let result, key; + let result; try { result = this.actual.parse(input, options); @@ -92,7 +92,7 @@ describe("generated parser behavior", function() { }; } else { if (details) { - for (key in details) { + for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { this.message = function() { diff --git a/spec/unit/compiler/passes/helpers.js b/spec/unit/compiler/passes/helpers.js index 113f2fa..c284111 100644 --- a/spec/unit/compiler/passes/helpers.js +++ b/spec/unit/compiler/passes/helpers.js @@ -16,13 +16,11 @@ beforeEach(function() { return value !== null && typeof value === "object"; } - let i, key; - if (isArray(details)) { if (!isArray(value)) { return false; } if (value.length !== details.length) { return false; } - for (i = 0; i < details.length; i++) { + for (let i = 0; i < details.length; i++) { if (!matchDetails(value[i], details[i])) { return false; } } @@ -30,7 +28,7 @@ beforeEach(function() { } else if (isObject(details)) { if (!isObject(value)) { return false; } - for (key in details) { + for (let key in details) { if (details.hasOwnProperty(key)) { if (!(key in value)) { return false; } @@ -61,8 +59,7 @@ beforeEach(function() { }, toReportError: function(grammar, details) { - let ast = peg.parser.parse(grammar), - key; + let ast = peg.parser.parse(grammar); try { this.actual(ast); @@ -75,7 +72,7 @@ beforeEach(function() { }; } else { if (details) { - for (key in details) { + for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { this.message = function() { diff --git a/spec/unit/parser.spec.js b/spec/unit/parser.spec.js index 6f6a85c..f7f29a0 100644 --- a/spec/unit/parser.spec.js +++ b/spec/unit/parser.spec.js @@ -194,7 +194,7 @@ describe("PEG.js grammar parser", function() { }, toFailToParse: function(details) { - let result, key; + let result; try { result = peg.parser.parse(this.actual); @@ -207,7 +207,7 @@ describe("PEG.js grammar parser", function() { }; } else { if (details) { - for (key in details) { + for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { this.message = function() {