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 removes the backslash "\" when target is "es5" even if "minify" option is not set #8871

Closed
SoonIter opened this issue Apr 17, 2024 · 5 comments · Fixed by #8873
Closed
Assignees
Labels
Milestone

Comments

@SoonIter
Copy link

SoonIter commented Apr 17, 2024

Describe the bug

The backslash was deleted when I was using swc, causing a syntax error in the js I inserted into the script tag of html.

<html>
   <script>
      var x = "</script></script>
</html>

Babel or TypeScript doesn't seem to have this behavior, which caused string to be modified before minimizing.

typescript-playground

babel-playground

Additionally, terser automatically avoids this error, and I am unaware whether it is a good behavior and whether it is worth for swc to follow.

// terser
// input
var x = "</" + "script>";
var y = "<\/script>";
var z = "</script>"
var h = "\/\/"
console.log(x, y, z, h);
// output
console.log("<\/script>","<\/script>","<\/script>","//");

Input code

const x = "</" + "script>"
const y = "<\/script>"
const z = "\/\/   \\"

export { x, y ,z }

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "loose": true,
    "minify": {
      "compress": false,
      "mangle": false
    },
    "transform": {
      "react": {
        "runtime": "classic",
        "development": false
      }
    },
    "target": "es5"
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.15-nightly-20240416.4&code=H4sIAAAAAAAAA0vOzysuUahQsFVQstFXUtBWUCpOLsosKLFT4koGS1WCpWL00YSrQMIx%2BjH6CgoKMTFKXFypFQX5RSUK1QoVOkA9OlUKtQB9gcrVWwAAAA%3D%3D&config=H4sIAAAAAAAAA21Qyw6DMAy78xUo5123w%2F5hH1GVgDr1pSRMQ4h%2FXwu0KtJuje3aideu7%2BHNGp79mp5piIoYqc4J4cWL%2BiYEZInImkwUuBVWOFOjsow7tB0M2BAYEyM04wk54824tNY6uEjIXByqq1N%2BsvjXWEh5HgO51ohQaWmADM1ejMseoK1iNroundgBP2hDdOjlkpJyrmmKJswSQL5DdxLgwjDv%2B52t5WYO0aMRlXvrbWD4VX7mYrrtB6t7kgaBAQAA

SWC Info output

No response

Expected behavior

Same as babel, backslash will not be deleted.

var x = "</" + "script>";
var y = "<\/script>";
var z = "\/\/   \\";

Actual behavior

var x = "</" + "script>";
var y = "</script>";
var z = "//   \\";

Version

1.4.15-nightly-20240416.4

Additional context

No response

@SoonIter SoonIter added the C-bug label Apr 17, 2024
@kdy1 kdy1 added this to the Planned milestone Apr 17, 2024
@kdy1 kdy1 self-assigned this Apr 17, 2024
@SoonIter
Copy link
Author

SoonIter commented Apr 17, 2024

Additionally, terser automatically avoids this error, and I am unaware whether it is a good behavior and whether it is worth for swc to follow.

related links, it has already been realized with jsc.minify.format.inlineScript
#8252
#7602
#7602 (comment)

But it doesn't seem to work in swc-playground now🤔. I guess it only works in swc_core rust api?

swc-playground

@noyobo
Copy link

noyobo commented Apr 18, 2024

It's really expected that the contents of the original string will be preserved. The developer knows what they are writing and why they are writing it, and does not want the content to be removed. #8863 even if they are equivalent。

@kdy1
Copy link
Member

kdy1 commented Apr 18, 2024

It should work in playground, and I investigated:

Investigation

The problem is that we drop Str.raw somewhere, and an invalid backslash is removed from Str.value.

@kdy1 kdy1 removed their assignment Apr 18, 2024
@kdy1
Copy link
Member

kdy1 commented Apr 18, 2024

Lowering priority as the behavior of swc is not wrong. If it causes a problem for real world app, please provide some details about the usecase.

@kdy1 kdy1 self-assigned this Apr 18, 2024
@kdy1 kdy1 closed this as completed in c7a06b1 Apr 23, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.17 Apr 23, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.17 Apr 23, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented May 23, 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 23, 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.

4 participants