Home / File/ SnippetBlock.js — svelte Source File

SnippetBlock.js — svelte Source File

Architecture documentation for SnippetBlock.js, a javascript file in the svelte codebase. 5 imports, 1 dependents.

File javascript Compiler Transformer 5 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  e0442a00_296c_5b68_2228_3e947a4a5278["SnippetBlock.js"]
  bf0d8f1b_17da_970d_bf44_fbcf099d5371["utils.js"]
  e0442a00_296c_5b68_2228_3e947a4a5278 --> bf0d8f1b_17da_970d_bf44_fbcf099d5371
  ea08cb05_2664_4e93_7551_6103e0cb3a87["validate_block_not_empty"]
  e0442a00_296c_5b68_2228_3e947a4a5278 --> ea08cb05_2664_4e93_7551_6103e0cb3a87
  7148e639_69d8_a03d_3f08_bd23f41e718a["validate_opening_tag"]
  e0442a00_296c_5b68_2228_3e947a4a5278 --> 7148e639_69d8_a03d_3f08_bd23f41e718a
  495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"]
  e0442a00_296c_5b68_2228_3e947a4a5278 --> 495501a4_a342_6a4d_ac11_e3e2fee8b218
  a146f6ac_0088_8736_b6ce_318f9f115170["e"]
  e0442a00_296c_5b68_2228_3e947a4a5278 --> a146f6ac_0088_8736_b6ce_318f9f115170
  4aa8a188_84d4_0274_ed83_cac0ab1d3572["index.js"]
  4aa8a188_84d4_0274_ed83_cac0ab1d3572 --> e0442a00_296c_5b68_2228_3e947a4a5278
  style e0442a00_296c_5b68_2228_3e947a4a5278 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { AST, Binding } from '#compiler' */
/** @import { Scope } from '../../scope' */
/** @import { Context } from '../types' */
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';
import * as e from '../../../errors.js';

/**
 * @param {AST.SnippetBlock} node
 * @param {Context} context
 */
export function SnippetBlock(node, context) {
	context.state.analysis.snippets.add(node);

	validate_block_not_empty(node.body, context);

	if (context.state.analysis.runes) {
		validate_opening_tag(node, context.state, '#');
	}

	for (const arg of node.parameters) {
		if (arg.type === 'RestElement') {
			e.snippet_invalid_rest_parameter(arg);
		}
	}

	context.next({ ...context.state, parent_element: null });

	const can_hoist =
		context.path.length === 1 &&
		context.path[0].type === 'Fragment' &&
		can_hoist_snippet(context.state.scope, context.state.scopes);

	const name = node.expression.name;

	if (can_hoist) {
		const binding = /** @type {Binding} */ (context.state.scope.get(name));
		context.state.analysis.module.scope.declarations.set(name, binding);
	}

	node.metadata.can_hoist = can_hoist;

	const { path } = context;
	const parent = path.at(-2);
	if (!parent) return;

	if (
		parent.type === 'Component' &&
		parent.attributes.some(
			(attribute) =>
				(attribute.type === 'Attribute' || attribute.type === 'BindDirective') &&
				attribute.name === node.expression.name
		)
	) {
		e.snippet_shadowing_prop(node, node.expression.name);
	}

	if (node.expression.name !== 'children') return;

	if (
		parent.type === 'Component' ||
		parent.type === 'SvelteComponent' ||
		parent.type === 'SvelteSelf'
	) {
		if (
			parent.fragment.nodes.some(
				(node) =>
					node.type !== 'SnippetBlock' &&
					(node.type !== 'Text' || node.data.trim()) &&
					node.type !== 'Comment'
			)
		) {
			e.snippet_conflict(node);
		}
	}
}

/**
 * @param {Map<AST.SvelteNode, Scope>} scopes
 * @param {Scope} scope
 */
function can_hoist_snippet(scope, scopes, visited = new Set()) {
	for (const [reference] of scope.references) {
		const binding = scope.get(reference);
		if (!binding) continue;

		if (binding.blocker) {
			return false;
		}

		if (binding.scope.function_depth === 0) {
			continue;
		}

		// ignore bindings declared inside the snippet (e.g. the snippet's own parameters)
		if (binding.scope.function_depth >= scope.function_depth) {
			continue;
		}

		if (binding.initial?.type === 'SnippetBlock') {
			if (visited.has(binding)) continue;
			visited.add(binding);
			const snippet_scope = /** @type {Scope} */ (scopes.get(binding.initial));

			if (can_hoist_snippet(snippet_scope, scopes, visited)) {
				continue;
			}
		}

		return false;
	}

	return true;
}

Domain

Subdomains

Frequently Asked Questions

What does SnippetBlock.js do?
SnippetBlock.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 SnippetBlock.js?
SnippetBlock.js defines 2 function(s): SnippetBlock, can_hoist_snippet.
What does SnippetBlock.js depend on?
SnippetBlock.js imports 5 module(s): e, errors.js, utils.js, validate_block_not_empty, validate_opening_tag.
What files import SnippetBlock.js?
SnippetBlock.js is imported by 1 file(s): index.js.
Where is SnippetBlock.js in the architecture?
SnippetBlock.js is located at packages/svelte/src/compiler/phases/2-analyze/visitors/SnippetBlock.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/2-analyze/visitors).

Analyze Your Own Codebase

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

Try Supermodel Free