@@ -19,7 +19,7 @@ import {
1919 isTypeScript ,
2020 printRaw ,
2121} from './print/node-helpers' ;
22- import { CommentNode , ElementNode , Node , ScriptNode , StyleNode } from './print/nodes' ;
22+ import { BaseNode , CommentNode , ElementNode , Node , ScriptNode , StyleNode } from './print/nodes' ;
2323import { extractAttributes } from './lib/extractAttributes' ;
2424import { base64ToString } from './base64-string' ;
2525
@@ -105,7 +105,8 @@ export function embed(path: FastPath, _options: Options) {
105105 ) + 1 ;
106106 parent . context = null ;
107107 parent . parameters = null ;
108- printSvelteBlockJS ( 'expression' ) ;
108+ node . isJS = true ;
109+ node . asFunction = true ;
109110 }
110111 break ;
111112 case 'Element' :
@@ -129,7 +130,9 @@ export function embed(path: FastPath, _options: Options) {
129130 parent . expression . end =
130131 options . originalText . indexOf (
131132 ')' ,
132- parent . argument ?. end ?? parent . expression . end ,
133+ parent . argument ?. end ?? // TODO: remove at some point, snippet API changed in .next-..
134+ parent . arguments ?. [ parent . arguments . length - 1 ] ?. end ??
135+ parent . expression . end ,
133136 ) + 1 ;
134137 parent . argument = null ;
135138 printJS ( parent , false , false , false , 'expression' ) ;
@@ -157,14 +160,14 @@ export function embed(path: FastPath, _options: Options) {
157160 // so we need to have another public parser and defer to that
158161 parser : 'svelteExpressionParser' ,
159162 singleQuote : node . forceSingleQuote ? true : options . singleQuote ,
163+ _svelte_asFunction : node . asFunction ,
160164 } ;
161165
166+ // If we have snipped content, it was done wrongly and we need to unsnip it.
167+ // This happens for example for {@html `<script>{foo}</script>` }
168+ const text = getText ( node , options , true ) ;
162169 let docs = await textToDoc (
163- forceIntoExpression (
164- // If we have snipped content, it was done wrongly and we need to unsnip it.
165- // This happens for example for {@html `<script>{foo}</script>` }
166- getText ( node , options , true ) ,
167- ) ,
170+ node . asFunction ? forceIntoFunction ( text ) : forceIntoExpression ( text ) ,
168171 embeddedOptions ,
169172 ) ;
170173 if ( node . forceSingleLine ) {
@@ -173,6 +176,14 @@ export function embed(path: FastPath, _options: Options) {
173176 if ( node . removeParentheses ) {
174177 docs = removeParentheses ( docs ) ;
175178 }
179+ if ( node . asFunction ) {
180+ if ( Array . isArray ( docs ) && typeof docs [ 0 ] === 'string' ) {
181+ docs [ 0 ] = docs [ 0 ] . replace ( 'function ' , '' ) ;
182+ docs . splice ( - 1 , 1 ) ;
183+ } else {
184+ throw new Error ( 'Prettier AST changed, asFunction logic needs to change' ) ;
185+ }
186+ }
176187 return docs ;
177188 } catch ( e ) {
178189 return getText ( node , options , true ) ;
@@ -240,6 +251,10 @@ function forceIntoExpression(statement: string) {
240251 return `(${ statement } \n)` ;
241252}
242253
254+ function forceIntoFunction ( statement : string ) {
255+ return `function ${ statement } {}` ;
256+ }
257+
243258function preformattedBody ( str : string ) : Doc {
244259 if ( ! str ) {
245260 return '' ;
@@ -382,11 +397,12 @@ function printJS(
382397 removeParentheses : boolean ,
383398 name : string ,
384399) {
385- if ( ! node [ name ] || typeof node [ name ] !== 'object' ) {
400+ const part = node [ name ] as BaseNode | undefined ;
401+ if ( ! part || typeof part !== 'object' ) {
386402 return ;
387403 }
388- node [ name ] . isJS = true ;
389- node [ name ] . forceSingleQuote = forceSingleQuote ;
390- node [ name ] . forceSingleLine = forceSingleLine ;
391- node [ name ] . removeParentheses = removeParentheses ;
404+ part . isJS = true ;
405+ part . forceSingleQuote = forceSingleQuote ;
406+ part . forceSingleLine = forceSingleLine ;
407+ part . removeParentheses = removeParentheses ;
392408}
0 commit comments