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

99.13% Statements 228/230
97.67% Branches 84/86
100% Functions 8/8
99.11% Lines 225/227

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 2282x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3089x 3089x 3089x 2143x 2143x 2143x 753x     753x 753x 1x 1x 753x 2142x 2143x 1x 1x 2143x 3073x 3073x 3073x 3073x 3073x 3073x 3080x 900x 900x 900x 3073x 3089x 36x 2x 2x 36x 3089x 2x 2x 2x 2x 2x 2x 2x 2x 4349x 66x 130x 130x 130x 130x 4349x 70x 108x 108x 108x 108x 4283x 3081x 3081x 3081x 3081x 3076x 3081x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 21x 23x 23x 23x 9x 23x 14x 14x 23x 3081x 4349x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1409x 1x 1x 1x 1409x 2x 2x 2x 2x 2x 2x 4058x 2411x 2411x 4058x 6x 6x 4058x 2x 2x 2x 2x 2x 2x 8137x 8137x 8655x 8655x 8655x 7789x 8655x 1x 1x 1x 8655x 8136x 2x 2x 2x 2x 2x 2x 2x 2x 9407x 9407x 9407x 9407x 8881x 8881x 9407x 6885x 9356x 209x 209x 6676x 6676x 9332x 9407x 9407x 4758x 9407x 9407x 2x 2x 2x 2x 2x 2x 2x 3176x 12x 12x 3164x 3164x 3176x 3094x 3094x 3094x 3094x 3094x 2539x 2539x 2539x 2539x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 26838x 26838x 26838x 26838x 26838x 26838x 26838x 26838x 16816x 26838x 13838x 13838x 13838x 4x 13838x 1x 1x 13838x 26838x  
/** @import { AssignmentExpression, Expression, Identifier, Pattern, PrivateIdentifier, Super, UpdateExpression, VariableDeclarator } from 'estree' */
/** @import { AST, Binding } from '#compiler' */
/** @import { AnalysisState, Context } from '../../types' */
/** @import { Scope } from '../../../scope' */
/** @import { NodeLike } from '../../../../errors.js' */
import * as e from '../../../../errors.js';
import { extract_identifiers, object } from '../../../../utils/ast.js';
import * as w from '../../../../warnings.js';
 
/**
 * @param {AssignmentExpression | UpdateExpression} node
 * @param {Pattern | Expression} argument
 * @param {AnalysisState} state
 */
export function validate_assignment(node, argument, state) {
	validate_no_const_assignment(node, argument, state.scope, false);
 
	if (argument.type === 'Identifier') {
		const binding = state.scope.get(argument.name);
 
		if (state.analysis.runes) {
			if (binding?.kind === 'derived') {
				e.constant_assignment(node, 'derived state');
			}
 
			if (binding?.kind === 'each') {
				e.each_item_invalid_assignment(node);
			}
		}
 
		if (binding?.kind === 'snippet') {
			e.snippet_parameter_assignment(node);
		}
	}
 
	let object = /** @type {Expression | Super} */ (argument);
 
	/** @type {Expression | PrivateIdentifier | null} */
	let property = null;
 
	while (object.type === 'MemberExpression') {
		property = object.property;
		object = object.object;
	}
 
	if (object.type === 'ThisExpression' && property?.type === 'PrivateIdentifier') {
		if (state.private_derived_state.includes(property.name)) {
			e.constant_assignment(node, 'derived state');
		}
	}
}
 
/**
 * @param {NodeLike} node
 * @param {Pattern | Expression} argument
 * @param {Scope} scope
 * @param {boolean} is_binding
 */
export function validate_no_const_assignment(node, argument, scope, is_binding) {
	if (argument.type === 'ArrayPattern') {
		for (const element of argument.elements) {
			if (element) {
				validate_no_const_assignment(node, element, scope, is_binding);
			}
		}
	} else if (argument.type === 'ObjectPattern') {
		for (const element of argument.properties) {
			if (element.type === 'Property') {
				validate_no_const_assignment(node, element.value, scope, is_binding);
			}
		}
	} else if (argument.type === 'Identifier') {
		const binding = scope.get(argument.name);
		if (
			binding?.kind === 'derived' ||
			binding?.declaration_kind === 'import' ||
			(binding?.declaration_kind === 'const' && binding.kind !== 'each')
		) {
			// e.invalid_const_assignment(
			// 	node,
			// 	is_binding,
			// 	// This takes advantage of the fact that we don't assign initial for let directives and then/catch variables.
			// 	// If we start doing that, we need another property on the binding to differentiate, or give up on the more precise error message.
			// 	binding.kind !== 'state' &&
			// 		binding.kind !== 'raw_state' &&
			// 		(binding.kind !== 'normal' || !binding.initial)
			// );
 
			// TODO have a more specific error message for assignments to things like `{:then foo}`
			const thing =
				binding.declaration_kind === 'import'
					? 'import'
					: binding.kind === 'derived'
						? 'derived state'
						: 'constant';
 
			if (is_binding) {
				e.constant_binding(node, thing);
			} else {
				e.constant_assignment(node, thing);
			}
		}
	}
}
 
