Skip to content
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

incorrect formatting of JSDoc type assertion by JS parsers other than babel-* #8171

Open
bradzacher opened this issue Apr 27, 2020 · 7 comments
Labels
area:comments Issues with how Prettier prints comments lang:javascript Issues affecting JS lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:enhancement A potential new feature to be added, or an improvement to how we print something

Comments

@bradzacher
Copy link

Prettier 2.0.5
Playground link

# Options (if any):
--parser typescript

Input:

declare const foo: any;

const bar1 = /** @type {string} */(foo);

Output:

declare const foo: any;

const bar1 = /** @type {string} */ foo;

Expected behavior:

declare const foo: any;

const bar1 = /** @type {string} */(foo);
// or
const bar1 = /** @type {string} */ (foo);

What's wrong?

In a JS Codebase, dropping the parens breaks the JSDoc type assertion, which will break type checking.

declare const foo: any;

const bar1 = /** @type {string} */(foo);
// typeof bar1 === string

const bar2 = /** @type {string} */ foo;
// typeof bar2 === any

See deeper explanation in typescript-eslint/typescript-eslint#1943

It looks like this only occurs with the typescript parser, as the babel parser works correctly (though note that the flow parser exhibits the same paren scrubbing behaviour).

Related issues (not exactly the same, but pertaining to JSDoc in TS): #8045, #5850

@thorn0
Copy link
Member

thorn0 commented Apr 27, 2020

It's a known issue. See the release notes for 2.0. TypeScript, the language, doesn't support such type casts. TypeScript, the compiler, supports them only in JS.

Check this playground:

declare const foo: any;

const bar1 = /** @type {string} */(foo);

bar1 is any.

@thorn0 thorn0 added area:comments Issues with how Prettier prints comments lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) labels Apr 27, 2020
@thorn0
Copy link
Member

thorn0 commented Apr 27, 2020

People might have reasons to use --parser typescript to format JS, but still I wouldn't call this a bug. If the parser supported ParenthesizedExpression, it'd work for TS, but it doesn't seem to be a big deal that it doesn't.

@bradzacher
Copy link
Author

No problem! I was just raising it because of the attached issue that someone raised - in testing I realised that prettier munges it.

I think we might need to add support for at least this case to the parser, which would fix the issue here as well.

@thorn0
Copy link
Member

thorn0 commented Apr 27, 2020

I didn't know the @typescript-eslint/* rules are used for JS.

BTW, it'd be interesting if the parser exposed the jsDoc property or even converted the types in it to ESTree-like nodes.

@bradzacher
Copy link
Author

Yeah (almost) all of our rules work fine on JS code. If you're good with your jsdoc typing, ones that use type information will even work pretty well!

Unfortunately it looks like JSDoc will never be an official part of the ESTree spec (estree/estree#201), but there's no reason we couldn't consider exposing it, given that it's parsed as an AST by TS itself.
Would be an interesting experiment

@hosseinmd

This comment was marked as off-topic.

@thorn0 thorn0 added type:enhancement A potential new feature to be added, or an improvement to how we print something lang:javascript Issues affecting JS labels Sep 6, 2022
@thorn0 thorn0 changed the title incorrect formatting of JSDoc type assertion incorrect formatting of JSDoc type assertion by JS parsers other than babel-* Sep 6, 2022
@thorn0
Copy link
Member

thorn0 commented Sep 6, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:comments Issues with how Prettier prints comments lang:javascript Issues affecting JS lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

3 participants