From 1390466d864e442a104fc4e30eea7d986e6be8f8 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 30 Jan 2024 19:05:49 -0500 Subject: [PATCH] simplify --- .../3-transform/client/visitors/template.js | 13 +++++++------ packages/svelte/src/internal/client/runtime.js | 18 +++--------------- packages/svelte/src/internal/common.js | 2 +- packages/svelte/src/internal/index.js | 2 +- packages/svelte/src/internal/server/index.js | 6 +----- packages/svelte/src/store/index.js | 5 +---- packages/svelte/src/store/utils.js | 4 ++-- 7 files changed, 16 insertions(+), 34 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js index 0e52ee609e..e196a09370 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js @@ -2481,14 +2481,15 @@ export const template_visitors = { if (!argument) continue; if (argument.type === 'Identifier') { - args.push(argument); + args.push({ + type: 'AssignmentPattern', + left: argument, + right: b.id('$.noop') + }); const binding = /** @type {import('#compiler').Binding} */ ( context.state.scope.get(argument.name) ); - // we can't use `b.maybe_call` because it can result in invalid javascript if - // this expression appears on the left side of an assignment somewhere. For example: - // `$.maybe_call(myArg).value = 1` is valid JavaScript, but `$.myArg?.().value = 1` is not - binding.expression = b.call('$.maybe_call', argument); + binding.expression = b.call(argument); continue; } @@ -2505,7 +2506,7 @@ export const template_visitors = { path.node, b.thunk( /** @type {import('estree').Expression} */ ( - context.visit(path.expression?.(b.call('$.maybe_call', b.id(arg_alias)))) + context.visit(path.expression?.(b.maybe_call(b.id(arg_alias)))) ) ) ) diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 388bff2a36..92e10cb547 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -1,6 +1,6 @@ import { DEV } from 'esm-env'; import { subscribe_to_store } from '../../store/utils.js'; -import { EMPTY_FUNC, run_all } from '../common.js'; +import { noop, run_all } from '../common.js'; import { array_prototype, get_descriptor, @@ -860,7 +860,7 @@ export function store_get(store, store_name, stores) { store: null, last_value: null, value: mutable_source(UNINITIALIZED), - unsubscribe: EMPTY_FUNC + unsubscribe: noop }; // TODO: can we remove this code? it was refactored out when we split up source/comptued signals // push_destroy_fn(entry.value, () => { @@ -890,7 +890,7 @@ export function store_get(store, store_name, stores) { function connect_store_to_signal(store, source) { if (store == null) { set(source, undefined); - return EMPTY_FUNC; + return noop; } /** @param {V} v */ @@ -2109,18 +2109,6 @@ if (DEV) { throw_rune_error('$props'); } -/** - * @template {Function | undefined} T - * @param {T} fn - * @returns {ReturnType | undefined} - */ -export function maybe_call(fn) { - if (fn === undefined) { - return undefined; - } - return fn(); -} - /** * Expects a value that was wrapped with `freeze` and makes it frozen. * @template T diff --git a/packages/svelte/src/internal/common.js b/packages/svelte/src/internal/common.js index 1ea482a801..2a4295f276 100644 --- a/packages/svelte/src/internal/common.js +++ b/packages/svelte/src/internal/common.js @@ -1,5 +1,5 @@ // eslint-disable-next-line @typescript-eslint/no-empty-function -export const EMPTY_FUNC = () => {}; +export const noop = () => {}; // Adapted from https://github.com/then/is-promise/blob/master/index.js // Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE diff --git a/packages/svelte/src/internal/index.js b/packages/svelte/src/internal/index.js index 207c6b43f0..ae1748b428 100644 --- a/packages/svelte/src/internal/index.js +++ b/packages/svelte/src/internal/index.js @@ -38,7 +38,6 @@ export { user_root_effect, inspect, unwrap, - maybe_call, freeze } from './client/runtime.js'; export * from './client/each.js'; @@ -54,3 +53,4 @@ export { $window as window, $document as document } from './client/operations.js'; +export { noop } from './common.js'; diff --git a/packages/svelte/src/internal/server/index.js b/packages/svelte/src/internal/server/index.js index cd152c9322..aafbd8252a 100644 --- a/packages/svelte/src/internal/server/index.js +++ b/packages/svelte/src/internal/server/index.js @@ -1,6 +1,6 @@ import * as $ from '../client/runtime.js'; import { set_is_ssr } from '../client/runtime.js'; -import { is_promise } from '../common.js'; +import { is_promise, noop } from '../common.js'; import { subscribe_to_store } from '../../store/utils.js'; import { DOMBooleanAttributes } from '../../constants.js'; @@ -523,10 +523,6 @@ export function bind_props(props_parent, props_now) { } } -function noop() { - // noop -} - /** * @template V * @param {Promise} promise diff --git a/packages/svelte/src/store/index.js b/packages/svelte/src/store/index.js index 728eec3d49..85954fc66c 100644 --- a/packages/svelte/src/store/index.js +++ b/packages/svelte/src/store/index.js @@ -1,3 +1,4 @@ +import { noop } from '../internal/common.js'; import { subscribe_to_store } from './utils.js'; /** @@ -5,10 +6,6 @@ import { subscribe_to_store } from './utils.js'; */ const subscriber_queue = []; -/** @returns {void} */ -// eslint-disable-next-line @typescript-eslint/no-empty-function -function noop() {} - /** * Creates a `Readable` store that allows reading by subscription. * diff --git a/packages/svelte/src/store/utils.js b/packages/svelte/src/store/utils.js index 979a2518d8..9bcc914fd1 100644 --- a/packages/svelte/src/store/utils.js +++ b/packages/svelte/src/store/utils.js @@ -1,4 +1,4 @@ -import { EMPTY_FUNC } from '../internal/common.js'; +import { noop } from '../internal/common.js'; /** * @template T @@ -15,7 +15,7 @@ export function subscribe_to_store(store, run, invalidate) { // @ts-expect-error if (invalidate) invalidate(undefined); - return EMPTY_FUNC; + return noop; } // Svelte store takes a private second argument