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 incorrectly handles double backslashes in template literals #8931

Closed
ayanamists opened this issue May 7, 2024 · 1 comment · Fixed by #8934
Closed

SWC incorrectly handles double backslashes in template literals #8931

ayanamists opened this issue May 7, 2024 · 1 comment · Fixed by #8934
Assignees
Labels
Milestone

Comments

@ayanamists
Copy link

ayanamists commented May 7, 2024

Describe the bug

I encountered an issue with SWC when compiling code that contains strings with double backslashes. It seems this problem only occurs when "target": "es2018" is set and compress is enabled. This problem will cause components as react-katex or other web latex library output weird results.

It seems this problem dose not occur in v1.3.1, and occurs in v.1.5.3 and above.

Input code

export function _jsx(a, b) {
    return b;
}

export const a = _jsx("math", `P \\vdash q`)
export const b = _jsx("math", "P \\vdash q")

Config

{
 // Enable minification
 "minify": true,
 // Optional, configure minification options
 "jsc": {
 "minify": {
 "compress": {
 "unused": true
 },
 "mangle": true
 },
 "target": "es2018"
 }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.3&code=H4sIAAAAAAAAA13K0QpAMBQG4PvzFH%2B7svIG8g4eQLHNhLKxnUnJu1Nu8F1%2Fdl98YPTJGR69QzPFPVM5tMRBuAXLKTjogk4i%2B2zjXWQolE8Xs%2BJB5Ggr1PXWqThgbeU3638Wrywk0QXDQon6iQAAAA%3D%3D&config=H4sIAAAAAAAAA41VwW7bMAy99ysCn3dodyiKfcBu%2BwZBsShHmSwaIpUmKPLvo2U7TRva2CWI%2BfRIkXykPp52u%2BZIbfNr9yF%2F5WOwmSDfvsVCl8T2LJYG2t5Sm8PAzY8FPdIIeRsJquk6IQ3b3AFXFv18fnmbGU1EJFgYs60PKfjLfcwW%2ByED0Z1NrOKy9JCYvvJnLOP7CHAu9%2FY9YgSbNhBjyYTE0EHWHLcYox0IzMlmxct4U5sDoRZiBAuDM0PGQcWTCxwwScxH1IF1pkUHChQytBxOoNEkltASSXpKPhV2sC9dV%2Fv8jQ0nG4tlJSaca0vktorXAwZi40vSSjiBKzWYwLm435nBmwxccnrkHTGklZ78BZAKREuUbA%2Ba33rCi57W2H6TGZIXyfJFwUXfWpYJOimqCcErlR0rA5mD1s0MrrQwVrbVrjPDK%2BWj4MCA96IVxTW9B24PWlC%2BDIBeAaS%2F1muqmgBzm8IVfByIDfi3ZMm6wOYTveXDOkqXfo9xI0APfEC3cUBawbgOZ9kS52EdL8mBSAOceqRQBR6XgAwAo4l1Xz5oQ8ZDPJou4v5zTcwHrrc93NvU1Xm%2F25eMQ4QTxDUZ%2F8eIbKJyt9Oo6mWyH6YX3jRRWhm88PL85dmQVJ6W35pU06MrdwlVTU7PyWvzeWh5OZaqNYH%2BLMTJdv0H%2FO0MreQGAAA%3D

SWC Info output

Operating System:
      Platform: linux
      Arch: x64
      Machine Type: x86_64
      Version: #29~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr  4 14:39:20 UTC 2
      CPU: (12 cores)
          Models: AMD Ryzen 5 3600 6-Core Processor

  Binaries:
      Node: 18.18.0
      npm: 9.8.1
      Yarn: 1.22.19
      pnpm: N/A

  Relevant Packages:
      @swc/core: 1.5.3
      @swc/helpers: N/A
      @swc/types: 0.1.6
      

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

  Next.js info:
      output: N/A

Expected behavior

The output should be

export function _jsx(t,s){return s}export const a=_jsx("math","P \\vdash q");export const b=_jsx("math","P \\vdash q");

Actual behavior

The output is

export function _jsx(t,s){return s}export const a=_jsx("math","P \\\vdash q");export const b=_jsx("math","P \\vdash q");

Note the \\\vdash here, this is incorrect.

Version

1.5.3

Additional context

No response

@ayanamists ayanamists changed the title SWC incorrectly handles double backslashes in string template SWC incorrectly handles double backslashes in template literals May 7, 2024
@kdy1 kdy1 self-assigned this May 7, 2024
@kdy1 kdy1 added this to the Planned milestone May 7, 2024
@kdy1 kdy1 closed this as completed in #8934 May 8, 2024
kdy1 added a commit that referenced this issue May 8, 2024
**Description:**

I added the method to `swc_ecma_ast` to reuse it from plugins.

**Related issue:**

 - Closes #8931
@kdy1 kdy1 modified the milestones: Planned, v1.5.5 May 8, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Jun 7, 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 Jun 7, 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