@@ -322,7 +322,10 @@ const instance_script = {
322
322
state . str . prependLeft ( /** @type {number } */ ( declarator . init . start ) , '$state(' ) ;
323
323
state . str . appendRight ( /** @type {number } */ ( declarator . init . end ) , ')' ) ;
324
324
} else {
325
- state . str . prependLeft ( /** @type {number } */ ( declarator . id . end ) , ' = $state()' ) ;
325
+ state . str . prependLeft (
326
+ /** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
327
+ ' = $state()'
328
+ ) ;
326
329
}
327
330
}
328
331
@@ -587,13 +590,13 @@ function extract_type_and_comment(declarator, str, path) {
587
590
}
588
591
589
592
/**
590
- * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').SvelteWindow | import('#compiler').SvelteDocument | import('#compiler').SvelteBody } node
593
+ * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').SvelteWindow | import('#compiler').SvelteDocument | import('#compiler').SvelteBody } element
591
594
* @param {State } state
592
595
*/
593
- function handle_events ( node , state ) {
596
+ function handle_events ( element , state ) {
594
597
/** @type {Map<string, import('#compiler').OnDirective[]> } */
595
598
const handlers = new Map ( ) ;
596
- for ( const attribute of node . attributes ) {
599
+ for ( const attribute of element . attributes ) {
597
600
if ( attribute . type !== 'OnDirective' ) continue ;
598
601
599
602
let name = `on${ attribute . name } ` ;
@@ -625,6 +628,7 @@ function handle_events(node, state) {
625
628
626
629
for ( let i = 0 ; i < nodes . length - 1 ; i += 1 ) {
627
630
const node = nodes [ i ] ;
631
+ const indent = get_indent ( state , node , element ) ;
628
632
if ( node . expression ) {
629
633
let body = '' ;
630
634
if ( node . expression . type === 'ArrowFunctionExpression' ) {
@@ -638,19 +642,20 @@ function handle_events(node, state) {
638
642
/** @type {number } */ ( node . expression . end )
639
643
) } ();`;
640
644
}
641
- // TODO check how many indents needed
645
+
642
646
for ( const modifier of node . modifiers ) {
643
647
if ( modifier === 'stopPropagation' ) {
644
- body = `\n${ state . indent } ${ payload_name } .stopPropagation();\n${ body } ` ;
648
+ body = `\n${ indent } ${ payload_name } .stopPropagation();\n${ body } ` ;
645
649
} else if ( modifier === 'preventDefault' ) {
646
- body = `\n${ state . indent } ${ payload_name } .preventDefault();\n${ body } ` ;
650
+ body = `\n${ indent } ${ payload_name } .preventDefault();\n${ body } ` ;
647
651
} else if ( modifier === 'stopImmediatePropagation' ) {
648
- body = `\n${ state . indent } ${ payload_name } .stopImmediatePropagation();\n${ body } ` ;
652
+ body = `\n${ indent } ${ payload_name } .stopImmediatePropagation();\n${ body } ` ;
649
653
} else {
650
- body = `\n${ state . indent } // @migration-task: incorporate ${ modifier } modifier\n${ body } ` ;
654
+ body = `\n${ indent } // @migration-task: incorporate ${ modifier } modifier\n${ body } ` ;
651
655
}
652
656
}
653
- prepend += `\n${ state . indent } ${ body } \n` ;
657
+
658
+ prepend += `\n${ indent } ${ body } \n` ;
654
659
} else {
655
660
if ( ! local ) {
656
661
local = state . scope . generate ( `on${ node . name } ` ) ;
@@ -663,7 +668,7 @@ function handle_events(node, state) {
663
668
type : '(event: any) => void'
664
669
} ) ;
665
670
}
666
- prepend += `\n${ state . indent } ${ local } ?.(${ payload_name } );\n` ;
671
+ prepend += `\n${ indent } ${ local } ?.(${ payload_name } );\n` ;
667
672
}
668
673
669
674
state . str . remove ( node . start , node . end ) ;
@@ -683,15 +688,17 @@ function handle_events(node, state) {
683
688
state . str . appendRight ( last . start + last . name . length + 3 , 'capture' ) ;
684
689
}
685
690
691
+ const indent = get_indent ( state , last , element ) ;
692
+
686
693
for ( const modifier of last . modifiers ) {
687
694
if ( modifier === 'stopPropagation' ) {
688
- prepend += `\n${ state . indent } ${ payload_name } .stopPropagation();\n` ;
695
+ prepend += `\n${ indent } ${ payload_name } .stopPropagation();\n` ;
689
696
} else if ( modifier === 'preventDefault' ) {
690
- prepend += `\n${ state . indent } ${ payload_name } .preventDefault();\n` ;
697
+ prepend += `\n${ indent } ${ payload_name } .preventDefault();\n` ;
691
698
} else if ( modifier === 'stopImmediatePropagation' ) {
692
- prepend += `\n${ state . indent } ${ payload_name } .stopImmediatePropagation();\n` ;
699
+ prepend += `\n${ indent } ${ payload_name } .stopImmediatePropagation();\n` ;
693
700
} else if ( modifier !== 'capture' ) {
694
- prepend += `\n${ state . indent } // @migration-task: incorporate ${ modifier } modifier\n` ;
701
+ prepend += `\n${ indent } // @migration-task: incorporate ${ modifier } modifier\n` ;
695
702
}
696
703
}
697
704
@@ -723,17 +730,20 @@ function handle_events(node, state) {
723
730
pos = /** @type {number } */ ( pos ) + ( needs_curlies ? 0 : 1 ) ;
724
731
if ( needs_curlies && state . str . original [ pos - 1 ] === '(' ) {
725
732
// Prettier does something like on:click={() => (foo = true)}, we need to remove the braces in this case
726
- state . str . update ( pos - 1 , pos , `{${ prepend } ${ state . indent } ` ) ;
727
- state . str . update ( end , end + 1 , '\n}' ) ;
733
+ state . str . update ( pos - 1 , pos , `{${ prepend } ${ indent } ` ) ;
734
+ state . str . update ( end , end + 1 , `\n ${ indent . slice ( state . indent . length ) } }` ) ;
728
735
} else {
729
- state . str . prependRight ( pos , `${ needs_curlies ? '{' : '' } ${ prepend } ${ state . indent } ` ) ;
730
- state . str . appendRight ( end , `\n${ needs_curlies ? '}' : '' } ` ) ;
736
+ state . str . prependRight ( pos , `${ needs_curlies ? '{' : '' } ${ prepend } ${ indent } ` ) ;
737
+ state . str . appendRight (
738
+ end ,
739
+ `\n${ indent . slice ( state . indent . length ) } ${ needs_curlies ? '}' : '' } `
740
+ ) ;
731
741
}
732
742
} else {
733
743
state . str . update (
734
744
/** @type {number } */ ( last . expression . start ) ,
735
745
/** @type {number } */ ( last . expression . end ) ,
736
- `(${ payload_name } ) => {${ prepend } \n${ state . indent } ${ state . str . original . substring (
746
+ `(${ payload_name } ) => {${ prepend } \n${ indent } ${ state . str . original . substring (
737
747
/** @type {number } */ ( last . expression . start ) ,
738
748
/** @type {number } */ ( last . expression . end )
739
749
) } ?.(${ payload_name } );\n}`
@@ -771,6 +781,29 @@ function handle_events(node, state) {
771
781
}
772
782
}
773
783
784
+ /**
785
+ * Returns the next indentation level of the first node that has all-whitespace before it
786
+ * @param {State } state
787
+ * @param {Array<{start: number; end: number}> } nodes
788
+ */
789
+ function get_indent ( state , ...nodes ) {
790
+ let indent = state . indent ;
791
+
792
+ for ( const node of nodes ) {
793
+ const line_start = state . str . original . lastIndexOf ( '\n' , node . start ) ;
794
+ indent = state . str . original . substring ( line_start + 1 , node . start ) ;
795
+
796
+ if ( indent . trim ( ) === '' ) {
797
+ indent = state . indent + indent ;
798
+ return indent ;
799
+ } else {
800
+ indent = state . indent ;
801
+ }
802
+ }
803
+
804
+ return indent ;
805
+ }
806
+
774
807
/**
775
808
* @param {import('#compiler').OnDirective } last
776
809
* @param {State } state
0 commit comments