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

93.89% Statements 200/213
91.02% Branches 71/78
100% Functions 2/2
93.77% Lines 196/209

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 2102x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5112x 5112x 5112x 5112x 5112x 5112x 3454x 1152x 1152x 3454x 3454x 5112x 5112x 53x 1x 1x 52x 52x 52x 53x 53x 52x 52x 52x 52x 53x 2x 2x 50x 50x 5112x 5112x 2x   2x 2x 2x     5112x 5112x 252x 1x 1x 251x 251x 251x 251x 251x 252x 250x 252x 2x 2x 249x 252x 1x 1x 248x 248x 5112x 5112x 5112x 5112x 5112x 1132x 1132x 122x 1132x 5x 5x 1127x 1132x 2x 1132x 2x 2x 1123x 1123x 5112x 5112x 5112x 152x 2x 2x 150x 150x 2x 2x 148x 148x 148x 148x 148x 148x 5112x 5112x 10x     10x 10x 5112x 5112x 14x     14x 14x 5112x 5112x 24x     24x 24x 5112x 5112x 4x     4x 4x 5112x 5112x 11x 1x 1x 10x 10x 5112x 5112x 4x     4x 4x 5112x 5089x 5112x 952x 952x 952x 5089x 5112x 157x 157x 157x 157x 157x 157x 157x 2x 2x 157x 5089x 5112x 3150x 3150x 3150x 1562x 1562x 3150x 5089x 5089x 5112x 163x 5112x 4926x 4926x 5112x 2x 2x 2x 2x 2x 2x 953x 953x 953x 953x 953x 424x 424x 424x 424x 9x 424x 1x 1x 424x 952x 952x 952x 952x 952x  
/** @import { CallExpression, VariableDeclarator } from 'estree' */
/** @import { SvelteNode } from '#compiler' */
/** @import { Context } from '../types' */
import { get_rune } from '../../scope.js';
import * as e from '../../../errors.js';
import { get_parent, unwrap_optional } from '../../../utils/ast.js';
import { is_safe_identifier } from './shared/utils.js';
 
/**
 * @param {CallExpression} node
 * @param {Context} context
 */
export function CallExpression(node, context) {
	const parent = /** @type {SvelteNode} */ (get_parent(context.path, -1));
 
	const rune = get_rune(node, context.state.scope);
 
	switch (rune) {
		case null:
			if (!is_safe_identifier(node.callee, context.state.scope)) {
				context.state.analysis.needs_context = true;
			}
 
			break;
 
		case '$bindable':
			if (node.arguments.length > 1) {
				e.rune_invalid_arguments_length(node, '$bindable', 'zero or one arguments');
			}
 
			if (
				parent.type !== 'AssignmentPattern' ||
				context.path.at(-3)?.type !== 'ObjectPattern' ||
				context.path.at(-4)?.type !== 'VariableDeclarator' ||
				get_rune(
					/** @type {VariableDeclarator} */ (context.path.at(-4)).init,
					context.state.scope
				) !== '$props'
			) {
				e.bindable_invalid_location(node);
			}
 
			break;
 
		case '$host':
			if (node.arguments.length > 0) {
				e.rune_invalid_arguments(node, '$host');
			} else if (context.state.ast_type === 'module' || !context.state.analysis.custom_element) {
				e.host_invalid_placement(node);
			}

			break;
 
		case '$props':
			if (context.state.has_props_rune) {
				e.props_duplicate(node);
			}
 
			context.state.has_props_rune = true;
 
			if (
				parent.type !== 'VariableDeclarator' ||
				context.state.ast_type !== 'instance' ||
				context.state.scope !== context.state.analysis.instance.scope
			) {
				e.props_invalid_placement(node);
			}
 
			if (node.arguments.length > 0) {
				e.rune_invalid_arguments(node, rune);
			}
 
			break;
 
		case '$state':
		case '$state.frozen':
		case '$derived':
		case '$derived.by':
			if (
				parent.type !== 'VariableDeclarator' &&
				!(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed)
			) {
				e.state_invalid_placement(node, rune);
			}
 
			if ((rune === '$derived' || rune === '$derived.by') && node.arguments.length !== 1) {
				e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
			} else if (rune === '$state' && node.arguments.length > 1) {
				e.rune_invalid_arguments_length(node, rune, 'zero or one arguments');
			}
 
			break;
 
		case '$effect':
		case '$effect.pre':
			if (parent.type !== 'ExpressionStatement') {
				e.effect_invalid_placement(node);
			}
 
			if (node.arguments.length !== 1) {
				e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
			}
 
			// `$effect` needs context because Svelte needs to know whether it should re-run
			// effects that invalidate themselves, and that's determined by whether we're in runes mode
			context.state.analysis.needs_context = true;
 
			break;
 
		case '$effect.tracking':
			if (node.arguments.length !== 0) {
				e.rune_invalid_arguments(node, rune);
			}
 
			break;
 
		case '$effect.root':
			if (node.arguments.length !== 1) {
				e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
			}
 
			break;
 
		case '$inspect':
			if (node.arguments.length < 1) {
				e.rune_invalid_arguments_length(node, rune, 'one or more arguments');
			}
 
			break;
 
		case '$inspect().with':
			if (node.arguments.length !== 1) {
				e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
			}
 
			break;
 
		case '$state.snapshot':
			if (node.arguments.length !== 1) {
				e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
			}
 
			break;
 
		case '$state.is':
			if (node.arguments.length !== 2) {
				e.rune_invalid_arguments_length(node, rune, 'exactly two arguments');
			}
 
			break;
	}
 
	if (context.state.expression && !is_known_safe_call(node, context)) {
		context.state.expression.has_call = true;
		context.state.expression.has_state = true;
	}
 
	if (context.state.render_tag) {
		// Find out which of the render tag arguments contains this call expression
		const arg_idx = unwrap_optional(context.state.render_tag.expression).arguments.findIndex(
			(arg) => arg === node || context.path.includes(arg)
		);
 
		// -1 if this is the call expression of the render tag itself
		if (arg_idx !== -1) {
			context.state.render_tag.metadata.args_with_call_expression.add(arg_idx);
		}
	}
 
	if (node.callee.type === 'Identifier') {
		const binding = context.state.scope.get(node.callee.name);
 
		if (binding !== null) {
			binding.is_called = true;
		}
	}
 
	// `$inspect(foo)` or `$derived(foo) should not trigger the `static-state-reference` warning
	if (rune === '$inspect' || rune === '$derived') {
		context.next({ ...context.state, function_depth: context.state.function_depth + 1 });
	} else {
		context.next();
	}
}
 
/**
 * @param {CallExpression} node
 * @param {Context} context
 * @returns {boolean}
 */
function is_known_safe_call(node, context) {
	const callee = node.callee;
 
	// String / Number / BigInt / Boolean casting calls
	if (callee.type === 'Identifier') {
		const name = callee.name;
		const binding = context.state.scope.get(name);
		if (
			binding === null &&
			(name === 'BigInt' || name === 'String' || name === 'Number' || name === 'Boolean')
		) {
			return true;
		}
	}
 
	// TODO add more cases
 
	return false;
}