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(ts): parenthesized assert and assign #12933
fix(ts): parenthesized assert and assign #12933
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/44539/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 62dc9f1:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that with the TS plugin and createParenthesizedExpression
({}) = x
still throws?
It broke that, what about adding a check like this to the superclass' isParenthesizedAssignable(node) {
const type = node.type
return (
type === "TSParameterProperty" ||
type === "TSAsExpression" ||
... ||
super.isParenthesizedAssignable(node)
)
} |
Co-Authored-By: Nicolò Ribaudo <7000710+nicolo-ribaudo@users.noreply.github.com>
case "ParenthesizedExpression": | ||
if ( | ||
node.expression.type === "TSAsExpression" || | ||
node.expression.type === "ParenthesizedExpression" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess TSNonNullExpression
and TSTypeAssertion
should be here too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried running some tests on those, but they work like magic😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like @thorn0 said, we still need to check for TSNonNullExpression
and TSTypeAssertion
. It the test is passing maybe there isn't a test which enables createParenthesizedExpressions
. Clearly Babel parser 7.12 fails: https://astexplorer.net/#/gist/910487b331a19aac83c5489fa4203e66/4180181cff60d53a164ab16afb82a8e6e7d5698c
The error is thrown from
this.raise(node.start, Errors.InvalidParenthesizedAssignment); |
TSNonNullExpression
, TSAsExpression
and TSTypeAssertion
plays a role of transparent expression wrapper, which should have been unwrapped before check. Consider handle this as we did in
babel/packages/babel-helper-skip-transparent-expression-wrappers/src/index.js
Lines 11 to 18 in efdca01
export function isTransparentExprWrapper(node: Node) { | |
return ( | |
t.isTSAsExpression(node) || | |
t.isTSTypeAssertion(node) || | |
t.isTSNonNullExpression(node) || | |
t.isTypeCastExpression(node) || | |
t.isParenthesizedExpression(node) | |
); |
I think we can also simplifies how we handle toAssignable
in the typescript plugin using this helper.
Maybe I got confused and I tested it with this code which does almost the same of that helper. |
Can you add following test cases with (a!) = null;
(<string>a) = null; Like Georgii mentioned before, they should not throw. |
Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge this in 15 mins, as soon as the release CI job on main
finishes
* Throw errors for invalid left-hand side assignment * Fix tests * FIx tests * Add tests from babel/babel#12933
We treat
ParenthesizedExpression
node as aTSAsExpression
in#toAssignable()
so that the function recurses one more time.