From 671c22e80f0ffdb83791057058abb867cc758334 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 21 Aug 2015 17:24:49 +0200 Subject: [PATCH] Avoid using |console| in default tracer and its tests when not defined This makes default tracer and its tests work in IE 8-10. --- lib/compiler/passes/generate-javascript.js | 14 ++++++++------ spec/api/generated-parser-api.spec.js | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index e82fb0d..b262994 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -817,12 +817,14 @@ function generateJavascript(ast, options) { ' return string + repeat(" ", length - string.length);', ' }', '', - ' console.log(', - ' event.location.start.line + ":" + event.location.start.column + "-"', - ' + event.location.end.line + ":" + event.location.end.column + " "', - ' + pad(event.type, 10) + " "', - ' + repeat(" ", that.indentLevel) + event.rule', - ' );', + ' if (typeof console === "object") {', // IE 8-10 + ' console.log(', + ' event.location.start.line + ":" + event.location.start.column + "-"', + ' + event.location.end.line + ":" + event.location.end.column + " "', + ' + pad(event.type, 10) + " "', + ' + repeat(" ", that.indentLevel) + event.rule', + ' );', + ' }', ' }', '', ' switch (event.type) {', diff --git a/spec/api/generated-parser-api.spec.js b/spec/api/generated-parser-api.spec.js index 68a27e7..403d5e4 100644 --- a/spec/api/generated-parser-api.spec.js +++ b/spec/api/generated-parser-api.spec.js @@ -53,17 +53,21 @@ describe("generated parser API", function() { ].join("\n"), { trace: true }); describe("default tracer", function() { - it("traces using console.log", function() { - spyOn(console, "log"); + it("traces using console.log (if console is defined)", function() { + if (typeof console === "object") { + spyOn(console, "log"); + } parser.parse("b"); - expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter start"); - expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter a"); - expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.fail a"); - expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter b"); - expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match b"); - expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match start"); + if (typeof console === "object") { + expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter start"); + expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter a"); + expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.fail a"); + expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter b"); + expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match b"); + expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match start"); + } }); });