Skip to content

formatter: Parentheses removed from expression inside JSDoc type cast #20182

Description

@Dunqing

Input

Found in sveltejs/svelte repo.

File: packages/svelte/src/internal/client/dom/css.js

var target = /** @type {ShadowRoot} */ (root).host
  ? /** @type {ShadowRoot} */ (root)
  : /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head;

Config

{
  "useTabs": true,
  "singleQuote": true,
  "trailingComma": "none",
  "printWidth": 100
}

Oxfmt output

var target = /** @type {ShadowRoot} */ (root).host
	? /** @type {ShadowRoot} */ (root)
	: /** @type {Document} */ ((root).head ?? /** @type {Document} */ (root.ownerDocument).head);

Prettier output

Prettier version: 3.9.5

var target = /** @type {ShadowRoot} */ root.host
	? /** @type {ShadowRoot} */ root
	: /** @type {Document} */ (root.head ?? /** @type {Document} */ root.ownerDocument.head);

Additional notes

Prettier removes the parentheses required by JSDoc type casts, changing /** @type {ShadowRoot} */ (root).host to /** @type {ShadowRoot} */ root.host.
While runtime-equivalent, this breaks the casts entirely: a JSDoc cast /** @type {T} */ (expr) is only recognized when the expression is parenthesized, so after formatting the comments become plain comments and TypeScript no longer applies the types.

Oxfmt preserves the cast parentheses, but wraps the alternate branch in an extra pair of parentheses, changing /** @type {Document} */ (root).head ?? ... to /** @type {Document} */ ((root).head ?? ...). This moves the cast from root to the entire ?? expression, which changes its meaning at the type level (the original casts root to Document before accessing .head; the new form casts the result of the whole expression).

Metadata

Metadata

Assignees

Labels

A-formatter-prettier-diffArea - Formatter difference with PrettierF-lang-jsFormatter - oxc_formatter(_js) related

Type

No type

Fields

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions