All files / src/compiler legacy.js

98.69% Statements 604/612
92.96% Branches 119/128
100% Functions 37/37
98.68% Lines 601/609

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 6102x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1889x 1889x 1889x 1889x 1889x 1816x 1719x 1814x 97x 97x 1816x 1889x 1815x 1725x 1813x 90x 90x 1815x 1889x 2x 2x 2x 2x 2x 2x 2x 2x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 61271x 61271x 61271x 61271x 61271x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 27x 27x 2x 2x 27x 27x 27x 27x 27x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4578x 4578x 4578x 4578x 4578x 4578x 4578x 4578x 4578x 4685x 4685x 3812x 3812x 3812x 3812x 3812x 4685x 4685x 59x 59x 59x 59x 59x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 18x 4685x 4685x 4685x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 92x 92x 92x 92x 92x 92x 92x 172x 172x 144x 144x 144x 144x 144x 144x 144x 144x 144x 172x 172x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 4685x 4685x 888x 4685x 4685x 51x 4685x 4685x 228x 228x 228x 228x 4685x 4685x 1295x 1295x 1295x 2188x 893x 893x 2188x 2188x 2188x 1295x 1295x 1295x 1295x 1295x 1295x 1295x 4685x 4685x 1152x 1152x 1152x 1152x 1152x 1152x 1152x 1275x 1152x 1152x 1152x 1668x 1152x 1152x 4685x 4685x 4685x 253x 253x 253x 253x     253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 4685x 4685x 4685x 30x 30x 30x 30x 30x 30x 30x 30x 29x 30x 30x 4685x 4685x 4685x 927x 927x 927x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 927x 927x 927x 927x 927x 927x 927x 927x 927x 927x 927x 927x 927x 927x 4685x 4685x 6725x 6725x 1918x 538x 538x 538x 538x 538x 538x 538x 1918x 6187x 6187x 6187x 6187x 6187x 6187x 6187x 4685x 4685x 100x 4685x 4685x 4685x 624x 624x 174x 174x 53x 53x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 194x 174x 174x 174x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 802x 624x 624x 624x 624x 4685x 4685x 1020x 4685x 4685x 4685x 81x 81x 81x 81x 81x 81x 81x 81x 81x 4685x 4685x 8352x 8352x 8352x 8352x 8352x 8352x 8352x 8352x 4685x 4685x 467x 467x 467x 467x 467x 467x 467x 335x 467x 467x 467x 190x 467x 467x 4685x 4685x 260x 4685x 4685x 370x 370x 370x 370x 370x 4685x 4685x 8x 8x 8x 8x 8x 8x 8x 10x 8x 8x 8x   8x 8x 4685x 4685x 86x 86x 86x 86x 86x 86x 86x 86x 85x 86x 86x 86x 28x 86x 86x 4685x 4685x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 4685x 4685x 139x 139x 139x 46x 46x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 4685x 4685x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 269x 63x 63x 4685x 4685x 52x 52x 52x 52x 52x 52x 52x   52x 52x 52x 224x 52x 52x 4685x 4685x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 4685x 4685x 18x 18x 18x 18x 18x 18x 18x 33x 18x 18x 18x   18x 18x 4685x 4685x 27x 27x 27x 27x 27x 27x 27x 35x 27x 27x 27x   27x 27x 4685x 4685x 21150x 21150x 5x 5x 5x 5x 5x 5x 5x 5x 4685x 4685x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 35x 31x 31x 4685x 4685x 272x 4685x 4685x 94x 4685x 4685x 209x 209x 4685x 4685x 4685x  
import { walk } from 'zimmerframe';
import {
	regex_ends_with_whitespaces,
	regex_not_whitespace,
	regex_starts_with_whitespaces
} from './phases/patterns.js';
import { extract_svelte_ignore } from './utils/extract_svelte_ignore.js';
 
/**
 * Some of the legacy Svelte AST nodes remove whitespace from the start and end of their children.
 * @param {import('./types/template.js').TemplateNode[]} nodes
 */
