Home / Function/ ClassBody() — svelte Function Reference

ClassBody() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf["ClassBody()"]
  ae63a5f8_3bca_b89d_d2cc_a72104d95a3b["ClassBody.js"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|defined in| ae63a5f8_3bca_b89d_d2cc_a72104d95a3b
  d69a156c_617a_9397_4c8b_e94508c59e30["get_name()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| d69a156c_617a_9397_4c8b_e94508c59e30
  bed91719_d047_2256_e199_ee875d5f49b9["get_rune()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| bed91719_d047_2256_e199_ee875d5f49b9
  c37a3aba_1e0c_9f3e_e144_1a7fa20244b3["is_state_creation_rune()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| c37a3aba_1e0c_9f3e_e144_1a7fa20244b3
  d0439414_5a58_b32e_0bc8_f7b2ab0181fc["state_field_duplicate()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| d0439414_5a58_b32e_0bc8_f7b2ab0181fc
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  13a28905_dd99_10ff_3a5b_5bd11030d45f["duplicate_class_field()"]
  0dab1d36_2fa3_6bd6_6043_59c1aed4bccf -->|calls| 13a28905_dd99_10ff_3a5b_5bd11030d45f
  style 0dab1d36_2fa3_6bd6_6043_59c1aed4bccf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js lines 15–156

export function ClassBody(node, context) {
	if (!context.state.analysis.runes) {
		context.next();
		return;
	}

	/** @type {string[]} */
	const private_ids = [];

	for (const prop of node.body) {
		if (
			(prop.type === 'MethodDefinition' || prop.type === 'PropertyDefinition') &&
			prop.key.type === 'PrivateIdentifier'
		) {
			private_ids.push(prop.key.name);
		}
	}

	/** @type {Map<string, StateField>} */
	const state_fields = new Map();

	/** @type {Map<string, Array<MethodDefinition['kind'] | 'prop' | 'assigned_prop'>>} */
	const fields = new Map();

	context.state.analysis.classes.set(node, state_fields);

	/** @type {MethodDefinition | null} */
	let constructor = null;

	/**
	 * @param {PropertyDefinition | AssignmentExpression} node
	 * @param {Expression | PrivateIdentifier} key
	 * @param {Expression | null | undefined} value
	 */
	function handle(node, key, value) {
		const name = get_name(key);
		if (name === null) return;

		const rune = get_rune(value, context.state.scope);

		if (rune && is_state_creation_rune(rune)) {
			if (state_fields.has(name)) {
				e.state_field_duplicate(node, name);
			}

			const _key = (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + name;
			const field = fields.get(_key);

			// if there's already a method or assigned field, error
			if (field && !(field.length === 1 && field[0] === 'prop')) {
				e.duplicate_class_field(node, _key);
			}

			state_fields.set(name, {
				node,
				type: rune,
				// @ts-expect-error for public state this is filled out in a moment
				key: key.type === 'PrivateIdentifier' ? key : null,
				value: /** @type {CallExpression} */ (value)
			});
		}
	}

	for (const child of node.body) {
		if (child.type === 'PropertyDefinition' && !child.computed && !child.static) {
			handle(child, child.key, child.value);
			const key = /** @type {string} */ (get_name(child.key));
			const field = fields.get(key);
			if (!field) {
				fields.set(key, [child.value ? 'assigned_prop' : 'prop']);
				continue;
			}
			e.duplicate_class_field(child, key);
		}

		if (child.type === 'MethodDefinition') {
			if (child.kind === 'constructor') {
				constructor = child;
			} else if (!child.computed) {
				const key = (child.static ? '@' : '') + get_name(child.key);
				const field = fields.get(key);

Domain

Subdomains

Frequently Asked Questions

What does ClassBody() do?
ClassBody() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js.
Where is ClassBody() defined?
ClassBody() is defined in packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js at line 15.
What does ClassBody() call?
ClassBody() calls 6 function(s): duplicate_class_field, get, get_name, get_rune, is_state_creation_rune, state_field_duplicate.

Analyze Your Own Codebase

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

Try Supermodel Free