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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1861x 1861x 1861x 1861x 1861x 1788x 1694x 1786x 94x 94x 1788x 1861x 1788x 1700x 1786x 88x 88x 1788x 1861x 2x 2x 2x 2x 2x 2x 2x 2x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 59785x 59785x 59785x 59785x 59785x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 26x 26x 2x 2x 26x 26x 26x 26x 26x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4453x 4453x 4453x 4453x 4453x 4453x 4453x 4453x 4453x 4552x 4552x 3705x 3705x 3705x 4552x 4552x 59x 59x 59x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 4552x 18x 4552x 4552x 4552x 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 4552x 4552x 876x 4552x 4552x 51x 4552x 4552x 1267x 1267x 1267x 2156x 889x 889x 2156x 2156x 2156x 1267x 1267x 1267x 1267x 1267x 1267x 1267x 4552x 4552x 1104x 1104x 1104x 1104x 1104x 1104x 1104x 1225x 1104x 1104x 1104x 1602x 1104x 1104x 4552x 4552x 4552x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 4552x 4552x 4552x 28x 28x 28x 28x 28x 28x 28x 28x 27x 28x 28x 4552x 4552x 4552x 920x 920x 920x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 920x 920x 920x 920x 920x 920x 920x 920x 920x 920x 920x 920x 920x 920x 4552x 4552x 6607x 6607x 1865x 536x 536x 536x 536x 536x 536x 536x 1865x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 4552x 4552x 96x 4552x 4552x 4552x 610x 610x 174x 174x 53x 53x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 194x 174x 174x 174x 610x 610x 610x 610x 610x 610x 610x 610x 610x 610x 788x 610x 610x 610x 610x 4552x 4552x 989x 4552x 4552x 4552x 76x 76x 76x 76x 76x 76x 76x 76x 76x 4552x 4552x 8195x 8195x 8195x 8195x 8195x 8195x 8195x 8195x 4552x 4552x 465x 465x 465x 465x 465x 465x 465x 335x 465x 465x 465x 190x 465x 465x 4552x 4552x 244x 4552x 4552x 362x 362x 362x 362x 362x 4552x 4552x 8x 8x 8x 8x 8x 8x 8x 10x 8x 8x 8x 8x 8x 4552x 4552x 86x 86x 86x 86x 86x 86x 86x 86x 85x 86x 86x 86x 28x 86x 86x 4552x 4552x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4552x 4552x 131x 131x 131x 38x 38x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 4552x 4552x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 269x 63x 63x 4552x 4552x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 224x 52x 52x 4552x 4552x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 4552x 4552x 18x 18x 18x 18x 18x 18x 18x 33x 18x 18x 18x 18x 18x 4552x 4552x 25x 25x 25x 25x 25x 25x 25x 33x 25x 25x 25x 25x 25x 4552x 4552x 20559x 20559x 5x 5x 5x 5x 5x 5x 5x 5x 4552x 4552x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 35x 31x 31x 4552x 4552x 272x 4552x 4552x 88x 4552x 4552x 209x 209x 4552x 4552x 4552x | import { walk } from 'zimmerframe'; import { regex_ends_with_whitespaces, regex_not_whitespace, regex_starts_with_whitespaces } from './phases/patterns.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; } if (module) { // @ts-ignore delete module.parent; } 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' }; }, ComplexSelector(node, { visit }) { 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' }; } }) ); } |