Skip to content
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

Attach jsdoc nodes to function params #108

Merged
merged 1 commit into from
Sep 28, 2017
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
12 changes: 11 additions & 1 deletion src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,12 @@ private void parseFunctionParams(FunctionNode fnNode)
destructuring.put(pname, expr);
} else {
if (mustMatchToken(Token.NAME, "msg.no.parm")) {
fnNode.addParam(createNameNode());
Name paramNameNode = createNameNode();
Comment jsdocNodeForName = getAndResetJsDoc();
if (jsdocNodeForName != null) {
paramNameNode.setJsDocNode(jsdocNodeForName);
}
fnNode.addParam(paramNameNode);
String paramName = ts.getString();
defineSymbol(Token.LP, paramName);
if (this.inUseStrictDirective) {
Expand Down Expand Up @@ -1379,7 +1384,12 @@ private TryStatement tryStatement()
lp = ts.tokenBeg;

mustMatchToken(Token.NAME, "msg.bad.catchcond");

Name varName = createNameNode();
Comment jsdocNodeForName = getAndResetJsDoc();
if (jsdocNodeForName != null) {
varName.setJsDocNode(jsdocNodeForName);
}
String varNameString = varName.getIdentifier();
if (inUseStrictDirective) {
if ("eval".equals(varNameString) ||
Expand Down
23 changes: 23 additions & 0 deletions testsrc/org/mozilla/javascript/tests/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,29 @@ public void testJSDocAttachment16() {
assertNotNull(st.getJsDoc());
}

public void testJSDocAttachment17() {
AstRoot root = parse(
"try { throw 'a'; } catch (/** @type {string} */ e) {\n" +
"}\n");
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());

TryStatement tryNode = (TryStatement) root.getFirstChild();
CatchClause catchNode = tryNode.getCatchClauses().get(0);
assertNotNull(catchNode.getVarName().getJsDoc());
}

public void testJSDocAttachment18() {
AstRoot root = parse(
"function f(/** @type {string} */ e) {}\n");
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());

FunctionNode function = (FunctionNode) root.getFirstChild();
AstNode param = function.getParams().get(0);
assertNotNull(param.getJsDoc());
}

public void testParsingWithoutJSDoc() {
AstRoot root = parse("var a = /** @type number */(x);", false);
assertNotNull(root.getComments());
Expand Down