Home / Function/ css_visitors.Rule() — svelte Function Reference

css_visitors.Rule() — svelte Function Reference

Architecture documentation for the css_visitors.Rule() function in css-analyze.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  919e9fb3_8782_e50f_200f_8193cf4baf92["css_visitors.Rule()"]
  ca006f7f_f554_f529_8a19_abaaa45dda8d["css-analyze.js"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|defined in| ca006f7f_f554_f529_8a19_abaaa45dda8d
  f2097f76_79f5_e830_d0c1_53a0e3cec450["css_global_block_invalid_modifier_start()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| f2097f76_79f5_e830_d0c1_53a0e3cec450
  4db7b3fa_b8aa_076d_d05a_f249948a16bb["css_global_block_invalid_combinator()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| 4db7b3fa_b8aa_076d_d05a_f249948a16bb
  fd4b9795_4fd5_5fdd_9769_f9daf4c00a2d["css_global_block_invalid_list()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| fd4b9795_4fd5_5fdd_9769_f9daf4c00a2d
  07e212eb_13e1_9984_8d5f_21dbd89249aa["css_global_block_invalid_declaration()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| 07e212eb_13e1_9984_8d5f_21dbd89249aa
  183b2281_b2c6_91db_2cc3_516dd4473fb3["css_global_block_invalid_modifier()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| 183b2281_b2c6_91db_2cc3_516dd4473fb3
  17214093_1c75_8be2_ddbd_dac09483e51e["is_unscoped()"]
  919e9fb3_8782_e50f_200f_8193cf4baf92 -->|calls| 17214093_1c75_8be2_ddbd_dac09483e51e
  style 919e9fb3_8782_e50f_200f_8193cf4baf92 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js lines 197–286

	Rule(node, context) {
		node.metadata.parent_rule = context.state.rule;

		// We gotta allow :global x, :global y because CSS preprocessors might generate that from :global { x, y {...} }
		for (const complex_selector of node.prelude.children) {
			let is_global_block = false;

			for (let selector_idx = 0; selector_idx < complex_selector.children.length; selector_idx++) {
				const child = complex_selector.children[selector_idx];
				const idx = child.selectors.findIndex(is_global_block_selector);

				if (is_global_block) {
					// All selectors after :global are unscoped
					child.metadata.is_global_like = true;
				}

				if (idx === 0) {
					if (
						child.selectors.length > 1 &&
						selector_idx === 0 &&
						node.metadata.parent_rule === null
					) {
						e.css_global_block_invalid_modifier_start(child.selectors[1]);
					} else {
						// `child` starts with `:global`
						node.metadata.is_global_block = is_global_block = true;

						for (let i = 1; i < child.selectors.length; i++) {
							walk(/** @type {AST.CSS.Node} */ (child.selectors[i]), null, {
								ComplexSelector(node) {
									node.metadata.used = true;
								}
							});
						}

						if (child.combinator && child.combinator.name !== ' ') {
							e.css_global_block_invalid_combinator(child, child.combinator.name);
						}

						const declaration = node.block.children.find((child) => child.type === 'Declaration');
						const is_lone_global =
							complex_selector.children.length === 1 &&
							complex_selector.children[0].selectors.length === 1; // just `:global`, not e.g. `:global x`

						if (is_lone_global && node.prelude.children.length > 1) {
							// `:global, :global x { z { ... } }` would become `x { z { ... } }` which means `z` is always
							// constrained by `x`, which is not what the user intended
							e.css_global_block_invalid_list(node.prelude);
						}

						if (
							declaration &&
							// :global { color: red; } is invalid, but foo :global { color: red; } is valid
							node.prelude.children.length === 1 &&
							is_lone_global
						) {
							e.css_global_block_invalid_declaration(declaration);
						}
					}
				} else if (idx !== -1) {
					e.css_global_block_invalid_modifier(child.selectors[idx]);
				}
			}

			if (node.metadata.is_global_block && !is_global_block) {
				e.css_global_block_invalid_list(node.prelude);
			}
		}

		const state = { ...context.state, rule: node };

		// visit selector list first, to populate child selector metadata
		context.visit(node.prelude, state);

		for (const selector of node.prelude.children) {
			node.metadata.has_global_selectors ||= selector.metadata.is_global;
			node.metadata.has_local_selectors ||= !selector.metadata.is_global;
		}

		// if this rule has a ComplexSelector whose RelativeSelector children are all
		// `:global(...)`, and the rule contains declarations (rather than just

Domain

Subdomains

Frequently Asked Questions

What does css_visitors.Rule() do?
css_visitors.Rule() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js.
Where is css_visitors.Rule() defined?
css_visitors.Rule() is defined in packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js at line 197.
What does css_visitors.Rule() call?
css_visitors.Rule() calls 6 function(s): css_global_block_invalid_combinator, css_global_block_invalid_declaration, css_global_block_invalid_list, css_global_block_invalid_modifier, css_global_block_invalid_modifier_start, is_unscoped.

Analyze Your Own Codebase

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

Try Supermodel Free