From b044a021c4966bfce0a3be412a602de13ed01c14 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 19 Aug 2016 17:07:31 +0200 Subject: [PATCH] Replace arrays.each with Array.prototype.forEach See #441. --- lib/compiler/index.js | 5 ++--- lib/compiler/passes/generate-bytecode.js | 2 +- lib/compiler/passes/generate-js.js | 2 +- lib/compiler/passes/remove-proxy-rules.js | 4 ++-- lib/compiler/passes/report-duplicate-labels.js | 3 +-- lib/compiler/visitor.js | 7 +++---- lib/peg.js | 5 ++--- lib/utils/arrays.js | 8 -------- 8 files changed, 12 insertions(+), 24 deletions(-) diff --git a/lib/compiler/index.js b/lib/compiler/index.js index 1248598..e1c956e 100644 --- a/lib/compiler/index.js +++ b/lib/compiler/index.js @@ -1,7 +1,6 @@ "use strict"; -var arrays = require("../utils/arrays"), - objects = require("../utils/objects"); +var objects = require("../utils/objects"); var compiler = { /* @@ -59,7 +58,7 @@ var compiler = { for (stage in passes) { if (passes.hasOwnProperty(stage)) { - arrays.each(passes[stage], function(p) { p(ast, options); }); + 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 60c81a9..113882e 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -282,7 +282,7 @@ function generateBytecode(ast) { var generate = visitor.build({ grammar: function(node) { - arrays.each(node.rules, generate); + node.rules.forEach(generate); node.consts = consts; }, diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index c579ac9..3bbc114 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1182,7 +1182,7 @@ function generateJS(ast, options) { parts.push(indent2(generateInterpreter())); parts.push(''); } else { - arrays.each(ast.rules, function(rule) { + ast.rules.forEach(function(rule) { parts.push(indent2(generateRuleFunction(rule))); parts.push(''); }); diff --git a/lib/compiler/passes/remove-proxy-rules.js b/lib/compiler/passes/remove-proxy-rules.js index 8d50548..d90c36f 100644 --- a/lib/compiler/passes/remove-proxy-rules.js +++ b/lib/compiler/passes/remove-proxy-rules.js @@ -25,7 +25,7 @@ function removeProxyRules(ast, options) { var indices = []; - arrays.each(ast.rules, function(rule, i) { + ast.rules.forEach(function(rule, i) { if (isProxyRule(rule)) { replaceRuleRefs(ast, rule.name, rule.expression.name); if (!arrays.contains(options.allowedStartRules, rule.name)) { @@ -36,7 +36,7 @@ function removeProxyRules(ast, options) { indices.reverse(); - arrays.each(indices, function(i) { ast.rules.splice(i, 1); }); + indices.forEach(function(i) { ast.rules.splice(i, 1); }); } module.exports = removeProxyRules; diff --git a/lib/compiler/passes/report-duplicate-labels.js b/lib/compiler/passes/report-duplicate-labels.js index 16a0c11..c42b57e 100644 --- a/lib/compiler/passes/report-duplicate-labels.js +++ b/lib/compiler/passes/report-duplicate-labels.js @@ -1,7 +1,6 @@ "use strict"; var GrammarError = require("../../grammar-error"), - arrays = require("../../utils/arrays"), objects = require("../../utils/objects"), visitor = require("../visitor"); @@ -17,7 +16,7 @@ function reportDuplicateLabels(ast) { }, choice: function(node, env) { - arrays.each(node.alternatives, function(alternative) { + node.alternatives.forEach(function(alternative) { check(alternative, objects.clone(env)); }); }, diff --git a/lib/compiler/visitor.js b/lib/compiler/visitor.js index 0a447bc..a7f764e 100644 --- a/lib/compiler/visitor.js +++ b/lib/compiler/visitor.js @@ -1,7 +1,6 @@ "use strict"; -var objects = require("../utils/objects"), - arrays = require("../utils/arrays"); +var objects = require("../utils/objects"); /* Simple AST node visitor builder. */ var visitor = { @@ -22,7 +21,7 @@ var visitor = { return function(node) { var extraArgs = Array.prototype.slice.call(arguments, 1); - arrays.each(node[property], function(child) { + node[property].forEach(function(child) { visit.apply(null, [child].concat(extraArgs)); }); }; @@ -36,7 +35,7 @@ var visitor = { visit.apply(null, [node.initializer].concat(extraArgs)); } - arrays.each(node.rules, function(rule) { + node.rules.forEach(function(rule) { visit.apply(null, [rule].concat(extraArgs)); }); }, diff --git a/lib/peg.js b/lib/peg.js index 29b759b..fa2144f 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -1,7 +1,6 @@ "use strict"; -var arrays = require("./utils/arrays"), - objects = require("./utils/objects"); +var objects = require("./utils/objects"); var peg = { /* PEG.js version (uses semantic versioning). */ @@ -45,7 +44,7 @@ var peg = { passes: convertPasses(peg.compiler.passes) }; - arrays.each(plugins, function(p) { p.use(config, options); }); + plugins.forEach(function(p) { p.use(config, options); }); return peg.compiler.compile( config.parser.parse(grammar), diff --git a/lib/utils/arrays.js b/lib/utils/arrays.js index 93a833c..650f311 100644 --- a/lib/utils/arrays.js +++ b/lib/utils/arrays.js @@ -56,14 +56,6 @@ var arrays = { return arrays.indexOf(array, valueOrPredicate) !== -1; }, - each: function(array, iterator) { - var length = array.length, i; - - for (i = 0; i < length; i++) { - iterator(array[i], i); - } - }, - map: function(array, iterator) { var length = array.length, result = new Array(length),