Replace suitable for loops with Array methods (in /spec)

See #441.
redux
David Majda 8 years ago
parent fad4ab74d1
commit df5f86103e

@ -56,8 +56,6 @@ describe("plugin API", function() {
it("receives configuration", function() { it("receives configuration", function() {
var plugin = { var plugin = {
use: function(config) { use: function(config) {
var i;
expect(config).toBeObject(); expect(config).toBeObject();
expect(config.parser).toBeObject(); expect(config.parser).toBeObject();
@ -66,19 +64,19 @@ describe("plugin API", function() {
expect(config.passes).toBeObject(); expect(config.passes).toBeObject();
expect(config.passes.check).toBeArray(); expect(config.passes.check).toBeArray();
for (i = 0; i < config.passes.check.length; i++) { config.passes.check.forEach(function(pass) {
expect(config.passes.check[i]).toBeFunction(); expect(pass).toBeFunction();
} });
expect(config.passes.transform).toBeArray(); expect(config.passes.transform).toBeArray();
for (i = 0; i < config.passes.transform.length; i++) { config.passes.transform.forEach(function(pass) {
expect(config.passes.transform[i]).toBeFunction(); expect(pass).toBeFunction();
} });
expect(config.passes.generate).toBeArray(); expect(config.passes.generate).toBeArray();
for (i = 0; i < config.passes.generate.length; i++) { config.passes.generate.forEach(function(pass) {
expect(config.passes.generate[i]).toBeFunction(); expect(pass).toBeFunction();
} });
} }
}; };

@ -26,15 +26,14 @@ describe("generated parser behavior", function() {
{ cache: true, optimize: "speed", trace: true }, { cache: true, optimize: "speed", trace: true },
{ cache: true, optimize: "size", trace: false }, { cache: true, optimize: "size", trace: false },
{ cache: true, optimize: "size", trace: true } { cache: true, optimize: "size", trace: true }
], ];
i;
for (i = 0; i < optionsVariants.length; i++) { optionsVariants.forEach(function(variant) {
describe( describe(
"with options " + jasmine.pp(optionsVariants[i]), "with options " + jasmine.pp(variant),
function() { block(clone(optionsVariants[i])); } function() { block(clone(variant)); }
); );
} });
} }
beforeEach(function() { beforeEach(function() {
@ -512,12 +511,12 @@ describe("generated parser behavior", function() {
input: "b" input: "b"
} }
], ],
parser, i; parser;
for (i = 0; i < testcases.length; i++) { testcases.forEach(function(testcase) {
parser = peg.generate(testcases[i].grammar, options); parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcase.input);
} });
}); });
}); });
@ -708,12 +707,12 @@ describe("generated parser behavior", function() {
input: "b" input: "b"
} }
], ],
parser, i; parser;
for (i = 0; i < testcases.length; i++) { testcases.forEach(function(testcase) {
parser = peg.generate(testcases[i].grammar, options); parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcase.input);
} });
}); });
}); });
@ -1077,12 +1076,12 @@ describe("generated parser behavior", function() {
input: "b" input: "b"
} }
], ],
parser, i; parser;
for (i = 0; i < testcases.length; i++) { testcases.forEach(function(testcase) {
parser = peg.generate(testcases[i].grammar, options); parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcase.input);
} });
}); });
}); });
@ -1497,20 +1496,16 @@ describe("generated parser behavior", function() {
var parser = peg.generate([ var parser = peg.generate([
'Expr = Sum', 'Expr = Sum',
'Sum = first:Product rest:(("+" / "-") Product)* {', 'Sum = first:Product rest:(("+" / "-") Product)* {',
' var result = first, i;', ' return rest.reduce(function(result, element) {',
' for (i = 0; i < rest.length; i++) {', ' if (element[0] == "+") { return result + element[1]; }',
' if (rest[i][0] == "+") { result += rest[i][1]; }', ' if (element[0] == "-") { return result - element[1]; }',
' if (rest[i][0] == "-") { result -= rest[i][1]; }', ' }, first);',
' }',
' return result;',
' }', ' }',
'Product = first:Value rest:(("*" / "/") Value)* {', 'Product = first:Value rest:(("*" / "/") Value)* {',
' var result = first, i;', ' return rest.reduce(function(result, element) {',
' for (i = 0; i < rest.length; i++) {', ' if (element[0] == "*") { return result * element[1]; }',
' if (rest[i][0] == "*") { result *= rest[i][1]; }', ' if (element[0] == "/") { return result / element[1]; }',
' if (rest[i][0] == "/") { result /= rest[i][1]; }', ' }, first);',
' }',
' return result;',
' }', ' }',
'Value = digits:[0-9]+ { return parseInt(digits.join(""), 10); }', 'Value = digits:[0-9]+ { return parseInt(digits.join(""), 10); }',
' / "(" expr:Expr ")" { return expr; }' ' / "(" expr:Expr ")" { return expr; }'

@ -122,29 +122,20 @@ describe("PEG.js grammar parser", function() {
function stripChildren(property) { function stripChildren(property) {
return function(node) { return function(node) {
var i;
delete node.location; delete node.location;
for (i = 0; i < node[property].length; i++) { node[property].forEach(strip);
strip(node[property][i]);
}
}; };
} }
var strip = buildVisitor({ var strip = buildVisitor({
grammar: function(node) { grammar: function(node) {
var i;
delete node.location; delete node.location;
if (node.initializer) { if (node.initializer) {
strip(node.initializer); strip(node.initializer);
} }
node.rules.forEach(strip);
for (i = 0; i < node.rules.length; i++) {
strip(node.rules[i]);
}
}, },
initializer: stripLeaf, initializer: stripLeaf,

Loading…
Cancel
Save