Skip to content

Commit d371ae7

Browse files
authored
No this-property assignments in TS (microsoft#40009)
* No this-property assignments in TS Even when `this` is aliased, which I mistakenly allowed in microsoft#39908. * remove errant file
1 parent 57e2fe0 commit d371ae7

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ namespace ts {
24872487
break;
24882488
case AssignmentDeclarationKind.Property:
24892489
const expression = ((node as BinaryExpression).left as AccessExpression).expression;
2490-
if (isIdentifier(expression)) {
2490+
if (isInJSFile(node) && isIdentifier(expression)) {
24912491
const symbol = lookupSymbolForName(blockScopeContainer, expression.escapedText);
24922492
if (isThisInitializedDeclaration(symbol?.valueDeclaration)) {
24932493
bindThisPropertyAssignment(node as BindablePropertyAssignmentExpression);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [inferringClassMembersFromAssignments8.ts]
2+
// no inference in TS files, even for `this` aliases:
3+
4+
var app = function() {
5+
var _this = this;
6+
_this.swap = function() { }
7+
}
8+
var a = new app()
9+
a
10+
11+
12+
//// [inferringClassMembersFromAssignments8.js]
13+
// no inference in TS files, even for `this` aliases:
14+
var app = function () {
15+
var _this = this;
16+
_this.swap = function () { };
17+
};
18+
var a = new app();
19+
a;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/salsa/inferringClassMembersFromAssignments8.ts ===
2+
// no inference in TS files, even for `this` aliases:
3+
4+
var app = function() {
5+
>app : Symbol(app, Decl(inferringClassMembersFromAssignments8.ts, 2, 3))
6+
7+
var _this = this;
8+
>_this : Symbol(_this, Decl(inferringClassMembersFromAssignments8.ts, 3, 7))
9+
10+
_this.swap = function() { }
11+
>_this : Symbol(_this, Decl(inferringClassMembersFromAssignments8.ts, 3, 7))
12+
}
13+
var a = new app()
14+
>a : Symbol(a, Decl(inferringClassMembersFromAssignments8.ts, 6, 3))
15+
>app : Symbol(app, Decl(inferringClassMembersFromAssignments8.ts, 2, 3))
16+
17+
a
18+
>a : Symbol(a, Decl(inferringClassMembersFromAssignments8.ts, 6, 3))
19+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/conformance/salsa/inferringClassMembersFromAssignments8.ts ===
2+
// no inference in TS files, even for `this` aliases:
3+
4+
var app = function() {
5+
>app : () => void
6+
>function() { var _this = this; _this.swap = function() { }} : () => void
7+
8+
var _this = this;
9+
>_this : any
10+
>this : any
11+
12+
_this.swap = function() { }
13+
>_this.swap = function() { } : () => void
14+
>_this.swap : any
15+
>_this : any
16+
>swap : any
17+
>function() { } : () => void
18+
}
19+
var a = new app()
20+
>a : any
21+
>new app() : any
22+
>app : () => void
23+
24+
a
25+
>a : any
26+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// no inference in TS files, even for `this` aliases:
2+
3+
var app = function() {
4+
var _this = this;
5+
_this.swap = function() { }
6+
}
7+
var a = new app()
8+
a

0 commit comments

Comments
 (0)