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 2x 2x 2x 2x 2011x 2011x 2011x 2011x 2011x 1930x 1832x 1928x 98x 98x 1930x 2011x 1929x 1840x 1927x 89x 89x 1929x 2011x 2x 2x 2x 2x 2x 2x 2x 2x 5102x 5102x 5102x 5102x 5102x 66041x 66041x 66041x 66041x 66041x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 42x 42x 2x 2x 42x 42x 42x 42x 42x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 4966x 4966x 4966x 4966x 4966x 4966x 4966x 4966x 4966x 5102x 5102x 4152x 4152x 4152x 4152x 4152x 5102x 5102x 70x 70x 70x 70x 70x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 5102x 20x 5102x 5102x 5102x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 102x 102x 102x 102x 102x 102x 102x 184x 184x 156x 156x 156x 156x 156x 156x 156x 156x 156x 184x 184x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 184x 5102x 5102x 981x 5102x 5102x 51x 5102x 5102x 263x 263x 263x 263x 5102x 5102x 1366x 1366x 1366x 2287x 921x 921x 2287x 2287x 2287x 1366x 1366x 1366x 1366x 1366x 1366x 1366x 5102x 5102x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 1283x 5102x 5102x 5102x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 5102x 5102x 5102x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 5102x 5102x 5102x 975x 975x 975x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 975x 975x 975x 975x 975x 975x 975x 975x 975x 975x 975x 975x 975x 975x 5102x 5102x 7310x 7310x 2220x 574x 574x 574x 574x 574x 574x 574x 2220x 6736x 6736x 6736x 6736x 6736x 6736x 6736x 5102x 5102x 114x 5102x 5102x 5102x 678x 678x 179x 179x 54x 54x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 678x 678x 678x 1x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 5102x 5102x 1056x 5102x 5102x 5102x 96x 96x 96x 96x 96x 96x 96x 96x 96x 5102x 5102x 8936x 8936x 8936x 8936x 8936x 8936x 8936x 8936x 5102x 5102x 483x 483x 483x 483x 483x 483x 483x 483x 483x 483x 483x 483x 5102x 5102x 5984x 1841x 1841x 1841x 1841x 1841x 1841x 1841x 1841x 5984x 4143x 4143x 5102x 5102x 86x 42x 42x 42x 42x 42x 42x 42x 42x 54x 44x 44x 5102x 5102x 282x 5102x 5102x 387x 387x 387x 387x 387x 5102x 5102x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 5102x 5102x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 5102x 5102x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 5102x 5102x 155x 155x 155x 155x 155x 55x 155x 2x 2x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 5102x 5102x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 5102x 5102x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 5102x 5102x 42x 42x 42x 42x 42x 42x 42x 42x 42x 5102x 5102x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 5102x 5102x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5102x 5102x 22741x 22741x 5x 5x 5x 5x 5x 5x 5x 5x 5102x 5102x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5102x 5102x 288x 5102x 5102x 102x 5102x 5102x 217x 217x 5102x 5102x 5102x | /** @import { Expression } from 'estree' */ /** @import { Ast } 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 {Ast.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 {Ast.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' }; } }) ); } |