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

Simple downlevelled optional chain generates an extra, unnecessary variable #7921

Closed
bradzacher opened this issue Sep 7, 2023 · 2 comments · Fixed by #7933
Closed

Simple downlevelled optional chain generates an extra, unnecessary variable #7921

bradzacher opened this issue Sep 7, 2023 · 2 comments · Fixed by #7933
Assignees
Labels
Milestone

Comments

@bradzacher
Copy link

bradzacher commented Sep 7, 2023

Describe the bug

It looks like SWC's logic is defined to always add a variable for optional chaining, even when it's unnecessary.

This generates a number of extra bytes that could otherwise be avoided.

Input code

export function foo(arg) {
  arg?.foo();
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2019",
    "transform": {
      "optimizer": {
        "simplify": true
      }
    }
  },
  "sourceMaps": true
}

Playground link

https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAA0utKMgvKlFIK81LLsnMz1NIy8%2FXSCxK11So5lJQADLs9UAimtZctVwAufDgFisAAAA%3D&config=H4sIAAAAAAAAA11Su27DMAzc8xWG5gxthwLt2BhBO3TqFxAKHbDQCyQF2A3y75Xs2HG8COIddTySuuyaxlBwFPAnZrYohxgUg5r3RjnjvvK%2FYkt4KdcSYK%2FIAdwnuoQsq7xCJmBBXpILIkNQ6AtidEgolimp2c%2FsCW1k0LjRqcwQwJP98imybkmV%2FgaNyPVWXYHPWJMNysvT85uZcYYgXWS%2FNpYFW%2BxK38fIBwciR0J3qj46cHKv5fAMdmhno1srjGB1pVuhHJQ8Vh%2BQNXpQskvLU%2BWPTE4pyLqLpY%2BSElNRoL%2BHUdZhkk%2BOumHzbDefo4Dx8ZQd3jdG5xAZ22mgj%2FuqO5nm9WqW9zJ%2BhG9Ii73rP1R%2Fu4gnAgAA

SWC Info output

No response

Expected behavior

Output should be

export function foo(arg) {
    arg === null || arg === void 0 ? void 0 : arg.foo();
}

Actual behavior

Output is

export function foo(arg) {
    var _arg;
    (_arg = arg) === null || _arg === void 0 ? void 0 : _arg.foo();
}

Version

1.3.74

Additional context

No response

@bradzacher
Copy link
Author

bradzacher commented Sep 7, 2023

Why is this a problem?

  1. It generates a lot more bytes than are necessary
  2. In some cases it causes even more code to be generated than necessary. EG in a bodyless arrow function SWC will insert a body when it doesn't need to:
// input
export const foo = (arg) => arg?.foo();

// expected output
export const foo = (arg) => arg === null || arg === void 0 ? void 0 : arg.foo();

// actual output
export const foo = (arg)=>{
    var _arg;
    return (_arg = arg) === null || _arg === void 0 ? void 0 : _arg.foo();
};

playground

@kdy1 kdy1 added this to the Planned milestone Sep 7, 2023
@kdy1 kdy1 self-assigned this Sep 7, 2023
kdy1 pushed a commit that referenced this issue Sep 13, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.85 Sep 15, 2023
@swc-bot
Copy link
Collaborator

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