Skip to content

Commit f44d075

Browse files
committed
fix(core): open block comments at the outer /* when looking back
A /* inside a block comment is text. lastIndexOf stopped at that inner opener, so a < after a semicolon-less statement was tokenized as less-than instead of a tag.
1 parent 32c60da commit f44d075

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎packages/tsrx/src/plugin.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,22 @@ export function TSRXPlugin(config) {
902902
this.input.charCodeAt(cursor) === CharCode.slash &&
903903
this.input.charCodeAt(cursor - 1) === CharCode.asterisk
904904
) {
905-
const open = this.input.lastIndexOf('/*', cursor - 3);
906-
// Only a comment in the gap before `index` counts: from its start, only
907-
// whitespace and comments may lead to `index`. A `*/` that ends a regex
908-
// or a string's text fails this check.
905+
// Comments do not nest, so this `*/` closes the earliest `/*` in the
906+
// gap after the previous token, not the nearest one
907+
// (`/* mention /* here */`). `this.end` is still that token's end:
908+
// finishToken has not run for the token at `index`. Only a comment in
909+
// the gap counts: from its start, only whitespace and comments may
910+
// lead to `index`. A `*/` that ends a regex or a string sits inside
911+
// the previous token and fails this check.
912+
let open = -1;
913+
let searchFrom = cursor - 3;
914+
while (searchFrom >= this.end) {
915+
const found = this.input.lastIndexOf('/*', searchFrom);
916+
if (found < this.end) break;
917+
if (this.input.indexOf('*/', found + 2) !== cursor - 1) break;
918+
open = found;
919+
searchFrom = found - 1;
920+
}
909921
if (open === -1 || skip_space_and_comments_from(this.input, open) !== index) break;
910922
for (let i = cursor - 2; i > open + 1; i--) {
911923
if (this.#isNewlineCharCode(i)) return i;

‎packages/tsrx/tests/utils/parser.test.js‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,15 @@ describe('TSRX parser', () => {
648648
});
649649

650650
it('starts an element after comments on the line after a semicolon-less statement', () => {
651-
for (const comment of ['/* render */', '/* a */ /* b */', '// note\n /* render */']) {
651+
for (const comment of [
652+
'/* render */',
653+
'/* a */ /* b */',
654+
'// note\n /* render */',
655+
// A `/*` inside the comment is text. The opener is the outer `/*`.
656+
'/* mention /* here */',
657+
'/** jsdoc mentions /* */',
658+
'/* a */ /* mention /* here */',
659+
]) {
652660
const block = findNode(
653661
`export function App() @{\n const x = a\n ${comment} <div />\n}`,
654662
'JSXCodeBlock',
@@ -666,12 +674,15 @@ describe('TSRX parser', () => {
666674
expect(program.body.map((node) => node.type)).toEqual(['ExpressionStatement', 'JSXElement']);
667675
}
668676

669-
// A comment that spans lines separates the statements like a line break.
670-
const block = findNode(
677+
// A comment that spans lines separates the statements like a line break,
678+
// including when a `/*` in the comment sits after that line break.
679+
for (const source of [
671680
'export function App() @{\n const x = a /* a\n b */ <div />\n}',
672-
'JSXCodeBlock',
673-
);
674-
expect(codeBlockRender(block).type).toBe('JSXElement');
681+
'export function App() @{\n const x = a /* mention\n still /* here */ <div />\n}',
682+
]) {
683+
const block = findNode(source, 'JSXCodeBlock');
684+
expect(codeBlockRender(block).type).toBe('JSXElement');
685+
}
675686
});
676687

677688
it('keeps a `<` after a comment on the same line as the previous value a comparison', () => {
@@ -683,6 +694,25 @@ describe('TSRX parser', () => {
683694
expect(() =>
684695
parseModule('export function App() @{\n const x = a /* note */ <div />\n}', 'App.tsrx'),
685696
).toThrow();
697+
expect(() =>
698+
parseModule('export function App() @{\n const x = a /* note /* x */ <div />\n}', 'App.tsrx'),
699+
).toThrow();
700+
// A `/*` inside an earlier string is not the comment opener, so a `<div />`
701+
// on the same line as the value is still not an element.
702+
expect(() =>
703+
parseModule(
704+
'export function App() @{\n const x = "/*" +\n a /* note /* x */ <div />\n}',
705+
'App.tsrx',
706+
),
707+
).toThrow();
708+
const compared = firstStatement(
709+
parseModule('x = "/*" +\na /* note /* x */ < b', 'App.tsrx'),
710+
'ExpressionStatement',
711+
);
712+
expect(
713+
as_type(as_type(compared.expression, 'AssignmentExpression').right, 'BinaryExpression')
714+
.operator,
715+
).toBe('<');
686716
});
687717

688718
it('parses mixed scalar and JSX return branches', () => {

0 commit comments

Comments
 (0)