Skip to content

Commit 83ca053

Browse files
committed
fix(core): clear stale lazy pattern errors after assignment
Reset lazyBindingPos once parseMaybeAssign converts a construct to an assignment, matching Acorn's shorthandAssign protocol, so parenthesized forms such as `({ pair: &{ a } } = obj)` parse as valid targets.
1 parent 6182e1d commit 83ca053

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎packages/tsrx/src/plugin.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,29 @@ export function TSRXPlugin(config) {
32693269
return expr;
32703270
}
32713271

3272+
/**
3273+
* Acorn resets `shorthandAssign` once `parseMaybeAssign` converts the
3274+
* left-hand side through `toAssignable`. Do the same for a recorded
3275+
* lazy binding pattern so enclosing `checkExpressionErrors` calls
3276+
* (parenthesized assignment, call arguments, …) do not treat a valid
3277+
* target as an expression-position error.
3278+
*
3279+
* @type {Parse.Parser['parseMaybeAssign']}
3280+
*/
3281+
parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse) {
3282+
const expr = super.parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse);
3283+
if (
3284+
expr &&
3285+
expr.type === 'AssignmentExpression' &&
3286+
refDestructuringErrors &&
3287+
get_lazy_binding_pos(refDestructuringErrors) >=
3288+
/** @type {AST.NodeWithLocation} */ (expr.left).start
3289+
) {
3290+
refDestructuringErrors.lazyBindingPos = -1;
3291+
}
3292+
return expr;
3293+
}
3294+
32723295
/**
32733296
* A recorded lazy binding pattern that never became a binding or
32743297
* assignment target is a pending error, exactly like `{a = b}` shorthand

‎packages/tsrx/tests/utils/parser.test.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,19 @@ foo();`;
32173217
const assignment = as_type(statement.expression, 'AssignmentExpression');
32183218
const target = as_type(assignment.left, 'ArrayPattern');
32193219
expect(as_type(target.elements[0], 'ObjectPattern').lazy).toBe(true);
3220+
3221+
// Object destructuring assignment must be parenthesized (a leading `{` is a
3222+
// block). The shared DestructuringErrors context is still live after the
3223+
// `=` conversion, so the pending lazy-pattern flag must be cleared.
3224+
const object_ast = parseModule(`({ pair: &{ a } } = obj);`, 'App.tsrx');
3225+
const object_assignment = as_type(
3226+
firstStatement(object_ast, 'ExpressionStatement').expression,
3227+
'AssignmentExpression',
3228+
);
3229+
const object_target = as_type(object_assignment.left, 'ObjectPattern');
3230+
expect(
3231+
as_type(as_type(object_target.properties[0], 'Property').value, 'ObjectPattern').lazy,
3232+
).toBe(true);
32203233
});
32213234

32223235
it('parses lazy binding patterns as for-of and for-in loop targets', () => {

0 commit comments

Comments
 (0)