From f3216a3a351a7d9dad197a3a5e8af63fc7ad203d Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 14 Aug 2026 22:20:03 +0200 Subject: [PATCH] fix: revert jsdoc type cast support (for now) Fixes #164; we'll follow up with a better solution --- .changeset/yellow-moons-judge.md | 5 +++ src/languages/ts/index.js | 36 ++----------------- .../comment-jsdoc-type-cast/expected.js | 2 -- .../comment-jsdoc-type-cast/expected.js.map | 11 ------ test/samples/comment-jsdoc-type-cast/input.js | 3 -- 5 files changed, 8 insertions(+), 49 deletions(-) create mode 100644 .changeset/yellow-moons-judge.md delete mode 100644 test/samples/comment-jsdoc-type-cast/expected.js delete mode 100644 test/samples/comment-jsdoc-type-cast/expected.js.map delete mode 100644 test/samples/comment-jsdoc-type-cast/input.js diff --git a/.changeset/yellow-moons-judge.md b/.changeset/yellow-moons-judge.md new file mode 100644 index 00000000..1ed9e675 --- /dev/null +++ b/.changeset/yellow-moons-judge.md @@ -0,0 +1,5 @@ +--- +'esrap': patch +--- + +fix: revert jsdoc type cast support (for now) diff --git a/src/languages/ts/index.js b/src/languages/ts/index.js index 0f9145be..f3b4cdda 100644 --- a/src/languages/ts/index.js +++ b/src/languages/ts/index.js @@ -315,11 +315,9 @@ export default (options = {}) => { * @param {{ line: number, column: number } | null} from * @param {{ line: number, column: number }} to * @param {boolean} pad - * @param {boolean} [is_next_to_expression] */ - function flush_comments_until(context, from, to, pad, is_next_to_expression = false) { + function flush_comments_until(context, from, to, pad) { let first = true; - let jsdoc_type_casts = 0; while (comment_index < comments.length) { const comment = comments[comment_index]; @@ -333,24 +331,10 @@ export default (options = {}) => { first = false; write_comment(comment, context); - // Acorn removes the parentheses that give a JSDoc `@type` comment cast semantics. - // We have to do a best guess (because we don't have access to the original source) - // to detect it based on the comment starting with `* @type {`, and only when - // it's an expression (e.g. `const foo = /** @type {number} */ (1);`), not something - // else like a statement (e.g. `/** @type {number} */ let foo;`). - const is_jsdoc_type_cast = - is_next_to_expression && - comment.type === 'Block' && - /(?:^|\n)\s*\*\s*@type\s*{/.test(comment.value); - - if (is_jsdoc_type_cast) { - context.write(' ('); - jsdoc_type_casts += 1; - } if (comment.loc.end.line < to.line) { context.newline(); - } else if (pad && !is_jsdoc_type_cast) { + } else if (pad) { context.write(' '); } @@ -359,8 +343,6 @@ export default (options = {}) => { break; } } - - return jsdoc_type_casts; } /** @@ -1058,24 +1040,12 @@ export default (options = {}) => { _(node, context, visit) { write_additional_comments(context, options.getLeadingComments?.(node), 'leading'); - let jsdoc_type_casts = 0; - if (node.loc) { - jsdoc_type_casts = flush_comments_until( - context, - null, - node.loc.start, - true, - node.type in EXPRESSIONS_PRECEDENCE - ); + flush_comments_until(context, null, node.loc.start, true); } visit(node); - if (jsdoc_type_casts > 0) { - context.write(')'.repeat(jsdoc_type_casts)); - } - // a JSX empty expression prints nothing and exists only to hold the // comments inside `{...}`. Flush them here, otherwise they are written // by whichever node comes next — after the closing brace, where they diff --git a/test/samples/comment-jsdoc-type-cast/expected.js b/test/samples/comment-jsdoc-type-cast/expected.js deleted file mode 100644 index 2138f530..00000000 --- a/test/samples/comment-jsdoc-type-cast/expected.js +++ /dev/null @@ -1,2 +0,0 @@ -const foo = /** @type {number} */ (1); -const bar = /** @type {number} */ (/** @type {number} */ (1)); diff --git a/test/samples/comment-jsdoc-type-cast/expected.js.map b/test/samples/comment-jsdoc-type-cast/expected.js.map deleted file mode 100644 index 5a8e2562..00000000 --- a/test/samples/comment-jsdoc-type-cast/expected.js.map +++ /dev/null @@ -1,11 +0,0 @@ -{ - "version": 3, - "names": [], - "sources": [ - "input.js" - ], - "sourcesContent": [ - "const foo = /** @type {number} */ (1);\n\nconst bar = /** @type {number} */ (/** @type {number} */ (1));\n" - ], - "mappings": "AAAA,MAAM,AAAA,GAAG,0BAA0B,CAAC;AAEpC,MAAM,AAAA,GAAG,iDAAiD,CAAC" -} diff --git a/test/samples/comment-jsdoc-type-cast/input.js b/test/samples/comment-jsdoc-type-cast/input.js deleted file mode 100644 index 4edc2689..00000000 --- a/test/samples/comment-jsdoc-type-cast/input.js +++ /dev/null @@ -1,3 +0,0 @@ -const foo = /** @type {number} */ (1); - -const bar = /** @type {number} */ (/** @type {number} */ (1));