Skip to content

Commit 135cc12

Browse files
authored
Merge pull request #9574 from Microsoft/fix-union-of-salsa-property-types
Fix type union of differing Salsa assignment-properties
2 parents 37eac5f + f84b731 commit 135cc12

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,17 +3166,17 @@ namespace ts {
31663166
}
31673167

31683168
let type: Type = undefined;
3169-
// Handle module.exports = expr or this.p = expr
3170-
if (declaration.kind === SyntaxKind.BinaryExpression) {
3171-
type = getUnionType(map(symbol.declarations, (decl: BinaryExpression) => checkExpressionCached(decl.right)));
3172-
}
3173-
else if (declaration.kind === SyntaxKind.PropertyAccessExpression) {
3174-
// Declarations only exist for property access expressions for certain
3175-
// special assignment kinds
3176-
if (declaration.parent.kind === SyntaxKind.BinaryExpression) {
3177-
// Handle exports.p = expr or className.prototype.method = expr
3178-
type = checkExpressionCached((<BinaryExpression>declaration.parent).right);
3179-
}
3169+
// Handle certain special assignment kinds, which happen to union across multiple declarations:
3170+
// * module.exports = expr
3171+
// * exports.p = expr
3172+
// * this.p = expr
3173+
// * className.prototype.method = expr
3174+
if (declaration.kind === SyntaxKind.BinaryExpression ||
3175+
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
3176+
type = getUnionType(map(symbol.declarations,
3177+
decl => decl.kind === SyntaxKind.BinaryExpression ?
3178+
checkExpressionCached((<BinaryExpression>decl).right) :
3179+
checkExpressionCached((<BinaryExpression>decl.parent).right)));
31803180
}
31813181

31823182
if (type === undefined) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [input.js]
2+
3+
function C() {
4+
this.m = null;
5+
}
6+
C.prototype.m = function() {
7+
this.nothing();
8+
};
9+
10+
11+
//// [output.js]
12+
function C() {
13+
this.m = null;
14+
}
15+
C.prototype.m = function () {
16+
this.nothing();
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/salsa/input.js ===
2+
3+
function C() {
4+
>C : Symbol(C, Decl(input.js, 0, 0))
5+
6+
this.m = null;
7+
>m : Symbol(C.m, Decl(input.js, 1, 14), Decl(input.js, 3, 1))
8+
}
9+
C.prototype.m = function() {
10+
>C.prototype : Symbol(C.m, Decl(input.js, 1, 14), Decl(input.js, 3, 1))
11+
>C : Symbol(C, Decl(input.js, 0, 0))
12+
>prototype : Symbol(Function.prototype, Decl(lib.d.ts, --, --))
13+
>m : Symbol(C.m, Decl(input.js, 1, 14), Decl(input.js, 3, 1))
14+
15+
this.nothing();
16+
};
17+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/salsa/input.js ===
2+
3+
function C() {
4+
>C : () => void
5+
6+
this.m = null;
7+
>this.m = null : null
8+
>this.m : any
9+
>this : any
10+
>m : any
11+
>null : null
12+
}
13+
C.prototype.m = function() {
14+
>C.prototype.m = function() { this.nothing();} : () => void
15+
>C.prototype.m : any
16+
>C.prototype : any
17+
>C : () => void
18+
>prototype : any
19+
>m : any
20+
>function() { this.nothing();} : () => void
21+
22+
this.nothing();
23+
>this.nothing() : any
24+
>this.nothing : any
25+
>this : { m: () => void; }
26+
>nothing : any
27+
28+
};
29+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @filename: input.js
2+
// @out: output.js
3+
// @allowJs: true
4+
5+
function C() {
6+
this.m = null;
7+
}
8+
C.prototype.m = function() {
9+
this.nothing();
10+
};

0 commit comments

Comments
 (0)