1
1
import { FastPath , Doc , doc , ParserOptions } from 'prettier' ;
2
- import { Node , MustacheTagNode , IfBlockNode } from './nodes' ;
2
+ import { Node , IfBlockNode , AttributeNode } from './nodes' ;
3
3
import { isASTNode , isPreTagContent } from './helpers' ;
4
4
import { extractAttributes } from '../lib/extractAttributes' ;
5
5
import { getText } from '../lib/getText' ;
@@ -12,6 +12,8 @@ import {
12
12
isInlineElement ,
13
13
isInlineNode ,
14
14
isEmptyNode ,
15
+ isLoneMustacheTag ,
16
+ isOrCanBeConvertedToShorthand ,
15
17
} from './node-helpers' ;
16
18
import {
17
19
isLine ,
@@ -224,43 +226,26 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
224
226
return node . expression . name ;
225
227
}
226
228
case 'Attribute' : {
227
- const hasLoneMustacheTag =
228
- node . value !== true &&
229
- node . value . length === 1 &&
230
- node . value [ 0 ] . type === 'MustacheTag' ;
231
- let isAttributeShorthand =
232
- node . value !== true &&
233
- node . value . length === 1 &&
234
- node . value [ 0 ] . type === 'AttributeShorthand' ;
235
-
236
- // Convert a={a} into {a}
237
- if ( hasLoneMustacheTag ) {
238
- const expression = ( node . value as [ MustacheTagNode ] ) [ 0 ] . expression ;
239
- isAttributeShorthand =
240
- expression . type === 'Identifier' && expression . name === node . name ;
241
- }
242
-
243
- if ( isAttributeShorthand && options . svelteAllowShorthand ) {
244
- return concat ( [ line , '{' , node . name , '}' ] ) ;
229
+ if ( isOrCanBeConvertedToShorthand ( node ) ) {
230
+ if ( options . svelteStrictMode ) {
231
+ return concat ( [ line , node . name , '="{' , node . name , '}"' ] ) ;
232
+ } else if ( options . svelteAllowShorthand ) {
233
+ return concat ( [ line , '{' , node . name , '}' ] ) ;
234
+ } else {
235
+ return concat ( [ line , node . name , '={' , node . name , '}' ] ) ;
236
+ }
245
237
} else {
246
- const def : Doc [ ] = [ line , node . name ] ;
247
- if ( node . value !== true ) {
248
- def . push ( '=' ) ;
249
- const quotes = ! hasLoneMustacheTag || options . svelteStrictMode ;
250
-
251
- quotes && def . push ( '"' ) ;
252
-
253
- const valueDocs = path . map ( ( childPath ) => childPath . call ( print ) , 'value' ) ;
254
-
255
- if ( ! quotes || ! formattableAttributes . includes ( node . name ) ) {
256
- def . push ( concat ( valueDocs ) ) ;
257
- } else {
258
- def . push ( indent ( group ( concat ( trim ( valueDocs , isLine ) ) ) ) ) ;
259
- }
238
+ if ( node . value === true ) {
239
+ return concat ( [ line , node . name ] ) ;
240
+ }
260
241
261
- quotes && def . push ( '"' ) ;
242
+ const quotes = ! isLoneMustacheTag ( node . value ) || options . svelteStrictMode ;
243
+ const attrNodeValue = printAttributeNodeValue ( path , print , quotes , node ) ;
244
+ if ( quotes ) {
245
+ return concat ( [ line , node . name , '=' , '"' , attrNodeValue , '"' ] ) ;
246
+ } else {
247
+ return concat ( [ line , node . name , '=' , attrNodeValue ] ) ;
262
248
}
263
- return concat ( def ) ;
264
249
}
265
250
}
266
251
case 'MustacheTag' :
@@ -488,6 +473,21 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
488
473
throw new Error ( 'unknown node type: ' + node . type ) ;
489
474
}
490
475
476
+ function printAttributeNodeValue (
477
+ path : FastPath < any > ,
478
+ print : PrintFn ,
479
+ quotes : boolean ,
480
+ node : AttributeNode ,
481
+ ) {
482
+ const valueDocs = path . map ( ( childPath ) => childPath . call ( print ) , 'value' ) ;
483
+
484
+ if ( ! quotes || ! formattableAttributes . includes ( node . name ) ) {
485
+ return concat ( valueDocs ) ;
486
+ } else {
487
+ return indent ( group ( concat ( trim ( valueDocs , isLine ) ) ) ) ;
488
+ }
489
+ }
490
+
491
491
function printChildren ( path : FastPath , print : PrintFn ) : Doc [ ] {
492
492
let childDocs : Doc [ ] = [ ] ;
493
493
let currentGroup : { doc : Doc ; node : Node } [ ] = [ ] ;
0 commit comments