Skip to content

Commit dbf3764

Browse files
committed
No this-property assignments in TS
Even when `this` is aliased, which I mistakenly allowed in #39908.
1 parent 57e2fe0 commit dbf3764

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @filename: mod1.js
5+
// @esModuleInterop: true
6+
7+
const x = function() { }
8+
module.exports.x = x
9+
// @filename: use1.js
10+
const { x } = require('./mod1')
11+
x
12+
13+
// @filename: mod2.js
14+
const x = function() { }
15+
module.exports = x
16+
// @filename: use2.js
17+
const x = require('./mod2')
18+
x
19+
20+
// @filename: mod3.js
21+
const x = function() { }
22+
export { x }
23+
// @filename: use3.js
24+
import { x } from './mod3'
25+
x
26+
27+
// @filename: mod4.js
28+
const x = function() { }
29+
export default x
30+
// @filename: use4.js
31+
import x from './mod4'
32+
x
33+

0 commit comments

Comments
 (0)