Skip to content

Commit a2231ba

Browse files
committed
fix(core): read a comment glued to type-argument angle brackets
A `<` followed by `/*` or `//` is whitespace, as after a class or function name, so superclass and expression type arguments parse instead of staying a closing-tag start.
1 parent db9d70f commit a2231ba

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎packages/tsrx/src/plugin.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,12 +3848,17 @@ export function TSRXPlugin(config) {
38483848
/**
38493849
* The same where a tag can't start but type arguments can: after a
38503850
* superclass, and after a class or function expression on its line. A
3851-
* closing tag's `</` stays a tag start, so an error stays at it. Returns
3852-
* whether the token is now `<`.
3851+
* closing tag's `</` stays a tag start, so an error stays at it. A
3852+
* comment glued to the `<` (`</*` or `<//`) is whitespace, as it is
3853+
* after a name, so that `<` is read again too. Returns whether the
3854+
* token is now `<`.
38533855
*/
38543856
#readTagStartAsTypeArgumentStart() {
38553857
if (this.type !== tstt.jsxTagStart) return false;
3856-
if (this.input.charCodeAt(this.start + 1) === CharCode.slash) return false;
3858+
if (this.input.charCodeAt(this.start + 1) === CharCode.slash) {
3859+
const after_slash = this.input.charCodeAt(this.start + 2);
3860+
if (after_slash !== CharCode.slash && after_slash !== CharCode.asterisk) return false;
3861+
}
38573862
this.#readTagStartAsTypeParameterStart();
38583863
return true;
38593864
}

‎packages/tsrx/tests/utils/tokenizer-context.test.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ describe('type arguments where a tag could start (#545, #578)', () => {
453453
/** @type {Array<[source: string, superClass: string, typeArguments: string, implements: string[]]>} */
454454
const cases = [
455455
['class A extends B\n<T> {}', 'B', '<T>', []],
456+
['class A extends B\n</* props */ T> {}', 'B', '</* props */ T>', []],
457+
['class A extends B\n<// props\nT> {}', 'B', '<// props\nT>', []],
456458
['class C extends (a || b)// d\n<T> {}', 'a || b', '<T>', []],
457459
['class A extends B.C\n <T, U>\n implements I\n{}', 'B.C', '<T, U>', ['I']],
458460
['const X = class extends B\n<T,> {};', 'B', '<T,>', []],
@@ -539,6 +541,46 @@ describe('type arguments where a tag could start (#545, #578)', () => {
539541
).toEqual(cases.map(([source]) => (source.includes('<string>') ? '<string>' : '<T>')));
540542
});
541543

544+
it('reads a comment glued to the `<` of a class or function expression', async () => {
545+
/** @type {Array<[source: string, path: string[], typeArguments: string]>} */
546+
const cases = [
547+
[
548+
'const A = class {}</* T */ string>;',
549+
['TSInstantiationExpression', 'ClassExpression'],
550+
'</* T */ string>',
551+
],
552+
[
553+
'const f = function () {}<// T\nstring>;',
554+
['TSInstantiationExpression', 'FunctionExpression'],
555+
'<// T\nstring>',
556+
],
557+
[
558+
'function App() @{\n const A = class {}</* T */ string>;\n <div />\n}',
559+
['TSInstantiationExpression', 'ClassExpression'],
560+
'</* T */ string>',
561+
],
562+
];
563+
const programs = await parse_all(cases.map(([source]) => source));
564+
expect(
565+
programs.map((program) => {
566+
const path = [];
567+
/** @type {(AST.Node & Record<string, any>) | undefined} */
568+
let node = value_of(statements(program)[0]);
569+
while (node) {
570+
path.push(node.type);
571+
node = node.expression ?? node.callee ?? node.tag ?? node.object;
572+
}
573+
return path;
574+
}),
575+
).toEqual(cases.map(([, path]) => path));
576+
expect(
577+
programs.map((program, index) => {
578+
const node = find(program, (type) => type === 'TSInstantiationExpression');
579+
return text(cases[index][0], node?.typeArguments);
580+
}),
581+
).toEqual(cases.map(([, , typeArguments]) => typeArguments));
582+
});
583+
542584
it('keeps a comparison, and an element on the next line, after a class or function expression', async () => {
543585
const [comparison, element] = await parse_all([
544586
// `(class {} < b) > c`, as in TypeScript.
@@ -567,6 +609,7 @@ describe('type arguments where a tag could start (#545, #578)', () => {
567609
// TypeScript reports these at the same token.
568610
['const f = function () {} <div />;', 'Unexpected token', '>;'],
569611
['x = class {}<div>a</div>;', 'Unexpected token', '</'],
612+
['x = class {}</div>;', 'Unexpected token', '</'],
570613
['x = <div>{function () {}</div>', "'}' expected.", '</'],
571614
]);
572615
});

0 commit comments

Comments
 (0)