Skip to content

Commit 9b1dd14

Browse files
authored
Merge pull request microsoft#13708 from Microsoft/propertyAssignment-is-not-assignment-target
Property assignment is not an assignment target
2 parents 64dd806 + cabcaaa commit 9b1dd14

5 files changed

+78
-3
lines changed

src/compiler/utilities.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1698,11 +1698,15 @@ namespace ts {
16981698
node = parent;
16991699
break;
17001700
case SyntaxKind.ShorthandPropertyAssignment:
1701-
if ((<ShorthandPropertyAssignment>parent).name !== node) {
1701+
if ((parent as ShorthandPropertyAssignment).name !== node) {
17021702
return AssignmentKind.None;
17031703
}
1704-
// Fall through
1704+
node = parent.parent;
1705+
break;
17051706
case SyntaxKind.PropertyAssignment:
1707+
if ((parent as ShorthandPropertyAssignment).name === node) {
1708+
return AssignmentKind.None;
1709+
}
17061710
node = parent.parent;
17071711
break;
17081712
default:
@@ -1714,7 +1718,8 @@ namespace ts {
17141718

17151719
// A node is an assignment target if it is on the left hand side of an '=' token, if it is parented by a property
17161720
// assignment in an object literal that is an assignment target, or if it is parented by an array literal that is
1717-
// an assignment target. Examples include 'a = xxx', '{ p: a } = xxx', '[{ p: a}] = xxx'.
1721+
// an assignment target. Examples include 'a = xxx', '{ p: a } = xxx', '[{ a }] = xxx'.
1722+
// (Note that `p` is not a target in the above examples, only `a`.)
17181723
export function isAssignmentTarget(node: Node): boolean {
17191724
return getAssignmentTargetKind(node) !== AssignmentKind.None;
17201725
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts]
2+
// test for #10668
3+
function qux(bar: { value: number }) {
4+
let foo: number;
5+
({ value: foo } = bar);
6+
let x = () => bar;
7+
}
8+
9+
10+
11+
//// [destructuringPropertyAssignmentNameIsNotAssignmentTarget.js]
12+
// test for #10668
13+
function qux(bar) {
14+
var foo;
15+
(foo = bar.value);
16+
var x = function () { return bar; };
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts ===
2+
// test for #10668
3+
function qux(bar: { value: number }) {
4+
>qux : Symbol(qux, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 0, 0))
5+
>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13))
6+
>value : Symbol(value, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 19))
7+
8+
let foo: number;
9+
>foo : Symbol(foo, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 2, 7))
10+
11+
({ value: foo } = bar);
12+
>value : Symbol(value, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 3, 6))
13+
>foo : Symbol(foo, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 2, 7))
14+
>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13))
15+
16+
let x = () => bar;
17+
>x : Symbol(x, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 4, 7))
18+
>bar : Symbol(bar, Decl(destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts, 1, 13))
19+
}
20+
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/destructuringPropertyAssignmentNameIsNotAssignmentTarget.ts ===
2+
// test for #10668
3+
function qux(bar: { value: number }) {
4+
>qux : (bar: { value: number; }) => void
5+
>bar : { value: number; }
6+
>value : number
7+
8+
let foo: number;
9+
>foo : number
10+
11+
({ value: foo } = bar);
12+
>({ value: foo } = bar) : { value: number; }
13+
>{ value: foo } = bar : { value: number; }
14+
>{ value: foo } : { value: number; }
15+
>value : number
16+
>foo : number
17+
>bar : { value: number; }
18+
19+
let x = () => bar;
20+
>x : () => { value: number; }
21+
>() => bar : () => { value: number; }
22+
>bar : { value: number; }
23+
}
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// test for #10668
2+
function qux(bar: { value: number }) {
3+
let foo: number;
4+
({ value: foo } = bar);
5+
let x = () => bar;
6+
}
7+

0 commit comments

Comments
 (0)