Home / File/ utils.js — svelte Source File

utils.js — svelte Source File

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

File javascript Compiler Analyzer 2 dependents 6 functions

Entity Profile

Dependency Diagram

graph LR
  ad593fd6_3727_04c3_f22e_c845647cea4d["utils.js"]
  ca006f7f_f554_f529_8a19_abaaa45dda8d["css-analyze.js"]
  ca006f7f_f554_f529_8a19_abaaa45dda8d --> ad593fd6_3727_04c3_f22e_c845647cea4d
  cb1bf043_dade_7352_cc2b_976ffa2968d8["css-prune.js"]
  cb1bf043_dade_7352_cc2b_976ffa2968d8 --> ad593fd6_3727_04c3_f22e_c845647cea4d
  style ad593fd6_3727_04c3_f22e_c845647cea4d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { AST } from '#compiler' */
/** @import { Node } from 'estree' */
const UNKNOWN = {};

/**
 * @param {Node} node
 * @param {boolean} is_class
 * @param {Set<any>} set
 * @param {boolean} is_nested
 */
function gather_possible_values(node, is_class, set, is_nested = false) {
	if (set.has(UNKNOWN)) {
		// no point traversing any further
		return;
	}

	if (node.type === 'Literal') {
		set.add(String(node.value));
	} else if (node.type === 'ConditionalExpression') {
		gather_possible_values(node.consequent, is_class, set, is_nested);
		gather_possible_values(node.alternate, is_class, set, is_nested);
	} else if (node.type === 'LogicalExpression') {
		if (node.operator === '&&') {
			// && is a special case, because the only way the left
			// hand value can be included is if it's falsy. this is
			// a bit of extra work but it's worth it because
			// `class={[condition && 'blah']}` is common,
			// and we don't want to deopt on `condition`
			const left = new Set();
			gather_possible_values(node.left, is_class, left, is_nested);

			if (left.has(UNKNOWN)) {
				// add all non-nullish falsy values, unless this is a `class` attribute that
				// will be processed by cslx, in which case falsy values are removed, unless
				// they're not inside an array/object (TODO 6.0 remove that last part)
				if (!is_class || !is_nested) {
					set.add('');
					set.add(false);
					set.add(NaN);
					set.add(0); // -0 and 0n are also falsy, but stringify to '0'
				}
			} else {
				for (const value of left) {
					if (!value && value != undefined && (!is_class || !is_nested)) {
						set.add(value);
					}
				}
			}

			gather_possible_values(node.right, is_class, set, is_nested);
		} else {
			gather_possible_values(node.left, is_class, set, is_nested);
			gather_possible_values(node.right, is_class, set, is_nested);
		}
	} else if (is_class && node.type === 'ArrayExpression') {
		for (const entry of node.elements) {
			if (entry) {
				gather_possible_values(entry, is_class, set, true);
			}
		}
// ... (118 more lines)

Domain

Subdomains

Frequently Asked Questions

What does utils.js do?
utils.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Analyzer subdomain.
What functions are defined in utils.js?
utils.js defines 6 function(s): gather_possible_values, get_parent_rules, get_possible_values, is_global, is_outer_global, is_unscoped_pseudo_class.
What files import utils.js?
utils.js is imported by 2 file(s): css-analyze.js, css-prune.js.
Where is utils.js in the architecture?
utils.js is located at packages/svelte/src/compiler/phases/2-analyze/css/utils.js (domain: Compiler, subdomain: Analyzer, directory: packages/svelte/src/compiler/phases/2-analyze/css).

Analyze Your Own Codebase

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

Try Supermodel Free