mirror of https://github.com/sveltejs/svelte
parent
2aa2f57834
commit
a74bee35a1
@ -0,0 +1,57 @@
|
||||
import * as w from '../warnings.js';
|
||||
import { sanitize_location } from './location.js';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {any} a
|
||||
* @param {any} b
|
||||
* @param {string} property
|
||||
* @param {string} location
|
||||
*/
|
||||
function compare(a, b, property, location) {
|
||||
if (a !== b) {
|
||||
w.assignment_value_stale(property, /** @type {string} */ (sanitize_location(location)));
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} object
|
||||
* @param {string} property
|
||||
* @param {any} value
|
||||
* @param {string} location
|
||||
*/
|
||||
export function assign(object, property, value, location) {
|
||||
return compare((object[property] = value), object[property], property, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} object
|
||||
* @param {string} property
|
||||
* @param {any} value
|
||||
* @param {string} location
|
||||
*/
|
||||
export function assign_and(object, property, value, location) {
|
||||
return compare((object[property] &&= value), object[property], property, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} object
|
||||
* @param {string} property
|
||||
* @param {any} value
|
||||
* @param {string} location
|
||||
*/
|
||||
export function assign_or(object, property, value, location) {
|
||||
return compare((object[property] ||= value), object[property], property, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} object
|
||||
* @param {string} property
|
||||
* @param {any} value
|
||||
* @param {string} location
|
||||
*/
|
||||
export function assign_nullish(object, property, value, location) {
|
||||
return compare((object[property] ??= value), object[property], property, location);
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>items: null</button>`,
|
||||
|
||||
test({ assert, target, warnings }) {
|
||||
const [btn1, btn2] = target.querySelectorAll('button');
|
||||
|
||||
flushSync(() => btn1.click());
|
||||
assert.htmlEqual(target.innerHTML, `<button>items: []</button>`);
|
||||
|
||||
assert.equal(warnings, ['TODO']);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
let object = $state({ items: null });
|
||||
</script>
|
||||
|
||||
<button onclick={() => (object.items ??= []).push(object.items.length)}>
|
||||
items: {JSON.stringify(object.items)}
|
||||
</button>
|
||||
Loading…
Reference in new issue