From f0cbb35e2d1f81e73dec339dcdbabdb352009ebb Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 5 Nov 2018 09:59:21 +0100 Subject: [PATCH] Force-retokenize regexps when a slash is found in expression position Issue #751 Issue #589 --- acorn/src/expression.js | 4 ++++ test/tests.js | 1 + 2 files changed, 5 insertions(+) diff --git a/acorn/src/expression.js b/acorn/src/expression.js index 310b3931f..04ecd6238 100644 --- a/acorn/src/expression.js +++ b/acorn/src/expression.js @@ -304,6 +304,10 @@ pp.parseSubscripts = function(base, startPos, startLoc, noCalls) { // or `{}`. pp.parseExprAtom = function(refDestructuringErrors) { + // If a division operator appears in an expression position, the + // tokenizer got confused, and we force it to read a regexp instead. + if (this.type === tt.slash) this.readRegexp() + let node, canBeArrow = this.potentialArrowAt === this.start switch (this.type) { case tt._super: diff --git a/test/tests.js b/test/tests.js index b958026e1..8ea25aae9 100644 --- a/test/tests.js +++ b/test/tests.js @@ -26840,6 +26840,7 @@ test("x = {foo: function x() {} / divide}", {}); test("foo; function f() {} /regexp/", {}); test("{function f() {} /regexp/}", {}); test("function fn() {return\nfunction foo() {}\n/42/}", {}); +test("var x\n/foo/", {}); test("{}/=/", { type: "Program",