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

* don't parse JSDoc on ExpressionStatement if it starts with ParenthesizedExpression

* update test
This commit is contained in:
Klaus Meinhardt 2020-03-12 22:42:49 +01:00 committed by GitHub
parent ddcf139668
commit 7bd6209fbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 2 deletions

View File

@ -1400,7 +1400,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;
@ -5469,7 +5469,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);
const expression = allowInAnd(parseExpression);
if (expression.kind === SyntaxKind.Identifier && parseOptional(SyntaxKind.ColonToken)) {
node.kind = SyntaxKind.LabeledStatement;

View File

@ -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))

View File

@ -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

View File

@ -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) {}