swc-project / swc

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

compress optimize escape `\\` in template string #9184

Closed stormslowly closed 3 months ago

stormslowly commented 4 months ago

Describe the bug

expect the output should be (foo - \*foo) or (bar - \*bar)

but

(foo - *foo) or (bar - *bar)

Input code

let pi= Math.random() >0.6 ? "foo": "bar";
let fi=`(${`${pi}`} - ${`\\*${pi}`})`

console.log(fi)

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2020",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true,
        "passes": 8
      },
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.6.7&code=H4sIAAAAAAAAA8tJLVEoyLRV8E0sydArSsxLyc%2FV0FSwM9AzU7BXUErLz1eyUlBKSixSsubKASpNy7RN0FCpTlCpLsisTahV0FUAcmJitKB8zQQuruT8vOL8nFS9nPx0jbRMTQD%2FL1pIYQAAAA%3D%3D&config=H4sIAAAAAAAAA32US47bMAyG9zlF4HUXg1kVPUB3PYOgSJSjVBINkcrEGOTupR%2FJpBM6O5sff1IiKX7u9vvuRK77tf%2BUT%2FkZbCWo93%2Bx0FjYXsTSgcuWXI0Ddz9u9EQTCjYRzKbrQjq2tQeeVfT%2B9v62KrqESHBTrLYcSwzjY06HeahA9GATq4RsGQrT%2F%2FqVVfyYANf2aD8gJrDlBTGWTCwMPVQtsMOU7EBgzrYqUaaT2hoJtRQTbAzeDBUHlRcfOWKRnM%2FUg%2FXGoQcFxQqO4xk0meQSWSG5nnKfGXs4tL6f%2B%2FxNDWebmmUlJ1zmlshplahHjMQmtKKVcIEbNVjgWtzvyhhMBW61POtOGMtGT%2F4CSAWSJSo2gxZ39ggyT1vq8FIZS5CR5VHhMt%2FaLQv0UlQTY1AqO1UGKketmxV8czBV1mnHWfFG%2BSh6MBCCzIoSmj4iu6OWlMcBMChA%2BmuDNlULMPdXuMGnB%2FEC%2F5Zbsj5gq0e2fNymNOYDphcJMvAR%2FQsHaQXjNq6yJS7DNm%2FFg4wGeNWl0Qyel4A8AEaT5n35NBvyPCSi6RMe1DUxyJzPTfy52q735Zxt6b%2BWwLKfd6tDl9G3Ga6bf2r6tK%2BlRxnLibovz9t%2Bvl%2Bpi%2FTnJp%2BOs7v%2BA6xLOoBKBgAA

SWC Info output

No response

Expected behavior

output (foo - \*foo) or (bar - \*bar)

Actual behavior

(foo - *foo) or (bar - *bar)

Version

1.6.7

Additional context

No response

stormslowly commented 4 months ago

hi @kdy1 i run the code with crates/swc_ecma_minifier/src/cli/bin.rs, the output is correct as expected. and i study the code for nested tpl optimize in crates/swc_ecma_minifier/src/compress/pure/strings.rs, i can't find a bug.

could you plz give some hint about the problem, maybe i can make a PR

kdy1 commented 4 months ago

Sorry. I missed your comment.

GFI guide

Testing

To make the test reliable, you would need to add

let pi= Math.random() >1.1 ? "foo": "bar";
let fi=`(${`${pi}`} - ${`\\*${pi}`})`

console.log(fi)
let pi= Math.random() < -0.1 ? "foo": "bar";
let fi=`(${`${pi}`} - ${`\\*${pi}`})`

console.log(fi)

so the Math.random() does not make test wrongly pass.

Debugging

I wrote the relevant documentation for debugging or testing to https://github.com/swc-project/swc/blob/e22f3ba9adf21eac057eab29284333b1631258b3/crates/swc_ecma_minifier/README.md#contributing

Fixing

Once you get the location of the relevant code, my guess is that you will need to modify some code about the string replacing logic.

stormslowly commented 3 months ago

@kdy1 thanks for your guide. PR is ready.

swc-bot commented 2 months ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.