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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9919x 9919x 9919x 9919x 9919x 9919x 9919x 9919x 9919x 1946x 1944x 1944x 1944x 1946x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 5051x 2x 2x 1965x 1965x 1965x 1965x 1965x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 644x 1321x 1956x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 992x 114x 114x 114x 114x 114x 114x 988x 992x 222x 222x 222x 222x 222x 222x 222x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 988x 329x 1820x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 190x 66x 4x 66x 62x 62x 62x 62x 66x 66x 66x 190x 17x 2x 17x 15x 15x 15x 15x 17x 17x 17x 124x 107x 107x 107x 190x 190x 190x 190x 190x 190x 139x 534x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 547x 547x 547x 547x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 2x 2x 421x 421x 421x 421x 421x 421x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 173x 126x 126x 126x 126x 181x 181x 181x 240x 417x 54x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 186x 187x 180x 91x 91x 91x 83x 83x 83x 83x 83x 91x 91x 91x 91x 91x 91x 91x 89x 90x 86x 86x 86x 80x 80x 80x 80x 80x 86x 86x 86x 86x 86x 86x 86x 3x 3x 3x 6x 6x 6x 2x 2x 1944x 1944x 1944x 1944x 1944x 1944x 1944x 635x 635x 635x 635x 55x 55x 55x 55x 635x 635x 635x 1944x 1944x 987x 987x 1944x 30x 30x 1944x 185x 185x 1944x 107x 107x 1944x 1944x 1944x 1944x 1944x 1309x 1309x 1309x 1309x 1309x 1309x 2x 2x 538x 538x 538x 538x 538x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 425x 530x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 424x 468x 269x 269x 269x 269x 269x 269x 269x 269x 269x 269x 269x 269x 2x 269x 1x 1x 1x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 422x 504x 155x 155x 155x 155x 155x 155x 155x 16x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 538x | /** @import { ArrowFunctionExpression, Expression, Identifier } from 'estree' */ /** @import { AwaitBlock, ConstTag, DebugTag, EachBlock, ExpressionTag, HtmlTag, IfBlock, KeyBlock, RenderTag, SnippetBlock } from '#compiler' */ /** @import { Parser } from '../index.js' */ 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'; import { parse_expression_at } from '../acorn.js'; import { create_expression_metadata } from '../../nodes.js'; const regex_whitespace_with_closing_curly_brace = /^\s*}/; /** @param {Parser} parser */ export default function tag(parser) { const start = parser.index; parser.index += 1; parser.allow_whitespace(); if (parser.eat('#')) return open(parser); if (parser.eat(':')) return next(parser); if (parser.eat('@')) return special(parser); if (parser.match('/')) { if (!parser.match('/*') && !parser.match('//')) { parser.eat('/'); return close(parser); } } const expression = read_expression(parser); parser.allow_whitespace(); parser.eat('}', true); /** @type {ReturnType<typeof parser.append<ExpressionTag>>} */ parser.append({ type: 'ExpressionTag', start, end: parser.index, expression, metadata: { expression: create_expression_metadata() } }); } /** @param {Parser} parser */ function open(parser) { let start = parser.index - 2; while (parser.template[start] !== '{') start -= 1; if (parser.eat('if')) { parser.require_whitespace(); /** @type {ReturnType<typeof parser.append<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 {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 {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<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<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<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.allow_whitespace(); const params_start = parser.index; parser.eat('(', true); let parentheses = 1; while (parser.index < parser.template.length && (!parser.match(')') || parentheses !== 1)) { if (parser.match('(')) parentheses++; if (parser.match(')')) parentheses--; parser.index += 1; } parser.eat(')', true); const prelude = parser.template.slice(0, params_start).replace(/\S/g, ' '); const params = parser.template.slice(params_start, parser.index); let function_expression = /** @type {ArrowFunctionExpression} */ ( parse_expression_at(prelude + `${params} => {}`, parser.ts, params_start) ); parser.allow_whitespace(); parser.eat('}', true); /** @type {ReturnType<typeof parser.append<SnippetBlock>>} */ const block = parser.append({ type: 'SnippetBlock', start, end: -1, expression: { type: 'Identifier', start: name_start, end: name_end, name }, parameters: function_expression.params, body: create_fragment() }); parser.stack.push(block); parser.fragments.push(block.body); return; } e.expected_block_type(parser.index); } /** @param {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); let elseif_start = start - 1; while (parser.template[elseif_start] !== '{') elseif_start -= 1; /** @type {ReturnType<typeof parser.append<IfBlock>>} */ const child = parser.append({ start: elseif_start, 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 {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 {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 {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<HtmlTag>>} */ parser.append({ type: 'HtmlTag', start, end: parser.index, expression }); return; } if (parser.eat('debug')) { /** @type {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 {Identifier[]} */ (expression.expressions) : [/** @type {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<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<ConstTag>>} */ parser.append({ type: 'ConstTag', start, end: parser.index, declaration: { type: 'VariableDeclaration', kind: 'const', declarations: [{ type: 'VariableDeclarator', id, init, start: id.start, end: init.end }], 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<RenderTag>>} */ parser.append({ type: 'RenderTag', start, end: parser.index, expression: expression, metadata: { dynamic: false, args_with_call_expression: new Set() } }); } } |