Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-moons-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esrap': patch
---

fix: revert jsdoc type cast support (for now)
36 changes: 3 additions & 33 deletions src/languages/ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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(' ');
}

Expand All @@ -359,8 +343,6 @@ export default (options = {}) => {
break;
}
}

return jsdoc_type_casts;
}

/**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions test/samples/comment-jsdoc-type-cast/expected.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/samples/comment-jsdoc-type-cast/expected.js.map

This file was deleted.

3 changes: 0 additions & 3 deletions test/samples/comment-jsdoc-type-cast/input.js

This file was deleted.

Loading