@@ -13,7 +13,6 @@ import type {
13
13
SvelteStartTag ,
14
14
SvelteName ,
15
15
SvelteStyleDirective ,
16
- SvelteMustacheTagText ,
17
16
SvelteStyleDirectiveLongform ,
18
17
SvelteStyleDirectiveShorthand ,
19
18
} from "../../ast"
@@ -22,9 +21,13 @@ import type { Context } from "../../context"
22
21
import type * as SvAST from "../svelte-ast-types"
23
22
import { getWithLoc , indexOf } from "./common"
24
23
import { convertMustacheTag } from "./mustache"
25
- import { convertTemplateLiteralToLiteral , convertTextToLiteral } from "./text"
24
+ import {
25
+ convertAttributeValueTokenToLiteral ,
26
+ convertTextToLiteral ,
27
+ } from "./text"
26
28
import { ParseError } from "../../errors"
27
29
import type { ScriptLetCallback } from "../../context/script-let"
30
+ import type { AttributeToken } from "../html"
28
31
29
32
/** Convert for Attributes */
30
33
export function * convertAttributes (
@@ -79,13 +82,16 @@ export function* convertAttributes(
79
82
yield convertLetDirective ( attr , parent , ctx )
80
83
continue
81
84
}
82
- if ( attr . type === "Style" ) {
83
- yield convertOldStyleDirective ( attr , parent , ctx )
84
- continue
85
- }
86
85
if ( attr . type === "Ref" ) {
87
86
throw new ParseError ( "Ref are not supported." , attr . start , ctx )
88
87
}
88
+ if ( ( attr as any ) . type === "Style" ) {
89
+ throw new ParseError (
90
+ `Svelte v3.46.0 is no longer supported. Please use Svelte>=v3.46.1.` ,
91
+ attr . start ,
92
+ ctx ,
93
+ )
94
+ }
89
95
throw new ParseError (
90
96
`Unknown directive or attribute (${ attr . type } ) are not supported.` ,
91
97
attr . start ,
@@ -94,6 +100,42 @@ export function* convertAttributes(
94
100
}
95
101
}
96
102
103
+ /** Convert for attribute tokens */
104
+ export function * convertAttributeTokens (
105
+ attributes : AttributeToken [ ] ,
106
+ parent : SvelteStartTag ,
107
+ ctx : Context ,
108
+ ) : IterableIterator < SvelteAttribute > {
109
+ for ( const attr of attributes ) {
110
+ const attribute : SvelteAttribute = {
111
+ type : "SvelteAttribute" ,
112
+ boolean : false ,
113
+ key : null as any ,
114
+ value : [ ] ,
115
+ parent,
116
+ ...ctx . getConvertLocation ( {
117
+ start : attr . key . start ,
118
+ end : attr . value ?. end ?? attr . key . end ,
119
+ } ) ,
120
+ }
121
+ attribute . key = {
122
+ type : "SvelteName" ,
123
+ name : attr . key . name ,
124
+ parent : attribute ,
125
+ ...ctx . getConvertLocation ( attr . key ) ,
126
+ }
127
+ ctx . addToken ( "HTMLIdentifier" , attr . key )
128
+ if ( attr . value == null ) {
129
+ attribute . boolean = true
130
+ } else {
131
+ attribute . value . push (
132
+ convertAttributeValueTokenToLiteral ( attr . value , attribute , ctx ) ,
133
+ )
134
+ }
135
+ yield attribute
136
+ }
137
+ }
138
+
97
139
/** Convert for Attribute */
98
140
function convertAttribute (
99
141
node : SvAST . Attribute ,
@@ -354,100 +396,6 @@ function convertStyleDirective(
354
396
return directive
355
397
}
356
398
357
- /** Convert for Style Directive for svelte v3.46.0 */
358
- function convertOldStyleDirective (
359
- node : SvAST . DirectiveForExpression ,
360
- parent : SvelteStyleDirective [ "parent" ] ,
361
- ctx : Context ,
362
- ) : SvelteStyleDirective {
363
- const directive : SvelteStyleDirective = {
364
- type : "SvelteStyleDirective" ,
365
- key : null as any ,
366
- shorthand : false ,
367
- value : [ ] ,
368
- parent,
369
- ...ctx . getConvertLocation ( node ) ,
370
- }
371
- processDirectiveKey ( node , directive , ctx )
372
- if ( processStyleDirectiveValue ( node , ctx ) ) {
373
- processDirectiveExpression ( node , directive , ctx , {
374
- processExpression ( expression ) {
375
- directive . value . push (
376
- convertTemplateLiteralToLiteral ( expression , directive , ctx ) ,
377
- )
378
- return [ ]
379
- } ,
380
- } )
381
- } else {
382
- processDirectiveExpression ( node , directive , ctx , {
383
- processExpression ( expression , shorthand ) {
384
- ; ( directive as any ) . shorthand = shorthand
385
- return ctx . scriptLet . addExpression (
386
- expression ,
387
- directive ,
388
- null ,
389
- ( e ) => {
390
- const mustache : SvelteMustacheTagText = {
391
- type : "SvelteMustacheTag" ,
392
- kind : "text" ,
393
- expression : e ,
394
- parent : directive ,
395
- ...ctx . getConvertLocation ( {
396
- start : ctx . code . lastIndexOf ( "{" , e . range ! [ 0 ] ) ,
397
- end : ctx . code . indexOf ( "}" , e . range ! [ 0 ] ) + 1 ,
398
- } ) ,
399
- }
400
- directive . value . push ( mustache )
401
- } ,
402
- )
403
- } ,
404
- } )
405
- }
406
-
407
- return directive
408
- }
409
-
410
- /** Process plain value */
411
- function processStyleDirectiveValue (
412
- node : SvAST . DirectiveForExpression ,
413
- ctx : Context ,
414
- ) : node is SvAST . DirectiveForExpression & {
415
- expression : ESTree . TemplateLiteral
416
- } {
417
- const { expression } = node
418
- if (
419
- ! expression ||
420
- expression . type !== "TemplateLiteral" ||
421
- expression . expressions . length !== 0
422
- ) {
423
- return false
424
- }
425
- const quasi = expression . quasis [ 0 ]
426
- if ( quasi . value . cooked != null ) {
427
- return false
428
- }
429
- const eqIndex = ctx . code . indexOf ( "=" , node . start )
430
- if ( eqIndex < 0 || eqIndex >= node . end ) {
431
- return false
432
- }
433
- const valueIndex = ctx . code . indexOf ( quasi . value . raw , eqIndex + 1 )
434
- if ( valueIndex < 0 || valueIndex >= node . end ) {
435
- return false
436
- }
437
- const maybeEnd = valueIndex + quasi . value . raw . length
438
- const maybeOpenQuote = ctx . code . slice ( eqIndex + 1 , valueIndex ) . trimStart ( )
439
- if ( maybeOpenQuote && maybeOpenQuote !== '"' && maybeOpenQuote !== "'" ) {
440
- return false
441
- }
442
- const maybeCloseQuote = ctx . code . slice ( maybeEnd , node . end ) . trimEnd ( )
443
- if ( maybeCloseQuote !== maybeOpenQuote ) {
444
- return false
445
- }
446
- getWithLoc ( expression ) . start = valueIndex
447
- getWithLoc ( expression ) . end = maybeEnd
448
- return true
449
- }
450
-
451
399
/** Convert for Transition Directive */
452
400
function convertTransitionDirective (
453
401
node : SvAST . TransitionDirective ,
@@ -648,7 +596,7 @@ function processDirectiveKey<
648
596
/** Common process for directive expression */
649
597
function processDirectiveExpression <
650
598
D extends SvAST . Directive ,
651
- S extends SvelteDirective | SvelteStyleDirective ,
599
+ S extends SvelteDirective ,
652
600
E extends D [ "expression" ] ,
653
601
> (
654
602
node : D & { expression : null | E } ,
@@ -679,9 +627,7 @@ function processDirectiveExpression<
679
627
getWithLoc ( node . expression ) . end = keyName . range [ 1 ]
680
628
}
681
629
processors . processExpression ( node . expression , shorthand ) . push ( ( es ) => {
682
- if ( directive . type === "SvelteDirective" ) {
683
- directive . expression = es
684
- }
630
+ directive . expression = es
685
631
} )
686
632
}
687
633
if ( ! shorthand ) {
0 commit comments