Skip to content

Commit 2a89704

Browse files
kfuledead-claudia
authored andcommitted
bundler: fix regular expression literals, including their flags
It seemed that, for example, `var i` might cause a suffix to be added to the "i" flag within a regular expression literal. So the entire regular expression literal, including flags, is now corrected within the bundler.
1 parent 749171e commit 2a89704

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

scripts/_bundler-impl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ module.exports = async (input) => {
159159

160160
// fix regexp literals
161161
// Note: This regexp, while it doesn't technically capture all cases a regexp could appear, should hopefully work for now.
162-
const regexpLiteral = /([=({[](?:[\s\u2028\u2029]|\/\/.*?[\r\n\u2028\u2029]|\/\*[\s\S]*?\*\/)*\/)((?:[^\\\/[\r\n\u2028\u2029]|\\[^\r\n\u2028\u2029]|\[(?:[^\]\\\r\n\u2028\u2029]|\\[^\r\n\u2028\u2029])*\])+)(\/[$\p{ID_Continue}]*)/ug
163-
code = code.replace(regexpLiteral, (match, open, data, close) => {
164-
const fixed = data.replace(variables, (match) => match.replace(/\d+$/, ""))
165-
return open + fixed + close
162+
const regexpLiteral = /([=({[](?:[\s\u2028\u2029]|\/\/.*?[\r\n\u2028\u2029]|\/\*[\s\S]*?\*\/)*)(\/(?:[^\\\/[\r\n\u2028\u2029]|\\[^\r\n\u2028\u2029]|\[(?:[^\]\\\r\n\u2028\u2029]|\\[^\r\n\u2028\u2029])*\])+\/[$\p{ID_Continue}]*)/ug
163+
code = code.replace(regexpLiteral, (match, pre, literal) => {
164+
const fixed = literal.replace(variables, (match) => match.replace(/\d+$/, ""))
165+
return pre + fixed
166166
})
167167

168168
//fix props

scripts/tests/test-bundler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ o.spec("bundler", async () => {
288288
o("does not mess up regexp literals", async () => {
289289
await setup({
290290
"a.js": 'var b = require("./b")\nvar c = require("./c")',
291-
"b.js": "var b = /b/\nmodule.exports = function() {return b}",
291+
"b.js": "var b = /b/\nvar g = 0\nmodule.exports = function() {return b}",
292292
"c.js": "var b =\n\t/ b \\/ \\/ [a-b]/g\nvar d = b/b\nmodule.exports = function() {return b}",
293293
})
294294

295-
o(await bundle(p("a.js"))).equals(";(function() {\nvar b0 = /b/\nvar b = function() {return b0}\nvar b1 =\n\t/ b \\/ \\/ [a-b]/g\nvar d = b1/b1\nvar c = function() {return b1}\n}());")
295+
o(await bundle(p("a.js"))).equals(";(function() {\nvar b0 = /b/\nvar g = 0\nvar b = function() {return b0}\nvar b1 =\n\t/ b \\/ \\/ [a-b]/g\nvar d = b1/b1\nvar c = function() {return b1}\n}());")
296296
})
297297
o("does not mess up properties", async () => {
298298
await setup({

0 commit comments

Comments
 (0)