swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.23k stars 1.23k forks source link

Minify options: properties.keep_quoted setting is ignored #2171

Open ofext opened 3 years ago

ofext commented 3 years ago

Describe the bug properties.keep_quoted property from minify is ignored Input code Source

import fetch from "node-fetch";
(async () => {
  let obj1 = {
    "key1": "val1",
    "key2": "val2",
  };
  let response = await fetch("https://postman-echo.com/post", {
    method: "POST",
    body: JSON.stringify(obj1),
    headers: { "Content-Type": "application/json" },
  });
  let json = await response.json();
  console.log("RESPONSE BODY: ", json.data);
  let obj2 = {
    key3: "Val3",
  };
  console.log("Unquoted obj: ", obj2);
})();
`
**Config**

```json
{
jsc: {
            "target": "es2021",
            parser: {
              syntax: "ecmascript",
            },
            transform: {},
            "minify": {
              "compress": {
                "unused": true,
                "dead_code": true,
                drop_console: false,
              },
              "mangle": {
                "topLevel": true,
                properties: {
                  "keep_quoted": true,
                },
              },
            },
          },
}

Expected behavior If you process this code with swc, the resulting code is as follow:

import a from "node-fetch";
(async ()=>{
    let b = {
        a: "val1",
        b: "val2"
    }, c = await a("https://postman-echo.com/post", {
        method: "POST",
        body: JSON.stringify(b),
        headers: {
            "c": "application/json"
        }
    }), d = await c.json();
    console.log("RESPONSE BODY: ", d.data);
    let e = {
        d: "Val3"
    };
    console.log("Unquoted obj: ", e);
})();

As you can see, it replaced "content-type" with "c" and it also replaced the key names "key1" to a and "key2" to b , which is incorrect since the config: minify.mangle.properties.keep_quoted means that it should not mangle those properties that are quoted. Version

"@swc/cli": "^0.1.49"

Additional context As you could have seen from the example above, the keep_quoted feature is really important, because in some cases you don't want to minify some properties because the actual name given is needed.

ofext commented 3 years ago

Can someone confirm if this is actually a bug or am I doing something wrong?