Skip to content

Commit 1fed4dd

Browse files
committed
use arguments
1 parent ea29bad commit 1fed4dd

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function SnippetBlock(node, context) {
2121
/** @type {Statement[]} */
2222
const declarations = [];
2323

24+
if (dev) {
25+
declarations.push(b.stmt(b.call('$.validate_snippet_args', b.spread(b.id('arguments')))));
26+
}
27+
2428
const transform = { ...context.state.transform };
2529
const child_state = { ...context.state, transform };
2630

@@ -71,33 +75,15 @@ export function SnippetBlock(node, context) {
7175
}
7276
}
7377
}
74-
if (dev) {
75-
const [anchor, ...validations_args] = args;
76-
declarations.unshift(
77-
b.stmt(
78-
b.call(
79-
'$.validate_snippet_args',
80-
.../** @type {Identifier[]} */ (
81-
[anchor, b.array(fallback_arr.map((i) => b.literal(i))), ...validations_args].map(
82-
(arg) =>
83-
arg?.type === 'Identifier' || arg.type === 'ArrayExpression' ? arg : arg?.left
84-
)
85-
)
86-
)
87-
)
88-
);
89-
}
9078
body = b.block([
9179
...declarations,
9280
.../** @type {BlockStatement} */ (context.visit(node.body, child_state)).body
9381
]);
9482

95-
/** @type {Expression} */
96-
let snippet = b.arrow(args, body);
97-
98-
if (dev) {
99-
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
100-
}
83+
// in dev we use a FunctionExpression (not arrow function) so we can use `arguments`
84+
let snippet = dev
85+
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), b.function(null, args, body))
86+
: b.arrow(args, body);
10187

10288
const declaration = b.const(node.expression, snippet);
10389

packages/svelte/src/internal/client/dev/validation.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { invalid_snippet_arguments } from '../../shared/errors.js';
22
/**
33
* @param {Node} anchor
4-
* @param {number[]} with_fallback_idx
54
* @param {...(()=>any)[]} args
65
*/
7-
export function validate_snippet_args(anchor, with_fallback_idx, ...args) {
6+
export function validate_snippet_args(anchor, ...args) {
87
if (typeof anchor !== 'object' || !(anchor instanceof Node)) {
98
invalid_snippet_arguments();
109
}
11-
for (let i = 0; i < args.length; i++) {
12-
const arg = args[i];
13-
if (typeof arg !== 'function' && !(with_fallback_idx.includes(i) && arg === undefined)) {
10+
for (let arg of args) {
11+
if (typeof arg !== 'function') {
1412
invalid_snippet_arguments();
1513
}
1614
}

0 commit comments

Comments
 (0)