profile
viewpoint

Ask questionsDestructure shadow mis-reference

swc version

$ grep swc package.json
    "@swc/cli": "0.1.18",
    "@swc/core": "1.0.45",
    "swc-loader": "0.1.6",

Input

function foo(bar) {
  const { foo } = bar;
  return foo;
}

Output (formatted to prevent wrapping)

function _throw(e) {
    throw e;
}
function foo(bar) {
  var ref = bar ? bar :
    _throw(new TypeError("Cannot destructure 'undefined' or 'null'")),
    foo1 = ref.foo1;
  return foo1;
}

Problem

After destructuring, the variable foo is undefined. Rather than allowing the inner foo to shadow the outer foo function, and getting its value from ref.foo, it references a non-existent ref.foo1.

swc-project/swc

Answer questions kdy1

@Austaras

why generate _throw though?

What do you mean? throw is a reserved word and throw will be changed to _throw if you target es3.

(Anyway, I found a bug. _throw is the name of a helper in swc)

useful!
source:https://uonfu.com/
Github User Rank List