Minify: unnecessary escaping in string concatinaion

Created on 23 Feb 2017  路  4Comments  路  Source: babel/minify

Hey,
For some reason I can't explain babili(?) escapes the backslash in the following string:
input:

var x =  '\'cool\'' + 'test'

babili output:

var x='\\\'cool\\\'test';

it only happens when i concat strings, it works perfectly when i input:

var x =  '\'cool\'test'

I use the babili cli without any presets

p.s the babel repl doesn't show the problem (probably browser html escaping)

bug

All 4 comments

I had the same issue, its not actually the backslash being quoted its the single quote, e.g. this is a simpler breaking case:

// In code
const s = " ' " + "quote breaks"

// Out code
const s = " \\' quote breaks"

Hmm I guess after #384 the strings are first run through jsesc by the minify-constant-folding transform, and then again by babel-generator. So backslashes end up being duplicated.

Maybe instead of using jsesc, minify-constant-folding should handle </script and <!-- manually? Or babel-generator could pass isScriptContext to jsesc, then babili doesn't need to worry about that either.

What would be a correct way to handle these ?

/cc @mathiasbynens

As @goto-bus-stop said: don鈥榯 escape strings twice.

The most sensible solution IMHO would be to make babel-generator pass isScriptContext: true to jsesc and to remove the additional/faulty escaping from minify-constant-folding.

Was this page helpful?
0 / 5 - 0 ratings