/**
 * Validates that the opening of a control flow block is `{` immediately followed by the expected character.
 * In legacy mode whitespace is allowed inbetween. TODO remove once legacy mode is gone and move this into parser instead.
 * @param {{start: number; end: number}} node
 * @param {AnalysisState} state
 * @param {string} expected
 */
export function validate_opening_tag(node, state, expected) {
	if (state.analysis.source[node.start + 1] !== expected) {
		// avoid a sea of red and only mark the first few characters
		e.block_unexpected_character({ start: node.start, end: node.start + 5 }, expected);
	}
}
 
/**
 * @param {AST.Fragment | null | undefined} node
 * @param {Context} context
 */
export function validate_block_not_empty(node, context) {
	if (!node) return;
	// Assumption: If the block has zero elements, someone's in the middle of typing it out,
	// so don't warn in that case because it would be distracting.
	if (node.nodes.length === 1 && node.nodes[0].type === 'Text' && !node.nodes[0].raw.trim()) {
		w.block_empty(node.nodes[0]);
	}
}
 
/**
 * @param {VariableDeclarator} node
 * @param {AnalysisState} state
 */
export function ensure_no_module_import_conflict(node, state) {
	const ids = extract_identifiers(node.id);
	for (const id of ids) {
		if (
			state.ast_type === 'instance' &&
			state.scope === state.analysis.instance.scope &&
			state.analysis.module.scope.get(id.name)?.declaration_kind === 'import'
		) {
			// TODO fix the message here
			e.declaration_duplicate_module_import(node.id);
		}
	}
}
 
/**
 * A 'safe' identifier means that the `foo` in `foo.bar` or `foo()` will not
 * call functions that require component context to exist
 * @param {Expression | Super} expression
 * @param {Scope} scope
 */
export function is_safe_identifier(expression, scope) {
	let node = expression;
	while (node.type === 'MemberExpression') node = node.object;
 
	if (node.type !== 'Identifier') return false;
 
	const binding = scope.get(node.name);
	if (!binding) return true;
 
	if (binding.kind === 'store_sub') {
		return is_safe_identifier({ name: node.name.slice(1), type: 'Identifier' }, scope);
	}
 
	return (
		binding.declaration_kind !== 'import' &&
		binding.kind !== 'prop' &&
		binding.kind !== 'bindable_prop' &&
		binding.kind !== 'rest_prop'
	);
}
 
/**
 * @param {Expression | Super} node
 * @param {Context} context
 * @returns {boolean}
 */
export function is_pure(node, context) {
	if (node.type !== 'Identifier' && node.type !== 'MemberExpression') {
		return false;
	}
 
	const left = object(node);
	if (!left) return false;
 
	if (left.type === 'Identifier') {
		const binding = context.state.scope.get(left.name);
		if (binding === null) return true; // globals are assumed to be safe
	}
 
	// TODO add more cases (safe Svelte imports, etc)
	return false;
}
 
/**
 * Checks if the name is valid, which it is when it's not starting with (or is) a dollar sign or if it's a function parameter.
 * The second argument is the depth of the scope, which is there for backwards compatibility reasons: In Svelte 4, you
 * were allowed to define `$`-prefixed variables anywhere below the top level of components. Once legacy mode is gone, this
 * argument can be removed / the call sites adjusted accordingly.
 * @param {Binding | null} binding
 * @param {number | undefined} [function_depth]
 */
export function validate_identifier_name(binding, function_depth) {
	if (!binding) return;
 
	const declaration_kind = binding.declaration_kind;
 
	if (
		declaration_kind !== 'synthetic' &&
		declaration_kind !== 'param' &&
		declaration_kind !== 'rest_param' &&
		(!function_depth || function_depth <= 1)
	) {
		const node = binding.node;
 
		if (node.name === '$') {
			e.dollar_binding_invalid(node);
		} else if (node.name.startsWith('$')) {
			e.dollar_prefix_invalid(node);
		}
	}
}