babel: [Bug]: Babel removes parenthesis at place where are required.
đź’»
- Would you like to work on a fix?
How are you using Babel?
Programmatic API (babel.transform
, babel.parse
)
Input code
import something from 'something';
export function myfunction(bool) {
return /** @type {SomeType} */ (
bool ? something.a() : something.b()
)
}
Configuration file name
No response
Configuration
No response
Current and expected behavior
const something = require('something');
exports.myfunction = myfunction;
function myfunction(bool) {
return; /** @type {SomeType} */
bool ? something.a() : something.b()
}
Environment
System: OS: Windows 11 10.0.22621 Binaries: Node: 18.6.0 - C:\Program Files\nodejs\node.EXE npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD npmPackages: @babel/cli: ^7.23.0 => 7.23.0 @babel/core: ^7.23.2 => 7.23.2 @babel/plugin-transform-modules-commonjs: ^7.23.3 => 7.23.3 @babel/preset-env: ^7.23.3 => 7.23.3 @babel/traverse: ^7.23.3 => 7.23.3 eslint: ^8.53.0 => 8.53.0 jest: ^29.7.0 => 29.7.0
Possible solution
No response
Additional context
The issue happens when there is /** @type {SomeType} */
in return statement, or in assigment. Causing either in invalid return, or even if code itself works, in output files, TypeScript throws error because of invalid types (as without parenthesis it doesn’t apply type cast on whole expression).
reproduction in comment below:
https://github.com/babel/babel/issues/16111#issuecomment-1825331645
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 23 (10 by maintainers)
@ahocevar @kungfooman Thank you for your detailed report! I think there are two problems here.
https://github.com/babel/babel/issues/16111#issuecomment-1825514962 Regression from
@babel/generator@7.23.4
, I can reproduce it, will fix it.And https://github.com/bjornharrtell/ol-issue-15358 is related to
@babel/parser
. <del>I get the error, but even if I downgrade@babel/parser
it doesn’t work.🤔<del>Suddenly it works fine and I’m investigating. 🤦‍♂️
@ahocevar I tried to reproduce the issue and it’s kinda confusing:
Create file test.mjs:
First run:
Nice parentheses, right?
But now run:
Result
Parentheses are gone! đź‘€
This only happens when using
{compact: true}
as generate option.Looks like this is a regression in
@babel/parser
. Downgrading to@babel/parser@7.19.0
(by manually editing the lockfile) makes the problem go away:This appears to be cause of https://github.com/openlayers/openlayers/issues/15358. A reproduction can be found at https://github.com/bjornharrtell/ol-issue-15358.
I’m releasing a patch right now that might fix this issue
Confirmed @babel/generator@7.23.5 resolves the issue and a lockfile regen/deps cleanup will bring that for me as transitive dependency.
FYI for anyone who might have started seeing the below error in their react application over the last week. This bug is the cause and it is fixed in
@babel/generator@7.23.5
Error: Minified React error #152; visit https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=f
Confirmed that downgrading to @babel/generator@7.23.0 resolves https://github.com/bjornharrtell/ol-issue-15358. Note that downgrading to @babel/generator@7.23.3 does not appear to be enough.
@liuxingbaoyu Thank you, I mean integrated into the PR build, because some plugins or options could manipulate the AST (e.g.
createParenthesizedExpressions
)Too bad, I would also like a “Show AST” option to quickly judge what’s going on.
Try a single-line type cast comment, it doesn’t work: REPL
Output:
No, even this is unexpected when
creatingParenthesizedExpressions: false
. This is different in AST.Unfortunately there is currently no easy way.