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

SWC compress removes string literal’s .toUpperCase() and produces incorrect code #8864

Closed
controversial opened this issue Apr 15, 2024 · 1 comment · Fixed by #8865
Closed
Assignees
Labels
Milestone

Comments

@controversial
Copy link

controversial commented Apr 15, 2024

Describe the bug

The attached playground shows a stripped-down version of a code file that appears in my next.js project.

The SWC minifier/“compressor” removes .toUpperCase() from a string literal without uppercasing the string literal. The result is incorrect output.

Playground link

Input code

export class Renderer {
  renderStaticFrame(string1, string2) {
    const line1Text = `${string1} and ${string2}`.toUpperCase();
    const line2Text = 'line 2 text'.toUpperCase();

    const text = `${line1Text}\n${line2Text}`;
    return text;
  }
}

console.log(new Renderer().renderStaticFrame('a', 'b'));

Output code

export class Renderer {
  renderStaticFrame(string1, string2) {
      let line1Text = `${string1} and ${string2}`.toUpperCase();
      return `${line1Text}\nline 2 text`;
  }
}
console.log(new Renderer().renderStaticFrame('a', 'b'));

Expected behavior

Both lines are uppercase.

❯ node swc-input.js   
A AND B
LINE 2 TEXT

Actual behavior

The second line is printed in lowercase, because the toUpperCase function was removed in the SWC output.

❯ node swc-output.js     
A AND B
line 2 text

SWC Info output

    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
        CPU: (10 cores)
            Models: Apple M1 Pro

    Binaries:
        Node: 20.12.2
        npm: 10.5.0
        Yarn: 1.22.19
        pnpm: N/A

    Relevant Packages:
        @swc/core: N/A
        @swc/helpers: 0.5.5
        @swc/types: N/A
        typescript: 5.4.5
        next: 14.2.0

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

    Next.js info:
        output: N/A

Version

1.4.14

Additional context

From playing with the version selector in the playground, it appears this issue was introduced in version 1.3.105.

@kdy1 kdy1 self-assigned this Apr 15, 2024
@kdy1 kdy1 added this to the Planned milestone Apr 15, 2024
kdy1 added a commit that referenced this issue Apr 16, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.15 Apr 17, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented May 17, 2024

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants