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/wasm-typescript incorrect output for return followed by a type with a newline #9878

Open
bakkot opened this issue Jan 14, 2025 · 4 comments · Fixed by #9882 · May be fixed by #9913
Open

@swc/wasm-typescript incorrect output for return followed by a type with a newline #9878

bakkot opened this issue Jan 14, 2025 · 4 comments · Fixed by #9882 · May be fixed by #9913
Assignees
Labels
Milestone

Comments

@bakkot
Copy link

bakkot commented Jan 14, 2025

Describe the bug

Type stripping has an edge case where it can have the wrong output when there is a return followed by a type with a newline, because of the no-lineterminator-here restriction after return.

Input code

function f() {
	return <T>
		(x: T)=>x;
}

Config

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

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.1&code=H4sIAAAAAAAAA0srzUsuyczPU0jT0FSo5uIsSi0pLcpTsAmx4%2BLk1KiwUgjRtLWrsOaq5QIAdBqy7yoAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BAWdSIifrKJVBTl7iUE0maH3xsz8jooBbNoeKq1PMsQkYX4nEsi2Sf8lARIOxTNJia49XaWvRrRCtVoOxpIyBOluiX3hoMNQajjLXPGmzH%2FC3VwkUnkCu4o%2BsnSVTc0JbjwXmrZDkk50qF%2FwA%2FqsvNjMPLqm4kXGrYvhlQioBQBAAA%3D&strip-types=

SWC Info output

No response

Expected behavior

The output should have the same semantics as the input. The easiest way to get this would be to add in 0, after the return as in

function f() {
	return  0,
		(x   )=>x;
}

or to move the initial parenthesis as in

function f() {
	return  (
		x   )=>x;
}

Actual behavior

Because of the newline, the return is treated as having no operator.

Version

1.7.1

Additional context

Issue comes from node: nodejs/node#56597 (comment)

Note that the same bug occurs with throw instead of return. It probably would also occur for yield but that doesn't parse at all.

@robpalme
Copy link

Given this kind of thing is already handled for async functions by bringing forward the opening parenthesis, I'd suggest doing the same here. That will also avoid the debugger stepping into the 0, value.

const f = async <
    T
>()=>0;
const f = async (
     
  )=>0;

^^^ Existing solution

https://play.swc.rs/?version=1.10.7&code=H4sIAAAAAAAAA0vOzysuUUhTsFVILK7MS1aw4VIAghAuOw1NWzsDawCz2sISHwAAAA%3D%3D&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3KFi6NA79BCIOhERAYQdqSjK3QsJpM1mv4%2Ff8yqkhIkMPOVaxrJEnQjTuReEsmf9KQhwjkgm2chw6yxTpQbtCHdoOxhgnUbk6kJSd6WaA1wIhN3RsNl6O%2BT%2FTBPmmJDoKqxS7UeH10TRUmEO72Un2y%2B179HgAT9RDzsPg6VXd3JaUGxfBMLf3xcBAAA%3D&strip-types=

@kdy1 kdy1 self-assigned this Jan 14, 2025
@kdy1 kdy1 added this to the Planned milestone Jan 14, 2025
@magic-akari magic-akari self-assigned this Jan 15, 2025
@kdy1 kdy1 removed their assignment Jan 15, 2025
kdy1 pushed a commit that referenced this issue Jan 15, 2025
@kdy1 kdy1 modified the milestones: Planned, 1.10.8 Jan 19, 2025
@marco-ippolito
Copy link
Contributor

I can confirm this issue also applies to yield and throw and has not be fully fixed:
nodejs/amaro#154

@kdy1 kdy1 reopened this Jan 21, 2025
@kdy1 kdy1 modified the milestones: 1.10.8, Planned Jan 21, 2025
@kdy1
Copy link
Member

kdy1 commented Jan 21, 2025

@magic-akari Can you take a look?

@robpalme
Copy link

throw <
   T
>(v: T) => v;

You may wish to reuse the test cases that were added for this issue in ts-blank-space

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