Skip to content

Commit d62c58e

Browse files
authored
Drop support for Svelte v3.46.0 (exact) (#137)
1 parent 145623c commit d62c58e

File tree

5 files changed

+68
-159
lines changed

5 files changed

+68
-159
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"prettier-plugin-svelte": "^2.5.0",
8686
"semver": "^7.3.5",
8787
"string-replace-loader": "^3.0.3",
88-
"svelte": "^3.44.1",
88+
"svelte": "^3.46.1",
8989
"ts-node": "^10.4.0",
9090
"typescript": "~4.5.0-0",
9191
"vue-eslint-parser": "^8.0.1"

src/parser/converts/attr.ts

Lines changed: 50 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
2221
import type * as SvAST from "../svelte-ast-types"
2322
import { getWithLoc, indexOf } from "./common"
2423
import { convertMustacheTag } from "./mustache"
25-
import { convertTemplateLiteralToLiteral, convertTextToLiteral } from "./text"
24+
import {
25+
convertAttributeValueTokenToLiteral,
26+
convertTextToLiteral,
27+
} from "./text"
2628
import { ParseError } from "../../errors"
2729
import type { ScriptLetCallback } from "../../context/script-let"
30+
import type { AttributeToken } from "../html"
2831

2932
/** Convert for Attributes */
3033
export 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 */
98140
function 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 */
452400
function convertTransitionDirective(
453401
node: SvAST.TransitionDirective,
@@ -648,7 +596,7 @@ function processDirectiveKey<
648596
/** Common process for directive expression */
649597
function 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) {

src/parser/converts/root.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type * as SvAST from "../svelte-ast-types"
22
import type {
3-
SvelteAttribute,
43
SvelteName,
54
SvelteProgram,
65
SvelteScriptElement,
@@ -9,7 +8,7 @@ import type {
98
import {} from "./common"
109
import type { Context } from "../../context"
1110
import { convertChildren, extractElementTags } from "./element"
12-
import { extractTextTokens } from "./text"
11+
import { convertAttributeTokens } from "./attr"
1312

1413
/**
1514
* Convert root
@@ -152,40 +151,8 @@ function extractAttributes(
152151
}
153152
const block = ctx.findBlock(element)
154153
if (block) {
155-
for (const attr of block.attrs) {
156-
const attrNode: SvelteAttribute = {
157-
type: "SvelteAttribute",
158-
boolean: false,
159-
key: null as any,
160-
value: [],
161-
parent: element.startTag,
162-
...ctx.getConvertLocation({
163-
start: attr.key.start,
164-
end: attr.value?.end ?? attr.key.end,
165-
}),
166-
}
167-
element.startTag.attributes.push(attrNode)
168-
attrNode.key = {
169-
type: "SvelteName",
170-
name: attr.key.name,
171-
parent: attrNode,
172-
...ctx.getConvertLocation(attr.key),
173-
}
174-
ctx.addToken("HTMLIdentifier", attr.key)
175-
if (attr.value == null) {
176-
attrNode.boolean = true
177-
} else {
178-
const valueLoc = attr.value.quote
179-
? { start: attr.value.start + 1, end: attr.value.end - 1 }
180-
: attr.value
181-
attrNode.value.push({
182-
type: "SvelteLiteral",
183-
value: attr.value.value,
184-
parent: attrNode,
185-
...ctx.getConvertLocation(valueLoc),
186-
})
187-
extractTextTokens(valueLoc, ctx)
188-
}
189-
}
154+
element.startTag.attributes.push(
155+
...convertAttributeTokens(block.attrs, element.startTag, ctx),
156+
)
190157
}
191158
}

src/parser/converts/text.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type ESTree from "estree"
21
import type { SvelteLiteral, SvelteText } from "../../ast"
32
import type { Context } from "../../context"
3+
import type { AttributeValueToken } from "../html"
44
import type * as SvAST from "../svelte-ast-types"
5-
import { getWithLoc } from "./common"
65
/** Convert for Text */
76
export function convertText(
87
node: SvAST.Text,
@@ -34,24 +33,28 @@ export function convertTextToLiteral(
3433
extractTextTokens(node, ctx)
3534
return text
3635
}
37-
/** Convert for Old StyleDir's TemplateLiteral to Literal for svelte v3.46.0 */
38-
export function convertTemplateLiteralToLiteral(
39-
node: ESTree.TemplateLiteral,
36+
37+
/** Convert for AttributeValueToken to Literal */
38+
export function convertAttributeValueTokenToLiteral(
39+
node: AttributeValueToken,
4040
parent: SvelteLiteral["parent"],
4141
ctx: Context,
4242
): SvelteLiteral {
43+
const valueLoc = node.quote
44+
? { start: node.start + 1, end: node.end - 1 }
45+
: node
4346
const text: SvelteLiteral = {
4447
type: "SvelteLiteral",
45-
value: node.quasis[0].value.raw,
48+
value: node.value,
4649
parent,
47-
...ctx.getConvertLocation(node),
50+
...ctx.getConvertLocation(valueLoc),
4851
}
49-
extractTextTokens(getWithLoc(node), ctx)
52+
extractTextTokens(valueLoc, ctx)
5053
return text
5154
}
5255

5356
/** Extract tokens */
54-
export function extractTextTokens(
57+
function extractTextTokens(
5558
node: { start: number; end: number },
5659
ctx: Context,
5760
): void {

src/parser/svelte-ast-types.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,7 @@ interface BaseDirective extends BaseNode {
198198
modifiers: string[]
199199
}
200200
export interface DirectiveForExpression extends BaseDirective {
201-
type:
202-
| "Action"
203-
| "Animation"
204-
| "Binding"
205-
| "Class"
206-
| "Style" // For Svelte 3.46.0
207-
| "EventHandler"
208-
| "Ref"
201+
type: "Action" | "Animation" | "Binding" | "Class" | "EventHandler" | "Ref"
209202
expression: null | ESTree.Expression
210203
}
211204
export interface LetDirective extends BaseDirective {

0 commit comments

Comments
 (0)