-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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]: Babel removes parenthesis at place where are required. #16111
Comments
Hey @GabrielGorta! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite. |
I'm releasing a patch right now that might fix this issue |
I'm worried this PR might not be helpful. |
Checking tests in PR #16104 and it really seems it will fix the issue. |
That test isn't actually what's being fixed, but I'm concerned it's a regression. Sorry for not stating this in the PR before. |
Isn't this because I wanted to test this in REPL, but I can't find a way to set |
No, even this is unexpected when
Unfortunately there is currently no easy way. |
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: function test(bool = false) {
return /** @type {number} */bool ? 1 : 2;
} |
You can view them via |
@liuxingbaoyu Thank you, I mean integrated into the PR build, because some plugins or options could manipulate the AST (e.g. |
To be honest, I even doubt whether the plugins have good support for such an option. :) |
Hi @GabrielGorta! This issue is missing some important information we'll need to be able to reproduce this issue. Please understand that we receive a high volume of issues, and there are only a limited number of volunteers that help maintain this project. The easier it is for us to decipher an issue with the info provided, the more likely it is that we'll be able to help. Please make sure you have the following information documented in this ticket:
Please provide either a link to the problem via the |
This appears to be cause of openlayers/openlayers#15358. A reproduction can be found at https://github.com/bjornharrtell/ol-issue-15358. |
Looks like this is a regression in diff --git a/package-lock.json b/package-lock.json
index c84d63d..3f3903c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -115,7 +115,7 @@
"@babel/helper-compilation-targets": "^7.22.15",
"@babel/helper-module-transforms": "^7.23.3",
"@babel/helpers": "^7.23.2",
- "@babel/parser": "^7.23.3",
+ "@babel/parser": "^7.19.0",
"@babel/template": "^7.22.15",
"@babel/traverse": "^7.23.3",
"@babel/types": "^7.23.3",
@@ -500,7 +500,7 @@
}
},
"node_modules/@babel/parser": {
- "version": "7.23.4",
+ "version": "7.19.0",
"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.23.4.tgz",
"integrity": "sha512-vf3Xna6UEprW+7t6EtOmFpHNAuxw3xqPZghy+brsnusscJRW5BMUzzHZc5ICjULee81WeUV2jjakG09MDglJXQ==",
"dev": true,
@@ -1849,7 +1849,7 @@
"dev": true,
"dependencies": {
"@babel/code-frame": "^7.22.13",
- "@babel/parser": "^7.22.15",
+ "@babel/parser": "^7.19.0",
"@babel/types": "^7.22.15"
},
"engines": {
@@ -1868,7 +1868,7 @@
"@babel/helper-function-name": "^7.23.0",
"@babel/helper-hoist-variables": "^7.22.5",
"@babel/helper-split-export-declaration": "^7.22.6",
- "@babel/parser": "^7.23.4",
+ "@babel/parser": "^7.19.0",
"@babel/types": "^7.23.4",
"debug": "^4.1.0",
"globals": "^11.1.0" |
Amazing job! I've just donwgraded the |
@ahocevar I tried to reproduce the issue and it's kinda confusing: mkdir babel_paren
cd babel_paren Create file test.mjs: import {parse} from '@babel/parser';
import generator from '@babel/generator';
const generate = generator.default;
const code = `
function getResolutionForValueFunction(power) {
power = power || 2;
const maxResolution = this.getConstrainedResolution(this.maxResolution_);
const minResolution = this.minResolution_;
const max = Math.log(maxResolution / minResolution) / Math.log(power);
return (
/**
* @param {number} value Value.
* @return {number} Resolution.
*/
function (value) {
const resolution = maxResolution / Math.pow(power, value * max);
return resolution;
}
);
}
`;
const ast = parse(code);
const out = generate(ast,
{
compact: true,
//createParenthesizedExpressions: true,
}
);
console.log(out); First run: rm -rf node_modules package.json package-lock.json && npm i @babel/parser@7.23.3 @babel/generator@7.23.3 && node test.mjs added 12 packages, and audited 13 packages in 737ms
found 0 vulnerabilities
{
code: 'function getResolutionForValueFunction(power){power=power||2;const maxResolution=this.getConstrainedResolution(this.maxResolution_);const minResolution=this.minResolution_;const max=Math.log(maxResolution/minResolution)/Math.log(power);return(/**\n' +
' * @param {number} value Value.\n' +
' * @return {number} Resolution.\n' +
' */function(value){const resolution=maxResolution/Math.pow(power,value*max);return resolution;});}',
decodedMap: undefined,
__mergedMap: [Getter],
map: [Getter/Setter],
rawMappings: [Getter/Setter]
} Nice parentheses, right? But now run: npm i @babel/generator && node test.mjs Result up to date, audited 13 packages in 439ms
found 0 vulnerabilities
{
code: 'function getResolutionForValueFunction(power){power=power||2;const maxResolution=this.getConstrainedResolution(this.maxResolution_);const minResolution=this.minResolution_;const max=Math.log(maxResolution/minResolution)/Math.log(power);return/**\n' +
' * @param {number} value Value.\n' +
' * @return {number} Resolution.\n' +
' */function(value){const resolution=maxResolution/Math.pow(power,value*max);return resolution;};}',
decodedMap: undefined,
__mergedMap: [Getter],
map: [Getter/Setter],
rawMappings: [Getter/Setter]
} Parentheses are gone! 👀 This only happens when using |
@ahocevar @kungfooman #16111 (comment) And https://github.com/bjornharrtell/ol-issue-15358 is related to Suddenly it works fine and I'm investigating. 🤦♂️ |
I don't understand why downgrading |
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. |
I downgraded it to |
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
|
Confirmed @babel/generator@7.23.5 resolves the issue and a lockfile regen/deps cleanup will bring that for me as transitive dependency. |
💻
How are you using Babel?
Programmatic API (
babel.transform
,babel.parse
)Input code
Configuration file name
No response
Configuration
No response
Current and expected behavior
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:
#16111 (comment)
The text was updated successfully, but these errors were encountered: