Skip to content

Commit ace9a15

Browse files
sandersnKingwl
authored andcommitted
Get jsdoc host from chained assignment (microsoft#36111)
* Get jsdoc host from chained assignment getSourceOfAssignment previously only checked one level of binary expression instead of following binary expressions all the way to the right. This meant that binding of `@constructor` would fail in the following example: ```js /** @constructor */ a = b = function () { } ``` * cleanup lint * use existing utility
1 parent b48c2c5 commit ace9a15

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,9 +2318,9 @@ namespace ts {
23182318

23192319
function getSourceOfAssignment(node: Node): Node | undefined {
23202320
return isExpressionStatement(node) &&
2321-
node.expression && isBinaryExpression(node.expression) &&
2321+
isBinaryExpression(node.expression) &&
23222322
node.expression.operatorToken.kind === SyntaxKind.EqualsToken
2323-
? node.expression.right
2323+
? getRightMostAssignedExpression(node.expression)
23242324
: undefined;
23252325
}
23262326

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/jsdoc/constructorTagOnNestedBinaryExpression.js ===
2+
// Fixes #35021
3+
/** @constructor */
4+
a = b = function c () {
5+
>c : Symbol(c, Decl(constructorTagOnNestedBinaryExpression.js, 2, 7))
6+
7+
console.log(this)
8+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
9+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
10+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
11+
>this : Symbol(c, Decl(constructorTagOnNestedBinaryExpression.js, 2, 7))
12+
13+
};
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/jsdoc/constructorTagOnNestedBinaryExpression.js ===
2+
// Fixes #35021
3+
/** @constructor */
4+
a = b = function c () {
5+
>a = b = function c () { console.log(this)} : typeof c
6+
>a : error
7+
>b = function c () { console.log(this)} : typeof c
8+
>b : error
9+
>function c () { console.log(this)} : typeof c
10+
>c : typeof c
11+
12+
console.log(this)
13+
>console.log(this) : void
14+
>console.log : (message?: any, ...optionalParams: any[]) => void
15+
>console : Console
16+
>log : (message?: any, ...optionalParams: any[]) => void
17+
>this : this
18+
19+
};
20+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @allowjs: true
2+
// @noemit: true
3+
// @Filename: constructorTagOnNestedBinaryExpression.js
4+
// Fixes #35021
5+
/** @constructor */
6+
a = b = function c () {
7+
console.log(this)
8+
};

0 commit comments

Comments
 (0)