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

92.79% Statements 567/611
85.32% Branches 93/109
100% Functions 6/6
92.73% Lines 562/606

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 6072x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9306x 9306x 9306x 9306x 9306x 9306x 9306x 9306x 9306x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 4782x 2x 2x 1832x 1832x 1832x 1832x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 1247x 1800x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x                       946x 946x 946x 946x 946x 946x 946x 946x 946x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 946x 112x 112x 112x     112x 112x 112x 942x 946x 197x 197x 197x 197x 197x 197x 197x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 942x 301x 1627x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 64x 4x 64x 60x 60x 60x 60x 64x 64x 64x 180x 17x 2x 17x 15x 15x 15x 15x 17x 17x 17x 116x 99x 99x 99x 180x 180x 180x 180x 180x 180x 121x 363x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 91x 91x 91x 91x 91x 91x 91x 91x 91x     91x 91x 91x 91x 91x 91x 91x 91x 91x 48x 48x 48x 48x 8x 8x 8x 8x 8x 8x 8x 48x 48x 48x 48x 6x 6x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x       2x 2x 404x 404x 404x 404x 404x 404x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 166x 121x 121x 121x 121x 174x 174x 174x 230x 392x 54x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 176x 177x 170x 83x     83x 83x 75x 75x 75x 75x 75x 83x 83x 83x 83x 83x 83x 83x 87x 88x 84x     84x 84x 80x 80x 80x 80x 80x 84x 84x 84x 84x 84x 84x 84x 3x 3x 3x 6x 6x 6x 2x 2x 1813x 1813x 1813x 1813x 1813x 1813x 1813x 576x 576x 576x 576x 53x 53x 53x 53x 576x 576x 576x 1813x 1813x 941x 941x 1813x 30x 30x 1813x 175x 175x 1813x 91x 91x 1813x 1813x       1813x 1813x   1813x 1237x 1237x 1237x 1237x 1237x 1237x 2x 2x 475x 475x 475x 475x 475x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 374x 473x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       373x 389x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 2x 263x 1x 1x 1x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 371x 447x 110x 110x 110x 110x 110x 110x 110x 16x 110x     110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 475x  
import read_pattern from '../read/context.js';
import read_expression from '../read/expression.js';
import * as e from '../../../errors.js';
import { create_fragment } from '../utils/create.js';
import { walk } from 'zimmerframe';
 
const regex_whitespace_with_closing_curly_brace = /^\s*}/;
 
/** @param {import('../index.js').Parser} parser */
export default function mustache(parser) {
	const start = parser.index;
	parser.index += 1;
 
	parser.allow_whitespace();
 
	if (parser.eat('#')) return open(parser);
	if (parser.eat('/')) return close(parser);
	if (parser.eat(':')) return next(parser);
	if (parser.eat('@')) return special(parser);
 
	const expression = read_expression(parser);
 
	parser.allow_whitespace();
	parser.eat('}', true);
 
	/** @type {ReturnType<typeof parser.append<import('#compiler').ExpressionTag>>} */
	parser.append({
		type: 'ExpressionTag',
		start,
		end: parser.index,
		expression,
		metadata: {
			contains_call_expression: false,
			dynamic: false
		}
	});
}
 