function remove_surrounding_whitespace_nodes(nodes) {
	const first = nodes.at(0);
	const last = nodes.at(-1);
 
	if (first?.type === 'Text') {
		if (!regex_not_whitespace.test(first.data)) {
			nodes.shift();
		} else {
			first.data = first.data.replace(regex_starts_with_whitespaces, '');
		}
	}
	if (last?.type === 'Text') {
		if (!regex_not_whitespace.test(last.data)) {
			nodes.pop();
		} else {
			last.data = last.data.replace(regex_ends_with_whitespaces, '');
		}
	}
}
 
/**
 * Transform our nice modern AST into the monstrosity emitted by Svelte 4
 * @param {string} source
 * @param {import('#compiler').Root} ast
 * @returns {import('./types/legacy-nodes.js').LegacyRoot}
 */
export function convert(source, ast) {
	const root =
		/** @type {import('./types/template.js').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode} */ (
			ast
		);
 
	return /** @type {import('./types/legacy-nodes.js').LegacyRoot} */ (
		walk(root, null, {
			_(node, { next }) {
				// @ts-ignore
				delete node.parent;
				// @ts-ignore
				delete node.metadata;
				next();
			},
			// @ts-ignore
			Root(node, { visit }) {
				const { instance, module, options } = node;
 
				// Insert svelte:options back into the root nodes
				if (/** @type {any} */ (options)?.__raw__) {
					let idx = node.fragment.nodes.findIndex((node) => options.end <= node.start);
					if (idx === -1) {
						idx = node.fragment.nodes.length;
					}
 
					// @ts-ignore
					delete options.__raw__.parent;
					node.fragment.nodes.splice(idx, 0, /** @type {any} */ (options).__raw__);
				}
 
				/** @type {number | null} */
				let start = null;
 
				/** @type {number | null} */
				let end = null;
 
				if (node.fragment.nodes.length > 0) {
					const first = /** @type {import('#compiler').BaseNode} */ (node.fragment.nodes.at(0));
					const last = /** @type {import('#compiler').BaseNode} */ (node.fragment.nodes.at(-1));
 
					start = first.start;
					end = last.end;
 
					while (/\s/.test(source[start])) start += 1;
					while (/\s/.test(source[end - 1])) end -= 1;
				}
 
				if (instance) {
					// @ts-ignore
					delete instance.parent;
					// @ts-ignore
					delete instance.attributes;
				}
 
				if (module) {
					// @ts-ignore
					delete module.parent;
					// @ts-ignore
					delete module.attributes;
				}
 
				return {
					html: {
						type: 'Fragment',
						start,
						end,
						children: node.fragment.nodes.map((child) => visit(child))
					},
					instance,
					module,
					css: ast.css ? visit(ast.css) : undefined
				};
			},
			AnimateDirective(node) {
				return { ...node, type: 'Animation' };
			},
			// @ts-ignore
			AwaitBlock(node, { visit }) {
				let pendingblock = {
					type: 'PendingBlock',
					/** @type {number | null} */
					start: null,
					/** @type {number | null} */
					end: null,
					children: node.pending?.nodes.map((child) => visit(child)) ?? [],
					skip: true
				};
 
				let thenblock = {
					type: 'ThenBlock',
					/** @type {number | null} */
					start: null,
					/** @type {number | null} */
					end: null,
					children: node.then?.nodes.map((child) => visit(child)) ?? [],
					skip: true
				};
 
				let catchblock = {
					type: 'CatchBlock',
					/** @type {number | null} */
					start: null,
					/** @type {number | null} */
					end: null,
					children: node.catch?.nodes.map((child) => visit(child)) ?? [],
					skip: true
				};
 
				if (node.pending) {
					const first = node.pending.nodes.at(0);
					const last = node.pending.nodes.at(-1);
 
					pendingblock.start = first?.start ?? source.indexOf('}', node.expression.end) + 1;
					pendingblock.end = last?.end ?? pendingblock.start;
					pendingblock.skip = false;
				}
 
				if (node.then) {
					const first = node.then.nodes.at(0);
					const last = node.then.nodes.at(-1);
 
					thenblock.start =
						pendingblock.end ?? first?.start ?? source.indexOf('}', node.expression.end) + 1;
					thenblock.end =
						last?.end ?? source.lastIndexOf('}', pendingblock.end ?? node.expression.end) + 1;
					thenblock.skip = false;
				}
 
				if (node.catch) {
					const first = node.catch.nodes.at(0);
					const last = node.catch.nodes.at(-1);
 
					catchblock.start =
						thenblock.end ??
						pendingblock.end ??
						first?.start ??
						source.indexOf('}', node.expression.end) + 1;
					catchblock.end =
						last?.end ??
						source.lastIndexOf('}', thenblock.end ?? pendingblock.end ?? node.expression.end) + 1;
					catchblock.skip = false;
				}
 
				return {
					type: 'AwaitBlock',
					start: node.start,
					end: node.end,
					expression: node.expression,
					value: node.value,
					error: node.error,
					pending: pendingblock,
					then: thenblock,
					catch: catchblock
				};
			},
			BindDirective(node) {
				return { ...node, type: 'Binding' };
			},
			ClassDirective(node) {
				return { ...node, type: 'Class' };
			},
			Comment(node) {
				return {
					...node,
					ignores: extract_svelte_ignore(node.start, node.data, false)
				};
			},
			ComplexSelector(node) {
				const children = [];
 
				for (const child of node.children) {
					if (child.combinator) {
						children.push(child.combinator);
					}
 
					children.push(...child.selectors);
				}
 
				return {
					type: 'Selector',
					start: node.start,
					end: node.end,
					children
				};
			},
			Component(node, { visit }) {
				return {
					type: 'InlineComponent',
					start: node.start,
					end: node.end,
					name: node.name,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			// @ts-ignore
			ConstTag(node) {
				if (
					/** @type {import('./types/legacy-nodes.js').LegacyConstTag} */ (node).expression !==
					undefined
				) {
					return node;
				}
 
				const modern_node = /** @type {import('#compiler').ConstTag} */ (node);
				const { id: left } = { ...modern_node.declaration.declarations[0] };
				// @ts-ignore
				delete left.typeAnnotation;
				return {
					type: 'ConstTag',
					start: modern_node.start,
					end: node.end,
					expression: {
						type: 'AssignmentExpression',
						start: (modern_node.declaration.start ?? 0) + 'const '.length,
						end: modern_node.declaration.end ?? 0,
						operator: '=',
						left,
						right: modern_node.declaration.declarations[0].init
					}
				};
			},
			// @ts-ignore
			KeyBlock(node, { visit }) {
				remove_surrounding_whitespace_nodes(node.fragment.nodes);
				return {
					type: 'KeyBlock',
					start: node.start,
					end: node.end,
					expression: node.expression,
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			// @ts-ignore
			EachBlock(node, { visit }) {
				let elseblock = undefined;
 
				if (node.fallback) {
					const first = node.fallback.nodes.at(0);
					const end = source.lastIndexOf('{', /** @type {number} */ (node.end) - 1);
					const start = first?.start ?? end;
 
					remove_surrounding_whitespace_nodes(node.fallback.nodes);
 
					elseblock = {
						type: 'ElseBlock',
						start,
						end,
						children: node.fallback.nodes.map((child) => visit(child))
					};
				}
 
				remove_surrounding_whitespace_nodes(node.body.nodes);
 
				return {
					type: 'EachBlock',
					start: node.start,
					end: node.end,
					children: node.body.nodes.map((child) => visit(child)),
					context: node.context,
					expression: node.expression,
					index: node.index,
					key: node.key,
					else: elseblock
				};
			},
			ExpressionTag(node, { path }) {
				const parent = path.at(-1);
				if (parent?.type === 'Attribute') {
					if (source[parent.start] === '{') {
						return {
							type: 'AttributeShorthand',
							start: node.start,
							end: node.end,
							expression: node.expression
						};
					}
				}
 
				return {
					type: 'MustacheTag',
					start: node.start,
					end: node.end,
					expression: node.expression
				};
			},
			HtmlTag(node) {
				return { ...node, type: 'RawMustacheTag' };
			},
			// @ts-ignore
			IfBlock(node, { visit }) {
				let elseblock = undefined;
				if (node.alternate) {
					let nodes = node.alternate.nodes;
					if (nodes.length === 1 && nodes[0].type === 'IfBlock' && nodes[0].elseif) {
						nodes = nodes[0].consequent.nodes;
					}
 
					const end = source.lastIndexOf('{', /** @type {number} */ (node.end) - 1);
					const start = nodes.at(0)?.start ?? end;
 
					remove_surrounding_whitespace_nodes(node.alternate.nodes);
 
					elseblock = {
						type: 'ElseBlock',
						start,
						end: end,
						children: node.alternate.nodes.map(
							(child) =>
								/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
						)
					};
				}
 
				remove_surrounding_whitespace_nodes(node.consequent.nodes);
 
				return {
					type: 'IfBlock',
					start: node.start,
					end: node.end,
					expression: node.test,
					children: node.consequent.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					),
					else: elseblock,
					elseif: node.elseif ? true : undefined
				};
			},
			OnDirective(node) {
				return { ...node, type: 'EventHandler' };
			},
			// @ts-expect-error
			SnippetBlock(node, { visit }) {
				remove_surrounding_whitespace_nodes(node.body.nodes);
				return {
					type: 'SnippetBlock',
					start: node.start,
					end: node.end,
					expression: node.expression,
					parameters: node.parameters,
					children: node.body.nodes.map((child) => visit(child))
				};
			},
			RegularElement(node, { visit }) {
				return {
					type: 'Element',
					start: node.start,
					end: node.end,
					name: node.name,
					attributes: node.attributes.map((child) => visit(child)),
					children: node.fragment.nodes.map((child) => visit(child))
				};
			},
			SlotElement(node, { visit }) {
				return {
					type: 'Slot',
					start: node.start,
					end: node.end,
					name: node.name,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SpreadAttribute(node) {
				return { ...node, type: 'Spread' };
			},
			StyleSheet(node, context) {
				return {
					...node,
					...context.next(),
					type: 'Style'
				};
			},
			SvelteBody(node, { visit }) {
				return {
					type: 'Body',
					name: 'svelte:body',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteComponent(node, { visit }) {
				return {
					type: 'InlineComponent',
					name: 'svelte:component',
					start: node.start,
					end: node.end,
					expression: node.expression,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteDocument(node, { visit }) {
				return {
					type: 'Document',
					name: 'svelte:document',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteElement(node, { visit }) {
				/** @type {import('estree').Expression | string} */
				let tag = node.tag;
				if (tag.type === 'Literal' && typeof tag.value === 'string') {
					tag = tag.value;
				}
 
				return {
					type: 'Element',
					name: 'svelte:element',
					start: node.start,
					end: node.end,
					tag,
					attributes: node.attributes.map((child) => visit(child)),
					children: node.fragment.nodes.map((child) => visit(child))
				};
			},
			SvelteFragment(node, { visit }) {
				return {
					type: 'SlotTemplate',
					name: 'svelte:fragment',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(a) => /** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(a))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteHead(node, { visit }) {
				return {
					type: 'Head',
					name: 'svelte:head',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteOptions(node, { visit }) {
				return {
					type: 'Options',
					name: 'svelte:options',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					)
				};
			},
			SvelteSelf(node, { visit }) {
				return {
					type: 'InlineComponent',
					name: 'svelte:self',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			SvelteWindow(node, { visit }) {
				return {
					type: 'Window',
					name: 'svelte:window',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			Text(node, { path }) {
				const parent = path.at(-1);
				if (parent?.type === 'RegularElement' && parent.name === 'style') {
					// these text nodes are missing `raw` for some dumb reason
					return /** @type {import('./types/template.js').Text} */ ({
						type: 'Text',
						start: node.start,
						end: node.end,
						data: node.data
					});
				}
			},
			TitleElement(node, { visit }) {
				return {
					type: 'Title',
					name: 'title',
					start: node.start,
					end: node.end,
					attributes: node.attributes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
					),
					children: node.fragment.nodes.map(
						(child) =>
							/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
					)
				};
			},
			TransitionDirective(node) {
				return { ...node, type: 'Transition' };
			},
			UseDirective(node) {
				return { ...node, type: 'Action' };
			},
			LetDirective(node) {
				return { ...node, type: 'Let' };
			}
		})
	);
}