All files / src/compiler/phases/1-parse/state element.js

97.93% Statements 807/824
96.12% Branches 248/258
100% Functions 10/10
97.9% Lines 796/813

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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 8142x 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 66x 66x 66x 107x 107x 107x 107x     2x 2x 771x 771x 771x 771x 1617x 1617x 368x 368x 368x 1617x 2x 2x 1617x 769x 769x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25916x 25916x 25916x 25916x 25916x 287x 287x 287x 287x 287x 287x 287x 287x 287x 287x 287x 287x 287x 25629x 25629x 25629x 25629x 25629x 25916x 241x 64x 64x 7x 64x 2x 2x 2x 2x 2x 241x 177x 1x 1x 176x 177x 2x 2x 174x 174x 174x 241x 25621x 25621x 25916x 25916x 24829x 24829x 22978x 22978x 22912x 22912x 25916x 25916x 25916x 1x 1x 25620x 25620x 25620x 25620x 25914x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 22143x 25916x 3477x 3477x 3477x 3477x 3477x 3477x 3477x 3477x 3477x 3477x 25916x 25916x 25916x 25916x 25916x 9270x 1x 1x 9269x 9269x 9269x 9269x 9270x 6x 3x 1x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 9266x 9266x 9266x 9266x 9270x 2x 2x 9266x 9266x 25916x 4x 4x 4x 4x 4x 4x 4x 4x 16350x 16350x 16350x 16350x 16350x 16350x 25916x 25916x 25916x 25916x 25916x 25916x 9325x 7145x 3x 3x 7145x 6654x 6654x 7145x 9322x 9322x 9322x 9322x 16336x 25916x 96x 96x 96x 96x 96x     96x 96x 96x 96x 96x     96x 96x 96x 16336x 25916x 162x 162x 162x 162x 162x 1x 1x 161x 161x 161x 161x 161x 162x 1x 1x 160x 162x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 162x 155x 155x 162x 16334x 25916x 4799x 4799x 4799x 4799x 4799x 795x 795x 795x     795x 795x 5x 5x 795x 329x 329x 795x 4799x 4799x 4377x 4377x 4x 4x 4x 4x 4x 4374x 4377x 78x 77x 4377x 4296x 4295x 4295x 4799x 422x 422x 422x 422x 412x 412x 4784x 4784x 11535x 11535x 11535x 25916x 25916x 25916x 25916x 25916x 2177x 2177x 25916x 60x 60x 60x 60x 60x 60x 60x 60x 9358x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 9298x 9285x 9285x 9285x 25916x 2x 2x 2x 2x 25629x 25629x 25629x 25629x 22x 22x 22x 22x 22x 22x 25x 25x 25x 25x 25x 4x 25x 21x 21x 21x 25x 22x 22x 1x 1x 21x 21x 21x 25607x 25629x 25629x 25210x 25629x 25072x 25072x 25072x 25629x 24830x 25629x 1x 1x 1x 24829x 25629x     24829x 24829x 24829x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4949x 4949x 4949x 4949x 4949x 150x 150x 150x 150x 4337x 75x 75x 75x     75x 75x 75x 75x 75x 74x 74x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 150x 4475x     150x 150x 150x 2x 2x 2x 2x 2x 20723x 20723x 20723x 20723x 846x 846x 846x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 845x 563x 563x 563x 563x 1x 1x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 846x 19877x 19877x 20723x 8339x 8339x 8339x 8339x 8339x 8339x 20723x 20723x 20723x 20723x 20723x 7445x 7445x 7445x 20723x 1x 1x 8334x 20723x 2917x 2917x 2917x 2x 2x 2915x 2917x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 2827x 2917x 2917x 2917x 2917x 2917x 2917x 2059x 2059x 2059x 1x 2059x 2058x 2058x 2058x 2058x 2059x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2826x 2917x 303x 303x 303x 303x 2826x 2826x 2826x 2917x 1068x 2917x 206x 206x 206x 206x 206x 206x 206x 2826x 2826x 2826x 5417x 5417x 5417x 2x 2x 2x 2x 2x 2931x 2931x 2931x 2931x 2919x 2917x 2857x 2825x 2931x 14x 14x 2x 2x 2x 2x 2x 7445x 7445x 7445x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 7400x 7400x 7400x 7400x 7400x 7400x 7400x 32192x 32192x 6989x 7400x 7400x 7400x 7445x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 7397x 7445x 1x 1x 7396x 7445x 7396x 7445x 4013x 7443x 3383x 3383x 7445x 2x 2x 2x 2x 2x 2x 2x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 7460x 11490x 3542x 3542x 3542x 3542x 11490x 7460x 7460x 32899x 32899x 32899x 7455x 7455x 32899x 4039x 2x 2x 2x 2x 4039x 2x 2x 2x 2x 2x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 4035x 25444x 21405x 21405x 32899x        
/** @import { Expression } from 'estree' */
/** @import * as Compiler from '#compiler' */
/** @import { Parser } from '../index.js' */
import { is_void } from '../../../../utils.js';
import read_expression from '../read/expression.js';
import { read_script } from '../read/script.js';
import read_style from '../read/style.js';
import { decode_character_references } from '../utils/html.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { create_fragment } from '../utils/create.js';
import { create_attribute, create_expression_metadata } from '../../nodes.js';
import { get_attribute_expression, is_expression_attribute } from '../../../utils/ast.js';
import { closing_tag_omitted } from '../../../../html-tree-validation.js';
 
