@@ -28,18 +28,44 @@ function convertNode(
2828 } ) ;
2929}
3030
31+ function findCorrectCommentBlockIndex (
32+ comments : File [ 'comments' ] ,
33+ start : number ,
34+ end : number ,
35+ ) : number {
36+ if ( ! comments ) {
37+ return - 1 ;
38+ }
39+
40+ return comments . findIndex ( ( comment ) => {
41+ const { start : commentStart , end : commentEnd } = comment ;
42+
43+ return (
44+ ( commentStart === start && commentEnd === end ) ||
45+ ( commentStart === start + 1 && commentEnd === end - 1 ) ||
46+ ( commentStart === start + 7 && commentEnd === end - 1 )
47+ ) ;
48+ } ) ;
49+ }
50+
3151/** Traverses the AST and replaces the transformed template parts with other AST */
3252function convertAst ( ast : File , templates : Template [ ] ) : void {
3353 traverse ( ast , {
3454 enter ( path ) {
55+ if ( templates . length === 0 ) {
56+ return null ;
57+ }
58+
3559 const { node } = path ;
3660
3761 switch ( node . type ) {
3862 case 'BlockStatement' :
3963 case 'ObjectExpression' :
4064 case 'StaticBlock' : {
41- assert ( 'expected range' , node . range ) ;
42- const [ start , end ] = node . range ;
65+ const [ start , end ] = [
66+ typescript . locStart ( node ) ,
67+ typescript . locEnd ( node ) ,
68+ ] ;
4369
4470 const templateIndex = templates . findIndex ( ( template ) => {
4571 const { utf16Range } = template ;
@@ -67,12 +93,16 @@ function convertAst(ast: File, templates: Template[]): void {
6793 ) ;
6894 }
6995
70- const index =
71- node . innerComments ?. [ 0 ] &&
72- ast . comments ?. indexOf ( node . innerComments [ 0 ] ) ;
96+ if ( ast . comments && ast . comments . length > 0 ) {
97+ const commentBlockIndex = findCorrectCommentBlockIndex (
98+ ast . comments ,
99+ start ,
100+ end ,
101+ ) ;
73102
74- if ( ast . comments && index !== undefined && index >= 0 ) {
75- ast . comments . splice ( index , 1 ) ;
103+ if ( commentBlockIndex !== undefined && commentBlockIndex >= 0 ) {
104+ ast . comments . splice ( commentBlockIndex , 1 ) ;
105+ }
76106 }
77107
78108 convertNode ( node , rawTemplate ) ;
0 commit comments