Skip to content

Commit 6e8267f

Browse files
trueadmRich-Harris
andauthored
fix: correctly update dynamic member expressions (#14359)
* fix: output template effect for svg xlink attribute * mark subtree dynamic in MemberExpression visitor * don't treat attributes and text nodes differently * Update .changeset/serious-spiders-bake.md --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 012166e commit 6e8267f

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

.changeset/serious-spiders-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly update dynamic member expressions

packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,11 @@ export function Identifier(node, context) {
126126
}
127127
}
128128

129-
if (!can_inline && context.state.expression) {
130-
context.state.expression.can_inline = false;
131-
}
129+
if (!can_inline) {
130+
if (context.state.expression) {
131+
context.state.expression.can_inline = false;
132+
}
132133

133-
/**
134-
* if the identifier is part of an expression tag of an attribute we want to check if it's inlinable
135-
* before marking the subtree as dynamic. This is because if it's inlinable it will be inlined in the template
136-
* directly making the whole thing actually static.
137-
*/
138-
if (!can_inline || !context.path.find((node) => node.type === 'Attribute')) {
139134
mark_subtree_dynamic(context.path);
140135
}
141136
}

packages/svelte/src/compiler/phases/2-analyze/visitors/MemberExpression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as e from '../../../errors.js';
44
import * as w from '../../../warnings.js';
55
import { object } from '../../../utils/ast.js';
66
import { is_pure, is_safe_identifier } from './shared/utils.js';
7+
import { mark_subtree_dynamic } from './shared/fragment.js';
78

89
/**
910
* @param {MemberExpression} node
@@ -20,6 +21,8 @@ export function MemberExpression(node, context) {
2021
if (context.state.expression && !is_pure(node, context)) {
2122
context.state.expression.has_state = true;
2223
context.state.expression.can_inline = false;
24+
25+
mark_subtree_dynamic(context.path);
2326
}
2427

2528
if (!is_safe_identifier(node, context.state.scope)) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<div><svg aria-hidden="true" height="14" width="13"><use xlink:href="test#done"></use></svg></div`
5+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import { sprites } from './sprites.js'
3+
</script>
4+
5+
<div>
6+
<svg width="13" height="14" aria-hidden="true">
7+
<use xlink:href="{sprites['a']}#done"></use>
8+
</svg>
9+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const sprites = {
2+
a: 'test'
3+
};

packages/svelte/tests/snapshot/samples/inline-module-vars/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var root = $.template(`<picture><source srcset="${__DECLARED_ASSET_0__}" type="i
1111

1212
export default function Inline_module_vars($$anchor) {
1313
var fragment = root();
14-
var p = $.sibling($.first_child(fragment), 2);
1514

15+
$.next(2);
1616
$.append($$anchor, fragment);
1717
}

0 commit comments

Comments
 (0)