Home / File/ component.js — svelte Source File

component.js — svelte Source File

Architecture documentation for component.js, a javascript file in the svelte codebase. 15 imports, 3 dependents.

File javascript Compiler Transformer 15 imports 3 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  057ad110_57ab_942c_4f8d_5c2f711bef54["component.js"]
  495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 495501a4_a342_6a4d_ac11_e3e2fee8b218
  a146f6ac_0088_8736_b6ce_318f9f115170["e"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> a146f6ac_0088_8736_b6ce_318f9f115170
  0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c["ast.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c
  a997caf9_1d66_f005_5b11_675724bd0ed8["get_attribute_expression"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> a997caf9_1d66_f005_5b11_675724bd0ed8
  e4437bdc_6df3_4ac6_3252_12819762cc5e["is_expression_attribute"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> e4437bdc_6df3_4ac6_3252_12819762cc5e
  087a1de5_6350_4c2b_e177_479d75ba0608["slot.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 087a1de5_6350_4c2b_e177_479d75ba0608
  a2d986c5_5ad1_b6ea_d335_4846d9af9e9f["determine_slot"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> a2d986c5_5ad1_b6ea_d335_4846d9af9e9f
  32df1fa2_adf7_5122_0b02_c399ea508ae0["attribute.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 32df1fa2_adf7_5122_0b02_c399ea508ae0
  c3dd29c6_654d_d119_4318_e8151ff6da98["validate_attribute"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> c3dd29c6_654d_d119_4318_e8151ff6da98
  cea20d98_b8d1_aa5c_5f46_4ac417b7053c["validate_attribute_name"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> cea20d98_b8d1_aa5c_5f46_4ac417b7053c
  c3969ef3_0f9b_1699_e3b6_75b4a9916c7d["validate_slot_attribute"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> c3969ef3_0f9b_1699_e3b6_75b4a9916c7d
  c4b4ac8d_9914_5ede_1aea_723bf80d2e9b["fragment.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> c4b4ac8d_9914_5ede_1aea_723bf80d2e9b
  313d2a82_30ea_3161_3aad_0cc2094979aa["mark_subtree_dynamic"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 313d2a82_30ea_3161_3aad_0cc2094979aa
  93f05ed0_daa8_8d1d_7b82_43c4a9020ecf["snippets.js"]
  057ad110_57ab_942c_4f8d_5c2f711bef54 --> 93f05ed0_daa8_8d1d_7b82_43c4a9020ecf
  style 057ad110_57ab_942c_4f8d_5c2f711bef54 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Expression } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { AnalysisState, Context } from '../../types' */
import * as e from '../../../../errors.js';
import { get_attribute_expression, is_expression_attribute } from '../../../../utils/ast.js';
import { determine_slot } from '../../../../utils/slot.js';
import {
	validate_attribute,
	validate_attribute_name,
	validate_slot_attribute
} from './attribute.js';
import { mark_subtree_dynamic } from './fragment.js';
import { is_resolved_snippet } from './snippets.js';

/**
 * @param {AST.Component | AST.SvelteComponent | AST.SvelteSelf} node
 * @param {Context} context
 */
export function visit_component(node, context) {
	node.metadata.path = [...context.path];

	// link this node to all the snippets that it could render, so that we can prune CSS correctly
	node.metadata.snippets = new Set();

	// 'resolved' means we know which snippets this component might render. if it is `false`,
	// then `node.metadata.snippets` is populated with every locally defined snippet
	// once analysis is complete
	let resolved = true;

	for (const attribute of node.attributes) {
		if (attribute.type === 'SpreadAttribute' || attribute.type === 'BindDirective') {
			resolved = false;
			continue;
		}

		if (attribute.type !== 'Attribute' || !is_expression_attribute(attribute)) {
			continue;
		}

		const expression = get_attribute_expression(attribute);

		// given an attribute like `foo={bar}`, if `bar` resolves to an import or a prop
		// then we know it doesn't reference a locally defined snippet. if it resolves
		// to a `{#snippet bar()}` then we know _which_ snippet it resolves to. in all
		// other cases, we can't know (without much more complex static analysis) which
		// snippets the component might render, so we treat the component as unresolved
		if (expression.type === 'Identifier') {
			const binding = context.state.scope.get(expression.name);

			resolved &&= is_resolved_snippet(binding);

			if (binding?.initial?.type === 'SnippetBlock') {
				node.metadata.snippets.add(binding.initial);
			}
		} else if (expression.type !== 'Literal') {
			resolved = false;
		}
	}

	if (resolved) {
// ... (118 more lines)

Domain

Subdomains

Frequently Asked Questions

What does component.js do?
component.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 component.js?
component.js defines 2 function(s): disallow_unparenthesized_sequences, visit_component.
What does component.js depend on?
component.js imports 15 module(s): ast.js, attribute.js, determine_slot, e, errors.js, fragment.js, get_attribute_expression, is_expression_attribute, and 7 more.
What files import component.js?
component.js is imported by 3 file(s): Component.js, SvelteComponent.js, SvelteSelf.js.
Where is component.js in the architecture?
component.js is located at packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/2-analyze/visitors/shared).

Analyze Your Own Codebase

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

Try Supermodel Free