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

bug: nullish coalescing with function invokation is not properly compiled #7977

Closed
layershifter opened this issue Sep 20, 2023 · 1 comment · Fixed by #7980
Closed

bug: nullish coalescing with function invokation is not properly compiled #7977

layershifter opened this issue Sep 20, 2023 · 1 comment · Fixed by #7980
Labels
Milestone

Comments

@layershifter
Copy link

layershifter commented Sep 20, 2023

Describe the bug

The problem appears on ArrowFunctionExpression that don't have BlockStatement:

const foo = () => baz() ?? qux;

SWC result 🟥

var _baz; // <<<< variable is in a module scope 💣 
const foo = ()=>(_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;

Babel 🟢

const foo = () => {
  var _baz;
  return (_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;
};

TS 🟢

const foo = () => {
    var _baz;
    return (_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;
};

Input code

const foo = () => baz() ?? qux;

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "dynamicImport": true
    },
    "transform": {
      "react": {
        "runtime": "automatic"
      }
    },
    "baseUrl": "./",
    "target": "es2019",
    "keepClassNames": true,
    "minify": {
      "compress": false
    }
  },
  "isModule": true,
  "module": {
    "type": "es6"
  },
  "minify": false
}

Playground link

https://play.swc.rs/?version=1.3.86&code=H4sIAAAAAAAAA0vOzysuUUjLz1ewVdDQVLC1U0hKrAIy7O0VCksrrAEQVEH%2BHwAAAA%3D%3D&config=H4sIAAAAAAAAA1WQTU7EMAyF93OKymKJWmCBxGxZsYAdBzAZdyZDnUS2K6hGvTtJ%2F1R28ffs9xzfDlUFd%2BouxAjHCi5mSY9Nc9UY6hmrRaE6yrnRHycO7svIVV1uv%2BVnLhKKkmx1JjoEw99iaEMideKTTYOTalqkFjuljZ2GgOzdG6collWTniZtnFvABIO2UXifI4TOdqCgPphnKtnYW2Q072CRx3%2BOX6j0KV3prBtYY1DOVCyB9Onh8WXl30TptUPVD2TSZcFFYx98O%2Bz3cpGTkOr6zcOaPmWD1%2Fd46jva2QCvZDlqOdy8xjNsg1vS7Dr%2BAd6pGL%2B%2FAQAA

SWC Info output

N/A

Expected behavior

As in Babel & TS, the variable should be created in a scope of function instead of module scope i.e. the following result:

const foo = () => {
    var _baz;
    return (_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;
};

Actual behavior

var _baz; // <<<< variable is in a module scope 💣 
const foo = ()=>(_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;

The variable will be created in a module scope, this creates memory leaks if baz() returns an object that references other objects.

Note: the following snippet is compiled properly:

const foo = () => { return baz() ?? qux; }
// ⬇️⬇️⬇️
const foo = ()=>{
    var _baz;
    return (_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;
};

Version

1.3.86

Additional context

No response

@swc-bot
Copy link
Collaborator

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