@@ -13,7 +13,6 @@ import type {
1313 SvelteStartTag ,
1414 SvelteName ,
1515 SvelteStyleDirective ,
16- SvelteMustacheTagText ,
1716 SvelteStyleDirectiveLongform ,
1817 SvelteStyleDirectiveShorthand ,
1918} from "../../ast"
@@ -22,9 +21,13 @@ import type { Context } from "../../context"
2221import type * as SvAST from "../svelte-ast-types"
2322import { getWithLoc , indexOf } from "./common"
2423import { convertMustacheTag } from "./mustache"
25- import { convertTemplateLiteralToLiteral , convertTextToLiteral } from "./text"
24+ import {
25+ convertAttributeValueTokenToLiteral ,
26+ convertTextToLiteral ,
27+ } from "./text"
2628import { ParseError } from "../../errors"
2729import type { ScriptLetCallback } from "../../context/script-let"
30+ import type { AttributeToken } from "../html"
2831
2932/** Convert for Attributes */
3033export function * convertAttributes (
@@ -79,13 +82,16 @@ export function* convertAttributes(
7982 yield convertLetDirective ( attr , parent , ctx )
8083 continue
8184 }
82- if ( attr . type === "Style" ) {
83- yield convertOldStyleDirective ( attr , parent , ctx )
84- continue
85- }
8685 if ( attr . type === "Ref" ) {
8786 throw new ParseError ( "Ref are not supported." , attr . start , ctx )
8887 }
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+ }
8995 throw new ParseError (
9096 `Unknown directive or attribute (${ attr . type } ) are not supported.` ,
9197 attr . start ,
@@ -94,6 +100,42 @@ export function* convertAttributes(
94100 }
95101}
96102
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+
97139/** Convert for Attribute */
98140function convertAttribute (
99141 node : SvAST . Attribute ,
@@ -354,100 +396,6 @@ function convertStyleDirective(
354396 return directive
355397}
356398
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-
451399/** Convert for Transition Directive */
452400function convertTransitionDirective (
453401 node : SvAST . TransitionDirective ,
@@ -648,7 +596,7 @@ function processDirectiveKey<
648596/** Common process for directive expression */
649597function processDirectiveExpression <
650598 D extends SvAST . Directive ,
651- S extends SvelteDirective | SvelteStyleDirective ,
599+ S extends SvelteDirective ,
652600 E extends D [ "expression" ] ,
653601> (
654602 node : D & { expression : null | E } ,
@@ -679,9 +627,7 @@ function processDirectiveExpression<
679627 getWithLoc ( node . expression ) . end = keyName . range [ 1 ]
680628 }
681629 processors . processExpression ( node . expression , shorthand ) . push ( ( es ) => {
682- if ( directive . type === "SvelteDirective" ) {
683- directive . expression = es
684- }
630+ directive . expression = es
685631 } )
686632 }
687633 if ( ! shorthand ) {
0 commit comments