Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compress optimize escape \\ in template string #9184

Closed
stormslowly opened this issue Jul 9, 2024 · 3 comments
Closed

compress optimize escape \\ in template string #9184

stormslowly opened this issue Jul 9, 2024 · 3 comments

Comments

@stormslowly
Copy link
Contributor

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

@kdy1 kdy1 added this to the Planned milestone Jul 9, 2024
@kdy1 kdy1 self-assigned this Jul 9, 2024
@stormslowly
Copy link
Contributor Author

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
Copy link
Member

kdy1 commented Jul 13, 2024

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
Copy link
Contributor Author

@kdy1 thanks for your guide.
PR is ready.

@kdy1 kdy1 closed this as completed in 6ddbfa0 Jul 17, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.7.0 Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants