Skip to content

Incorrect "Duplicate identifier" error in JS file when JSDoc @typedef followed by parenthetical expression #36179

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

Closed
davidpcaldwell opened this issue Jan 14, 2020 · 3 comments · Fixed by #36289
Labels
Bug A bug in TypeScript
Milestone

Comments

@davidpcaldwell
Copy link

davidpcaldwell commented Jan 14, 2020

TypeScript Version: 3.7.4

Search terms
duplicate identifier typedef jsdoc

Code
duplicate-identifier-expression.js

//@ts-check
/** @typedef {object} NotADuplicateIdentifier */

(2 * 2)

Expected behavior:
Compiles without error

Actual behavior:

$ env PATH="${PATH}:../slime/local/jsh/lib/node/bin" ../slime/local/jsh/lib/node/bin/tsc duplicate-identifier-expression.js --allowJs --noemit
duplicate-identifier-expression.js:2:23 - error TS2300: Duplicate identifier 'NotADuplicateIdentifier'.

2 /** @typedef {object} NotADuplicateIdentifier */
                        ~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error.

Notes:

  • Did not see equivalent error for .ts file.
  • Did not see equivalent error when expression not enclosed in parentheses.

Playground Link:
Could not recreate there.

Related Issues:
None found.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 14, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 14, 2020
@ajafff
Copy link
Contributor

ajafff commented Jan 16, 2020

The issue is that the same JSDoc is parsed (and bound) for the ExpressionStatement and the ParenthesizedExpression.

There's another bug where the ParenthesizedExpression contains the JSDoc twice: https://ts-ast-viewer.com/#code/PQKhAIAEBcE8AcCmATRAzcBvA9gIwFaIDG0AvuAGLbbgjABQAFAIwCU4A1OMwNxA (check "Show Internals" option and select ParenthesizedExpression)

/** @typedef {object} Foo */
(1) + 1;

I'll try to fix the original issue by not parsing JSDoc for ExpressionStatement if it starts with a ParenthesizedExpression. Maybe I'll fix the other parsing issue right away or open a follow-up issue.

@ajafff
Copy link
Contributor

ajafff commented Jan 16, 2020

It looks like parsing JSDoc twice only happens for JSDoc comments starting in the first line of the file. So it could simply be a duplicate of #29625

@ajafff
Copy link
Contributor

ajafff commented Jan 18, 2020

Fix is up at #36289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
3 participants