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

100% Statements 41/41
100% Branches 10/10
100% Functions 1/1
100% Lines 39/39

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 402x 2x 2x 2x 2x 2x 2x 2x 2x 2x 77x 1x 1x 76x 76x 76x 15x 15x 15x 15x 15x 4x 4x 15x 15x 15x 15x 15x 77x 61x 61x 61x 83x 55x 55x 55x 55x 61x 77x  
/** @import { StyleDirective } from '#compiler' */
/** @import { Context } from '../types' */
import * as e from '../../../errors.js';
import { get_attribute_chunks } from '../../../utils/ast.js';
 
/**
 * @param {StyleDirective} node
 * @param {Context} context
 */
export function StyleDirective(node, context) {
	if (node.modifiers.length > 1 || (node.modifiers.length && node.modifiers[0] !== 'important')) {
		e.style_directive_invalid_modifier(node);
	}
 
	// the case for node.value different from true is already covered by the Identifier visitor
	if (node.value === true) {
		// get the binding for node.name and change the binding to state
		let binding = context.state.scope.get(node.name);
 
		if (binding) {
			if (!context.state.analysis.runes && binding.mutated) {
				binding.kind = 'state';
			}
 
			if (binding.kind !== 'normal') {
				node.metadata.expression.has_state = true;
			}
		}
	} else {
		context.next();
 
		for (const chunk of get_attribute_chunks(node.value)) {
			if (chunk.type !== 'ExpressionTag') continue;
 
			node.metadata.expression.has_state ||= chunk.metadata.expression.has_state;
			node.metadata.expression.has_call ||= chunk.metadata.expression.has_call;
		}
	}
}