Skip to content

Recognize object literal method JSDoc comments #6870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3960,7 +3960,7 @@ namespace ts {
shorthandDeclaration.equalsToken = equalsToken;
shorthandDeclaration.objectAssignmentInitializer = allowInAnd(parseAssignmentExpressionOrHigher);
}
return finishNode(shorthandDeclaration);
return addJSDocComment(finishNode(shorthandDeclaration));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this function returns early if tryParseAccessorDeclaration is successful (or via parseMethodDeclaration@3943). Do we handle those cases, such as getters/setters? (Good tests to add also).

}
else {
const propertyAssignment = <PropertyAssignment>createNode(SyntaxKind.PropertyAssignment, fullStart);
Expand All @@ -3969,7 +3969,7 @@ namespace ts {
propertyAssignment.questionToken = questionToken;
parseExpected(SyntaxKind.ColonToken);
propertyAssignment.initializer = allowInAnd(parseAssignmentExpressionOrHigher);
return finishNode(propertyAssignment);
return addJSDocComment(finishNode(propertyAssignment));
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,13 +1212,19 @@ namespace ts {
}

// Also recognize when the node is the RHS of an assignment expression
const parent = node.parent;
const isSourceOfAssignmentExpressionStatement =
node.parent && node.parent.parent &&
node.parent.kind === SyntaxKind.BinaryExpression &&
(node.parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken &&
node.parent.parent.kind === SyntaxKind.ExpressionStatement;
parent && parent.parent &&
parent.kind === SyntaxKind.BinaryExpression &&
(parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken &&
parent.parent.kind === SyntaxKind.ExpressionStatement;
if (isSourceOfAssignmentExpressionStatement) {
return node.parent.parent.jsDocComment;
return parent.parent.jsDocComment;
}

const isPropertyAssignmentExpression = parent && parent.kind === SyntaxKind.PropertyAssignment;
if (isPropertyAssignmentExpression) {
return parent.jsDocComment;
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/cases/fourslash/jsDocFunctionSignatures3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
///<reference path="fourslash.ts" />

// @allowNonTsExtensions: true
// @Filename: Foo.js

//// var someObject = {
//// /**
//// * @param {string} param1 Some string param.
//// * @param {number} parm2 Some number param.
//// */
//// someMethod: function(param1, param2) {
//// console.log(param1/*1*/);
//// return false;
//// },
//// /**
//// * @param {number} p1 Some number param.
//// */
//// otherMethod(p1) {
//// p1/*2*/
//// }
////
//// };

goTo.marker('1');
edit.insert('.');
verify.memberListContains('substr', undefined, undefined, 'method');
edit.backspace();

goTo.marker('2');
edit.insert('.');
verify.memberListContains('toFixed', undefined, undefined, 'method');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: instead of checking the member list which is dependent on the apparent type (and which can potentially be affected by various usages of identifiers), I think it's generally a better idea to stick with the quick info since that will tell you precisely the type you're using.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a method for just saying "I expect this to be string ?" I don't want to write out the entire quickinfo signature in every test (laziness)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit.backspace();