google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.37k stars 1.15k forks source link

JSCompiler does not properly handle escaped backslash in tagged template literal #2606

Open mitchellwills opened 7 years ago

mitchellwills commented 7 years ago

Using a tagged template literal with an escaped backslash causes both backslashes to be passed to the template function, instead of just the single backslash. The raw property contains both backslashes as expected.

For example

test`\\.`
compiled to: test({0: "\\\\.", raw: "\\\\."})
expected: test({0: "\\.", raw: "\\\\."})

However, other escape sequences work as expected

test`\n.`
compiled to: test({0: "\n.", raw: "\\n."})

https://closure-compiler-debugger.appspot.com/#input0%3Dfunction%2520test(...args)%2520%257B%250A%2520%2520console.log(args)%253B%250A%257D%250A%250Atest%2560%255C%255C.%2560%250Atest%2560%255Cn.%2560%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26MISSING_PROPERTIES%3D1%26TRANSPILE%3D1%26CHECK_TYPES%3D1%26CLOSURE_PASS%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1

MatrixFrog commented 7 years ago

cc @Dominator008

mitchellwills commented 7 years ago

FYI the running that code directly in Chrome 60 works as expected and the Typescript 2.3 ES5 transpile works as well

mitchellwills commented 7 years ago

Looks like cookString may just need another case https://github.com/google/closure-compiler/blob/a4cb49131c9db49227f188f9ee25d418f82e3bd3/src/com/google/javascript/jscomp/Es6TemplateLiterals.java#L150

Might be worth revisiting to see if anything else is missing there