Skip to content

Commit

Permalink
Add parser support for annotating function parameters and catch expre…
Browse files Browse the repository at this point in the history
…ssions.
  • Loading branch information
Nick Santos authored and gbrail committed Sep 28, 2017
1 parent 6a87171 commit af03579
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,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 @@ -1584,7 +1589,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 @@ -1038,6 +1038,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

0 comments on commit af03579

Please sign in to comment.