Skip to content

Commit 8869f96

Browse files
authored
Fix parsing error when object pattern with let: (#159)
1 parent 001d816 commit 8869f96

File tree

4 files changed

+2042
-29
lines changed

4 files changed

+2042
-29
lines changed

src/parser/converts/attr.ts

+47-29
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ function convertLetDirective(
495495
...ctx.getConvertLocation(node),
496496
}
497497
processDirective(node, directive, ctx, {
498-
processExpression(pattern) {
498+
processPattern(pattern) {
499499
return ctx.letDirCollections
500500
.getCollection()
501501
.addPattern(pattern, directive, "any")
@@ -515,6 +515,32 @@ function convertLetDirective(
515515
return directive
516516
}
517517

518+
type DirectiveProcessors<
519+
D extends SvAST.Directive,
520+
S extends SvelteDirective,
521+
E extends D["expression"] & S["expression"],
522+
> =
523+
| {
524+
processExpression: (
525+
expression: E,
526+
shorthand: boolean,
527+
) => ScriptLetCallback<NonNullable<E>>[]
528+
processPattern?: undefined
529+
processName?: (
530+
expression: SvelteName,
531+
) => ScriptLetCallback<ESTree.Identifier>[]
532+
}
533+
| {
534+
processExpression?: undefined
535+
processPattern: (
536+
expression: E,
537+
shorthand: boolean,
538+
) => ScriptLetCallback<NonNullable<E>>[]
539+
processName?: (
540+
expression: SvelteName,
541+
) => ScriptLetCallback<ESTree.Identifier>[]
542+
}
543+
518544
/** Common process for directive */
519545
function processDirective<
520546
D extends SvAST.Directive,
@@ -524,15 +550,7 @@ function processDirective<
524550
node: D & { expression: null | E },
525551
directive: S,
526552
ctx: Context,
527-
processors: {
528-
processExpression: (
529-
expression: E,
530-
shorthand: boolean,
531-
) => ScriptLetCallback<NonNullable<E>>[]
532-
processName?: (
533-
expression: SvelteName,
534-
) => ScriptLetCallback<ESTree.Identifier>[]
535-
},
553+
processors: DirectiveProcessors<D, S, E>,
536554
) {
537555
processDirectiveKey(node, directive, ctx)
538556
processDirectiveExpression<D, S, E>(node, directive, ctx, processors)
@@ -609,15 +627,7 @@ function processDirectiveExpression<
609627
node: D & { expression: null | E },
610628
directive: S,
611629
ctx: Context,
612-
processors: {
613-
processExpression: (
614-
expression: E,
615-
shorthand: boolean,
616-
) => ScriptLetCallback<NonNullable<E>>[]
617-
processName?: (
618-
expression: SvelteName,
619-
) => ScriptLetCallback<ESTree.Identifier>[]
620-
},
630+
processors: DirectiveProcessors<D, S, E>,
621631
) {
622632
const key = directive.key
623633
const keyName = key.name as SvelteName
@@ -633,16 +643,24 @@ function processDirectiveExpression<
633643
// e.g. bind:value=""
634644
getWithLoc(node.expression).end = keyName.range[1]
635645
}
636-
processors.processExpression(node.expression, shorthand).push((es) => {
637-
if (node.expression && es.type !== node.expression.type) {
638-
throw new ParseError(
639-
`Expected ${node.expression.type}, but ${es.type} found.`,
640-
es.range![0],
641-
ctx,
642-
)
643-
}
644-
directive.expression = es
645-
})
646+
if (processors.processExpression) {
647+
processors
648+
.processExpression(node.expression, shorthand)
649+
.push((es) => {
650+
if (node.expression && es.type !== node.expression.type) {
651+
throw new ParseError(
652+
`Expected ${node.expression.type}, but ${es.type} found.`,
653+
es.range![0],
654+
ctx,
655+
)
656+
}
657+
directive.expression = es
658+
})
659+
} else {
660+
processors.processPattern(node.expression, shorthand).push((es) => {
661+
directive.expression = es
662+
})
663+
}
646664
}
647665
if (!shorthand) {
648666
if (processors.processName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Hoverable from './Hoverable.svelte';
3+
</script>
4+
5+
<Hoverable let:hovering={ {foo:active} }>
6+
<div class:active />
7+
</Hoverable>

0 commit comments

Comments
 (0)