Home / Class/ Scope Class — svelte Architecture

Scope Class — svelte Architecture

Architecture documentation for the Scope class in scope.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  36ef071d_b929_6d90_0038_9f22de99efbe["Scope"]
  ee93d8a6_6fde_b1c1_e15b_3a4da5326305["scope.js"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|defined in| ee93d8a6_6fde_b1c1_e15b_3a4da5326305
  db5e38b6_b71a_4de0_9a78_473b4ad0ae61["constructor()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| db5e38b6_b71a_4de0_9a78_473b4ad0ae61
  44642633_e3c9_8ae1_48af_53507481a2cb["declare()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| 44642633_e3c9_8ae1_48af_53507481a2cb
  2f404503_a86a_3205_14f4_8e1279b02d1c["child()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| 2f404503_a86a_3205_14f4_8e1279b02d1c
  8980dd2b_1c7a_2c03_2400_e31c60358534["generate()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| 8980dd2b_1c7a_2c03_2400_e31c60358534
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  620ed390_0877_e37c_0313_1a3448ba6a56["get_bindings()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| 620ed390_0877_e37c_0313_1a3448ba6a56
  fe09b863_ce2a_faad_7b1b_3bc9e4c3c6a0["owner()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| fe09b863_ce2a_faad_7b1b_3bc9e4c3c6a0
  e5ba0f80_c4ba_4a9e_c19c_5b02f60481ca["reference()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| e5ba0f80_c4ba_4a9e_c19c_5b02f60481ca
  c460607c_df6f_f0bc_cf31_b4c25e6d1506["evaluate()"]
  36ef071d_b929_6d90_0038_9f22de99efbe -->|method| c460607c_df6f_f0bc_cf31_b4c25e6d1506

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/scope.js lines 599–792

export class Scope {
	/** @type {ScopeRoot} */
	root;

	/**
	 * The immediate parent scope
	 * @type {Scope | null}
	 */
	parent;

	/**
	 * Whether or not `var` declarations are contained by this scope
	 * @type {boolean}
	 */
	#porous;

	/**
	 * A map of every identifier declared by this scope, and all the
	 * identifiers that reference it
	 * @type {Map<string, Binding>}
	 */
	declarations = new Map();

	/**
	 * A map of declarators to the bindings they declare
	 * @type {Map<VariableDeclarator | AST.LetDirective, Binding[]>}
	 */
	declarators = new Map();

	/**
	 * A set of all the names referenced with this scope
	 * — useful for generating unique names
	 * @type {Map<string, { node: Identifier; path: AST.SvelteNode[] }[]>}
	 */
	references = new Map();

	/**
	 * The scope depth allows us to determine if a state variable is referenced in its own scope,
	 * which is usually an error. Block statements do not increase this value
	 */
	function_depth = 0;

	/**
	 * If tracing of reactive dependencies is enabled for this scope
	 * @type {null | Expression}
	 */
	tracing = null;

	/**
	 *
	 * @param {ScopeRoot} root
	 * @param {Scope | null} parent
	 * @param {boolean} porous
	 */
	constructor(root, parent, porous) {
		this.root = root;
		this.parent = parent;
		this.#porous = porous;
		this.function_depth = parent ? parent.function_depth + (porous ? 0 : 1) : 0;
	}

	/**
	 * @param {Identifier} node
	 * @param {Binding['kind']} kind
	 * @param {DeclarationKind} declaration_kind
	 * @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock | AST.SnippetBlock} initial
	 * @returns {Binding}
	 */
	declare(node, kind, declaration_kind, initial = null) {
		if (this.parent) {
			if (declaration_kind === 'var' && this.#porous) {
				return this.parent.declare(node, kind, declaration_kind);
			}

			if (declaration_kind === 'import') {
				return this.parent.declare(node, kind, declaration_kind, initial);
			}
		}

		if (this.declarations.has(node.name)) {
			const binding = this.declarations.get(node.name);

Domain

Frequently Asked Questions

What is the Scope class?
Scope is a class in the svelte codebase, defined in packages/svelte/src/compiler/phases/scope.js.
Where is Scope defined?
Scope is defined in packages/svelte/src/compiler/phases/scope.js at line 599.

Analyze Your Own Codebase

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

Try Supermodel Free