Skip to content

Commit c9c6ea7

Browse files
authored
(fix) fallback param for built-in transition/animate (#790)
Up to Svelte version 3.32.0, the built-in animate/transition functions have optional parameters, but according to its typings they were mandatory. To not show unnecessary type errors to those users, `{}` should be added as a fallback parameter if the user did not provide one. It may be the case that someone has a custom animation with the same name that expects different parameters, but that possibility is far less likely. #785
1 parent 611e665 commit c9c6ea7

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

packages/svelte2tsx/src/htmlxtojsx/nodes/animation-directive.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Node } from 'estree-walker';
33
import { isQuote } from '../utils/node-utils';
44

55
/**
6-
* animation:xxx(yyy) ---> {...__sveltets_ensureAnimation(xxx(__sveltets_ElementNode,__sveltets_AnimationMove,(yyy)))}
6+
* animate:xxx(yyy) ---> {...__sveltets_ensureAnimation(xxx(__sveltets_ElementNode,__sveltets_AnimationMove,(yyy)))}
77
*/
88
export function handleAnimateDirective(htmlx: string, str: MagicString, attr: Node): void {
99
str.overwrite(
@@ -13,7 +13,11 @@ export function handleAnimateDirective(htmlx: string, str: MagicString, attr: No
1313
);
1414

1515
if (!attr.expression) {
16-
str.appendLeft(attr.end, '(__sveltets_ElementNode,__sveltets_AnimationMove))}');
16+
if (animationsThatNeedParam.has(attr.name)) {
17+
str.appendLeft(attr.end, '(__sveltets_ElementNode,__sveltets_AnimationMove,{}))}');
18+
} else {
19+
str.appendLeft(attr.end, '(__sveltets_ElementNode,__sveltets_AnimationMove))}');
20+
}
1721
return;
1822
}
1923
str.overwrite(
@@ -26,3 +30,16 @@ export function handleAnimateDirective(htmlx: string, str: MagicString, attr: No
2630
str.remove(attr.end - 1, attr.end);
2731
}
2832
}
33+
34+
/**
35+
* Up to Svelte version 3.32.0, the following built-in animate functions have
36+
* optional parameters, but according to its typings they were mandatory.
37+
* To not show unnecessary type errors to those users, `{}` should be added
38+
* as a fallback parameter if the user did not provide one.
39+
* It may be the case that someone has a custom animation with the same name
40+
* that expects different parameters, or that someone did an import alias fly as foo,
41+
* but those are very unlikely.
42+
*
43+
* Remove this "hack" some day.
44+
*/
45+
const animationsThatNeedParam = new Set(['flip']);

packages/svelte2tsx/src/htmlxtojsx/nodes/transition-directive.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export function handleTransitionDirective(htmlx: string, str: MagicString, attr:
1818
}
1919

2020
if (!attr.expression) {
21-
str.appendLeft(attr.end, '(__sveltets_ElementNode))}');
21+
if (transitionsThatNeedParam.has(attr.name)) {
22+
str.appendLeft(attr.end, '(__sveltets_ElementNode,{}))}');
23+
} else {
24+
str.appendLeft(attr.end, '(__sveltets_ElementNode))}');
25+
}
2226
return;
2327
}
2428

@@ -32,3 +36,16 @@ export function handleTransitionDirective(htmlx: string, str: MagicString, attr:
3236
str.remove(attr.end - 1, attr.end);
3337
}
3438
}
39+
40+
/**
41+
* Up to Svelte version 3.32.0, the following built-in transition functions have
42+
* optional parameters, but according to its typings they were mandatory.
43+
* To not show unnecessary type errors to those users, `{}` should be added
44+
* as a fallback parameter if the user did not provide one.
45+
* It may be the case that someone has a custom transition with the same name
46+
* that expects different parameters, or that someone did an import alias fly as foo,
47+
* but those are very unlikely.
48+
*
49+
* Remove this "hack" some day.
50+
*/
51+
const transitionsThatNeedParam = new Set(['blur', 'fade', 'fly', 'slide', 'scale', 'draw']);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<>
2+
<h1 {...__sveltets_ensureTransition(blur(__sveltets_ElementNode,{}))}>Hello</h1>
3+
<h1 {...__sveltets_ensureTransition(fade(__sveltets_ElementNode,{}))}>Hello</h1>
4+
<h1 {...__sveltets_ensureTransition(fly(__sveltets_ElementNode,{}))}>Hello</h1>
5+
<h1 {...__sveltets_ensureTransition(slide(__sveltets_ElementNode,{}))}>Hello</h1>
6+
<h1 {...__sveltets_ensureTransition(scale(__sveltets_ElementNode,{}))}>Hello</h1>
7+
<h1 {...__sveltets_ensureTransition(draw(__sveltets_ElementNode,{}))}>Hello</h1>
8+
9+
<h1 {...__sveltets_ensureTransition(blur(__sveltets_ElementNode,{}))}>Hello</h1>
10+
<h1 {...__sveltets_ensureTransition(blur(__sveltets_ElementNode,{}))}>Hello</h1>
11+
12+
<h1 {...__sveltets_ensureAnimation(flip(__sveltets_ElementNode,__sveltets_AnimationMove,{}))}>Hello</h1>
13+
14+
<h1 {...__sveltets_ensureTransition(foo(__sveltets_ElementNode))}>Hello</h1>
15+
<h1 {...__sveltets_ensureTransition(foo(__sveltets_ElementNode))}>Hello</h1>
16+
<h1 {...__sveltets_ensureTransition(foo(__sveltets_ElementNode))}>Hello</h1>
17+
<h1 {...__sveltets_ensureAnimation(foo(__sveltets_ElementNode,__sveltets_AnimationMove))}>Hello</h1></>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- transitions -->
2+
<h1 transition:blur>Hello</h1>
3+
<h1 transition:fade>Hello</h1>
4+
<h1 transition:fly>Hello</h1>
5+
<h1 transition:slide>Hello</h1>
6+
<h1 transition:scale>Hello</h1>
7+
<h1 transition:draw>Hello</h1>
8+
<!-- also for in/out transitions -->
9+
<h1 in:blur>Hello</h1>
10+
<h1 out:blur>Hello</h1>
11+
<!-- animate -->
12+
<h1 animate:flip>Hello</h1>
13+
<!-- not others -->
14+
<h1 transition:foo>Hello</h1>
15+
<h1 in:foo>Hello</h1>
16+
<h1 out:foo>Hello</h1>
17+
<h1 animate:foo>Hello</h1>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<><div {...__sveltets_ensureTransition(slide(__sveltets_ElementNode))}>
1+
<><div {...__sveltets_ensureTransition(slide(__sveltets_ElementNode,{}))}>
22
{item}
33
</div></>

0 commit comments

Comments
 (0)