Skip to content

Commit 822fe6b

Browse files
committed
use a switch
1 parent c76ca70 commit 822fe6b

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export function RegularElement(node, context) {
6262
context.state.metadata.context.template_contains_script_tag = true;
6363
}
6464

65-
const metadata = context.state.metadata;
6665
const child_metadata = {
6766
...context.state.metadata,
6867
namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
@@ -79,6 +78,9 @@ export function RegularElement(node, context) {
7978
/** @type {AST.StyleDirective[]} */
8079
const style_directives = [];
8180

81+
/** @type {Array<AST.AnimateDirective | AST.BindDirective | AST.OnDirective | AST.TransitionDirective | AST.UseDirective} */
82+
const other_directives = [];
83+
8284
/** @type {ExpressionStatement[]} */
8385
const lets = [];
8486

@@ -89,7 +91,7 @@ export function RegularElement(node, context) {
8991
// custom element until the template is connected to the dom, which would
9092
// cause problems when setting properties on the custom element.
9193
// Therefore we need to use importNode instead, which doesn't have this caveat.
92-
metadata.context.template_needs_import_node = true;
94+
context.state.metadata.context.template_needs_import_node = true;
9395
}
9496

9597
/** @type {Map<string, AST.Attribute>} */
@@ -99,37 +101,62 @@ export function RegularElement(node, context) {
99101
const bindings = new Map();
100102

101103
let has_spread = false;
104+
let has_use = false;
102105

103106
for (const attribute of node.attributes) {
104-
if (attribute.type === 'SpreadAttribute') {
105-
has_spread = true;
106-
} else if (attribute.type === 'Attribute') {
107-
lookup.set(attribute.name, attribute);
108-
} else if (attribute.type === 'BindDirective') {
109-
bindings.set(attribute.name, attribute);
110-
} else if (attribute.type === 'LetDirective') {
111-
// visit let directives before everything else, to set state
112-
lets.push(/** @type {ExpressionStatement} */ (context.visit(attribute)));
107+
switch (attribute.type) {
108+
case 'SpreadAttribute':
109+
attributes.push(attribute);
110+
has_spread = true;
111+
break;
112+
113+
case 'Attribute':
114+
attributes.push(attribute);
115+
lookup.set(attribute.name, attribute);
116+
break;
117+
118+
case 'BindDirective':
119+
bindings.set(attribute.name, attribute);
120+
other_directives.push(attribute);
121+
break;
122+
123+
case 'ClassDirective':
124+
class_directives.push(attribute);
125+
break;
126+
127+
case 'StyleDirective':
128+
style_directives.push(attribute);
129+
break;
130+
131+
case 'LetDirective':
132+
// visit let directives before everything else, to set state
133+
lets.push(/** @type {ExpressionStatement} */ (context.visit(attribute)));
134+
break;
135+
136+
case 'UseDirective':
137+
has_use = true;
138+
other_directives.push(attribute);
139+
break;
140+
141+
case 'OnDirective':
142+
other_directives.push(attribute);
143+
break;
144+
145+
case 'AnimateDirective':
146+
case 'TransitionDirective':
147+
other_directives.push(attribute);
148+
break;
113149
}
114150
}
115151

116-
for (const attribute of node.attributes) {
117-
if (attribute.type === 'Attribute') {
118-
attributes.push(attribute);
119-
} else if (attribute.type === 'SpreadAttribute') {
120-
attributes.push(attribute);
121-
} else if (attribute.type === 'ClassDirective') {
122-
class_directives.push(attribute);
123-
} else if (attribute.type === 'StyleDirective') {
124-
style_directives.push(attribute);
125-
} else if (attribute.type === 'OnDirective') {
152+
for (const attribute of other_directives) {
153+
if (attribute.type === 'OnDirective') {
126154
const handler = /** @type {Expression} */ (context.visit(attribute));
127-
const has_action_directive = node.attributes.find((a) => a.type === 'UseDirective');
128155

129156
context.state.after_update.push(
130-
b.stmt(has_action_directive ? b.call('$.effect', b.thunk(handler)) : handler)
157+
b.stmt(has_use ? b.call('$.effect', b.thunk(handler)) : handler)
131158
);
132-
} else if (attribute.type !== 'LetDirective') {
159+
} else {
133160
context.visit(attribute);
134161
}
135162
}
@@ -257,10 +284,7 @@ export function RegularElement(node, context) {
257284

258285
if (
259286
is_load_error_element(node.name) &&
260-
(has_spread ||
261-
lookup.has('onload') ||
262-
lookup.has('onerror') ||
263-
node.attributes.some((attribute) => attribute.type === 'UseDirective'))
287+
(has_spread || has_use || lookup.has('onload') || lookup.has('onerror'))
264288
) {
265289
context.state.after_update.push(b.stmt(b.call('$.replay_events', node_id)));
266290
}

0 commit comments

Comments
 (0)