/** @param {import('../index.js').Parser} parser */
function open(parser) {
	const start = parser.index - 2;
 
	if (parser.eat('if')) {
		parser.require_whitespace();
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
		const block = parser.append({
			type: 'IfBlock',
			elseif: false,
			start,
			end: -1,
			test: read_expression(parser),
			consequent: create_fragment(),
			alternate: null
		});
 
		parser.allow_whitespace();
		parser.eat('}', true);
 
		parser.stack.push(block);
		parser.fragments.push(block.consequent);
 
		return;
	}
 
	if (parser.eat('each')) {
		parser.require_whitespace();
 
		const template = parser.template;
		let end = parser.template.length;
 
		/** @type {import('estree').Expression | undefined} */
		let expression;
 
		// we have to do this loop because `{#each x as { y = z }}` fails to parse —
		// the `as { y = z }` is treated as an Expression but it's actually a Pattern.
		// the 'fix' is to backtrack and hide everything from the `as` onwards, until
		// we get a valid expression
		while (!expression) {
			try {
				expression = read_expression(parser);
			} catch (err) {
				end = /** @type {any} */ (err).position[0] - 2;

				while (end > start && parser.template.slice(end, end + 2) !== 'as') {
					end -= 1;
				}

				if (end <= start) throw err;

				// @ts-expect-error parser.template is meant to be readonly, this is a special case
				parser.template = template.slice(0, end);
			}
		}
 
		// @ts-expect-error
		parser.template = template;
 
		parser.allow_whitespace();
 
		// {#each} blocks must declare a context – {#each list as item}
		if (!parser.match('as')) {
			// this could be a TypeScript assertion that was erroneously eaten.
 
			if (expression.type === 'SequenceExpression') {
				expression = expression.expressions[0];
			}
 
			let assertion = null;
			let end = expression.end;
 
			expression = walk(expression, null, {
				// @ts-expect-error
				TSAsExpression(node, context) {
					if (node.end === /** @type {import('estree').Expression} */ (expression).end) {
						assertion = node;
						end = node.expression.end;
						return node.expression;
					}

					context.next();
				}
			});
 
			expression.end = end;
 
			if (assertion) {
				// we can't reset `parser.index` to `expression.expression.end` because
				// it will ignore any parentheses — we need to jump through this hoop
				let end = /** @type {any} */ (/** @type {any} */ (assertion).typeAnnotation).start - 2;
				while (parser.template.slice(end, end + 2) !== 'as') end -= 1;
 
				parser.index = end;
			}
		}
		parser.eat('as', true);
		parser.require_whitespace();
 
		const context = read_pattern(parser);
 
		parser.allow_whitespace();
 
		let index;
		let key;
 
		if (parser.eat(',')) {
			parser.allow_whitespace();
			index = parser.read_identifier();
			if (!index) {
				e.expected_identifier(parser.index);
			}
 
			parser.allow_whitespace();
		}
 
		if (parser.eat('(')) {
			parser.allow_whitespace();
 
			key = read_expression(parser);
			parser.allow_whitespace();
			parser.eat(')', true);
			parser.allow_whitespace();
		}
 
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').EachBlock>>} */
		const block = parser.append({
			type: 'EachBlock',
			start,
			end: -1,
			expression,
			body: create_fragment(),
			context,
			index,
			key,
			metadata: /** @type {any} */ (null) // filled in later
		});
 
		parser.stack.push(block);
		parser.fragments.push(block.body);
 
		return;
	}
 
	if (parser.eat('await')) {
		parser.require_whitespace();
		const expression = read_expression(parser);
		parser.allow_whitespace();
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').AwaitBlock>>} */
		const block = parser.append({
			type: 'AwaitBlock',
			start,
			end: -1,
			expression,
			value: null,
			error: null,
			pending: null,
			then: null,
			catch: null
		});
 
		if (parser.eat('then')) {
			if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) {
				parser.allow_whitespace();
			} else {
				parser.require_whitespace();
				block.value = read_pattern(parser);
				parser.allow_whitespace();
			}
 
			block.then = create_fragment();
			parser.fragments.push(block.then);
		} else if (parser.eat('catch')) {
			if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) {
				parser.allow_whitespace();
			} else {
				parser.require_whitespace();
				block.error = read_pattern(parser);
				parser.allow_whitespace();
			}
 
			block.catch = create_fragment();
			parser.fragments.push(block.catch);
		} else {
			block.pending = create_fragment();
			parser.fragments.push(block.pending);
		}
 
		parser.eat('}', true);
		parser.stack.push(block);
 
		return;
	}
 
	if (parser.eat('key')) {
		parser.require_whitespace();
 
		const expression = read_expression(parser);
		parser.allow_whitespace();
 
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').KeyBlock>>} */
		const block = parser.append({
			type: 'KeyBlock',
			start,
			end: -1,
			expression,
			fragment: create_fragment()
		});
 
		parser.stack.push(block);
		parser.fragments.push(block.fragment);
 
		return;
	}
 
	if (parser.eat('snippet')) {
		parser.require_whitespace();
 
		const name_start = parser.index;
		const name = parser.read_identifier();
		const name_end = parser.index;
 
		if (name === null) {
			e.expected_identifier(parser.index);
		}
 
		parser.eat('(', true);
 
		parser.allow_whitespace();
 
		/** @type {import('estree').Pattern[]} */
		const parameters = [];
 
		while (!parser.match(')')) {
			let pattern = read_pattern(parser, true);
 
			parser.allow_whitespace();
			if (parser.eat('=')) {
				parser.allow_whitespace();
				pattern = {
					type: 'AssignmentPattern',
					left: pattern,
					right: read_expression(parser)
				};
			}
 
			parameters.push(pattern);
 
			if (!parser.eat(',')) break;
			parser.allow_whitespace();
		}
 
		parser.eat(')', true);
 
		parser.allow_whitespace();
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
		const block = parser.append({
			type: 'SnippetBlock',
			start,
			end: -1,
			expression: {
				type: 'Identifier',
				start: name_start,
				end: name_end,
				name
			},
			parameters,
			body: create_fragment()
		});
 
		parser.stack.push(block);
		parser.fragments.push(block.body);
 
		return;
	}

	e.expected_block_type(parser.index);
}
 
