Skip to content

Minify option incorrectly strips escape characters #7678

@SarahAnnTolsma

Description

@SarahAnnTolsma

Describe the bug

When escaping specific strings, SWC strips necessary escape characters in minified output. This results in the incorrect string being output. See the given input code and playground link for examples.

Input code

let str = "\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";

let obj = {
    "\\uD83D\\uDC68\\u200D\\uD83D\\uDE80": "wrong"
};

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": true,
  "isModule": false
}

Playground link

https://play.swc.rs/?version=1.3.70&code=H4sIAAAAAAAAA8tJLVEoLilSsFVQiokpdbEwdgFRzmYWQMrIwMAFSdDVwkDJmosrB6gjPykLqKOaSwEIiNJnpaBUXpSfl67EVWsNAEudP25zAAAA&config=H4sIAAAAAAAAA1VPOw7DIAzdcwrkuUNbqUvv0EMg6kRE%2FIQdqSjK3QME2maz39deByFgJgVPseYxL0FGwvjdM0LJsfxkBDgFJBV1YLh0lqlQozSEFdoOBljGCbm4kO7X26M5wHhP2B0Ns9rpMf13Km9DRKKzsEilmwyeG4fWCta%2Fl0q2X8q95YKcZr2bCX7K3shxqeGg6dXdR%2FS2AzVy5M4cAQAA

Expected behavior

SWC should not strip escape characters and produce the following output:

let str="\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";let obj={"\\uD83D\\uDC68\\u200D\\uD83D\\uDE80":"wrong"};

Actual behavior

SWC strips escape characters and produces the following output:

let str="\uD83D\uDC68\\u200D\uD83D\uDE80";let obj={"\uD83D\uDC68\\u200D\uD83D\uDE80":"wrong"};

Note the missing \ before \uD83D (twice), \uDC68 and \uDE80.

Version

1.3.70

Additional context

As a workaround, the following produces the correct output:

let workaround = {
    [`\\uD83D\\uDC68\\u200D\\uD83D\\uDE80`]: "correct",
};

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions