From 1fb4f72fc0e8d82dbb75317c49c225af4b845f5e Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Thu, 16 Jan 2020 21:23:47 +0100 Subject: [PATCH 1/2] don't parse JSDoc on ExpressionStatement if it starts with ParenthesizedExpression --- src/compiler/parser.ts | 4 ++-- ...pedefBeforeParenthesizedExpression.symbols | 12 +++++++++++ ...TypedefBeforeParenthesizedExpression.types | 21 +++++++++++++++++++ ...docTypedefBeforeParenthesizedExpression.ts | 14 +++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols create mode 100644 tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types create mode 100644 tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a10c2cbd6d42a..fdd1705d307cc 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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(node); } return node; @@ -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 = createNodeWithJSDoc(SyntaxKind.Unknown); + const node = createNodeWithJSDoc(token() === SyntaxKind.Identifier ? SyntaxKind.Unknown : SyntaxKind.ExpressionStatement); const expression = allowInAnd(parseExpression); if (expression.kind === SyntaxKind.Identifier && parseOptional(SyntaxKind.ColonToken)) { node.kind = SyntaxKind.LabeledStatement; diff --git a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols new file mode 100644 index 0000000000000..0e223697640ba --- /dev/null +++ b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/test.js === +; +No type information for this code./** @typedef {object} NotADuplicateIdentifier */ +No type information for this code. +No type information for this code.(2 * 2); +No type information for this code. +No type information for this code./** @typedef {object} AlsoNotADuplicate */ +No type information for this code. +No type information for this code.(2 * 2) + 1; +No type information for this code. +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types new file mode 100644 index 0000000000000..5e990ff0cbb5d --- /dev/null +++ b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types @@ -0,0 +1,21 @@ +=== tests/cases/compiler/test.js === +; +/** @typedef {object} NotADuplicateIdentifier */ + +(2 * 2); +>(2 * 2) : number +>2 * 2 : number +>2 : 2 +>2 : 2 + +/** @typedef {object} AlsoNotADuplicate */ + +(2 * 2) + 1; +>(2 * 2) + 1 : number +>(2 * 2) : number +>2 * 2 : number +>2 : 2 +>2 : 2 +>1 : 1 + + diff --git a/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts b/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts new file mode 100644 index 0000000000000..f147d46a4641c --- /dev/null +++ b/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts @@ -0,0 +1,14 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: test.js +; +/** @typedef {object} NotADuplicateIdentifier */ + +(2 * 2); + +/** @typedef {object} AlsoNotADuplicate */ + +(2 * 2) + 1; + From d8222bf80a5e529ed23f81f6466ecf5f722ac284 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sat, 18 Jan 2020 19:37:21 +0100 Subject: [PATCH 2/2] update test --- ...pedefBeforeParenthesizedExpression.symbols | 31 ++++++++++++------- ...TypedefBeforeParenthesizedExpression.types | 16 ++++++++-- ...docTypedefBeforeParenthesizedExpression.ts | 14 ++++++--- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols index 0e223697640ba..49b1f8ffe5f64 100644 --- a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols +++ b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.symbols @@ -1,12 +1,21 @@ === tests/cases/compiler/test.js === -; -No type information for this code./** @typedef {object} NotADuplicateIdentifier */ -No type information for this code. -No type information for this code.(2 * 2); -No type information for this code. -No type information for this code./** @typedef {object} AlsoNotADuplicate */ -No type information for this code. -No type information for this code.(2 * 2) + 1; -No type information for this code. -No type information for this code. -No type information for this code. \ No newline at end of file +// @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)) + diff --git a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types index 5e990ff0cbb5d..af85829184743 100644 --- a/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types +++ b/tests/baselines/reference/jsdocTypedefBeforeParenthesizedExpression.types @@ -1,6 +1,6 @@ === tests/cases/compiler/test.js === -; -/** @typedef {object} NotADuplicateIdentifier */ +// @ts-check +/** @typedef {number} NotADuplicateIdentifier */ (2 * 2); >(2 * 2) : number @@ -8,7 +8,7 @@ >2 : 2 >2 : 2 -/** @typedef {object} AlsoNotADuplicate */ +/** @typedef {number} AlsoNotADuplicate */ (2 * 2) + 1; >(2 * 2) + 1 : number @@ -19,3 +19,13 @@ >1 : 1 +/** + * + * @param a {NotADuplicateIdentifier} + * @param b {AlsoNotADuplicate} + */ +function makeSureTypedefsAreStillRecognized(a, b) {} +>makeSureTypedefsAreStillRecognized : (a: number, b: number) => void +>a : number +>b : number + diff --git a/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts b/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts index f147d46a4641c..04ae3fc406aec 100644 --- a/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts +++ b/tests/cases/compiler/jsdocTypedefBeforeParenthesizedExpression.ts @@ -1,14 +1,20 @@ // @allowJs: true -// @checkJs: true // @noEmit: true // @filename: test.js -; -/** @typedef {object} NotADuplicateIdentifier */ +// @ts-check +/** @typedef {number} NotADuplicateIdentifier */ (2 * 2); -/** @typedef {object} AlsoNotADuplicate */ +/** @typedef {number} AlsoNotADuplicate */ (2 * 2) + 1; + +/** + * + * @param a {NotADuplicateIdentifier} + * @param b {AlsoNotADuplicate} + */ +function makeSureTypedefsAreStillRecognized(a, b) {}