/** @param {import('../index.js').Parser} parser */
function next(parser) {
	const start = parser.index - 1;
 
	const block = parser.current(); // TODO type should not be TemplateNode, that's much too broad
 
	if (block.type === 'IfBlock') {
		if (!parser.eat('else')) e.expected_token(start, '{:else} or {:else if}');
		if (parser.eat('if')) e.block_invalid_elseif(start);
 
		parser.allow_whitespace();
 
		parser.fragments.pop();
 
		block.alternate = create_fragment();
		parser.fragments.push(block.alternate);
 
		// :else if
		if (parser.eat('if')) {
			parser.require_whitespace();
 
			const expression = read_expression(parser);
 
			parser.allow_whitespace();
			parser.eat('}', true);
 
			/** @type {ReturnType<typeof parser.append<import('#compiler').IfBlock>>} */
			const child = parser.append({
				start: parser.index,
				end: -1,
				type: 'IfBlock',
				elseif: true,
				test: expression,
				consequent: create_fragment(),
				alternate: null
			});
 
			parser.stack.push(child);
			parser.fragments.pop();
			parser.fragments.push(child.consequent);
		} else {
			// :else
			parser.allow_whitespace();
			parser.eat('}', true);
		}
 
		return;
	}
 
	if (block.type === 'EachBlock') {
		if (!parser.eat('else')) e.expected_token(start, '{:else}');
 
		parser.allow_whitespace();
		parser.eat('}', true);
 
		block.fallback = create_fragment();
 
		parser.fragments.pop();
		parser.fragments.push(block.fallback);
 
		return;
	}
 
	if (block.type === 'AwaitBlock') {
		if (parser.eat('then')) {
			if (block.then) {
				e.block_duplicate_clause(start, '{:then}');
			}
 
			if (!parser.eat('}')) {
				parser.require_whitespace();
				block.value = read_pattern(parser);
				parser.allow_whitespace();
				parser.eat('}', true);
			}
 
			block.then = create_fragment();
			parser.fragments.pop();
			parser.fragments.push(block.then);
 
			return;
		}
 
		if (parser.eat('catch')) {
			if (block.catch) {
				e.block_duplicate_clause(start, '{:catch}');
			}
 
			if (!parser.eat('}')) {
				parser.require_whitespace();
				block.error = read_pattern(parser);
				parser.allow_whitespace();
				parser.eat('}', true);
			}
 
			block.catch = create_fragment();
			parser.fragments.pop();
			parser.fragments.push(block.catch);
 
			return;
		}
 
		e.expected_token(start, '{:then ...} or {:catch ...}');
	}
 
	e.block_invalid_continuation_placement(start);
}
 
