@@ -28,27 +28,42 @@ 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+ const check1 = ( commentStart === start && commentEnd === end ) ;
44+ const check2 = ( commentStart === start + 1 && commentEnd === end - 1 ) ;
45+ const check3 = ( commentStart === start + 7 && commentEnd === end - 1 ) ;
46+
47+ return check1 || check2 || check3 ;
48+ } ) ;
49+
50+ }
51+
3152/** Traverses the AST and replaces the transformed template parts with other AST */
3253function convertAst ( ast : File , templates : Template [ ] ) : void {
3354 traverse ( ast , {
3455 enter ( path ) {
56+ if ( templates . length === 0 ) {
57+ return null ;
58+ }
59+
3560 const { node } = path ;
3661
3762 switch ( node . type ) {
3863 case 'BlockStatement' :
3964 case 'ObjectExpression' :
4065 case 'StaticBlock' : {
41- if (
42- ! node . range &&
43- typeof node . start === 'number' &&
44- typeof node . end === 'number'
45- ) {
46- // prettier 3.6.0 onwards doesn't have `node.range`
47- // as it was removed in babel
48- node . range = [ node . start , node . end ] ;
49- }
50- assert ( 'expected range' , node . range ) ;
51- const [ start , end ] = node . range ;
66+ const [ start , end ] = [ typescript . locStart ( node ) , typescript . locEnd ( node ) ] ;
5267
5368 const templateIndex = templates . findIndex ( ( template ) => {
5469 const { utf16Range } = template ;
@@ -76,12 +91,16 @@ function convertAst(ast: File, templates: Template[]): void {
7691 ) ;
7792 }
7893
79- const index =
80- node . innerComments ?. [ 0 ] &&
81- ast . comments ?. indexOf ( node . innerComments [ 0 ] ) ;
94+ if ( ast . comments && ast . comments . length > 0 ) {
95+ const commentBlockIndex = findCorrectCommentBlockIndex (
96+ ast . comments ,
97+ start ,
98+ end ,
99+ ) ;
82100
83- if ( ast . comments && index !== undefined && index >= 0 ) {
84- ast . comments . splice ( index , 1 ) ;
101+ if ( commentBlockIndex !== undefined && commentBlockIndex >= 0 ) {
102+ ast . comments . splice ( commentBlockIndex , 1 ) ;
103+ }
85104 }
86105
87106 convertNode ( node , rawTemplate ) ;
0 commit comments