All files / src/compiler/phases/2-analyze validation.js

94.8% Statements 456/481
90.25% Branches 139/154
96.15% Functions 25/26
94.38% Lines 420/445

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 4462x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1403x 1403x 1498x 1498x 1498x 1498x 1498x 302x 1498x 4x 4x 1494x 1494x 1498x 60x 1498x 1x 1x 1493x 1498x 937x 194x 194x 194x 187x 187x 2x 2x 2x 2x 1x 2x 2x 187x 194x 935x 935x 935x 937x 23x 23x 937x 1498x 1394x 1394x 1394x 1394x 1394x 1394x 1394x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25910x 25910x 3972x     3972x 2x 2x 33x 33x 1x 1x 32x 32x 32x 1x 1x 2x 2x 2x 7170x 7081x 4152x     4152x 7170x 2x 2x 2x 2x 6134x 6134x 6134x 354x 354x 354x 5718x 282x 6134x 353x 353x 1x 6134x     2x 2x 1482x 1482x 1482x 2x 2x 2x 101x 101x 2x 2x 2x 2x 2x 2x 7858x 7858x 7858x 8356x 8356x 7586x 8356x 1x 1x 8356x 7857x 2x 2x 2x 2x 2x 2x 2x 2x 2x 37x 37x 37x 37x 27x 27x 27x 37x 1x 37x 1x 26x 1x 25x     2x 2x 7x 2x 2x 4x 2x 2x 90x 90x 90x 90x 232x 232x 232x 36x 232x 26x 26x 4x 4x 26x 232x 90x 90x 90x 90x 90x 2x 2x 85x 85x 85x 85x 85x 85x 1x 1x 2x 2x 137x 2x 2x 2x 2x 14440x 14440x 14440x 14440x 14440x 14440x 14440x 1607x 14440x 1564x 1564x 1564x 1564x 1564x 108x 108x 108x 108x 108x 108x 108x 2x 1x 1x 1x 1x 1x 108x 1562x 1564x 2x 2x 1564x 14440x 2x 2x 2x 2x 2x 2x 2x 2x 2x 101x 101x 101x 101x 101x 101x 2x 2x 8x 8x 8x 8x 8x 8x     8x 8x 8x             2x 2x 2x 2x 2x 92x 2x 2x 10x 2x 2x 18x 2x 2x   2x 2x 143x 2x 2x 1724x 1724x 1724x 1724x 1724x 1724x 1237x 1237x 1237x 1237x 1724x 1x 1724x 1x 1236x 244x 1x 1x 243x 243x 243x 244x 1x 1x 242x 244x     242x 244x 1x 1x 241x 244x 238x 346x 318x     318x 318x 1x 1x 317x 317x 318x 318x 318x     318x 346x 237x 244x 1231x 1724x 110x 110x 110x 22x 110x     110x 2x 2x 17x 17x 17x 2x 2x 312x 312x 312x 294x 294x 2x 2x 2x 2x 2x 2x 2x  
/** @import { Expression, Identifier, VariableDeclarator } from 'estree' */
/** @import { Component, SvelteComponent, SvelteSelf } from '#compiler' */
/** @import { AnalysisState, Context, Visitors } from './types.js' */
import is_reference from 'is-reference';
import * as e from '../../errors.js';
import {
	extract_identifiers,
	get_attribute_expression,
	is_expression_attribute
} from '../../utils/ast.js';
import * as w from '../../warnings.js';
import { Runes } from '../constants.js';
import { regex_not_whitespace } from '../patterns.js';
import { get_rune } from '../scope.js';
import { merge } from '../visitors.js';
import { a11y_validators } from './visitors/shared/a11y.js';
import { is_tag_valid_with_parent } from '../../../html-tree-validation.js';
import { AssignmentExpression } from './visitors/AssignmentExpression.js';
import { AwaitBlock } from './visitors/AwaitBlock.js';
import { BindDirective } from './visitors/BindDirective.js';
import { CallExpression } from './visitors/CallExpression.js';
import { ConstTag } from './visitors/ConstTag.js';
import { EachBlock } from './visitors/EachBlock.js';
import { ExportDefaultDeclaration } from './visitors/ExportDefaultDeclaration.js';
import { ExportNamedDeclaration } from './visitors/ExportNamedDeclaration.js';
import { ExpressionStatement } from './visitors/ExpressionStatement.js';
import { IfBlock } from './visitors/IfBlock.js';
import { ImportDeclaration } from './visitors/ImportDeclaration.js';
import { KeyBlock } from './visitors/KeyBlock.js';
import { LabeledStatement } from './visitors/LabeledStatement.js';
import { LetDirective } from './visitors/LetDirective.js';
import { MemberExpression } from './visitors/MemberExpression.js';
import { RegularElement } from './visitors/RegularElement.js';
import { RenderTag } from './visitors/RenderTag.js';
import { SlotElement } from './visitors/SlotElement.js';
import { SnippetBlock } from './visitors/SnippetBlock.js';
import { StyleDirective } from './visitors/StyleDirective.js';
import { SvelteElement } from './visitors/SvelteElement.js';
import { SvelteFragment } from './visitors/SvelteFragment.js';
import { SvelteHead } from './visitors/SvelteHead.js';
import { UpdateExpression } from './visitors/UpdateExpression.js';
import { validate_assignment, validate_opening_tag } from './visitors/shared/utils.js';
import {
	validate_attribute,
	validate_attribute_name,
	validate_slot_attribute
} from './visitors/shared/attribute.js';
 
