Skip to content

Fix: mv wrapReturn behind transform#383

Merged
carbonrobot merged 1 commit intoFormidableLabs:masterfrom
childrentime:fix/early-wrapReturn
Feb 5, 2024
Merged

Fix: mv wrapReturn behind transform#383
carbonrobot merged 1 commit intoFormidableLabs:masterfrom
childrentime:fix/early-wrapReturn

Conversation

@childrentime
Copy link
Contributor

Solve this #381

image

When Sucrase compiles the optional chaining syntax, it generates a function called _optionalChain. If we wrap it with a return statement during the first transform, it can lead to certain issues.

This is the output after the first transform and wrapping with a return. Clearly, it's no longer a valid JavaScript syntax. To solve this problem, we can move the wrapReturn to after the second transform

return (function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }function Demo() {
    console.log('???',_optionalChain(['1', 'optionalAccess', _ => _.toString, 'call', _2 => _2()]));
    return React.createElement('h3', { style: {
      background: 'darkslateblue',
      color: 'white',
      padding: 8,
      borderRadius: 4
    }, __self: this, __source: {fileName: _jsxFileName, lineNumber: 3}}, "Hello World! 👋"

    )
    })

@vercel
Copy link

vercel bot commented Feb 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-live-docs-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 3, 2024 0:15am

@childrentime
Copy link
Contributor Author

@keithluchtel Could you help review this please💘

@carbonrobot carbonrobot added the bug label Feb 5, 2024
@carbonrobot carbonrobot merged commit 2794a3b into FormidableLabs:master Feb 5, 2024
@carbonrobot carbonrobot mentioned this pull request Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants