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

Concatenation of += operator #9504

Open
dithom opened this issue Aug 27, 2024 · 2 comments
Open

Concatenation of += operator #9504

dithom opened this issue Aug 27, 2024 · 2 comments
Assignees
Labels
Milestone

Comments

@dithom
Copy link

dithom commented Aug 27, 2024

Describe the bug

When using the += operator first on a number and then on a string while the compress option is active, the output will be concatenated. This leads to unwanted results at runtime.

Please see 'additional context' for full explanation, as @swc/core is used via swc-loader, terser-webpack-plugin and webpack. I can not reproduce it on the playground, but maybe someone knows, whats going on. Thanks!

Already tried it here: webpack-contrib/terser-webpack-plugin#610 (comment)

Input code

For example:

x = 100;
x += dist;
x += 'px';

should output

x=100,x+=dist,x+='px'

which outputs `150px` at runtime. Instead it outputs

x=100,x+=dist+'px'

So at runtime the result ist not `150px` but `10050px`.

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.18&code=H4sIAAAAAAAAA31TUU%2FbMBB%2B51fcHkYcQC17JYoQGkziAYZapiGhPXjJpbXk2pF9adNB%2Fztnt0lKxxbJie9899133zlVYwpS1kAtzY%2B6lIQgMIWXI%2BBHVSA%2BLewSr5XDGHbT1rzBsouAkDcl6QhyqKT2mO38DqlxZmtt4ruQlXCyujUltmnWF%2BgAGBK6Ywbjrah25ET6MvBjehlswjrqMT7kCHmeQ3KZBOB%2FBMAM6e6jIxHx%2Fws%2FSBBlqh0u0dC0cFZrOD4G5R9tU8xvgpc5BxbvY3Ig12DfB7C57jG3qDiidR3GATHRj3ChSBwgwyUkFByhkwQuICmdnEXjDJSpbKjetcPD4EFQMRfoHAP3xZfSQRs5SeM163xrFJ31bABK5Wmr2DXvhJaeHqxXQZJQRvVWmu3rMrdO%2FbGGpIbXV6hUi%2BVPVdI8WLIhG410r%2B%2FxGCaocSkNgVYGPfxGbVf9eQuneSSTvXcldZtkfY%2FIV3EPNHRXoytYMTnDJ%2B7jcXJ1P%2F32fXLH8sXWTkARLjx%2Fv5yfwxiEWCpc1Zav9inMGiJkvU7Aa1XiV9sYusdVetHl7nL%2BTjlguUfikP%2FngX43lCIIxxq4kae1xuc4nMq6xRWR%2B9VNKzgeHLK2XDaswWs9sXv4CzdvSAKKdPADAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF95wCed1tu%2BgdcghEnYiIn2wiFUW5e4FA2%2BzwvDFP3oWUsLKGp9zLswxRESN950I4%2B6TehQBqp1iTiQluI125RrOyjA0dZwJJ0YKpbfG962BDYBx6Z854M%2Bf%2FQh1cJGS%2BilVVfrF4rRO9Elx4bS3sh6Qc8ax%2FwE8aZYm29i8YnsZiZeL4ADoI3bUTAQAA

SWC Info output

Operating System:
Platform: darwin
Arch: arm64
Machine Type: arm64
Version: Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
CPU: (10 cores)
Models: Apple M1 Pro

Binaries:
    Node: 20.7.0
    npm: 10.1.0
    Yarn: N/A
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.3.101
    @swc/helpers: N/A
    @swc/types: 0.1.5
    typescript: 5.1.6

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

The output should be:

x=100,x+=dist,x+='px'

Actual behavior

The output is:

x=100,x+=dist+'px'

Version

1.3.101

Additional context

I came across this while working with https://github.com/ganlanyuan/tiny-slider, see https://github.com/ganlanyuan/tiny-slider/blob/4d709735c417c2483e77a22d017fc1b18c04f0d4/src/tiny-slider.js#L2606.

How Do We Reproduce?

Use the plugin to bundle the TinySlider code, with the following config:

minimizer: [
    new TerserPlugin({
        minify: TerserPlugin.swcMinify,
        terserOptions: {
            compress: true,
        },
        parallel: true,
    }),
],

What I already checked

  • Terser CLI with command --compress --mangle --mangle-props -- node_modules/tiny-slider/src/tiny-slider.js. This does not output the same behavior, which leads me to think the plugin being the issue.
  • Checked Terser commits regarding this issue from 5.26.0 to 5.31.5.
  • Checked Webpack commits regarding this issue from 5.89.0 to 5.94.0.
System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 69.23 MB / 16.00 GB
  Binaries:
    Node: 20.7.0 - ~/.nvm/versions/node/v20.7.0/bin/node
    npm: 10.1.0 - ~/.nvm/versions/node/v20.7.0/bin/npm
  Browsers:
    Brave Browser: 127.1.68.141
    Safari: 17.5
  Packages:
    css-loader: 6.8.1 => 6.8.1 
    file-loader: 6.2.0 => 6.2.0 
    postcss-loader: 7.3.4 => 7.3.4 
    sass-loader: 13.3.3 => 13.3.3 
    style-loader: 3.3.3 => 3.3.3 
    swc-loader: ^0.2.3 => 0.2.3 
    terser-webpack-plugin: 5.3.10 => 5.3.10 
    webpack: 5.89.0 => 5.89.0 
    webpack-cli: 5.1.4 => 5.1.4 
    webpack-merge: 5.10.0 => 5.10.0 
    webpackbar: 6.0.0 => 6.0.0

Screenshot of the actual issue inside the project:

SCR-20240802-ttbu
@dithom dithom added the C-bug label Aug 27, 2024
@kdy1 kdy1 added this to the Planned milestone Aug 28, 2024
@kdy1 kdy1 self-assigned this Aug 28, 2024
@dithom
Copy link
Author

dithom commented Sep 10, 2024

@SeeSpring thanks for the working playground example! Additionally the problem especially occurs, when dist is provided with a negative value. Then the resulting string is not even a valid number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants
@dithom @kdy1 @SeeSpring and others