From f3a83788aa451d30cb3a49f86670c918200337c8 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 27 Apr 2014 13:31:49 +0200 Subject: [PATCH] Inline functions extracted just because of JSHint Rather than extracting functions just because JSHint complained about defining functions inside a loop, let's inline then and silence the warning. --- .jshintrc | 1 + lib/compiler.js | 10 +------- lib/compiler/passes/generate-javascript.js | 10 +------- spec/generated-parser.spec.js | 29 ++++++++-------------- spec/parser.spec.js | 20 +++++---------- 5 files changed, 19 insertions(+), 51 deletions(-) diff --git a/.jshintrc b/.jshintrc index fab2850..d9bd925 100644 --- a/.jshintrc +++ b/.jshintrc @@ -7,6 +7,7 @@ "immed": true, "latedef": "nofunc", "laxbreak": true, + "loopfunc": true, "noarg": true, "noempty": true, "nonew": true, diff --git a/lib/compiler.js b/lib/compiler.js index ffdff9b..fdaf739 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -32,14 +32,6 @@ module.exports = { var options = arguments.length > 2 ? utils.clone(arguments[2]) : {}, stage; - /* - * Extracted into a function just to silence JSHint complaining about - * creating functions in a loop. - */ - function runPass(pass) { - pass(ast, options); - } - utils.defaults(options, { allowedStartRules: [ast.rules[0].name], cache: false, @@ -49,7 +41,7 @@ module.exports = { for (stage in passes) { if (passes.hasOwnProperty(stage)) { - utils.each(passes[stage], runPass); + utils.each(passes[stage], function(p) { p(ast, options); }); } } diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index ad6de1d..3c10dbc 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -435,7 +435,7 @@ module.exports = function(ast, options) { var value = c(bc[ip + 1]) + '(' + utils.map( bc.slice(ip + baseLength, ip + baseLength + paramsLength), - stackIndex + function(p) { return stack.index(p); } ).join(', ') + ')'; stack.pop(bc[ip + 2]); @@ -443,14 +443,6 @@ module.exports = function(ast, options) { ip += baseLength + paramsLength; } - /* - * Extracted into a function just to silence JSHint complaining about - * creating functions in a loop. - */ - function stackIndex(p) { - return stack.index(p); - } - while (ip < end) { switch (bc[ip]) { case op.PUSH: // PUSH c diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index 4ec57f8..b527a93 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -76,24 +76,8 @@ describe("generated parser", function() { var options = arguments.length > 2 ? arguments[1] : {}, details = arguments.length > 1 ? arguments[arguments.length - 1] - : undefined; - - /* - * Extracted into a function just to silence JSHint complaining about - * creating functions in a loop. - */ - function buildKeyMessage(key, value) { - return function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + "to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but " + jasmine.pp(key) + " " - + "is " + jasmine.pp(value) + "."; - }; - } - - var result; + : undefined, + result; try { result = this.actual.parse(input, options); @@ -127,7 +111,14 @@ describe("generated parser", function() { for (key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { - this.message = buildKeyMessage(key, e[key]); + this.message = function() { + return "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + "to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but " + jasmine.pp(key) + " " + + "is " + jasmine.pp(e[key]) + "."; + }; return false; } diff --git a/spec/parser.spec.js b/spec/parser.spec.js index 020e66c..eb8be04 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -127,19 +127,6 @@ describe("PEG.js grammar parser", function() { }, toFailToParse: function(details) { - /* - * Extracted into a function just to silence JSHint complaining about - * creating functions in a loop. - */ - function buildKeyMessage(key, value) { - return function() { - return "Expected " + jasmine.pp(this.actual) + " to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but " + jasmine.pp(key) + " " - + "is " + jasmine.pp(value) + "."; - }; - } - var result; try { @@ -170,7 +157,12 @@ describe("PEG.js grammar parser", function() { for (key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { - this.message = buildKeyMessage(key, e[key]); + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but " + jasmine.pp(key) + " " + + "is " + jasmine.pp(e[key]) + "."; + }; return false; }