All files / src/compiler/phases/2-analyze/visitors Identifier.js

98.74% Statements 157/159
97.01% Branches 65/67
100% Functions 1/1
98.7% Lines 152/154

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 1552x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 45671x 45671x 45671x 45671x 9385x 9385x 36286x 36286x 36286x 45671x 3x 45671x 1x 1x 36285x 36285x 45671x 12x 12x 36285x 45671x 10439x 10439x 10439x 1635x 10439x 1590x 1590x 1590x 1590x 1590x 112x 112x 112x 112x 112x 112x 112x 2x 1x 1x 1x 1x 1x 112x 1588x 1590x 2x 2x 1590x 10439x 36281x 36281x 36281x 40000x 25846x 68x 68x 25846x 25846x 51x 51x 25846x 25846x 25846x 14118x 14118x 14118x 25846x 9262x 751x 751x 22x 751x 4x 751x 4x 4x 9262x 8511x 2807x 2807x 8511x 685x 8511x 7826x 7826x 14x 7826x     25846x 330x 330x 330x 1488x 1488x 1488x 350x 350x 1488x 330x 298x 24x 24x 298x 330x 330x 1488x 330x 25846x 36281x 45671x 16954x 7326x 7326x 7326x 16954x 16954x 16954x 16954x 16954x 130x 130x 130x 58x 39x 39x 39x 130x 130x 16954x 34x 16954x 21x 16954x 16x 16x 16954x 45671x  
/** @import { Expression, Identifier } from 'estree' */
/** @import { SvelteNode } from '#compiler' */
/** @import { Context } from '../types' */
import is_reference from 'is-reference';
import { Runes } from '../../constants.js';
import { should_proxy_or_freeze } from '../../3-transform/client/utils.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
 
/**
 * @param {Identifier} node
 * @param {Context} context
 */
export function Identifier(node, context) {
	let i = context.path.length;
	let parent = /** @type {Expression} */ (context.path[--i]);
 
	if (!is_reference(node, parent)) {
		return;
	}
 
	// If we are using arguments outside of a function, then throw an error
	if (
		node.name === 'arguments' &&
		!context.path.some((n) => n.type === 'FunctionDeclaration' || n.type === 'FunctionExpression')
	) {
		e.invalid_arguments_usage(node);
	}
 
	// `$$slots` exists even in runes mode
	if (node.name === '$$slots') {
		context.state.analysis.uses_slots = true;
	}
 
	if (context.state.analysis.runes) {
		if (
			Runes.includes(/** @type {Runes[number]} */ (node.name)) &&
			context.state.scope.get(node.name) === null &&
			context.state.scope.get(node.name.slice(1)) === null
		) {
			/** @type {Expression} */
			let current = node;
			let name = node.name;
 
			while (parent.type === 'MemberExpression') {
				if (parent.computed) e.rune_invalid_computed_property(parent);
				name += `.${/** @type {Identifier} */ (parent.property).name}`;
 
				current = parent;
				parent = /** @type {Expression} */ (context.path[--i]);
 
				if (!Runes.includes(/** @type {Runes[number]} */ (name))) {
					if (name === '$effect.active') {
						e.rune_renamed(parent, '$effect.active', '$effect.tracking');
					}
 
					e.rune_invalid_name(parent, name);
				}
			}
 
			if (parent.type !== 'CallExpression') {
				e.rune_missing_parentheses(current);
			}
		}
	}
 
	let binding = context.state.scope.get(node.name);
 
	if (!context.state.analysis.runes) {
		if (node.name === '$$props') {
			context.state.analysis.uses_props = true;
		}
 
		if (node.name === '$$restProps') {
			context.state.analysis.uses_rest_props = true;
		}
 
		if (
			binding?.kind === 'normal' &&
			((binding.scope === context.state.instance_scope &&
				binding.declaration_kind !== 'function') ||
				binding.declaration_kind === 'import')
		) {
			if (binding.declaration_kind === 'import') {
				if (
					binding.mutated &&
					// TODO could be more fine-grained - not every mention in the template implies a state binding
					(context.state.reactive_statement || context.state.ast_type === 'template') &&
					parent.type === 'MemberExpression'
				) {
					binding.kind = 'legacy_reactive_import';
				}
			} else if (
				binding.mutated &&
				// TODO could be more fine-grained - not every mention in the template implies a state binding
				(context.state.reactive_statement || context.state.ast_type === 'template')
			) {
				binding.kind = 'state';
			} else if (
				context.state.reactive_statement &&
				parent.type === 'AssignmentExpression' &&
				parent.left === binding.node
			) {
				binding.kind = 'derived';
			}
		} else if (binding?.kind === 'each' && binding.mutated) {
			// Ensure that the array is marked as reactive even when only its entries are mutated
			let i = context.path.length;
			while (i--) {
				const ancestor = context.path[i];
				if (
					ancestor.type === 'EachBlock' &&
					context.state.analysis.template.scopes.get(ancestor)?.declarations.get(node.name) ===
						binding
				) {
					for (const binding of ancestor.metadata.references) {
						if (binding.kind === 'normal') {
							binding.kind = 'state';
						}
					}
					break;
				}
			}
		}
	}
 
	if (binding && binding.kind !== 'normal') {
		if (context.state.expression) {
			context.state.expression.dependencies.add(binding);
			context.state.expression.has_state = true;
		}
 
		if (
			context.state.analysis.runes &&
			node !== binding.node &&
			context.state.function_depth === binding.scope.function_depth &&
			// If we have $state that can be proxied or frozen and isn't re-assigned, then that means
			// it's likely not using a primitive value and thus this warning isn't that helpful.
			((binding.kind === 'state' &&
				(binding.reassigned ||
					(binding.initial?.type === 'CallExpression' &&
						binding.initial.arguments.length === 1 &&
						binding.initial.arguments[0].type !== 'SpreadElement' &&
						!should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) ||
				binding.kind === 'frozen_state' ||
				binding.kind === 'derived') &&
			// We're only concerned with reads here
			(parent.type !== 'AssignmentExpression' || parent.left !== node) &&
			parent.type !== 'UpdateExpression'
		) {
			w.state_referenced_locally(node);
		}
	}
}