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

TypeScript cast with comment breaks return evaluation #8004

Closed
JasonGore opened this issue Sep 25, 2023 · 3 comments · Fixed by #8005
Closed

TypeScript cast with comment breaks return evaluation #8004

JasonGore opened this issue Sep 25, 2023 · 3 comments · Fixed by #8005
Assignees
Labels
Milestone

Comments

@JasonGore
Copy link

JasonGore commented Sep 25, 2023

Describe the bug

Adding a comment to a parantheses wrapped return evaluation with embedded comment ends up stripping the wrapping parantheses and breaking the return evaluation.

Input code

const string1 = { errorCode: 'test' };
const string2 = 'test';

function evaluateWithoutComment() {
  return string1.errorCode === string2;
}

function evaluateWithComment() {
  return ( 
    // This comment doesn't break return evaluation
    string1?.errorCode === string2
  );
}

function evaluateWithCast() {
  return (
    (string1 as { errorCode: string })?.errorCode === string2
  );
}

function evaluateWithCastAndComment() {
  return (
    // This comment seems to cause wrapping parantheses to get stripped, breaking the return evaluation
    (string1 as { errorCode: string })?.errorCode === string2
  );
}

// Works, returns true
console.log(`evaluateWithoutComment: ${evaluateWithoutComment()}`);
// Works, returns true
console.log(`evaluateWithComment: ${evaluateWithComment()}`);
// Works, returns true
console.log(`evaluateWithCast: ${evaluateWithCast()}`);
// Breaks, returns undefined due to stripped parantheses
console.log(`evaluateWithCastAndComment: ${evaluateWithCastAndComment()}`);

Config

{
  "jsc": {
    "transform": {
      "decoratorVersion": "2022-03"
    },
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2021",
    "loose": false,
  },
  "module": {
    "type": "commonjs"
  },
  "isModule": true,
  "minify": false
}

Playground link

https://play.swc.rs/?version=1.3.89&code=H4sIAAAAAAAAA6WSwW6DMBBE73zFHioBUpSoPQahqs0vVMo1Lt4ACtjIXrcHxL%2FXGNzS1kRqc7Ps4e0wO4UUmkCTqkV5Dzn0gEpJdZAc9xATaophyKJiIXuwsukli6KzEQXVUgC%2BscYwwmNNlTR0kG2LgpIU%2BghAIRkl%2FJjt5wjI89xTs2hYwQVZCdgzwG4HL1WtoZg0wCVqERO8KmQXr51hluu%2BmW08hn1YSXrFC9M%2FjThm4iNk%2BnuG0z0M6b%2FHPQkeTiAYgEZsNZCEghmN8K5Y140GOqaYoAo1utcSp3V2HfLNlNaosoKV0G7%2FQev0KNVFb%2BYJ1ocy6KolG9w2skxO4Rbt4a5f69dwsvS%2Fole4N0Ltrn4RXV087nmMecEzguO5FsiBGxy34jey3Nb1gV%2FlCI1eVseZ%2BADOKfkp7wMAAA%3D%3D&config=H4sIAAAAAAAAA1WPPQ6DMAyF954CeaYSpVvv0LV7FEwVROLINlIR4u5NoEF0s997n3%2BWS1XBIBYe1ZLK1CibID2xP6QkdmiJjRK%2FkMVRSB60TdtemztsmbXe6WhYkM%2BozEHNJwM6RxTLLirUxVXJVm9Gwb85aviNmimUtOj2I2AkEixE1jYAPHXTiKcn0qoMW%2FKewiBwJJ08S1Z5wp12wfXzccf6BfgI7zMVAQAA

SWC Info output

No response

Expected behavior

The wrapped parantheses are preserved so that the return evaluation evaulates to true similar to the other cases.

Actual behavior

As can be seen in evaluateWithCastAndComment, the stripped parantheses end up returning undefined.

Version

1.3.89

Additional context

No response

@JasonGore
Copy link
Author

I have to fix the repro here. In simplifying it I left a bit that should have been removed. Will update ASAP.

@JasonGore
Copy link
Author

Updated.

@kdy1 kdy1 added this to the Planned milestone Sep 26, 2023
@kdy1 kdy1 self-assigned this Sep 26, 2023
kdy1 added a commit that referenced this issue Sep 26, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.90 Sep 27, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 27, 2023

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 Oct 27, 2023
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