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

Comments in multiline return statments can cause the wrong value to be returned #5348

Closed
Brandon-Beck opened this issue Sep 3, 2020 · 2 comments · Fixed by sveltejs/svelte-repl#133

Comments

@Brandon-Beck
Copy link

Brandon-Beck commented Sep 3, 2020

Describe the bug
Comments can alter code generation in a way that produces broken code.
This has been noticed specifically in functions
with comments that occur between the return keyword and the end of the returned statement.

Logs
No errors reported

To Reproduce
Here is a repl demonstrating the issue with comments between the null coalescing operator.
A simpler example included below for brevity

let a = undefined
  // This comment is perfectly safe.
  ?? 1
let b = (() => {
  return undefined
  // This comment breaks something in svelte's code generation
  ?? 1
})()
console.log(a) // 1
console.log(b) // undefined

Expected behavior
All functions should return 1.

Information about your Svelte project:

  • Your browser and the version: Firefox 81.0b3

  • Svelte version: REPL 3.24.1

Severity
High. The fact that a comment can silently break code is a major issue for me on the level of ditching the framework entirely.
That said, I would say impact is between moderate and low. I have not checked and identified all cases in which this will be triggered, but so long as it is only triggered in the return statement, I cannot imagine it affecting too many projects.

As it occurs with at least the null coalescing operator and the *ternary operator *, I believe it is ok to assume some projects are potentially imparted by this in the wild. Depending on what other operations and expressions trigger this bug, it could have up to a moderate impact.

So long as this issue is only present in return statements, Impact probably isn't much higher than moderate (depending on what operations and expressions are affected, could be low).
That said, projects that are affected may not notice since the issue (as presently understood) only occurs when the operators should return their secondary/fallback values.

Additional context
Originally was being used in a sort operation with the intent of being used for in a multi-select component.

Rant The number of silent bugs I have encountered are starting to pile up, so I may be leaving with this. Svelte is a very fun language to type, so I believe it can take over. But the DX, while improving daily, is still horrid imho for projects scripted with moderate complexity.

Its hard to convince myself that I am hitting corner cases with the quantity I have had to work around in the last 3 weeks of play.
And, unfortunately, I am not good enough of a developer to identify and work around compiler bugs in addition to analyzing and debugging the code I wrote this often. Mentally, I am marking svelte, the language, as beta(lots of things either needs more thorough docs or a better compiler, can't say which without reading rfcs, but assuming the latter) and svelte, the compiler, as an (mentally)unstable alpha.

@Conduitry
Copy link
Member

Probably the same as Rich-Harris/code-red#36

@tanhauhau
Copy link
Member

tanhauhau commented Sep 4, 2020

Apparently the issue in the REPL was not due to code-red, i've read through the output JS, it looks okay to me.

Surprisingly this is a bug from rollup, see Rollup repl

rollup/rollup#3761

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

Successfully merging a pull request may close this issue.

3 participants