feat(es/parser): Reject indirect opt chain in assignment (#4447)

This commit is contained in:
Pig Fang 2022-04-27 11:07:57 +08:00 committed by GitHub
parent 20f35cbcbd
commit df7b3f611a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 1 deletions

View File

@ -269,7 +269,13 @@ pub(super) trait ExprExt {
expr.is_valid_simple_assignment_target(strict)
}
Expr::Member(..) | Expr::SuperProp(..) => true,
Expr::Member(MemberExpr { ref obj, .. }) => match obj.as_ref() {
Expr::Member(..) => obj.is_valid_simple_assignment_target(strict),
Expr::OptChain(..) => false,
_ => true,
},
Expr::SuperProp(..) => true,
Expr::New(..) | Expr::Call(..) => false,
// TODO: Spec only mentions `new.target`

View File

@ -0,0 +1,6 @@
x The left-hand side of an assignment expression must be a variable or a property access.
,-[$DIR/tests/typescript-errors/optional-chaining/indirect-assign/input.ts:1:1]
1 | a.b.c?.d.e[f] = 0;
: ^^^^^^^^^^^^^
`----

View File

@ -0,0 +1 @@
obj[0]['foo'] = 1

View File

@ -0,0 +1,100 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 17,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 17,
"ctxt": 0
},
"expression": {
"type": "AssignmentExpression",
"span": {
"start": 0,
"end": 17,
"ctxt": 0
},
"operator": "=",
"left": {
"type": "MemberExpression",
"span": {
"start": 0,
"end": 13,
"ctxt": 0
},
"object": {
"type": "MemberExpression",
"span": {
"start": 0,
"end": 6,
"ctxt": 0
},
"object": {
"type": "Identifier",
"span": {
"start": 0,
"end": 3,
"ctxt": 0
},
"value": "obj",
"optional": false
},
"property": {
"type": "Computed",
"span": {
"start": 3,
"end": 6,
"ctxt": 0
},
"expression": {
"type": "NumericLiteral",
"span": {
"start": 4,
"end": 5,
"ctxt": 0
},
"value": 0.0,
"raw": "0"
}
}
},
"property": {
"type": "Computed",
"span": {
"start": 6,
"end": 13,
"ctxt": 0
},
"expression": {
"type": "StringLiteral",
"span": {
"start": 7,
"end": 12,
"ctxt": 0
},
"value": "foo",
"raw": "'foo'"
}
}
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 16,
"end": 17,
"ctxt": 0
},
"value": 1.0,
"raw": "1"
}
}
}
],
"interpreter": null
}