Home / Function/ build_event_handler() — svelte Function Reference

build_event_handler() — svelte Function Reference

Architecture documentation for the build_event_handler() function in events.js from the svelte codebase.

Function javascript Compiler Transformer calls 1 called by 3

Entity Profile

Dependency Diagram

graph TD
  cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28["build_event_handler()"]
  c5a9d025_4aac_a689_1a38_c0fce5e694c0["events.js"]
  cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28 -->|defined in| c5a9d025_4aac_a689_1a38_c0fce5e694c0
  1fd2e260_163e_201e_1773_5744f70a7e9c["OnDirective()"]
  1fd2e260_163e_201e_1773_5744f70a7e9c -->|calls| cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28
  2b6a1e4d_7060_eb43_d939_71517fa01ad9["build_component()"]
  2b6a1e4d_7060_eb43_d939_71517fa01ad9 -->|calls| cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28
  e35222ea_3daa_de5e_3d5f_66469f2fb218["visit_event_attribute()"]
  e35222ea_3daa_de5e_3d5f_66469f2fb218 -->|calls| cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28
  e1127c1f_686b_70bf_3d7c_fb7c292eef2a["has_side_effects()"]
  cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28 -->|calls| e1127c1f_686b_70bf_3d7c_fb7c292eef2a
  style cd53dd1a_8bd9_06e2_6bfe_ed90344d0b28 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/events.js lines 91–156

export function build_event_handler(node, metadata, context) {
	if (node === null) {
		// bubble event
		return b.function(
			null,
			[b.id('$$arg')],
			b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))])
		);
	}

	let handler = /** @type {Expression} */ (context.visit(node));

	// inline handler
	if (handler.type === 'ArrowFunctionExpression' || handler.type === 'FunctionExpression') {
		return handler;
	}

	// function declared in the script
	if (handler.type === 'Identifier') {
		const binding = context.state.scope.get(handler.name);

		if (binding?.is_function()) {
			return handler;
		}

		// local variable can be assigned directly
		// except in dev mode where when need $.apply()
		// in order to handle warnings.
		if (!dev && binding?.declaration_kind !== 'import') {
			return handler;
		}
	}

	if (metadata.has_call) {
		// memoize where necessary
		const id = b.id(context.state.scope.generate('event_handler'));

		context.state.init.push(b.var(id, b.call('$.derived', b.thunk(handler))));
		handler = b.call('$.get', id);
	}

	// wrap the handler in a function, so the expression is re-evaluated for each event
	let call = b.call(b.member(handler, 'apply', false, true), b.this, b.id('$$args'));

	if (dev) {
		const loc = locator(/** @type {number} */ (node.start));

		const remove_parens =
			node.type === 'CallExpression' &&
			node.arguments.length === 0 &&
			node.callee.type === 'Identifier';

		call = b.call(
			'$.apply',
			b.thunk(handler),
			b.this,
			b.id('$$args'),
			b.id(context.state.analysis.name),
			b.array([b.literal(loc.line), b.literal(loc.column)]),
			has_side_effects(node) && b.true,
			remove_parens && b.true
		);
	}

	return b.function(null, [b.rest(b.id('$$args'))], b.block([b.stmt(call)]));
}

Domain

Subdomains

Frequently Asked Questions

What does build_event_handler() do?
build_event_handler() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/events.js.
Where is build_event_handler() defined?
build_event_handler() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/events.js at line 91.
What does build_event_handler() call?
build_event_handler() calls 1 function(s): has_side_effects.
What calls build_event_handler()?
build_event_handler() is called by 3 function(s): OnDirective, build_component, visit_event_attribute.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free