@@ -1538,6 +1538,34 @@ namespace ts {
1538
1538
return getJSDocCommentsAndTags ( node ) ;
1539
1539
}
1540
1540
1541
+ export function getSourceOfAssignment ( node : Node ) : Node {
1542
+ return isExpressionStatement ( node ) &&
1543
+ node . expression && isBinaryExpression ( node . expression ) &&
1544
+ node . expression . operatorToken . kind === SyntaxKind . EqualsToken &&
1545
+ node . expression . right ;
1546
+ }
1547
+
1548
+ export function getSingleInitializerOfVariableStatement ( node : Node , child ?: Node ) : Node {
1549
+ return isVariableStatement ( node ) &&
1550
+ node . declarationList . declarations . length > 0 &&
1551
+ ( ! child || node . declarationList . declarations [ 0 ] . initializer === child ) &&
1552
+ node . declarationList . declarations [ 0 ] . initializer ;
1553
+ }
1554
+
1555
+ export function getSingleVariableOfVariableStatement ( node : Node , child ?: Node ) : Node {
1556
+ return isVariableStatement ( node ) &&
1557
+ node . declarationList . declarations . length > 0 &&
1558
+ ( ! child || node . declarationList . declarations [ 0 ] === child ) &&
1559
+ node . declarationList . declarations [ 0 ] ;
1560
+ }
1561
+
1562
+ export function getNestedModuleDeclaration ( node : Node ) : Node {
1563
+ return node . kind === SyntaxKind . ModuleDeclaration &&
1564
+ ( node as ModuleDeclaration ) . body &&
1565
+ ( node as ModuleDeclaration ) . body . kind === SyntaxKind . ModuleDeclaration &&
1566
+ ( node as ModuleDeclaration ) . body ;
1567
+ }
1568
+
1541
1569
export function getJSDocCommentsAndTags ( node : Node ) : ( JSDoc | JSDocTag ) [ ] {
1542
1570
let result : ( JSDoc | JSDocTag ) [ ] | undefined ;
1543
1571
getJSDocCommentsAndTagsWorker ( node ) ;
@@ -1551,35 +1579,15 @@ namespace ts {
1551
1579
// * @returns {number }
1552
1580
// */
1553
1581
// var x = function(name) { return name.length; }
1554
- const isInitializerOfVariableDeclarationInStatement =
1555
- isVariableLike ( parent ) &&
1556
- parent . initializer === node &&
1557
- parent . parent . parent . kind === SyntaxKind . VariableStatement ;
1558
- const isVariableOfVariableDeclarationStatement = isVariableLike ( node ) &&
1559
- parent . parent . kind === SyntaxKind . VariableStatement ;
1560
- const variableStatementNode =
1561
- isInitializerOfVariableDeclarationInStatement ? parent . parent . parent :
1562
- isVariableOfVariableDeclarationStatement ? parent . parent :
1563
- undefined ;
1564
- if ( variableStatementNode ) {
1565
- getJSDocCommentsAndTagsWorker ( variableStatementNode ) ;
1582
+ if ( parent && ( parent . kind === SyntaxKind . PropertyAssignment || getNestedModuleDeclaration ( parent ) ) ) {
1583
+ getJSDocCommentsAndTagsWorker ( parent ) ;
1566
1584
}
1567
-
1568
- // Also recognize when the node is the RHS of an assignment expression
1569
- const isSourceOfAssignmentExpressionStatement =
1570
- parent && parent . parent &&
1571
- parent . kind === SyntaxKind . BinaryExpression &&
1572
- ( parent as BinaryExpression ) . operatorToken . kind === SyntaxKind . EqualsToken &&
1573
- parent . parent . kind === SyntaxKind . ExpressionStatement ;
1574
- if ( isSourceOfAssignmentExpressionStatement ) {
1585
+ if ( parent && parent . parent &&
1586
+ ( getSingleVariableOfVariableStatement ( parent . parent , node ) || getSourceOfAssignment ( parent . parent ) ) ) {
1575
1587
getJSDocCommentsAndTagsWorker ( parent . parent ) ;
1576
1588
}
1577
-
1578
- const isModuleDeclaration = node . kind === SyntaxKind . ModuleDeclaration &&
1579
- parent && parent . kind === SyntaxKind . ModuleDeclaration ;
1580
- const isPropertyAssignmentExpression = parent && parent . kind === SyntaxKind . PropertyAssignment ;
1581
- if ( isModuleDeclaration || isPropertyAssignmentExpression ) {
1582
- getJSDocCommentsAndTagsWorker ( parent ) ;
1589
+ if ( parent && parent . parent && parent . parent . parent && getSingleInitializerOfVariableStatement ( parent . parent . parent , node ) ) {
1590
+ getJSDocCommentsAndTagsWorker ( parent . parent . parent ) ;
1583
1591
}
1584
1592
1585
1593
// Pull parameter comments from declaring function as well
@@ -1606,24 +1614,16 @@ namespace ts {
1606
1614
return undefined ;
1607
1615
}
1608
1616
const name = node . name . escapedText ;
1609
- const decl = getJSDocHost ( node ) ;
1610
- let func : FunctionLike ;
1611
- if ( isExpressionStatement ( decl ) && isBinaryExpression ( decl . expression ) && isFunctionLike ( decl . expression . right ) ) {
1612
- func = decl . expression . right ;
1613
- }
1614
- else if ( isVariableStatement ( decl ) && decl . declarationList . declarations . length === 1 && isVariableDeclaration ( decl . declarationList . declarations [ 0 ] )
1615
- && isFunctionLike ( decl . declarationList . declarations [ 0 ] . initializer ) ) {
1616
- func = decl . declarationList . declarations [ 0 ] . initializer as FunctionLike ;
1617
- }
1618
- else if ( isFunctionLike ( decl ) ) {
1619
- func = decl ;
1620
- }
1621
- else {
1622
- return undefined ;
1617
+ const host = getJSDocHost ( node ) ;
1618
+ const decl = getSourceOfAssignment ( host ) ||
1619
+ getSingleInitializerOfVariableStatement ( host ) ||
1620
+ getSingleVariableOfVariableStatement ( host ) ||
1621
+ getNestedModuleDeclaration ( host ) ||
1622
+ host ;
1623
+ if ( decl && isFunctionLike ( decl ) ) {
1624
+ const parameter = find ( decl . parameters , p => p . name . kind === SyntaxKind . Identifier && p . name . escapedText === name ) ;
1625
+ return parameter && parameter . symbol ;
1623
1626
}
1624
- const parameter = find ( func . parameters , p =>
1625
- p . name . kind === SyntaxKind . Identifier && p . name . escapedText === name ) ;
1626
- return parameter && parameter . symbol ;
1627
1627
}
1628
1628
1629
1629
export function getJSDocHost ( node : JSDocTag ) : HasJSDoc {
0 commit comments