/**
 * @param {Component | SvelteComponent | SvelteSelf} node
 * @param {Context} context
 */
function validate_component(node, context) {
	for (const attribute of node.attributes) {
		if (
			attribute.type !== 'Attribute' &&
			attribute.type !== 'SpreadAttribute' &&
			attribute.type !== 'LetDirective' &&
			attribute.type !== 'OnDirective' &&
			attribute.type !== 'BindDirective'
		) {
			e.component_invalid_directive(attribute);
		}
 
		if (
			attribute.type === 'OnDirective' &&
			(attribute.modifiers.length > 1 || attribute.modifiers.some((m) => m !== 'once'))
		) {
			e.event_handler_invalid_component_modifier(attribute);
		}
 
		if (attribute.type === 'Attribute') {
			if (context.state.analysis.runes) {
				validate_attribute(attribute, node);
 
				if (is_expression_attribute(attribute)) {
					const expression = get_attribute_expression(attribute);
					if (expression.type === 'SequenceExpression') {
						let i = /** @type {number} */ (expression.start);
						while (--i > 0) {
							const char = context.state.analysis.source[i];
							if (char === '(') break; // parenthesized sequence expressions are ok
							if (char === '{') e.attribute_invalid_sequence_expression(expression);
						}
					}
				}
			}
 
			validate_attribute_name(attribute);
 
			if (attribute.name === 'slot') {
				validate_slot_attribute(context, attribute, true);
			}
		}
	}
 
	context.next({
		...context.state,
		parent_element: null,
		component_slots: new Set()
	});
}
 
/**
 * @type {Visitors}
 */
const validation = {
	ExpressionStatement,
	MemberExpression,
	AssignmentExpression,
	BindDirective,
	ExportDefaultDeclaration,
	ConstTag,
	ImportDeclaration,
	LetDirective,
	RegularElement,
	RenderTag,
	IfBlock,
	EachBlock,
	AwaitBlock,
	KeyBlock,
	SnippetBlock,
	StyleDirective,
	SvelteHead,
	SvelteElement,
	SvelteFragment,
	SlotElement,
	Component: validate_component,
	SvelteComponent: validate_component,
	SvelteSelf: validate_component,
	Text(node, context) {
		if (!node.parent) return;
		if (context.state.parent_element && regex_not_whitespace.test(node.data)) {
			if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
				e.node_invalid_placement(node, 'Text node', context.state.parent_element);
			}
		}
	},
	TitleElement(node) {
		const attribute = node.attributes[0];
		if (attribute) {
			e.title_illegal_attribute(attribute);
		}
 
		const child = node.fragment.nodes.find((n) => n.type !== 'Text' && n.type !== 'ExpressionTag');
		if (child) {
			e.title_invalid_content(child);
		}
	},
	UpdateExpression,
	ExpressionTag(node, context) {
		if (!node.parent) return;
		if (context.state.parent_element) {
			if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
				e.node_invalid_placement(node, '`{expression}`', context.state.parent_element);
			}
		}
	}
};
 
