Home / File/ assignments.js — svelte Source File

assignments.js — svelte Source File

Architecture documentation for assignments.js, a javascript file in the svelte codebase. 6 imports, 2 dependents.

File javascript Compiler Transformer 6 imports 2 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  47b19192_eefb_9217_3996_3a0f4e07c6ed["assignments.js"]
  0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c["ast.js"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> 0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c
  c254e734_2224_c309_f1f8_bb064e80b1af["extract_paths"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> c254e734_2224_c309_f1f8_bb064e80b1af
  bdb5b0f7_5673_570b_155a_0bd0f9f8169a["is_expression_async"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> bdb5b0f7_5673_570b_155a_0bd0f9f8169a
  1a53d630_ca18_6783_bd92_8c72517a9306["declarations.js"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> 1a53d630_ca18_6783_bd92_8c72517a9306
  b1380aab_0ea6_e12d_3df0_c3526fef2b75["get_value"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> b1380aab_0ea6_e12d_3df0_c3526fef2b75
  95c28355_f14c_c3cd_5a03_d5a53ca255bc["builders"]
  47b19192_eefb_9217_3996_3a0f4e07c6ed --> 95c28355_f14c_c3cd_5a03_d5a53ca255bc
  96ee438d_6c6f_9aff_59f4_d00e63e9d98c["AssignmentExpression.js"]
  96ee438d_6c6f_9aff_59f4_d00e63e9d98c --> 47b19192_eefb_9217_3996_3a0f4e07c6ed
  f75f2dc9_b2ee_72df_1184_7fbc8c163e48["AssignmentExpression.js"]
  f75f2dc9_b2ee_72df_1184_7fbc8c163e48 --> 47b19192_eefb_9217_3996_3a0f4e07c6ed
  style 47b19192_eefb_9217_3996_3a0f4e07c6ed fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { AssignmentExpression, AssignmentOperator, Expression, Node, Pattern, Statement } from 'estree' */
/** @import { Context as ClientContext } from '../client/types.js' */
/** @import { Context as ServerContext } from '../server/types.js' */
import { extract_paths, is_expression_async } from '../../../utils/ast.js';
import * as b from '#compiler/builders';
import { get_value } from '../client/visitors/shared/declarations.js';

/**
 * @template {ClientContext | ServerContext} Context
 * @param {AssignmentExpression} node
 * @param {Context} context
 * @param {(operator: AssignmentOperator, left: Pattern, right: Expression, context: Context) => Expression | null} build_assignment
 * @returns
 */
export function visit_assignment_expression(node, context, build_assignment) {
	if (
		node.left.type === 'ArrayPattern' ||
		node.left.type === 'ObjectPattern' ||
		node.left.type === 'RestElement'
	) {
		const value = /** @type {Expression} */ (context.visit(node.right));
		const should_cache = value.type !== 'Identifier';
		const rhs = should_cache ? b.id('$$value') : value;

		let changed = false;

		const { inserts, paths } = extract_paths(node.left, rhs);

		for (const { id } of inserts) {
			id.name = context.state.scope.generate('$$array');
		}

		const assignments = paths.map((path) => {
			const value = path.expression;

			let assignment = build_assignment('=', path.node, value, context);
			if (assignment !== null) changed = true;

			return (
				assignment ??
				b.assignment(
					'=',
					/** @type {Pattern} */ (context.visit(path.node)),
					/** @type {Expression} */ (context.visit(value))
				)
			);
		});

		if (!changed) {
			// No change to output -> nothing to transform -> we can keep the original assignment
			return null;
		}

		const is_standalone = /** @type {Node} */ (context.path.at(-1)).type.endsWith('Statement');

		if (inserts.length > 0 || should_cache) {
			/** @type {Statement[]} */
			const statements = [
				...inserts.map(({ id, value }) => b.var(id, value)),
				...assignments.map(b.stmt)
			];

			if (!is_standalone) {
				// this is part of an expression, we need the sequence to end with the value
				statements.push(b.return(rhs));
			}

			const async =
				is_expression_async(value) ||
				assignments.some((assignment) => is_expression_async(assignment));

			const iife = b.arrow([rhs], b.block(statements), async);
			const call = b.call(iife, value);

			return async ? b.await(call) : call;
		}

		const sequence = b.sequence(assignments);

		if (!is_standalone) {
			// this is part of an expression, we need the sequence to end with the value
			sequence.expressions.push(rhs);
		}

		return sequence;
	}

	if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') {
		throw new Error(`Unexpected assignment type ${node.left.type}`);
	}

	return build_assignment(node.operator, node.left, node.right, context);
}

Domain

Subdomains

Frequently Asked Questions

What does assignments.js do?
assignments.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Transformer subdomain.
What functions are defined in assignments.js?
assignments.js defines 1 function(s): visit_assignment_expression.
What does assignments.js depend on?
assignments.js imports 6 module(s): ast.js, builders, declarations.js, extract_paths, get_value, is_expression_async.
What files import assignments.js?
assignments.js is imported by 2 file(s): AssignmentExpression.js, AssignmentExpression.js.
Where is assignments.js in the architecture?
assignments.js is located at packages/svelte/src/compiler/phases/3-transform/shared/assignments.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/3-transform/shared).

Analyze Your Own Codebase

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

Try Supermodel Free