Skip to content

Don't parse duplicate JSDoc for ExpressionStatement starting with ParenthesizedExpression #36289

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
Mar 12, 2020
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 @@ -1335,7 +1335,7 @@ namespace ts {

function createNodeWithJSDoc(kind: SyntaxKind, pos?: number): Node {
const node = createNode(kind, pos);
if (scanner.getTokenFlags() & TokenFlags.PrecedingJSDocComment) {
if (scanner.getTokenFlags() & TokenFlags.PrecedingJSDocComment && (kind !== SyntaxKind.ExpressionStatement || token() !== SyntaxKind.OpenParenToken)) {
addJSDocComment(<HasJSDoc>node);
}
return node;
Expand Down Expand Up @@ -5391,7 +5391,7 @@ namespace ts {
// Avoiding having to do the lookahead for a labeled statement by just trying to parse
// out an expression, seeing if it is identifier and then seeing if it is followed by
// a colon.
const node = <ExpressionStatement | LabeledStatement>createNodeWithJSDoc(SyntaxKind.Unknown);
const node = <ExpressionStatement | LabeledStatement>createNodeWithJSDoc(token() === SyntaxKind.Identifier ? SyntaxKind.Unknown : SyntaxKind.ExpressionStatement);
Copy link
Member

Choose a reason for hiding this comment

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

What other things can token be here? Why is it that only Identifier leaves the node's kind as unknown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is necessary so we can correctly handle ExpressionStatement inside createNodeWithJSDoc. You are right, that it would've been set later anyway, but that's too late as the JSDoc would already be attached to the ExpressionStatement node.

Identifier has a special handling here, because it's the only token that could be the start of a LabeledStatement. This change is only about ExpressionStatements starting with parentheses. Hence we can leave this as Unknown and avoid the lookahead to determine whether we are actually dealing with a LabeledStatement.

const expression = allowInAnd(parseExpression);
if (expression.kind === SyntaxKind.Identifier && parseOptional(SyntaxKind.ColonToken)) {
node.kind = SyntaxKind.LabeledStatement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/test.js ===
// @ts-check
/** @typedef {number} NotADuplicateIdentifier */

(2 * 2);

/** @typedef {number} AlsoNotADuplicate */

(2 * 2) + 1;


/**
*
* @param a {NotADuplicateIdentifier}
* @param b {AlsoNotADuplicate}
*/
function makeSureTypedefsAreStillRecognized(a, b) {}
>makeSureTypedefsAreStillRecognized : Symbol(makeSureTypedefsAreStillRecognized, Decl(test.js, 7, 12))
>a : Symbol(a, Decl(test.js, 15, 44))
>b : Symbol(b, Decl(test.js, 15, 46))

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/compiler/test.js ===
// @ts-check
/** @typedef {number} NotADuplicateIdentifier */

(2 * 2);
>(2 * 2) : number
>2 * 2 : number
>2 : 2
>2 : 2

/** @typedef {number} AlsoNotADuplicate */

(2 * 2) + 1;
>(2 * 2) + 1 : number
>(2 * 2) : number
>2 * 2 : number
>2 : 2
>2 : 2
>1 : 1


/**
*
* @param a {NotADuplicateIdentifier}
* @param b {AlsoNotADuplicate}
*/
function makeSureTypedefsAreStillRecognized(a, b) {}
>makeSureTypedefsAreStillRecognized : (a: number, b: number) => void
>a : number
>b : number

20 changes: 20 additions & 0 deletions tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @allowJs: true
// @noEmit: true

// @filename: test.js
// @ts-check
/** @typedef {number} NotADuplicateIdentifier */

(2 * 2);

/** @typedef {number} AlsoNotADuplicate */

(2 * 2) + 1;


/**
*
* @param a {NotADuplicateIdentifier}
* @param b {AlsoNotADuplicate}
*/
function makeSureTypedefsAreStillRecognized(a, b) {}