Skip to content

Commit 7318945

Browse files
committed
fix(core): keep JSDoc directives between template children
A `/** @ts-expect-error */` body starts with `*`, so the parser did not mark the surrounding whitespace as a directive and dropped it before the editor could place the comment in `{}`.
1 parent 95681a5 commit 7318945

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tsrx/core': patch
3+
---
4+
5+
Keep a JSDoc `@ts-expect-error` or `@ts-ignore` between template children for the editor. `/** @ts-expect-error */` and a directive on a later line of the comment were dropped, so the editor's TypeScript never saw them, while `// @ts-expect-error` and `/* @ts-expect-error */` were kept.

‎packages/tsrx/src/plugin.js‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,19 @@ function can_start_tag_after_lt(input, index) {
759759
/** The start of an end tag for a script, in any letter case. */
760760
const regex_script_end_tag_start = /<\/script/giu;
761761

762+
/**
763+
* A comment body (the text after `//` or `/*`) that may be a tooling
764+
* directive. The editor keeps it where the text around it renders nothing.
765+
* The directive is `@` at the start of a line (`// @ts-expect-error`,
766+
* `/* @ts-expect-error *\/`). A JSDoc comment's body starts with `*`
767+
* (`/** @ts-expect-error *\/` is `* @ts-expect-error`), and the `@` may sit
768+
* on a later line (`/**\n * @ts-expect-error\n *\/`).
769+
* @param {string} body
770+
*/
771+
function comment_body_has_directive(body) {
772+
return /(?:^|[\n\r])\s*\**\s*@/.test(body);
773+
}
774+
762775
/**
763776
* HTML's whitespace: tab, line feed, form feed, carriage return, and space.
764777
* @param {number} code
@@ -1684,9 +1697,9 @@ export function TSRXPlugin(config) {
16841697
/** @type {Array<[number, number]>} */
16851698
const pieces = [];
16861699
let piece_start = start;
1687-
// A comment that may be a tooling directive (`// @ts-expect-error`),
1688-
// which the editor's TypeScript keeps even where the text renders
1689-
// nothing.
1700+
// A comment that may be a tooling directive (`// @ts-expect-error`,
1701+
// including the JSDoc spelling `/** @ts-expect-error */`), which the
1702+
// editor's TypeScript keeps even where the text renders nothing.
16901703
let has_directive = false;
16911704
while (index < this.input.length) {
16921705
if (this.#isTemplateLineCommentStart(index, start)) {
@@ -1704,7 +1717,9 @@ export function TSRXPlugin(config) {
17041717
if (comment_start >= token_end) {
17051718
this.#emitTemplateLineComment(comment_start, index, null);
17061719
}
1707-
if (/^\s*@/.test(this.input.slice(comment_start + 2, index))) has_directive = true;
1720+
if (comment_body_has_directive(this.input.slice(comment_start + 2, index))) {
1721+
has_directive = true;
1722+
}
17081723
piece_start = index;
17091724
continue;
17101725
}
@@ -1727,7 +1742,9 @@ export function TSRXPlugin(config) {
17271742
null,
17281743
);
17291744
}
1730-
if (/^\s*@/.test(this.input.slice(comment_start + 2, value_end))) has_directive = true;
1745+
if (comment_body_has_directive(this.input.slice(comment_start + 2, value_end))) {
1746+
has_directive = true;
1747+
}
17311748
piece_start = index;
17321749
continue;
17331750
}

‎packages/tsrx/tests/shared/source-mappings.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,32 @@ export function List({ items =${whitespace}EMPTY_ARRAY as string[] }: { items?:
361361
expect(result.code).toContain('plain {} more');
362362
expect(result.code).not.toContain('not a directive');
363363
});
364+
365+
// `/** @ts-expect-error */` is the same directive. Its body starts with
366+
// `*`, and the `@` may be on a later line.
367+
it('keeps a JSDoc directive on the line before the child', () => {
368+
const source = `export function App() @{
369+
<div>
370+
/** @ts-expect-error */
371+
<JsDoc a={1} />
372+
/**
373+
* @ts-expect-error
374+
*/
375+
<Multi a={1} />
376+
/**@ts-ignore*/
377+
<Tight a={1} />
378+
/** note */
379+
<Plain />
380+
</div>
381+
}`;
382+
const result = compile_to_volar_mappings(source, 'App.tsrx', { loose: true });
383+
expect(result.errors).toEqual([]);
384+
expect(result.code).toMatch(/\{\/\*\* {1,2}@ts-expect-error \*\/\}\s*<JsDoc a=\{1\} \/>/);
385+
expect(result.code).toMatch(/@ts-expect-error \*\/\}\s*<Multi a=\{1\} \/>/);
386+
expect(result.code).toMatch(/\{\/\*\* @ts-ignore \*\/\}\s*<Tight a=\{1\} \/>/);
387+
expect(result.code).not.toContain('note');
388+
expect(result.code).toContain('<Plain />');
389+
});
364390
});
365391

366392
describe(`[${name}] whitespace or a comment after an element's \`<\``, () => {

‎packages/tsrx/types/index.d.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export interface BaseNodeMetaData {
127127
* TSX, so each piece follows JSX's whitespace rules on its own;
128128
* `analyzeTsrx` splits the text into these pieces.
129129
*/
130-
/** A template `JSXText` with a comment in it that starts with `@`. */
130+
/**
131+
* A template `JSXText` with a comment in it that is a tooling directive
132+
* (`@` at the start of a line, including after a JSDoc `*`).
133+
*/
131134
text_directive?: boolean;
132135
/**
133136
* The empty `JSXEmptyExpression` that `analyzeTsrx` puts where a comment was

0 commit comments

Comments
 (0)