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

Fix: mv wrapReturn behind transform #383

Merged

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! 👋"

    )
    })

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
3 checks passed
@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