Repository: sveltejs/svelte
File: packages/svelte/src/compiler/migrate/index.js (JavaScript)

Code before:
				const possible_derived = bindings.every((binding) =>
					binding.references.every((reference) => {
						const declaration_idx = reference.path.findIndex(
							(el) => el.type === 'VariableDeclaration'
						);
						const assignment_idx = reference.path.findIndex(
							(el) => el.type === 'AssignmentExpression'
						);
						const update_idx = reference.path.findIndex((el) => el.type === 'UpdateExpression');
						const labeled_idx = reference.path.findIndex(
							(el) => el.type === 'LabeledStatement' && el.label.name === '$'
						);

						if (assignment_idx !== -1 && labeled_idx !== -1) {
							if (assignment_in_labeled) return false;
							assignment_in_labeled = /** @type {AssignmentExpression} */ (
								reference.path[assignment_idx]
							);
							labeled_statement = /** @type {LabeledStatement} */ (reference.path[labeled_idx]);
						}

						return (
							update_idx === -1 &&
							(declaration_idx !== -1 ||
								(labeled_idx !== -1 && assignment_idx !== -1) ||
								(labeled_idx === -1 && assignment_idx === -1))
						);
					})
				);

				const labeled_has_single_assignment =
					labeled_statement?.body.type === 'BlockStatement' &&
					labeled_statement.body.body.length === 1;

				if (
					possible_derived &&
					assignment_in_labeled &&
					labeled_statement &&
					labeled_has_single_assignment
				) {
					state.str.appendRight(
						/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
						' = $derived('
					);
					visit(assignment_in_labeled.right);
					state.str.appendRight(
						/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
						state.str
							.snip(
								/** @type {number} */ (assignment_in_labeled.right.start),
								/** @type {number} */ (assignment_in_labeled.right.end)

Review comment (suggestion):
This might be a bit simpler if you're not searching for the index but rather the object directly. Not as much `-1`, and no `reference.path[idx]` above

Code after:
							(el) => el.type === 'LabeledStatement' && el.label.name === '$'
						);

						if (assignment && labeled) {
							if (assignment_in_labeled) return false;
							assignment_in_labeled = /** @type {AssignmentExpression} */ (assignment);
							labeled_statement = /** @type {LabeledStatement} */ (labeled);
						}

						return !update && (declaration || (labeled && assignment) || (!labeled && !assignment));
					})
				);

				const labeled_has_single_assignment =
					labeled_statement?.body.type === 'BlockStatement' &&
					labeled_statement.body.body.length === 1;

				const is_expression_assignment =
					labeled_statement?.body.type === 'ExpressionStatement' &&
					labeled_statement.body.expression.type === 'AssignmentExpression';

				let should_be_state = false;

				if (is_expression_assignment) {
					const body = /**@type {ExpressionStatement}*/ (labeled_statement?.body);
					const expression = /**@type {AssignmentExpression}*/ (body.expression);
					const [, ids] = extract_all_identifiers_from_expression(expression.right);
					if (ids.length === 0) {
						should_be_state = true;
						state.derived_labeled_statements.add(
							/** @type {LabeledStatement} */ (labeled_statement)
						);
					}
				}

				if (
					!should_be_state &&
					possible_derived &&
					assignment_in_labeled &&
					labeled_statement &&
					(labeled_has_single_assignment || is_expression_assignment)
				) {
					// Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
					state.str.appendRight(
						/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
						' = $derived('
					);
					visit(assignment_in_labeled.right);
					state.str.appendRight(
						/** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end),
						state.str