From 6fa8ad63f9fb7bf393459b5166092cc3920e13cb Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 9 Sep 2016 13:27:24 +0200 Subject: [PATCH] Replace some functions with arrow functions Because arrow functions work rather differently than normal functions (a bad design mistake if you ask me), I decided to be conservative with the conversion. I converted: * event handlers * callbacks * arguments to Array.prototype.map & co. * small standalone lambda functions I didn't convert: * functions assigned to object literal properties (the new shorthand syntax would be better here) * functions passed to "describe", "it", etc. in specs (because Jasmine relies on dynamic "this") See #442. --- benchmark/index.js | 4 +- benchmark/runner.js | 6 +- benchmark/server | 4 +- bin/pegjs | 10 +-- lib/compiler/asts.js | 4 +- lib/compiler/index.js | 2 +- lib/compiler/js.js | 16 ++-- lib/compiler/passes/generate-bytecode.js | 18 ++--- lib/compiler/passes/generate-js.js | 44 +++++------ lib/compiler/passes/remove-proxy-rules.js | 4 +- .../passes/report-duplicate-labels.js | 2 +- .../passes/report-infinite-recursion.js | 2 +- lib/compiler/visitor.js | 4 +- lib/parser.js | 4 +- lib/peg.js | 2 +- spec/api/generated-parser-api.spec.js | 6 +- spec/api/pegjs-api.spec.js | 28 ++----- spec/api/plugin-api.spec.js | 37 +++++---- .../generated-parser-behavior.spec.js | 75 +++++++++---------- spec/server | 4 +- spec/unit/compiler/passes/helpers.js | 48 ++++++------ spec/unit/parser.spec.js | 51 ++++++------- src/parser.pegjs | 4 +- 23 files changed, 172 insertions(+), 207 deletions(-) diff --git a/benchmark/index.js b/benchmark/index.js index cce8fcb..c423e8f 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -3,7 +3,7 @@ let benchmarks = require("./benchmarks.js"), Runner = require("./runner.js"); -$("#run").click(function() { +$("#run").click(() => { "use strict"; /* Results Table Manipulation */ @@ -135,7 +135,7 @@ $("#run").click(function() { }); -$(document).ready(function() { +$(document).ready(() => { "use strict"; $("#run").focus(); diff --git a/benchmark/runner.js b/benchmark/runner.js index 8eb2d9b..b61886e 100644 --- a/benchmark/runner.js +++ b/benchmark/runner.js @@ -24,7 +24,7 @@ let Runner = { * We can't use |arguments.callee| here because |this| would get * messed-up in that case. */ - setTimeout(function() { Q.run(); }, 0); + setTimeout(() => { Q.run(); }, 0); } } }; @@ -107,9 +107,9 @@ let Runner = { /* Main */ Q.add(initialize); - benchmarks.forEach(function(benchmark) { + benchmarks.forEach(benchmark => { Q.add(benchmarkInitializer(benchmark)); - benchmark.tests.forEach(function(test) { + benchmark.tests.forEach(test => { Q.add(testRunner(benchmark, test)); }); Q.add(benchmarkFinalizer(benchmark)); diff --git a/benchmark/server b/benchmark/server index 1125531..6852f22 100755 --- a/benchmark/server +++ b/benchmark/server @@ -21,7 +21,7 @@ app.use(logger("dev")); app.use(express.static(__dirname)); app.use("/examples", express.static(__dirname + "/../examples")); -app.get("/bundle.js", function(req, res) { +app.get("/bundle.js", (req, res) => { let files = glob.sync(__dirname + "/**/*.js", { ignore: __dirname + "/vendor/**/*" }); @@ -32,7 +32,7 @@ app.get("/bundle.js", function(req, res) { .pipe(res); }); -app.listen(8000, function() { +app.listen(8000, () => { console.log("Benchmark server running at http://localhost:8000..."); }); diff --git a/bin/pegjs b/bin/pegjs index 7a624b0..f29f7f7 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -100,8 +100,8 @@ function nextArg() { function readStream(inputStream, callback) { let input = ""; - inputStream.on("data", function(data) { input += data; }); - inputStream.on("end", function() { callback(input); }); + inputStream.on("data", (data) => { input += data; }); + inputStream.on("end", () => { callback(input); }); } /* Main */ @@ -296,7 +296,7 @@ if (outputFile === null) { if (inputFile === "-") { process.stdin.resume(); inputStream = process.stdin; - inputStream.on("error", function() { + inputStream.on("error", () => { abort("Can't read from file \"" + inputFile + "\"."); }); } else { @@ -307,12 +307,12 @@ if (outputFile === "-") { outputStream = process.stdout; } else { outputStream = fs.createWriteStream(outputFile); - outputStream.on("error", function() { + outputStream.on("error", () => { abort("Can't write to file \"" + outputFile + "\"."); }); } -readStream(inputStream, function(input) { +readStream(inputStream, (input) => { let source; try { diff --git a/lib/compiler/asts.js b/lib/compiler/asts.js index b24979f..559e75d 100644 --- a/lib/compiler/asts.js +++ b/lib/compiler/asts.js @@ -6,11 +6,11 @@ let arrays = require("../utils/arrays"), /* AST utilities. */ let asts = { findRule: function(ast, name) { - return arrays.find(ast.rules, function(r) { return r.name === name; }); + return arrays.find(ast.rules, r => r.name === name); }, indexOfRule: function(ast, name) { - return arrays.indexOf(ast.rules, function(r) { return r.name === name; }); + return arrays.indexOf(ast.rules, r => r.name === name); }, alwaysConsumesOnSuccess: function(ast, node) { diff --git a/lib/compiler/index.js b/lib/compiler/index.js index 0ee44d5..f0b02bf 100644 --- a/lib/compiler/index.js +++ b/lib/compiler/index.js @@ -56,7 +56,7 @@ let compiler = { for (let stage in passes) { if (passes.hasOwnProperty(stage)) { - passes[stage].forEach(function(p) { p(ast, options); }); + passes[stage].forEach(p => { p(ast, options); }); } } diff --git a/lib/compiler/js.js b/lib/compiler/js.js index 5f91bc4..f0d9d84 100644 --- a/lib/compiler/js.js +++ b/lib/compiler/js.js @@ -23,10 +23,10 @@ let js = { .replace(/\v/g, '\\v') // vertical tab .replace(/\f/g, '\\f') // form feed .replace(/\r/g, '\\r') // carriage return - .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); }) - .replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); }) - .replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); }); + .replace(/[\x00-\x0F]/g, ch => '\\x0' + hex(ch)) + .replace(/[\x10-\x1F\x7F-\xFF]/g, ch => '\\x' + hex(ch)) + .replace(/[\u0100-\u0FFF]/g, ch => '\\u0' + hex(ch)) + .replace(/[\u1000-\uFFFF]/g, ch => '\\u' + hex(ch)); }, regexpClassEscape: function(s) { @@ -48,10 +48,10 @@ let js = { .replace(/\v/g, '\\v') // vertical tab .replace(/\f/g, '\\f') // form feed .replace(/\r/g, '\\r') // carriage return - .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); }) - .replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); }) - .replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); }); + .replace(/[\x00-\x0F]/g, ch => '\\x0' + hex(ch)) + .replace(/[\x10-\x1F\x7F-\xFF]/g, ch => '\\x' + hex(ch)) + .replace(/[\u0100-\u0FFF]/g, ch => '\\u0' + hex(ch)) + .replace(/[\u1000-\uFFFF]/g, ch => '\\u' + hex(ch)); } }; diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index c54945c..4870aa5 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -222,7 +222,7 @@ function generateBytecode(ast) { } function buildCall(functionIndex, delta, env, sp) { - let params = objects.values(env).map(function(p) { return sp - p; }); + let params = objects.values(env).map(p => sp - p); return [op.CALL, functionIndex, delta, params.length].concat(params); } @@ -567,20 +567,20 @@ function generateBytecode(ast) { "class": function(node) { let regexp = '/^[' + (node.inverted ? '^' : '') - + node.parts.map(function(part) { - return Array.isArray(part) + + node.parts.map(part => + Array.isArray(part) ? js.regexpClassEscape(part[0]) + '-' + js.regexpClassEscape(part[1]) - : js.regexpClassEscape(part); - }).join('') + : js.regexpClassEscape(part) + ).join('') + ']/' + (node.ignoreCase ? 'i' : ''), parts = '[' - + node.parts.map(function(part) { - return Array.isArray(part) + + node.parts.map(part => + Array.isArray(part) ? '["' + js.stringEscape(part[0]) + '", "' + js.stringEscape(part[1]) + '"]' - : '"' + js.stringEscape(part) + '"'; - }).join(', ') + : '"' + js.stringEscape(part) + '"' + ).join(', ') + ']', regexpIndex = addConst(regexp), expectedIndex = addConst( diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 60df4c1..b4ef9b2 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -21,19 +21,17 @@ function generateJS(ast, options) { '],', '', 'peg$bytecode = [', - indent2(ast.rules.map(function(rule) { - return 'peg$decode("' - + js.stringEscape(rule.bytecode.map( - function(b) { return String.fromCharCode(b + 32); } - ).join('')) - + '")'; - }).join(',\n')), + indent2(ast.rules.map(rule => + 'peg$decode("' + + js.stringEscape(rule.bytecode.map( + b => String.fromCharCode(b + 32) + ).join('')) + + '")' + ).join(',\n')), '],' ].join('\n'); } else { - return ast.consts.map( - function(c, i) { return 'peg$c' + i + ' = ' + c + ','; } - ).join('\n'); + return ast.consts.map((c, i) => 'peg$c' + i + ' = ' + c + ',').join('\n'); } } @@ -513,7 +511,7 @@ function generateJS(ast, options) { let value = c(bc[ip + 1]) + '(' + bc.slice(ip + baseLength, ip + baseLength + paramsLength).map( - function(p) { return stack.index(p); } + p => stack.index(p) ).join(', ') + ')'; stack.pop(bc[ip + 2]); @@ -937,7 +935,7 @@ function generateJS(ast, options) { if (options.optimize === "size") { let startRuleIndices = '{ ' + options.allowedStartRules.map( - function(r) { return r + ': ' + asts.indexOfRule(ast, r); } + r => r + ': ' + asts.indexOfRule(ast, r) ).join(', ') + ' }'; let startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]); @@ -949,7 +947,7 @@ function generateJS(ast, options) { } else { let startRuleFunctions = '{ ' + options.allowedStartRules.map( - function(r) { return r + ': peg$parse' + r; } + r => r + ': peg$parse' + r ).join(', ') + ' }'; let startRuleFunction = 'peg$parse' + options.allowedStartRules[0]; @@ -986,7 +984,7 @@ function generateJS(ast, options) { if (options.optimize === "size") { let ruleNames = '[' + ast.rules.map( - function(r) { return '"' + js.stringEscape(r.name) + '"'; } + r => '"' + js.stringEscape(r.name) + '"' ).join(', ') + ']'; @@ -1156,7 +1154,7 @@ function generateJS(ast, options) { parts.push(indent2(generateInterpreter())); parts.push(''); } else { - ast.rules.forEach(function(rule) { + ast.rules.forEach(rule => { parts.push(indent2(generateRuleFunction(rule))); parts.push(''); }); @@ -1242,12 +1240,10 @@ function generateJS(ast, options) { let parts = [], dependencyVars = Object.keys(options.dependencies), requires = dependencyVars.map( - function(variable) { - return variable - + ' = require("' - + js.stringEscape(options.dependencies[variable]) - + '")'; - } + variable => variable + + ' = require("' + + js.stringEscape(options.dependencies[variable]) + + '")' ); parts.push([ @@ -1277,7 +1273,7 @@ function generateJS(ast, options) { dependencyVars = Object.keys(options.dependencies), dependencies = '[' + dependencyIds.map( - function(id) { return '"' + js.stringEscape(id) + '"'; } + id => '"' + js.stringEscape(id) + '"' ).join(', ') + ']', params = dependencyVars.join(', '); @@ -1315,11 +1311,11 @@ function generateJS(ast, options) { dependencyVars = Object.keys(options.dependencies), dependencies = '[' + dependencyIds.map( - function(id) { return '"' + js.stringEscape(id) + '"'; } + id => '"' + js.stringEscape(id) + '"' ).join(', ') + ']', requires = dependencyIds.map( - function(id) { return 'require("' + js.stringEscape(id) + '")'; } + id => 'require("' + js.stringEscape(id) + '")' ).join(', '), params = dependencyVars.join(', '); diff --git a/lib/compiler/passes/remove-proxy-rules.js b/lib/compiler/passes/remove-proxy-rules.js index f8036c0..5aa2eb2 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) { let indices = []; - ast.rules.forEach(function(rule, i) { + ast.rules.forEach((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(); - indices.forEach(function(i) { ast.rules.splice(i, 1); }); + indices.forEach(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 f13659f..a027bd9 100644 --- a/lib/compiler/passes/report-duplicate-labels.js +++ b/lib/compiler/passes/report-duplicate-labels.js @@ -16,7 +16,7 @@ function reportDuplicateLabels(ast) { }, choice: function(node, env) { - node.alternatives.forEach(function(alternative) { + node.alternatives.forEach(alternative => { check(alternative, objects.clone(env)); }); }, diff --git a/lib/compiler/passes/report-infinite-recursion.js b/lib/compiler/passes/report-infinite-recursion.js index fc733d0..985ef51 100644 --- a/lib/compiler/passes/report-infinite-recursion.js +++ b/lib/compiler/passes/report-infinite-recursion.js @@ -28,7 +28,7 @@ function reportInfiniteRecursion(ast) { }, sequence: function(node) { - node.elements.every(function(element) { + node.elements.every(element => { check(element); return !asts.alwaysConsumesOnSuccess(ast, element); diff --git a/lib/compiler/visitor.js b/lib/compiler/visitor.js index 58545b0..ec5469b 100644 --- a/lib/compiler/visitor.js +++ b/lib/compiler/visitor.js @@ -21,7 +21,7 @@ let visitor = { return function(node) { let extraArgs = Array.prototype.slice.call(arguments, 1); - node[property].forEach(function(child) { + node[property].forEach(child => { visit.apply(null, [child].concat(extraArgs)); }); }; @@ -35,7 +35,7 @@ let visitor = { visit.apply(null, [node.initializer].concat(extraArgs)); } - node.rules.forEach(function(rule) { + node.rules.forEach(rule => { visit.apply(null, [rule].concat(extraArgs)); }); }, diff --git a/lib/parser.js b/lib/parser.js index b9edab9..4632b9b 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -330,7 +330,7 @@ function peg$parse(input, options) { peg$c98 = function(inverted, parts, ignoreCase) { return { type: "class", - parts: parts.filter(function(part) { return part !== ""; }), + parts: parts.filter(part => part !== ""), inverted: inverted !== null, ignoreCase: ignoreCase !== null, location: location() @@ -4982,7 +4982,7 @@ function peg$parse(input, options) { } function extractList(list, index) { - return list.map(function(element) { return element[index]; }); + return list.map(element => element[index]); } function buildList(head, tail, index) { diff --git a/lib/peg.js b/lib/peg.js index 5af89b8..cc03c97 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -44,7 +44,7 @@ let peg = { passes: convertPasses(peg.compiler.passes) }; - plugins.forEach(function(p) { p.use(config, options); }); + plugins.forEach(p => { p.use(config, options); }); return peg.compiler.compile( config.parser.parse(grammar), diff --git a/spec/api/generated-parser-api.spec.js b/spec/api/generated-parser-api.spec.js index 85a50b5..c261b55 100644 --- a/spec/api/generated-parser-api.spec.js +++ b/spec/api/generated-parser-api.spec.js @@ -16,7 +16,7 @@ describe("generated parser API", function() { it("throws an exception on syntax error", function() { let parser = peg.generate('start = "a"'); - expect(function() { parser.parse("b"); }).toThrow(); + expect(() => { parser.parse("b"); }).toThrow(); }); describe("start rule", function() { @@ -41,9 +41,7 @@ describe("generated parser API", function() { describe("when |startRule| is set to a disallowed start rule", function() { it("throws an exception", function() { - expect( - function() { parser.parse("x", { startRule: "a" }); } - ).toThrow(); + expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow(); }); }); }); diff --git a/spec/api/pegjs-api.spec.js b/spec/api/pegjs-api.spec.js index 1e2e69e..6acadc6 100644 --- a/spec/api/pegjs-api.spec.js +++ b/spec/api/pegjs-api.spec.js @@ -12,11 +12,11 @@ describe("PEG.js API", function() { }); it("throws an exception on syntax error", function() { - expect(function() { peg.generate('start = @'); }).toThrow(); + expect(() => { peg.generate('start = @'); }).toThrow(); }); it("throws an exception on semantic error", function() { - expect(function() { peg.generate('start = undefined'); }).toThrow(); + expect(() => { peg.generate('start = undefined'); }).toThrow(); }); describe("allowed start rules", function() { @@ -37,12 +37,8 @@ describe("PEG.js API", function() { let parser = peg.generate(grammar, { optimize: "speed" }); expect(parser.parse("x", { startRule: "a" })).toBe("x"); - expect( - function() { parser.parse("x", { startRule: "b" }); } - ).toThrow(); - expect( - function() { parser.parse("x", { startRule: "c" }); } - ).toThrow(); + expect(() => { parser.parse("x", { startRule: "b" }); }).toThrow(); + expect(() => { parser.parse("x", { startRule: "c" }); }).toThrow(); }); }); @@ -53,9 +49,7 @@ describe("PEG.js API", function() { allowedStartRules: ["b", "c"] }); - expect( - function() { parser.parse("x", { startRule: "a" }); } - ).toThrow(); + expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow(); expect(parser.parse("x", { startRule: "b" })).toBe("x"); expect(parser.parse("x", { startRule: "c" })).toBe("x"); }); @@ -68,12 +62,8 @@ describe("PEG.js API", function() { let parser = peg.generate(grammar, { optimize: "size" }); expect(parser.parse("x", { startRule: "a" })).toBe("x"); - expect( - function() { parser.parse("x", { startRule: "b" }); } - ).toThrow(); - expect( - function() { parser.parse("x", { startRule: "c" }); } - ).toThrow(); + expect(() => { parser.parse("x", { startRule: "b" }); }).toThrow(); + expect(() => { parser.parse("x", { startRule: "c" }); }).toThrow(); }); }); @@ -84,9 +74,7 @@ describe("PEG.js API", function() { allowedStartRules: ["b", "c"] }); - expect( - function() { parser.parse("x", { startRule: "a" }); } - ).toThrow(); + expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow(); expect(parser.parse("x", { startRule: "b" })).toBe("x"); expect(parser.parse("x", { startRule: "c" })).toBe("x"); }); diff --git a/spec/api/plugin-api.spec.js b/spec/api/plugin-api.spec.js index da0cd19..5e3d2bb 100644 --- a/spec/api/plugin-api.spec.js +++ b/spec/api/plugin-api.spec.js @@ -6,31 +6,28 @@ describe("plugin API", function() { beforeEach(function() { this.addMatchers({ toBeObject: function() { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + (this.isNot ? "not " : "") - + "to be an object."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to be an object."; return this.actual !== null && typeof this.actual === "object"; }, toBeArray: function() { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + (this.isNot ? "not " : "") - + "to be an array."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to be an array."; return Object.prototype.toString.apply(this.actual) === "[object Array]"; }, toBeFunction: function() { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + (this.isNot ? "not " : "") - + "to be a function."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to be a function."; return typeof this.actual === "function"; } @@ -64,17 +61,17 @@ describe("plugin API", function() { expect(config.passes).toBeObject(); expect(config.passes.check).toBeArray(); - config.passes.check.forEach(function(pass) { + config.passes.check.forEach(pass => { expect(pass).toBeFunction(); }); expect(config.passes.transform).toBeArray(); - config.passes.transform.forEach(function(pass) { + config.passes.transform.forEach(pass => { expect(pass).toBeFunction(); }); expect(config.passes.generate).toBeArray(); - config.passes.generate.forEach(function(pass) { + config.passes.generate.forEach(pass => { expect(pass).toBeFunction(); }); } @@ -123,7 +120,7 @@ describe("plugin API", function() { it("can change compiler passes", function() { let plugin = { use: function(config) { - let pass = function(ast) { + let pass = ast => { ast.code = '({ parse: function() { return 42; } })'; }; @@ -151,7 +148,7 @@ describe("plugin API", function() { plugins: [plugin] }); - expect(function() { parser.parse("x", { startRule: "a" }); }).toThrow(); + expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow(); expect(parser.parse("x", { startRule: "b" })).toBe("x"); expect(parser.parse("x", { startRule: "c" })).toBe("x"); }); diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 2e06104..3c4c929 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -30,7 +30,7 @@ describe("generated parser behavior", function() { { cache: true, optimize: "size", trace: true } ]; - optionsVariants.forEach(function(variant) { + optionsVariants.forEach(variant => { describe( "with options " + jasmine.pp(variant), function() { block(clone(variant)); } @@ -48,25 +48,23 @@ describe("generated parser behavior", function() { try { result = this.actual.parse(input, options); } catch (e) { - this.message = function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + "to parse" + (expected !== undefined ? " as " + jasmine.pp(expected) : "") + ", " - + "but it failed to parse with message " - + jasmine.pp(e.message) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + "to parse" + (expected !== undefined ? " as " + jasmine.pp(expected) : "") + ", " + + "but it failed to parse with message " + + jasmine.pp(e.message) + "."; return false; } if (expected !== undefined) { - this.message = function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + (this.isNot ? "not " : "") - + "to parse as " + jasmine.pp(expected) + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + (this.isNot ? "not " : "") + + "to parse as " + jasmine.pp(expected) + ", " + + "but it parsed as " + jasmine.pp(result) + "."; return this.env.equals_(result, expected); } else { @@ -83,26 +81,24 @@ describe("generated parser behavior", function() { result = this.actual.parse(input, options); } catch (e) { if (this.isNot) { - this.message = function() { - return "Expected " + jasmine.pp(input) - + "with options " + jasmine.pp(options) + " " - + "to parse, " - + "but it failed with message " - + jasmine.pp(e.message) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(input) + + "with options " + jasmine.pp(options) + " " + + "to parse, " + + "but it failed with message " + + jasmine.pp(e.message) + "."; } else { if (details) { for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[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]) + "."; - }; + this.message = () => + "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; } @@ -114,13 +110,12 @@ describe("generated parser behavior", function() { return true; } - this.message = function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + "to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + "to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but it parsed as " + jasmine.pp(result) + "."; return false; } @@ -515,7 +510,7 @@ describe("generated parser behavior", function() { ], parser; - testcases.forEach(function(testcase) { + testcases.forEach(testcase => { parser = peg.generate(testcase.grammar, options); expect(parser).toFailToParse(testcase.input); }); @@ -711,7 +706,7 @@ describe("generated parser behavior", function() { ], parser; - testcases.forEach(function(testcase) { + testcases.forEach(testcase => { parser = peg.generate(testcase.grammar, options); expect(parser).toFailToParse(testcase.input); }); @@ -1080,7 +1075,7 @@ describe("generated parser behavior", function() { ], parser; - testcases.forEach(function(testcase) { + testcases.forEach(testcase => { parser = peg.generate(testcase.grammar, options); expect(parser).toFailToParse(testcase.input); }); diff --git a/spec/server b/spec/server index d6e7526..fcd29f7 100755 --- a/spec/server +++ b/spec/server @@ -20,7 +20,7 @@ let app = express(); app.use(logger("dev")); app.use(express.static(__dirname)); -app.get("/bundle.js", function(req, res) { +app.get("/bundle.js", (req, res) => { let files = glob.sync(__dirname + "/**/*.js", { ignore: __dirname + "/vendor/**/*" }); @@ -31,7 +31,7 @@ app.get("/bundle.js", function(req, res) { .pipe(res); }); -app.listen(8000, function() { +app.listen(8000, () => { console.log("Spec server running at http://localhost:8000..."); }); diff --git a/spec/unit/compiler/passes/helpers.js b/spec/unit/compiler/passes/helpers.js index c284111..9dbaa6d 100644 --- a/spec/unit/compiler/passes/helpers.js +++ b/spec/unit/compiler/passes/helpers.js @@ -46,14 +46,13 @@ beforeEach(function() { this.actual(ast, options); - this.message = function() { - return "Expected the pass " - + "with options " + jasmine.pp(options) + " " - + (this.isNot ? "not " : "") - + "to change the AST " + jasmine.pp(ast) + " " - + "to match " + jasmine.pp(details) + ", " - + "but it " + (this.isNot ? "did" : "didn't") + "."; - }; + this.message = () => + "Expected the pass " + + "with options " + jasmine.pp(options) + " " + + (this.isNot ? "not " : "") + + "to change the AST " + jasmine.pp(ast) + " " + + "to match " + jasmine.pp(details) + ", " + + "but it " + (this.isNot ? "did" : "didn't") + "."; return matchDetails(ast, details); }, @@ -65,23 +64,21 @@ beforeEach(function() { this.actual(ast); } catch (e) { if (this.isNot) { - this.message = function() { - return "Expected the pass not to report an error " - + "for grammar " + jasmine.pp(grammar) + ", " - + "but it did."; - }; + this.message = () => + "Expected the pass not to report an error " + + "for grammar " + jasmine.pp(grammar) + ", " + + "but it did."; } else { if (details) { for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[key])) { - this.message = function() { - return "Expected the pass to report an error " - + "with details " + jasmine.pp(details) + " " - + "for grammar " + jasmine.pp(grammar) + ", " - + "but " + jasmine.pp(key) + " " - + "is " + jasmine.pp(e[key]) + "."; - }; + this.message = () => + "Expected the pass to report an error " + + "with details " + jasmine.pp(details) + " " + + "for grammar " + jasmine.pp(grammar) + ", " + + "but " + jasmine.pp(key) + " " + + "is " + jasmine.pp(e[key]) + "."; return false; } @@ -93,12 +90,11 @@ beforeEach(function() { return true; } - this.message = function() { - return "Expected the pass to report an error " - + (details ? "with details " + jasmine.pp(details) + " ": "") - + "for grammar " + jasmine.pp(grammar) + ", " - + "but it didn't."; - }; + this.message = () => + "Expected the pass to report an error " + + (details ? "with details " + jasmine.pp(details) + " ": "") + + "for grammar " + jasmine.pp(grammar) + ", " + + "but it didn't."; return false; } diff --git a/spec/unit/parser.spec.js b/spec/unit/parser.spec.js index f7f29a0..c7ec8d4 100644 --- a/spec/unit/parser.spec.js +++ b/spec/unit/parser.spec.js @@ -171,24 +171,22 @@ describe("PEG.js grammar parser", function() { try { result = peg.parser.parse(this.actual); } catch (e) { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + "to parse as " + jasmine.pp(expected) + ", " - + "but it failed to parse with message " - + jasmine.pp(e.message) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " " + + "to parse as " + jasmine.pp(expected) + ", " + + "but it failed to parse with message " + + jasmine.pp(e.message) + "."; return false; } stripLocation(result); - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + (this.isNot ? "not " : "") - + "to parse as " + jasmine.pp(expected) + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to parse as " + jasmine.pp(expected) + ", " + + "but it parsed as " + jasmine.pp(result) + "."; return this.env.equals_(result, expected); }, @@ -200,22 +198,20 @@ describe("PEG.js grammar parser", function() { result = peg.parser.parse(this.actual); } catch (e) { if (this.isNot) { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " to parse, " - + "but it failed with message " - + jasmine.pp(e.message) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " to parse, " + + "but it failed with message " + + jasmine.pp(e.message) + "."; } else { if (details) { for (let key in details) { if (details.hasOwnProperty(key)) { if (!this.env.equals_(e[key], details[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]) + "."; - }; + this.message = () => + "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; } @@ -229,11 +225,10 @@ describe("PEG.js grammar parser", function() { stripLocation(result); - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; + this.message = () => + "Expected " + jasmine.pp(this.actual) + " to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but it parsed as " + jasmine.pp(result) + "."; return false; } diff --git a/src/parser.pegjs b/src/parser.pegjs index 61cc7ac..b035699 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -46,7 +46,7 @@ } function extractList(list, index) { - return list.map(function(element) { return element[index]; }); + return list.map(element => element[index]); } function buildList(head, tail, index) { @@ -364,7 +364,7 @@ CharacterClassMatcher "character class" { return { type: "class", - parts: parts.filter(function(part) { return part !== ""; }), + parts: parts.filter(part => part !== ""), inverted: inverted !== null, ignoreCase: ignoreCase !== null, location: location()