/** @param {import('../index.js').Parser} parser */
function close(parser) {
	const start = parser.index - 1;
 
	let block = parser.current();
 
	switch (block.type) {
		case 'IfBlock':
			parser.eat('if', true);
			parser.allow_whitespace();
			parser.eat('}', true);
			while (block.elseif) {
				block.end = parser.index;
				parser.stack.pop();
				block = /** @type {import('#compiler').IfBlock} */ (parser.current());
			}
			block.end = parser.index;
			parser.pop();
			return;
 
		case 'EachBlock':
			parser.eat('each', true);
			break;
		case 'KeyBlock':
			parser.eat('key', true);
			break;
		case 'AwaitBlock':
			parser.eat('await', true);
			break;
		case 'SnippetBlock':
			parser.eat('snippet', true);
			break;
 
		case 'RegularElement':
			// TODO handle implicitly closed elements
			e.block_unexpected_close(start);
			break;
 
		default:
			e.block_unexpected_close(start);
	}
 
	parser.allow_whitespace();
	parser.eat('}', true);
	block.end = parser.index;
	parser.pop();
}
 
/** @param {import('../index.js').Parser} parser */
function special(parser) {
	let start = parser.index;
	while (parser.template[start] !== '{') start -= 1;
 
	if (parser.eat('html')) {
		// {@html content} tag
		parser.require_whitespace();
 
		const expression = read_expression(parser);
 
		parser.allow_whitespace();
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').HtmlTag>>} */
		parser.append({
			type: 'HtmlTag',
			start,
			end: parser.index,
			expression
		});
 
		return;
	}
 
	if (parser.eat('debug')) {
		/** @type {import('estree').Identifier[]} */
		let identifiers;
 
		// Implies {@debug} which indicates "debug all"
		if (parser.read(regex_whitespace_with_closing_curly_brace)) {
			identifiers = [];
		} else {
			const expression = read_expression(parser);
 
			identifiers =
				expression.type === 'SequenceExpression'
					? /** @type {import('estree').Identifier[]} */ (expression.expressions)
					: [/** @type {import('estree').Identifier} */ (expression)];
 
			identifiers.forEach(
				/** @param {any} node */ (node) => {
					if (node.type !== 'Identifier') {
						e.debug_tag_invalid_arguments(/** @type {number} */ (node.start));
					}
				}
			);
 
			parser.allow_whitespace();
			parser.eat('}', true);
		}

		/** @type {ReturnType<typeof parser.append<import('#compiler').DebugTag>>} */
		parser.append({
			type: 'DebugTag',
			start,
			end: parser.index,
			identifiers
		});

		return;
	}
 
	if (parser.eat('const')) {
		parser.require_whitespace();
 
		const id = read_pattern(parser);
		parser.allow_whitespace();
 
		parser.eat('=', true);
		parser.allow_whitespace();
 
		const expression_start = parser.index;
		const init = read_expression(parser);
		if (
			init.type === 'SequenceExpression' &&
			!parser.template.substring(expression_start, init.start).includes('(')
		) {
			// const a = (b, c) is allowed but a = b, c = d is not;
			e.const_tag_invalid_expression(init);
		}
		parser.allow_whitespace();
 
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').ConstTag>>} */
		parser.append({
			type: 'ConstTag',
			start,
			end: parser.index,
			declaration: {
				type: 'VariableDeclaration',
				kind: 'const',
				declarations: [{ type: 'VariableDeclarator', id, init }],
				start: start + 2, // start at const, not at @const
				end: parser.index - 1
			}
		});
	}
 
	if (parser.eat('render')) {
		// {@render foo(...)}
		parser.require_whitespace();
 
		const expression = read_expression(parser);
 
		if (
			expression.type !== 'CallExpression' &&
			(expression.type !== 'ChainExpression' || expression.expression.type !== 'CallExpression')
		) {
			e.render_tag_invalid_expression(expression);
		}
 
		parser.allow_whitespace();
		parser.eat('}', true);
 
		/** @type {ReturnType<typeof parser.append<import('#compiler').RenderTag>>} */
		parser.append({
			type: 'RenderTag',
			start,
			end: parser.index,
			expression: expression
		});
	}
}