mirror of https://github.com/sveltejs/svelte
fix: add snippet argument validation in dev (#15521)
* init * fix * make `Payload` a class * doh * lint * tweak changeset * fix * only export things that should be available on $ * tweak message * fix --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/15731/head
parent
0020e597e2
commit
ec1d85c89e
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: add snippet argument validation in dev
|
@ -0,0 +1,15 @@
|
|||||||
|
import { invalid_snippet_arguments } from '../../shared/errors.js';
|
||||||
|
/**
|
||||||
|
* @param {Node} anchor
|
||||||
|
* @param {...(()=>any)[]} args
|
||||||
|
*/
|
||||||
|
export function validate_snippet_args(anchor, ...args) {
|
||||||
|
if (typeof anchor !== 'object' || !(anchor instanceof Node)) {
|
||||||
|
invalid_snippet_arguments();
|
||||||
|
}
|
||||||
|
for (let arg of args) {
|
||||||
|
if (typeof arg !== 'function') {
|
||||||
|
invalid_snippet_arguments();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
export class Payload {
|
||||||
|
/** @type {Set<{ hash: string; code: string }>} */
|
||||||
|
css = new Set();
|
||||||
|
out = '';
|
||||||
|
uid = () => '';
|
||||||
|
|
||||||
|
head = {
|
||||||
|
/** @type {Set<{ hash: string; code: string }>} */
|
||||||
|
css: new Set(),
|
||||||
|
title: '',
|
||||||
|
out: '',
|
||||||
|
uid: () => ''
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(id_prefix = '') {
|
||||||
|
this.uid = props_id_generator(id_prefix);
|
||||||
|
this.head.uid = this.uid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in legacy mode to handle bindings
|
||||||
|
* @param {Payload} to_copy
|
||||||
|
* @returns {Payload}
|
||||||
|
*/
|
||||||
|
export function copy_payload({ out, css, head, uid }) {
|
||||||
|
const payload = new Payload();
|
||||||
|
|
||||||
|
payload.out = out;
|
||||||
|
payload.css = new Set(css);
|
||||||
|
payload.uid = uid;
|
||||||
|
|
||||||
|
payload.head = {
|
||||||
|
title: head.title,
|
||||||
|
out: head.out,
|
||||||
|
css: new Set(head.css),
|
||||||
|
uid: head.uid
|
||||||
|
};
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assigns second payload to first
|
||||||
|
* @param {Payload} p1
|
||||||
|
* @param {Payload} p2
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
export function assign_payload(p1, p2) {
|
||||||
|
p1.out = p2.out;
|
||||||
|
p1.css = p2.css;
|
||||||
|
p1.head = p2.head;
|
||||||
|
p1.uid = p2.uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an ID generator
|
||||||
|
* @param {string} prefix
|
||||||
|
* @returns {() => string}
|
||||||
|
*/
|
||||||
|
function props_id_generator(prefix) {
|
||||||
|
let uid = 1;
|
||||||
|
return () => `${prefix}s${uid++}`;
|
||||||
|
}
|
Loading…
Reference in new issue