// eslint-disable-next-line no-useless-escape
const valid_tag_name = /^\!?[a-zA-Z]{1,}:?[a-zA-Z0-9\-]*/;
 
/** Invalid attribute characters if the attribute is not surrounded by quotes */
const regex_starts_with_invalid_attr_value = /^(\/>|[\s"'=<>`])/;
 
/** @type {Map<string, Compiler.Ast.ElementLike['type']>} */
const root_only_meta_tags = new Map([
	['svelte:head', 'SvelteHead'],
	['svelte:options', 'SvelteOptions'],
	['svelte:window', 'SvelteWindow'],
	['svelte:document', 'SvelteDocument'],
	['svelte:body', 'SvelteBody']
]);
 
/** @type {Map<string, Compiler.Ast.ElementLike['type']>} */
const meta_tags = new Map([
	...root_only_meta_tags,
	['svelte:element', 'SvelteElement'],
	['svelte:component', 'SvelteComponent'],
	['svelte:self', 'SvelteSelf'],
	['svelte:fragment', 'SvelteFragment']
]);
 
const valid_meta_tags = Array.from(meta_tags.keys());
 
const SELF = /^svelte:self(?=[\s/>])/;
const COMPONENT = /^svelte:component(?=[\s/>])/;
const SLOT = /^svelte:fragment(?=[\s/>])/;
const ELEMENT = /^svelte:element(?=[\s/>])/;
 
/** @param {Compiler.Ast.TemplateNode[]} stack */
function parent_is_head(stack) {
	let i = stack.length;
	while (i--) {
		const { type } = stack[i];
		if (type === 'SvelteHead') return true;
		if (type === 'RegularElement' || type === 'Component') return false;
	}
	return false;
}
 
/** @param {Compiler.Ast.TemplateNode[]} stack */
function parent_is_shadowroot_template(stack) {
	// https://developer.chrome.com/docs/css-ui/declarative-shadow-dom#building_a_declarative_shadow_root
	let i = stack.length;
	while (i--) {
		if (
			stack[i].type === 'RegularElement' &&
			/** @type {Compiler.Ast.RegularElement} */ (stack[i]).attributes.some(
				(a) => a.type === 'Attribute' && a.name === 'shadowrootmode'
			)
		) {
			return true;
		}
	}
	return false;
}
 
const regex_closing_textarea_tag = /^<\/textarea(\s[^>]*)?>/i;
const regex_closing_comment = /-->/;
const regex_component_name = /^(?:[A-Z]|[A-Za-z][A-Za-z0-9_$]*\.)/;
const regex_valid_component_name =
	/^(?:[A-Z][A-Za-z0-9_$.]*|[a-z][A-Za-z0-9_$]*\.[A-Za-z0-9_$])[A-Za-z0-9_$.]*$/;
 
/** @param {Parser} parser */
export default function element(parser) {
	const start = parser.index++;
 
	let parent = parser.current();
 
	if (parser.eat('!--')) {
		const data = parser.read_until(regex_closing_comment);
		parser.eat('-->', true);
 
		/** @type {ReturnType<typeof parser.append<Compiler.Ast.Comment>>} */
		parser.append({
			type: 'Comment',
			start,
			end: parser.index,
			data
		});
 
		return;
	}
 
	const is_closing_tag = parser.eat('/');
 
	const name = read_tag_name(parser);
 
	if (root_only_meta_tags.has(name)) {
		if (is_closing_tag) {
			if (
				['svelte:options', 'svelte:window', 'svelte:body', 'svelte:document'].includes(name) &&
				/** @type {Compiler.Ast.ElementLike} */ (parent).fragment.nodes.length
			) {
				e.svelte_meta_invalid_content(
					/** @type {Compiler.Ast.ElementLike} */ (parent).fragment.nodes[0].start,
					name
				);
			}
		} else {
			if (name in parser.meta_tags) {
				e.svelte_meta_duplicate(start, name);
			}
 
			if (parent.type !== 'Root') {
				e.svelte_meta_invalid_placement(start, name);
			}
 
			parser.meta_tags[name] = true;
		}
	}
 
	const type = meta_tags.has(name)
		? meta_tags.get(name)
		: regex_component_name.test(name)
			? 'Component'
			: name === 'title' && parent_is_head(parser.stack)
				? 'TitleElement'
				: // TODO Svelte 6/7: once slots are removed in favor of snippets, always keep slot as a regular element
					name === 'slot' && !parent_is_shadowroot_template(parser.stack)
					? 'SlotElement'
					: 'RegularElement';
 
	if (type === 'Component' && !regex_valid_component_name.test(name)) {
		e.component_invalid_name({ start: start + 1, end: start + name.length + 1 });
	}
 
	/** @type {Compiler.Ast.ElementLike} */
	const element =
		type === 'RegularElement'
			? {
					type,
					start,
					end: -1,
					name,
					attributes: [],
					fragment: create_fragment(true),
					metadata: {
						svg: false,
						mathml: false,
						scoped: false,
						has_spread: false
					},
					parent: null
				}
			: /** @type {Compiler.Ast.ElementLike} */ ({
					type,
					start,
					end: -1,
					name,
					attributes: [],
					fragment: create_fragment(true),
					parent: null,
					metadata: {
						// unpopulated at first, differs between types
					}
				});
 
	parser.allow_whitespace();
 
	if (is_closing_tag) {
		if (is_void(name)) {
			e.void_element_invalid_content(start);
		}
 
		parser.eat('>', true);
 
		// close any elements that don't have their own closing tags, e.g. <div><p></div>
		while (/** @type {Compiler.Ast.RegularElement} */ (parent).name !== name) {
			if (parent.type !== 'RegularElement') {
				if (parser.last_auto_closed_tag && parser.last_auto_closed_tag.tag === name) {
					e.element_invalid_closing_tag_autoclosed(start, name, parser.last_auto_closed_tag.reason);
				} else {
					e.element_invalid_closing_tag(start, name);
				}
			}
 
			parent.end = start;
			parser.pop();
 
			parent = parser.current();
		}
 
		parent.end = parser.index;
		parser.pop();
 
		if (parser.last_auto_closed_tag && parser.stack.length < parser.last_auto_closed_tag.depth) {
			parser.last_auto_closed_tag = undefined;
		}
 
		return;
	} else if (parent.type === 'RegularElement' && closing_tag_omitted(parent.name, name)) {
		parent.end = start;
		parser.pop();
		parser.last_auto_closed_tag = {
			tag: parent.name,
			reason: name,
			depth: parser.stack.length
		};
	}
 
	/** @type {string[]} */
	const unique_names = [];
 
	const current = parser.current();
	const is_top_level_script_or_style =
		(name === 'script' || name === 'style') && current.type === 'Root';
 
	const read = is_top_level_script_or_style ? read_static_attribute : read_attribute;
 
	let attribute;
	while ((attribute = read(parser))) {
		if (attribute.type === 'Attribute' || attribute.type === 'BindDirective') {
			if (unique_names.includes(attribute.name)) {
				e.attribute_duplicate(attribute);
				// <svelte:element bind:this this=..> is allowed
			} else if (attribute.name !== 'this') {
				unique_names.push(attribute.name);
			}
		}
 
		element.attributes.push(attribute);
		parser.allow_whitespace();
	}
 
	if (element.type === 'SvelteComponent') {
		const index = element.attributes.findIndex(
			/** @param {any} attr */
			(attr) => attr.type === 'Attribute' && attr.name === 'this'
		);
		if (index === -1) {
			e.svelte_component_missing_this(start);
		}
 
		const definition = /** @type {Compiler.Ast.Attribute} */ (
			element.attributes.splice(index, 1)[0]
		);
		if (!is_expression_attribute(definition)) {
			e.svelte_component_invalid_this(definition.start);
		}
 
		element.expression = get_attribute_expression(definition);
	}
 
	if (element.type === 'SvelteElement') {
		const index = element.attributes.findIndex(
			/** @param {any} attr */
			(attr) => attr.type === 'Attribute' && attr.name === 'this'
		);
		if (index === -1) {
			e.svelte_element_missing_this(start);
		}
 
		const definition = /** @type {Compiler.Ast.Attribute} */ (
			element.attributes.splice(index, 1)[0]
		);
 
		if (definition.value === true) {
			e.svelte_element_missing_this(definition);
		}
 
		if (!is_expression_attribute(definition)) {
			w.svelte_element_invalid_this(definition);
 
			// note that this is wrong, in the case of e.g. `this="h{n}"` — it will result in `<h>`.
			// it would be much better to just error here, but we are preserving the existing buggy
			// Svelte 4 behaviour out of an overabundance of caution regarding breaking changes.
			// TODO in 6.0, error
			const chunk = /** @type {Array<Compiler.Ast.ExpressionTag | Compiler.Ast.Text>} */ (
				definition.value
			)[0];
			element.tag =
				chunk.type === 'Text'
					? {
							type: 'Literal',
							value: chunk.data,
							raw: `'${chunk.raw}'`,
							start: chunk.start,
							end: chunk.end
						}
					: chunk.expression;
		} else {
			element.tag = get_attribute_expression(definition);
		}
	}
 
	if (is_top_level_script_or_style) {
		parser.eat('>', true);
 
		/** @type {Compiler.Ast.Comment | null} */
		let prev_comment = null;
		for (let i = current.fragment.nodes.length - 1; i >= 0; i--) {
			const node = current.fragment.nodes[i];
 
			if (i === current.fragment.nodes.length - 1 && node.end !== start) {
				break;
			}
 
			if (node.type === 'Comment') {
				prev_comment = node;
				break;
			} else if (node.type !== 'Text' || node.data.trim()) {
				break;
			}
		}
 
		if (name === 'script') {
			const content = read_script(parser, start, element.attributes);
			if (prev_comment) {
				// We take advantage of the fact that the root will never have leadingComments set,
				// and set the previous comment to it so that the warning mechanism can later
				// inspect the root and see if there was a html comment before it silencing specific warnings.
				content.content.leadingComments = [{ type: 'Line', value: prev_comment.data }];
			}
 
			if (content.context === 'module') {
				if (current.module) e.script_duplicate(start);
				current.module = content;
			} else {
				if (current.instance) e.script_duplicate(start);
				current.instance = content;
			}
		} else {
			const content = read_style(parser, start, element.attributes);
			content.content.comment = prev_comment;
 
			if (current.css) e.style_duplicate(start);
			current.css = content;
		}
		return;
	}
 
	parser.append(element);
 
	const self_closing = parser.eat('/') || is_void(name);
 
	parser.eat('>', true);
 
	if (self_closing) {
		// don't push self-closing elements onto the stack
		element.end = parser.index;
	} else if (name === 'textarea') {
		// special case
		element.fragment.nodes = read_sequence(
			parser,
			() => regex_closing_textarea_tag.test(parser.template.slice(parser.index)),
			'inside <textarea>'
		);
		parser.read(regex_closing_textarea_tag);
		element.end = parser.index;
	} else if (name === 'script' || name === 'style') {
		// special case
		const start = parser.index;
		const data = parser.read_until(new RegExp(`</${name}>`));
		const end = parser.index;
 
		/** @type {Compiler.Ast.Text} */
		const node = {
			start,
			end,
			type: 'Text',
			data,
			raw: data,
			parent: null
		};
 
		element.fragment.nodes.push(node);
		parser.eat(`</${name}>`, true);
		element.end = parser.index;
	} else {
		parser.stack.push(element);
		parser.fragments.push(element.fragment);
	}
}
 
const regex_whitespace_or_slash_or_closing_tag = /(\s|\/|>)/;
 
/** @param {Parser} parser */
function read_tag_name(parser) {
	const start = parser.index;
 
	if (parser.read(SELF)) {
		// check we're inside a block, otherwise this
		// will cause infinite recursion
		let i = parser.stack.length;
		let legal = false;
 
		while (i--) {
			const fragment = parser.stack[i];
			if (
				fragment.type === 'IfBlock' ||
				fragment.type === 'EachBlock' ||
				fragment.type === 'Component' ||
				fragment.type === 'SnippetBlock'
			) {
				legal = true;
				break;
			}
		}
 
		if (!legal) {
			e.svelte_self_invalid_placement(start);
		}
 
		return 'svelte:self';
	}
 
	if (parser.read(COMPONENT)) return 'svelte:component';
	if (parser.read(ELEMENT)) return 'svelte:element';
 
	if (parser.read(SLOT)) return 'svelte:fragment';
 
	const name = parser.read_until(regex_whitespace_or_slash_or_closing_tag);
 
	if (meta_tags.has(name)) return name;
 
	if (name.startsWith('svelte:')) {
		const list = `${valid_meta_tags.slice(0, -1).join(', ')} or ${valid_meta_tags[valid_meta_tags.length - 1]}`;
		e.svelte_meta_invalid_tag(start, list);
	}
 
	if (!valid_tag_name.test(name)) {
		e.element_invalid_tag_name(start);
	}
 
	return name;
}
 
// eslint-disable-next-line no-useless-escape
const regex_token_ending_character = /[\s=\/>"']/;
const regex_starts_with_quote_characters = /^["']/;
const regex_attribute_value = /^(?:"([^"]*)"|'([^'])*'|([^>\s]+))/;
 
/**
 * @param {Parser} parser
 * @returns {Compiler.Ast.Attribute | null}
 */
function read_static_attribute(parser) {
	const start = parser.index;
 
	const name = parser.read_until(regex_token_ending_character);
	if (!name) return null;
 
	/** @type {true | Array<Compiler.Ast.Text | Compiler.Ast.ExpressionTag>} */
	let value = true;
 
	if (parser.eat('=')) {
		parser.allow_whitespace();
		let raw = parser.match_regex(regex_attribute_value);
		if (!raw) {
			e.expected_attribute_value(parser.index);
		}
 
		parser.index += raw.length;
 
		const quoted = raw[0] === '"' || raw[0] === "'";
		if (quoted) {
			raw = raw.slice(1, -1);
		}
 
		value = [
			{
				start: parser.index - raw.length - (quoted ? 1 : 0),
				end: quoted ? parser.index - 1 : parser.index,
				type: 'Text',
				raw: raw,
				data: decode_character_references(raw, true),
				parent: null
			}
		];
	}
 
	if (parser.match_regex(regex_starts_with_quote_characters)) {
		e.expected_token(parser.index, '=');
	}
 
	return create_attribute(name, start, parser.index, value);
}
 
/**
 * @param {Parser} parser
 * @returns {Compiler.Ast.Attribute | Compiler.Ast.SpreadAttribute | Compiler.Ast.Directive | null}
 */
function read_attribute(parser) {
	const start = parser.index;
 
	if (parser.eat('{')) {
		parser.allow_whitespace();
 
		if (parser.eat('...')) {
			const expression = read_expression(parser);
 
			parser.allow_whitespace();
			parser.eat('}', true);
 
			/** @type {Compiler.Ast.SpreadAttribute} */
			const spread = {
				type: 'SpreadAttribute',
				start,
				end: parser.index,
				expression,
				parent: null,
				metadata: {
					expression: create_expression_metadata()
				}
			};
 
			return spread;
		} else {
			const value_start = parser.index;
			const name = parser.read_identifier();
 
			if (name === null) {
				e.attribute_empty_shorthand(start);
			}
 
			parser.allow_whitespace();
			parser.eat('}', true);
 
			/** @type {Compiler.Ast.ExpressionTag} */
			const expression = {
				type: 'ExpressionTag',
				start: value_start,
				end: value_start + name.length,
				expression: {
					start: value_start,
					end: value_start + name.length,
					type: 'Identifier',
					name
				},
				parent: null,
				metadata: {
					expression: create_expression_metadata()
				}
			};
 
			return create_attribute(name, start, parser.index, expression);
		}
	}
 
	const name = parser.read_until(regex_token_ending_character);
	if (!name) return null;
 
	let end = parser.index;
 
	parser.allow_whitespace();
 
	const colon_index = name.indexOf(':');
	const type = colon_index !== -1 && get_directive_type(name.slice(0, colon_index));
 
	/** @type {true | Compiler.Ast.ExpressionTag | Array<Compiler.Ast.Text | Compiler.Ast.ExpressionTag>} */
	let value = true;
	if (parser.eat('=')) {
		parser.allow_whitespace();
		value = read_attribute_value(parser);
		end = parser.index;
	} else if (parser.match_regex(regex_starts_with_quote_characters)) {
		e.expected_token(parser.index, '=');
	}
 
	if (type) {
		const [directive_name, ...modifiers] = name.slice(colon_index + 1).split('|');
 
		if (directive_name === '') {
			e.directive_missing_name({ start, end: start + colon_index + 1 }, name);
		}
 
		if (type === 'StyleDirective') {
			return {
				start,
				end,
				type,
				name: directive_name,
				modifiers: /** @type {Array<'important'>} */ (modifiers),
				value,
				parent: null,
				metadata: {
					expression: create_expression_metadata()
				}
			};
		}
 
		const first_value = value === true ? undefined : Array.isArray(value) ? value[0] : value;
 
		/** @type {Expression | null} */
		let expression = null;
 
		if (first_value) {
			const attribute_contains_text =
				/** @type {any[]} */ (value).length > 1 || first_value.type === 'Text';
			if (attribute_contains_text) {
				e.directive_invalid_value(/** @type {number} */ (first_value.start));
			} else {
				// TODO throw a parser error in a future version here if this `[ExpressionTag]` instead of `ExpressionTag`,
				// which means stringified value, which isn't allowed for some directives?
				expression = first_value.expression;
			}
		}
 
		/** @type {Compiler.Ast.Directive} */
		// @ts-expect-error TODO can't figure out this error
		const directive = {
			start,
			end,
			type,
			name: directive_name,
			modifiers,
			expression,
			metadata: {
				expression: create_expression_metadata()
			}
		};
 
		if (directive.type === 'TransitionDirective') {
			const direction = name.slice(0, colon_index);
			directive.intro = direction === 'in' || direction === 'transition';
			directive.outro = direction === 'out' || direction === 'transition';
		}
 
		// Directive name is expression, e.g. <p class:isRed />
		if (
			(directive.type === 'BindDirective' || directive.type === 'ClassDirective') &&
			!directive.expression
		) {
			directive.expression = /** @type {any} */ ({
				start: start + colon_index + 1,
				end,
				type: 'Identifier',
				name: directive.name
			});
		}
 
		return directive;
	}
 
	return create_attribute(name, start, end, value);
}
 
/**
 * @param {string} name
 * @returns {any}
 */
function get_directive_type(name) {
	if (name === 'use') return 'UseDirective';
	if (name === 'animate') return 'AnimateDirective';
	if (name === 'bind') return 'BindDirective';
	if (name === 'class') return 'ClassDirective';
	if (name === 'style') return 'StyleDirective';
	if (name === 'on') return 'OnDirective';
	if (name === 'let') return 'LetDirective';
	if (name === 'in' || name === 'out' || name === 'transition') return 'TransitionDirective';
	return false;
}
 
/**
 * @param {Parser} parser
 * @return {Compiler.Ast.ExpressionTag | Array<Compiler.Ast.ExpressionTag | Compiler.Ast.Text>}
 */
function read_attribute_value(parser) {
	const quote_mark = parser.eat("'") ? "'" : parser.eat('"') ? '"' : null;
	if (quote_mark && parser.eat(quote_mark)) {
		return [
			{
				start: parser.index - 1,
				end: parser.index - 1,
				type: 'Text',
				raw: '',
				data: '',
				parent: null
			}
		];
	}
 
	/** @type {Array<Compiler.Ast.ExpressionTag | Compiler.Ast.Text>} */
	let value;
	try {
		value = read_sequence(
			parser,
			() => {
				// handle common case of quote marks existing outside of regex for performance reasons
				if (quote_mark) return parser.match(quote_mark);
				return !!parser.match_regex(regex_starts_with_invalid_attr_value);
			},
			'in attribute value'
		);
	} catch (/** @type {any} */ error) {
		if (error.code === 'js_parse_error') {
			// if the attribute value didn't close + self-closing tag
			// eg: `<Component test={{a:1} />`
			// acorn may throw a `Unterminated regular expression` because of `/>`
			const pos = error.position?.[0];
			if (pos !== undefined && parser.template.slice(pos - 1, pos + 1) === '/>') {
				parser.index = pos;
				e.expected_token(pos, quote_mark || '}');
			}
		}
		throw error;
	}
 
	if (value.length === 0 && !quote_mark) {
		e.expected_attribute_value(parser.index);
	}
 
	if (quote_mark) parser.index += 1;
 
	if (quote_mark || value.length > 1 || value[0].type === 'Text') {
		return value;
	} else {
		return value[0];
	}
}
 
/**
 * @param {Parser} parser
 * @param {() => boolean} done
 * @param {string} location
 * @returns {any[]}
 */
function read_sequence(parser, done, location) {
	/** @type {Compiler.Ast.Text} */
	let current_chunk = {
		start: parser.index,
		end: -1,
		type: 'Text',
		raw: '',
		data: '',
		parent: null
	};
 
	/** @type {Array<Compiler.Ast.Text | Compiler.Ast.ExpressionTag>} */
	const chunks = [];
 
	/** @param {number} end */
	function flush(end) {
		if (current_chunk.raw) {
			current_chunk.data = decode_character_references(current_chunk.raw, true);
			current_chunk.end = end;
			chunks.push(current_chunk);
		}
	}
 
	while (parser.index < parser.template.length) {
		const index = parser.index;
 
		if (done()) {
			flush(parser.index);
			return chunks;
		} else if (parser.eat('{')) {
			if (parser.match('#')) {
				const index = parser.index - 1;
				parser.eat('#');
				const name = parser.read_until(/[^a-z]/);
				e.block_invalid_placement(index, name, location);
			} else if (parser.match('@')) {
				const index = parser.index - 1;
				parser.eat('@');
				const name = parser.read_until(/[^a-z]/);
				e.tag_invalid_placement(index, name, location);
			}
 
			flush(parser.index - 1);
 
			parser.allow_whitespace();
			const expression = read_expression(parser);
			parser.allow_whitespace();
			parser.eat('}', true);
 
			/** @type {Compiler.Ast.ExpressionTag} */
			const chunk = {
				type: 'ExpressionTag',
				start: index,
				end: parser.index,
				expression,
				parent: null,
				metadata: {
					expression: create_expression_metadata()
				}
			};
 
			chunks.push(chunk);
 
			current_chunk = {
				start: parser.index,
				end: -1,
				type: 'Text',
				raw: '',
				data: '',
				parent: null
			};
		} else {
			current_chunk.raw += parser.template[parser.index++];
		}
	}

	e.unexpected_eof(parser.template.length);
}