export const validation_legacy = merge(validation, a11y_validators, {
	VariableDeclarator(node, { state }) {
		ensure_no_module_import_conflict(node, state);
 
		if (node.init?.type !== 'CallExpression') return;
 
		const callee = node.init.callee;
		if (
			callee.type !== 'Identifier' ||
			(callee.name !== '$state' && callee.name !== '$derived' && callee.name !== '$props')
		) {
			return;
		}
 
		if (state.scope.get(callee.name)?.kind !== 'store_sub') {
			e.rune_invalid_usage(node.init, callee.name);
		}
	},
	AssignmentExpression(node, { state, path }) {
		const parent = path.at(-1);
		if (parent && parent.type === 'ConstTag') return;
		validate_assignment(node, node.left, state);
	},
	LabeledStatement,
	UpdateExpression(node, { state }) {
		validate_assignment(node, node.argument, state);
	}
});
 
/**
 * @param {VariableDeclarator} node
 * @param {AnalysisState} state
 */
function ensure_no_module_import_conflict(node, state) {
	const ids = extract_identifiers(node.id);
	for (const id of ids) {
		if (
			state.scope === state.analysis.instance.scope &&
			state.analysis.module.scope.get(id.name)?.declaration_kind === 'import'
		) {
			e.declaration_duplicate_module_import(node.id);
		}
	}
}
 
/**
 * @type {Visitors}
 */
export const validation_runes_js = {
	ImportDeclaration,
	ExportNamedDeclaration,
	CallExpression,
	VariableDeclarator(node, { state }) {
		const init = node.init;
		const rune = get_rune(init, state.scope);
 
		if (rune === null) return;
 
		const args = /** @type {import('estree').CallExpression} */ (init).arguments;
 
		if ((rune === '$derived' || rune === '$derived.by') && args.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		} else if (rune === '$state' && args.length > 1) {
			e.rune_invalid_arguments_length(node, rune, 'zero or one arguments');
		} else if (rune === '$props') {
			e.props_invalid_placement(node);
		} else if (rune === '$bindable') {
			e.bindable_invalid_location(node);
		}
	},
	AssignmentExpression(node, { state }) {
		validate_assignment(node, node.left, state);
	},
	UpdateExpression(node, { state }) {
		validate_assignment(node, node.argument, state);
	},
	ClassBody(node, context) {
		/** @type {string[]} */
		const private_derived_state = [];
 
		for (const definition of node.body) {
			if (
				definition.type === 'PropertyDefinition' &&
				definition.key.type === 'PrivateIdentifier' &&
				definition.value?.type === 'CallExpression'
			) {
				const rune = get_rune(definition.value, context.state.scope);
				if (rune === '$derived' || rune === '$derived.by') {
					private_derived_state.push(definition.key.name);
				}
			}
		}
 
		context.next({
			...context.state,
			private_derived_state
		});
	},
	ClassDeclaration(node, context) {
		// In modules, we allow top-level module scope only, in components, we allow the component scope,
		// which is function_depth of 1. With the exception of `new class` which is also not allowed at
		// component scope level either.
		const allowed_depth = context.state.ast_type === 'module' ? 0 : 1;
 
		if (context.state.scope.function_depth > allowed_depth) {
			w.perf_avoid_nested_class(node);
		}
	},
	NewExpression(node, context) {
		if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
			w.perf_avoid_inline_class(node);
		}
	},
	Identifier(node, { path, state }) {
		let i = path.length;
		let parent = /** @type {Expression} */ (path[--i]);
 
		if (
			Runes.includes(/** @type {Runes[number]} */ (node.name)) &&
			is_reference(node, parent) &&
			state.scope.get(node.name) === null &&
			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} */ (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);
			}
		}
	}
};
 
