@@ -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