@@ -24,63 +24,77 @@ export function preaction() {
2424 if ( ! script ) continue ;
2525 const str = s . slice ( script . start , script . end ) ;
2626 if ( ! str . includes ( PACKAGE_NAME ) ) continue ;
27- walk ( /** @type {import('estree').ImportDeclaration } */ ( /** @type {unknown } */ ( script ) ) , null , {
28- ImportDeclaration ( node , { next } ) {
29- if ( node . source . value !== PACKAGE_NAME ) return next ( ) ;
30- const applyAction = node . specifiers . find (
31- ( specifier ) => specifier . type === 'ImportSpecifier' && specifier . imported . name === 'apply' ,
32- ) ;
33- if ( applyAction ) applyActionName = applyAction . local . name ;
27+ walk (
28+ /** @type {import('estree').ImportDeclaration } */ ( /** @type {unknown } */ ( script ) ) ,
29+ null ,
30+ {
31+ ImportDeclaration ( node , { next } ) {
32+ if ( node . source . value !== PACKAGE_NAME ) return next ( ) ;
33+ const applyAction = node . specifiers . find (
34+ ( specifier ) =>
35+ specifier . type === 'ImportSpecifier' &&
36+ specifier . imported . type === 'Identifier' &&
37+ specifier . imported . name === 'apply' ,
38+ ) ;
39+ if ( applyAction ) applyActionName = applyAction . local . name ;
40+ } ,
3441 } ,
35- } ) ;
42+ ) ;
3643 break ;
3744 }
3845 if ( ! applyActionName ) return ;
3946
4047 /** @type {string[] } */
4148 const declarations = [ ] ;
4249
43- // TODO: adjust typing when https://github.com/sveltejs/svelte/issues/12292 is resolved
44- // walk(/** @type {import('svelte/compiler').ElementLike } */ (ast.fragment), null, {
45- walk ( /** @type {any } */ ( ast . fragment ) , null , {
46- RegularElement ( node , { next } ) {
47- const uses = node . attributes . filter (
48- ( attr ) => attr . type === 'UseDirective' && attr . name === applyActionName ,
49- ) ;
50- if ( ! uses . length ) return next ( ) ;
51-
52- const firstAttribute = node . attributes [ 0 ] ;
53-
54- for ( const use of uses ) {
55- // 0 - generate unique name for eaction
56- const name = `__preaction_${ count ++ } ` ;
57-
58- // 1 - add to declarations: const preaction_#count = preaction(param)
59- const expression = s . slice ( use . expression . start , use . expression . end ) ;
60- declarations . push ( `const ${ name } = ${ expression } ` ) ;
61-
62- // 2 - transform use directive: use:preaction_#count.action={param}
63- const arg = use . expression . arguments [ 0 ] ;
64- let argStr = arg ? `={${ s . slice ( arg . start , arg . end ) } }` : '' ;
65- s . overwrite ( use . start , use . end , `use:${ name } .action${ argStr } ` ) ;
66-
67- // 3 - add attribute spread to start of tag: {...preaction_#count.attributes}
68- s . prependLeft (
69- firstAttribute . start ,
70- `{...(${ name } .attributes ?? {})} ` ,
50+ walk (
51+ /** @type {import('svelte/compiler').AST.RegularElement } */ (
52+ /** @type {unknown } */ ( ast . fragment )
53+ ) ,
54+ null ,
55+ {
56+ RegularElement ( node , { next } ) {
57+ const uses = /** @type {import('svelte/compiler').AST.UseDirective[] } */ (
58+ node . attributes . filter (
59+ ( attr ) => attr . type === 'UseDirective' && attr . name === applyActionName ,
60+ )
7161 ) ;
72- }
62+ if ( ! uses . length ) return next ( ) ;
63+
64+ const firstAttribute = node . attributes [ 0 ] ;
65+
66+ for ( const use of uses ) {
67+ // 0 - generate unique name for preaction
68+ const name = `__preaction_${ count ++ } ` ;
69+
70+ if ( ! use . expression || use . expression . type !== 'CallExpression' ) continue ;
71+
72+ // 1 - add to declarations: const preaction_#count = preaction(param)
73+ const { start : expStart , end : expEnd } = /** @type {any } */ ( use . expression ) ;
74+ const expression = s . slice ( expStart , expEnd ) ;
75+ declarations . push ( `const ${ name } = ${ expression } ` ) ;
7376
74- return next ( ) ;
77+ // 2 - transform use directive: use:preaction_#count.action={param}
78+ const arg = use . expression . arguments [ 0 ] ;
79+ const { start : argStart , end : argEnd } = /** @type {any } */ ( arg ) ;
80+ let argStr = arg ? `={${ s . slice ( argStart , argEnd ) } }` : '' ;
81+ s . overwrite ( use . start , use . end , `use:${ name } .action${ argStr } ` ) ;
82+
83+ // 3 - add attribute spread to start of tag: {...preaction_#count.attributes}
84+ s . prependLeft ( firstAttribute . start , `{...(${ name } .attributes ?? {})} ` ) ;
85+ }
86+
87+ return next ( ) ;
88+ } ,
7589 } ,
76- } ) ;
90+ ) ;
7791
7892 // add declarations to end of script tag
7993 const declarationsStr = '\n' + declarations . join ( '\n' ) + '\n' ;
8094 if ( ast . instance ) {
81- s . prependLeft ( /** @type {any } */ ( ast . instance . content ) . end , declarationsStr )
95+ s . prependLeft ( /** @type {any } */ ( ast . instance . content ) . end , declarationsStr ) ;
8296 } else {
83- s . append ( `<script>${ declarationsStr } </script>` )
97+ s . append ( `<script>${ declarationsStr } </script>` ) ;
8498 }
8599
86100 return {
@@ -92,4 +106,3 @@ export function preaction() {
92106}
93107
94108export default preaction ;
95-
0 commit comments