Skip to content

Commit 2dcc660

Browse files
committed
Consolidate jsdoc node getters
They are now used both in getJSDocCommentsAndTagsWorker and in geParameterSymbolFromJSDoc.
1 parent cae12cb commit 2dcc660

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

src/compiler/utilities.ts

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,34 @@ namespace ts {
15381538
return getJSDocCommentsAndTags(node);
15391539
}
15401540

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+
15411569
export function getJSDocCommentsAndTags(node: Node): (JSDoc | JSDocTag)[] {
15421570
let result: (JSDoc | JSDocTag)[] | undefined;
15431571
getJSDocCommentsAndTagsWorker(node);
@@ -1551,35 +1579,15 @@ namespace ts {
15511579
// * @returns {number}
15521580
// */
15531581
// 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);
15661584
}
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))) {
15751587
getJSDocCommentsAndTagsWorker(parent.parent);
15761588
}
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);
15831591
}
15841592

15851593
// Pull parameter comments from declaring function as well
@@ -1606,24 +1614,16 @@ namespace ts {
16061614
return undefined;
16071615
}
16081616
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;
16231626
}
1624-
const parameter = find(func.parameters, p =>
1625-
p.name.kind === SyntaxKind.Identifier && p.name.escapedText === name);
1626-
return parameter && parameter.symbol;
16271627
}
16281628

16291629
export function getJSDocHost(node: JSDocTag): HasJSDoc {

0 commit comments

Comments
 (0)