export const validation_runes = merge(validation, a11y_validators, {
	ImportDeclaration,
	LabeledStatement,
	ExportNamedDeclaration,
	CallExpression,
	EachBlock,
	IfBlock(node, { state, path }) {
		const parent = path.at(-1);
		const expected =
			path.at(-2)?.type === 'IfBlock' && parent?.type === 'Fragment' && parent.nodes.length === 1
				? ':'
				: '#';
		validate_opening_tag(node, state, expected);
	},
	AwaitBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
 
		if (node.value) {
			const start = /** @type {number} */ (node.value.start);
			const match = state.analysis.source.substring(start - 10, start).match(/{(\s*):then\s+$/);
			if (match && match[1] !== '') {
				e.block_unexpected_character({ start: start - 10, end: start }, ':');
			}
		}
 
		if (node.error) {
			const start = /** @type {number} */ (node.error.start);
			const match = state.analysis.source.substring(start - 10, start).match(/{(\s*):catch\s+$/);
			if (match && match[1] !== '') {
				e.block_unexpected_character({ start: start - 10, end: start }, ':');
			}
		}
	},
	KeyBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
	},
	SnippetBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
	},
	ConstTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	HtmlTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	DebugTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	RenderTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	VariableDeclarator(node, { state }) {
		ensure_no_module_import_conflict(node, state);
 
		const init = node.init;
		const rune = get_rune(init, state.scope);
 
		if (rune === null) return;
 
		const args = /** @type {import('estree').CallExpression} */ (init).arguments;
 
		// TODO some of this is duplicated with above, seems off
		if ((rune === '$derived' || rune === '$derived.by') && args.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		} else if (rune === '$state' && args.length > 1) {
			e.rune_invalid_arguments_length(node, rune, 'zero or one arguments');
		} else if (rune === '$props') {
			if (state.has_props_rune) {
				e.props_duplicate(node);
			}
 
			state.has_props_rune = true;
 
			if (args.length > 0) {
				e.rune_invalid_arguments(node, rune);
			}
 
			if (node.id.type !== 'ObjectPattern' && node.id.type !== 'Identifier') {
				e.props_invalid_identifier(node);
			}
 
			if (state.scope !== state.analysis.instance.scope) {
				e.props_invalid_placement(node);
			}
 
			if (node.id.type === 'ObjectPattern') {
				for (const property of node.id.properties) {
					if (property.type === 'Property') {
						if (property.computed) {
							e.props_invalid_pattern(property);
						}
 
						if (property.key.type === 'Identifier' && property.key.name.startsWith('$$')) {
							e.props_illegal_name(property);
						}
 
						const value =
							property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
 
						if (value.type !== 'Identifier') {
							e.props_invalid_pattern(property);
						}
					}
				}
			}
		}
 
		if (rune === '$derived') {
			const arg = args[0];
			if (
				arg.type === 'CallExpression' &&
				(arg.callee.type === 'ArrowFunctionExpression' || arg.callee.type === 'FunctionExpression')
			) {
				w.derived_iife(node);
			}
		}
	},
	SlotElement(node, { state }) {
		if (!state.analysis.custom_element) {
			w.slot_element_deprecated(node);
		}
	},
	OnDirective(node, { path }) {
		const parent_type = path.at(-1)?.type;
		// Don't warn on component events; these might not be under the author's control so the warning would be unactionable
		if (parent_type === 'RegularElement' || parent_type === 'SvelteElement') {
			w.event_directive_deprecated(node, node.name);
		}
	},
	// TODO this is a code smell. need to refactor this stuff
	ClassBody: validation_runes_js.ClassBody,
	ClassDeclaration: validation_runes_js.ClassDeclaration,
	Identifier: validation_runes_js.Identifier,
	NewExpression: validation_runes_js.NewExpression
});