From 58666bc2ce5ec83ae8962092c40a7f971f061738 Mon Sep 17 00:00:00 2001 From: Monroe Ekilah Date: Mon, 17 Aug 2020 12:56:57 -0700 Subject: [PATCH 1/5] workaround for broken Edge versions 15-18 --- src/parsimmon.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parsimmon.js b/src/parsimmon.js index 028f701..c4ff4b1 100644 --- a/src/parsimmon.js +++ b/src/parsimmon.js @@ -695,7 +695,12 @@ function formatError(input, error) { function flags(re) { var s = "" + re; - return s.slice(s.lastIndexOf("/") + 1); + var fs = s.slice(s.lastIndexOf("/") + 1); + if (fs === "undefined") { + // MS Edge v15-18 workaround + return "" + } + return fs } function anchoredRegexp(re) { From cc668ff9599af4e29e89a71312517c92d354ba06 Mon Sep 17 00:00:00 2001 From: Monroe Ekilah Date: Mon, 17 Aug 2020 14:33:47 -0700 Subject: [PATCH 2/5] use .flags when it is present --- src/parsimmon.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/parsimmon.js b/src/parsimmon.js index c4ff4b1..6ffea47 100644 --- a/src/parsimmon.js +++ b/src/parsimmon.js @@ -694,13 +694,17 @@ function formatError(input, error) { } function flags(re) { - var s = "" + re; - var fs = s.slice(s.lastIndexOf("/") + 1); - if (fs === "undefined") { - // MS Edge v15-18 workaround - return "" + if (re.flags !== undefined) { + return re.flags; } - return fs + // legacy browser support + return [ + re.global ? "g" : "", + re.ignoreCase ? "i" : "", + re.multiline ? "m" : "", + re.unicode ? "u" : "", + re.sticky ? "y" : "", + ].join(""); } function anchoredRegexp(re) { From 9372b12470030d989d3e9a1ab74a0964bef2d658 Mon Sep 17 00:00:00 2001 From: Monroe Ekilah Date: Mon, 24 Aug 2020 17:31:38 -0700 Subject: [PATCH 3/5] lint:fix --- src/parsimmon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parsimmon.js b/src/parsimmon.js index 6ffea47..3a19356 100644 --- a/src/parsimmon.js +++ b/src/parsimmon.js @@ -703,7 +703,7 @@ function flags(re) { re.ignoreCase ? "i" : "", re.multiline ? "m" : "", re.unicode ? "u" : "", - re.sticky ? "y" : "", + re.sticky ? "y" : "" ].join(""); } From 37b45b17e874211747d6bc900fd7822a44715fbc Mon Sep 17 00:00:00 2001 From: Monroe Ekilah Date: Mon, 24 Aug 2020 17:41:54 -0700 Subject: [PATCH 4/5] add test for flags function --- src/parsimmon.js | 1 + test/core/flags.test.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/core/flags.test.js diff --git a/src/parsimmon.js b/src/parsimmon.js index 3a19356..8c8c23d 100644 --- a/src/parsimmon.js +++ b/src/parsimmon.js @@ -1365,6 +1365,7 @@ Parsimmon.empty = empty; Parsimmon.end = end; Parsimmon.eof = eof; Parsimmon.fail = fail; +Parsimmon.flags = flags; Parsimmon.formatError = formatError; Parsimmon.index = index; Parsimmon.isParser = isParser; diff --git a/test/core/flags.test.js b/test/core/flags.test.js new file mode 100644 index 0000000..819658c --- /dev/null +++ b/test/core/flags.test.js @@ -0,0 +1,21 @@ +"use strict"; + +testSetScenario(function() { + describe("Parsimmon.flags()", function() { + it("works in modern browsers", function() { + var flags = Parsimmon.flags(/a/gim); + assert.strictEqual(flags, "gim"); + }); + + it("works on legacy browsers without Regexp.flags property", function() { + var oldRegExp = /a/gim; + + // Simulate old RegExp without the flags property + Object.defineProperty(oldRegExp, "flags", { value: undefined }); + assert.strictEqual(oldRegExp.flags, undefined); + + var flags = Parsimmon.flags(oldRegExp); + assert.strictEqual(flags, "gim"); + }); + }); +}); From 0577c93c6562a38e2b0d1b7c4291fea071bae071 Mon Sep 17 00:00:00 2001 From: Monroe Ekilah Date: Mon, 24 Aug 2020 18:08:08 -0700 Subject: [PATCH 5/5] 100% test coverage --- test/.eslintrc | 3 +++ test/core/flags.test.js | 24 +++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/test/.eslintrc b/test/.eslintrc index 4c7229e..cc00932 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -7,5 +7,8 @@ "Parsimmon": true, "assert": true, "testSetScenario": true + }, + "rules": { + "no-invalid-regexp": ["error", {"allowConstructorFlags": ["u", "y"]}] } } diff --git a/test/core/flags.test.js b/test/core/flags.test.js index 819658c..7500ad2 100644 --- a/test/core/flags.test.js +++ b/test/core/flags.test.js @@ -3,19 +3,33 @@ testSetScenario(function() { describe("Parsimmon.flags()", function() { it("works in modern browsers", function() { - var flags = Parsimmon.flags(/a/gim); - assert.strictEqual(flags, "gim"); + // eslint-disable-next-line no-invalid-regexp + var flags = Parsimmon.flags(new RegExp("a", "gimuy")); + assert.strictEqual(flags, "gimuy"); }); - it("works on legacy browsers without Regexp.flags property", function() { - var oldRegExp = /a/gim; + it("works on legacy browsers without Regexp.flags property with flags", function() { + // eslint-disable-next-line no-invalid-regexp + var oldRegExp = new RegExp("a", "gimuy"); // Simulate old RegExp without the flags property Object.defineProperty(oldRegExp, "flags", { value: undefined }); assert.strictEqual(oldRegExp.flags, undefined); var flags = Parsimmon.flags(oldRegExp); - assert.strictEqual(flags, "gim"); + assert.strictEqual(flags, "gimuy"); + }); + + it("works on legacy browsers without Regexp.flags property without flags", function() { + // eslint-disable-next-line no-invalid-regexp + var oldRegExp = new RegExp("a", ""); + + // Simulate old RegExp without the flags property + Object.defineProperty(oldRegExp, "flags", { value: undefined }); + assert.strictEqual(oldRegExp.flags, undefined); + + var flags = Parsimmon.flags(oldRegExp); + assert.strictEqual(flags, ""); }); }); });