Skip to content

Commit 598f998

Browse files
committed
fix(core): look through TypeScript wrappers in the lazy pattern walk
pattern_position_contains_lazy did not follow TSAsExpression, TSTypeAssertion, or TSNonNullExpression, so a lazy pattern in a real pattern slot wrapped in a type or non-null assertion was treated as an expression use. toAssignable then left lazyBindingPos set, and checkExpressionErrors rejected assignments that conversion accepts.
1 parent b839985 commit 598f998

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎packages/tsrx/src/plugin.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ function pattern_position_contains_lazy(node, pos) {
165165
case 'RestElement':
166166
return pattern_position_contains_lazy(node.argument, pos);
167167
case 'ParenthesizedExpression':
168-
return pattern_position_contains_lazy(node.expression, pos);
168+
case 'TSAsExpression':
169+
case 'TSTypeAssertion':
170+
case 'TSNonNullExpression':
171+
return pattern_position_contains_lazy(
172+
/** @type {{ expression: AST.Node }} */ (node).expression,
173+
pos,
174+
);
169175
default:
170176
return false;
171177
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,6 +3252,30 @@ foo();`;
32523252
expect(() => parseModule(`({ k: &{ a }.b } = obj);`, 'App.tsrx')).toThrow(
32533253
/Lazy binding patterns are only valid as binding or assignment targets/,
32543254
);
3255+
expect(() => parseModule(`((&{ a }!).b = x);`, 'App.tsrx')).toThrow(
3256+
/Lazy binding patterns are only valid as binding or assignment targets/,
3257+
);
3258+
expect(() => parseModule(`({ k: &{ a }!.b } = obj);`, 'App.tsrx')).toThrow(
3259+
/Lazy binding patterns are only valid as binding or assignment targets/,
3260+
);
3261+
});
3262+
3263+
it('parses lazy binding patterns wrapped in TypeScript non-null assertions', () => {
3264+
// Non-null assertions are valid assignment targets; nested conversion
3265+
// does not pass refDestructuringErrors, so the walk must look through
3266+
// those wrappers itself.
3267+
expect(() => parseModule(`([&{ a }!] = pairs);`, 'App.tsrx')).not.toThrow();
3268+
expect(() => parseModule(`({ pair: &{ a }! } = obj);`, 'App.tsrx')).not.toThrow();
3269+
expect(() => parseModule(`(&{ name }! = obj);`, 'App.tsrx')).not.toThrow();
3270+
3271+
const ast = parseModule(`({ pair: &{ a }! } = obj);`, 'App.tsrx');
3272+
const statement = firstStatement(ast, 'ExpressionStatement');
3273+
const assignment = as_type(statement.expression, 'AssignmentExpression');
3274+
const target = as_type(assignment.left, 'ObjectPattern');
3275+
const pair = as_type(target.properties[0], 'Property');
3276+
expect(
3277+
as_type(as_type(pair.value, 'TSNonNullExpression').expression, 'ObjectPattern').lazy,
3278+
).toBe(true);
32553279
});
32563280

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

0 commit comments

Comments
 (0)