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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2064x 2064x 2064x 2064x 2064x 1983x 1875x 1981x 108x 108x 1983x 2064x 1982x 1889x 1980x 93x 93x 1982x 2064x 2x 2x 2x 2x 2x 2x 2x 2x 5267x 5267x 5267x 5267x 5267x 68763x 68763x 68763x 68763x 68763x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 53x 53x 4x 4x 53x 53x 53x 53x 53x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5121x 5121x 5121x 5121x 5121x 5121x 5121x 5121x 5121x 5267x 5267x 4276x 4276x 4276x 4276x 4276x 5267x 5267x 75x 75x 75x 75x 75x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 5267x 20x 5267x 5267x 5267x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 104x 104x 104x 104x 104x 104x 104x 186x 186x 158x 158x 158x 158x 158x 158x 158x 158x 158x 186x 186x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 5267x 5267x 996x 5267x 5267x 60x 5267x 5267x 266x 266x 266x 266x 5267x 5267x 1490x 1490x 1490x 2455x 969x 969x 2455x 2455x 2455x 1490x 1490x 1490x 1490x 1490x 1490x 1490x 5267x 5267x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 1316x 5267x 5267x 5267x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 5267x 5267x 5267x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5267x 5267x 5267x 997x 997x 997x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 997x 997x 997x 997x 997x 997x 997x 997x 997x 997x 997x 997x 997x 997x 5267x 5267x 7576x 7576x 2370x 582x 582x 582x 582x 582x 582x 582x 2370x 6994x 6994x 6994x 6994x 6994x 6994x 6994x 5267x 5267x 118x 5267x 5267x 5267x 697x 697x 180x 180x 54x 54x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 180x 697x 697x 697x 1x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 697x 5267x 5267x 1066x 5267x 5267x 5267x 104x 104x 104x 104x 104x 104x 104x 104x 104x 5267x 5267x 9287x 9287x 9287x 9287x 9287x 9287x 9287x 9287x 5267x 5267x 487x 487x 487x 487x 487x 487x 487x 487x 487x 487x 487x 487x 5267x 5267x 6252x 1987x 1987x 1987x 1987x 1987x 1987x 1987x 1987x 6252x 4265x 4265x 5267x 5267x 93x 46x 46x 46x 46x 46x 46x 46x 46x 61x 47x 47x 5267x 5267x 296x 5267x 5267x 401x 401x 401x 401x 401x 5267x 5267x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 5267x 5267x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 5267x 5267x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 5267x 5267x 155x 155x 155x 155x 155x 55x 155x 2x 2x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 5267x 5267x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 5267x 5267x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 5267x 5267x 53x 53x 53x 53x 53x 53x 53x 53x 53x 5267x 5267x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 5267x 5267x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5267x 5267x 23549x 23549x 5x 5x 5x 5x 5x 5x 5x 5x 5267x 5267x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5267x 5267x 294x 5267x 5267x 124x 5267x 5267x 217x 217x 5267x 5267x 5267x | /** @import { Expression } from 'estree' */ /** @import { AST, SvelteNode, TemplateNode } from '#compiler' */ /** @import * as Legacy from './types/legacy-nodes.js' */ 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 {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 {AST.Root} ast * @returns {Legacy.LegacyRoot} */ export function convert(source, ast) { const root = /** @type {SvelteNode | Legacy.LegacySvelteNode} */ (ast); return /** @type {Legacy.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 {AST.BaseNode} */ (node.fragment.nodes.at(0)); const last = /** @type {AST.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 {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, // @ts-ignore ConstTag(node) { if (/** @type {Legacy.LegacyConstTag} */ (node).expression !== undefined) { return node; } const modern_node = /** @type {AST.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 {Legacy.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 {Legacy.LegacyElementLike} */ (visit(child)) ) }; } const start = node.elseif ? node.consequent.nodes[0]?.start ?? source.lastIndexOf('{', /** @type {number} */ (node.end) - 1) : node.start; remove_surrounding_whitespace_nodes(node.consequent.nodes); return { type: 'IfBlock', start, end: node.end, expression: node.test, children: node.consequent.nodes.map( (child) => /** @type {Legacy.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 {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, Attribute(node, { visit, next, path }) { if (node.value !== true && !Array.isArray(node.value)) { path.push(node); const value = /** @type {Legacy.LegacyAttribute['value']} */ ([visit(node.value)]); path.pop(); return { ...node, value }; } else { return next(); } }, StyleDirective(node, { visit, next, path }) { if (node.value !== true && !Array.isArray(node.value)) { path.push(node); const value = /** @type {Legacy.LegacyStyleDirective['value']} */ ([visit(node.value)]); path.pop(); return { ...node, value }; } else { return next(); } }, 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 {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.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 {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, SvelteDocument(node, { visit }) { return { type: 'Document', name: 'svelte:document', start: node.start, end: node.end, attributes: node.attributes.map( (child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, SvelteElement(node, { visit }) { /** @type {Expression | string} */ let tag = node.tag; if ( tag.type === 'Literal' && typeof tag.value === 'string' && source[/** @type {number} */ (node.tag.start) - 1] !== '{' ) { 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 {Legacy.LegacyAttributeLike} */ (visit(a)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, SvelteHead(node, { visit }) { return { type: 'Head', name: 'svelte:head', start: node.start, end: node.end, attributes: node.attributes.map( (child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, SvelteOptions(node, { visit }) { return { type: 'Options', name: 'svelte:options', start: node.start, end: node.end, attributes: node.attributes.map( (child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child)) ) }; }, SvelteSelf(node, { visit }) { return { type: 'InlineComponent', name: 'svelte:self', start: node.start, end: node.end, attributes: node.attributes.map( (child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, SvelteWindow(node, { visit }) { return { type: 'Window', name: 'svelte:window', start: node.start, end: node.end, attributes: node.attributes.map( (child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.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 {AST.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 {Legacy.LegacyAttributeLike} */ (visit(child)) ), children: node.fragment.nodes.map( (child) => /** @type {Legacy.LegacyElementLike} */ (visit(child)) ) }; }, TransitionDirective(node) { return { ...node, type: 'Transition' }; }, UseDirective(node) { return { ...node, type: 'Action' }; }, LetDirective(node) { return { ...node, type: 'Let' }; } }) ); } |