Skip to content

[parser] lit template parser #4942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

e111077
Copy link
Contributor

@e111077 e111077 commented Feb 28, 2025

DO NOT SUBMIT WIP

By default uses oxc-parser to generate an estree of a TS file, but can also accept a TS AST. Then finds all the tagged template literals in there, and then uses heuristics or comments to determine whether they are lit-html templates.

From there it parses the template's html and produces parse5-inspired AST and attaches them to the ESTree (or TS AST)'s template literal and each bound expression.

This differs from a parse5 ast node like so:

// input html`<d${literal`iv`} .asdf=${123}>${tmpl}</d${literal`iv`}>`
// truncated parse5 output
{
  nodeName: 'd${literal`iv`}',
  attrs: [
    {
        name: '.asdf',
        value: '${123}'
    },
    childNodes: [
      {
        nodeName: '#text',
        value: '${tmpl}'
      }
    ]
  ]
}

into this:

{
  type: 'DocumentFragment',
  children: [
    {
      type: 'Element',
      // stringified tag name: 'd${...}'
      tagName: [ // tagname is actually an array not a string
        {
          type: 'LitTagLiteral', // lit-parser-specific node
          value: 'd'
        },
        {
          type: 'LitHtmlExpression', // lit-parser-specific node
          nodeName: '#lit-html-expression',
          expression: {
            type: 'TaggedTemplateExpression', // estree node
            tag: {
              type: 'Identifier', // estree node
              name: 'literal'
            },
            quasi: {
              type: 'TemplateLiteral' // estree node
            }
          }
        }
      ],
      attributes: [
        {
          type: 'Property', // lit-parser-specific node
          name: {
            // stringified tag name: '.asdf',
            raw: [
              {
                type: 'LitTagLiteral', // lit-parser-specific node
                value: '.asdf'
              }
            ]
          },
          value: [
            {
              nodeName: '#lit-html-expression',
              type: 'LitHtmlExpression', // lit-parser-specific node
              node: {
                type: 'Literal', // estree node
                value: 123
              }
            }
          ]
        }
      ],
      childNodes: [
        {
          nodeName: '#lit-html-expression',
          type: 'LitHtmlExpression', // lit-parser-specific node
          node: {
            type: 'Identifier', // estree node
            name: 'tmpl'
          }
        }
      ]
    }
  ]
}

It also adds and hooks into the documentFragment property on the estree's TaggedTemplateExpression and adds an isLit: true on it so that you can filter for it.

Additionally every expression bound into a lit-html template will have a litHtmlExpression property that will point to the associated LitHtmlExpression which also includes the ${} wrapper around the expression.

fork of #4267

Copy link

changeset-bot bot commented Feb 28, 2025

⚠️ No Changeset found

Latest commit: 42fcb6d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@e111077 e111077 changed the title [analyzer] lit template analyzer [analyzer] lit template analyzer fork Feb 28, 2025
Copy link
Contributor

github-actions bot commented Feb 28, 2025

📊 Tachometer Benchmark Results

Summary

nop-update

  • this-change, tip-of-tree, previous-release: unsure 🔍 -11% - +9% (-1.36ms - +1.06ms)
    this-change vs tip-of-tree

render

  • this-change: 43.17ms - 56.89ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -5% - +4% (-0.90ms - +0.66ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +2% (-0.35ms - +0.65ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -15% - +47% (-6.50ms - +22.12ms)
    this-change vs tip-of-tree

update

  • this-change: 473.89ms - 478.96ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -7% - +2% (-2.68ms - +0.86ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +2% (-0.78ms - +1.32ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +0% (-8.17ms - +2.43ms)
    this-change vs tip-of-tree

update-reflect

  • this-change: 462.68ms - 465.15ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +1% (-3.56ms - +4.76ms)
    this-change vs tip-of-tree

Results

this-change

render

VersionAvg timevs
43.17ms - 56.89ms-

update

VersionAvg timevs
473.89ms - 478.96ms-

update-reflect

VersionAvg timevs
462.68ms - 465.15ms-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
18.08ms - 19.08ms-unsure 🔍
-5% - +4%
-0.90ms - +0.66ms
unsure 🔍
-4% - +4%
-0.78ms - +0.69ms
tip-of-tree
tip-of-tree
18.10ms - 19.30msunsure 🔍
-4% - +5%
-0.66ms - +0.90ms
-unsure 🔍
-4% - +5%
-0.74ms - +0.88ms
previous-release
previous-release
18.08ms - 19.17msunsure 🔍
-4% - +4%
-0.69ms - +0.78ms
unsure 🔍
-5% - +4%
-0.88ms - +0.74ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
34.47ms - 36.97ms-unsure 🔍
-7% - +2%
-2.68ms - +0.86ms
unsure 🔍
-8% - +1%
-3.03ms - +0.39ms
tip-of-tree
tip-of-tree
35.37ms - 37.89msunsure 🔍
-2% - +8%
-0.86ms - +2.68ms
-unsure 🔍
-6% - +4%
-2.13ms - +1.31ms
previous-release
previous-release
35.87ms - 38.21msunsure 🔍
-1% - +9%
-0.39ms - +3.03ms
unsure 🔍
-4% - +6%
-1.31ms - +2.13ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
11.26ms - 12.93ms-unsure 🔍
-11% - +9%
-1.36ms - +1.06ms
unsure 🔍
-10% - +10%
-1.18ms - +1.19ms
tip-of-tree
tip-of-tree
11.37ms - 13.12msunsure 🔍
-9% - +11%
-1.06ms - +1.36ms
-unsure 🔍
-9% - +11%
-1.06ms - +1.37ms
previous-release
previous-release
11.25ms - 12.93msunsure 🔍
-10% - +10%
-1.19ms - +1.18ms
unsure 🔍
-11% - +9%
-1.37ms - +1.06ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
34.27ms - 35.01ms-unsure 🔍
-1% - +2%
-0.35ms - +0.65ms
unsure 🔍
-2% - +1%
-0.58ms - +0.37ms
tip-of-tree
tip-of-tree
34.16ms - 34.83msunsure 🔍
-2% - +1%
-0.65ms - +0.35ms
-unsure 🔍
-2% - +1%
-0.70ms - +0.20ms
previous-release
previous-release
34.45ms - 35.04msunsure 🔍
-1% - +2%
-0.37ms - +0.58ms
unsure 🔍
-1% - +2%
-0.20ms - +0.70ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
68.45ms - 69.92ms-unsure 🔍
-1% - +2%
-0.78ms - +1.32ms
unsure 🔍
-1% - +2%
-0.80ms - +1.22ms
tip-of-tree
tip-of-tree
68.17ms - 69.67msunsure 🔍
-2% - +1%
-1.32ms - +0.78ms
-unsure 🔍
-2% - +1%
-1.08ms - +0.96ms
previous-release
previous-release
68.28ms - 69.67msunsure 🔍
-2% - +1%
-1.22ms - +0.80ms
unsure 🔍
-1% - +2%
-0.96ms - +1.08ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
46.84ms - 68.17ms-unsure 🔍
-15% - +47%
-6.50ms - +22.12ms
unsure 🔍
-28% - +22%
-16.67ms - +13.43ms
tip-of-tree
tip-of-tree
40.17ms - 59.23msunsure 🔍
-37% - +9%
-22.12ms - +6.50ms
-unsure 🔍
-38% - +6%
-23.70ms - +4.84ms
previous-release
previous-release
48.51ms - 69.75msunsure 🔍
-24% - +29%
-13.43ms - +16.67ms
unsure 🔍
-12% - +50%
-4.84ms - +23.70ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
479.72ms - 486.20ms-unsure 🔍
-2% - +0%
-8.17ms - +2.43ms
unsure 🔍
-1% - +1%
-7.03ms - +3.71ms
tip-of-tree
tip-of-tree
481.64ms - 490.03msunsure 🔍
-1% - +2%
-2.43ms - +8.17ms
-unsure 🔍
-1% - +1%
-4.78ms - +7.20ms
previous-release
previous-release
480.34ms - 488.90msunsure 🔍
-1% - +1%
-3.71ms - +7.03ms
unsure 🔍
-1% - +1%
-7.20ms - +4.78ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
484.74ms - 490.41ms-unsure 🔍
-1% - +1%
-3.56ms - +4.76ms
unsure 🔍
-1% - +1%
-3.60ms - +4.58ms
tip-of-tree
tip-of-tree
483.93ms - 490.03msunsure 🔍
-1% - +1%
-4.76ms - +3.56ms
-unsure 🔍
-1% - +1%
-4.35ms - +4.13ms
previous-release
previous-release
484.14ms - 490.04msunsure 🔍
-1% - +1%
-4.58ms - +3.60ms
unsure 🔍
-1% - +1%
-4.13ms - +4.35ms
-

tachometer-reporter-action v2 for Benchmarks

Copy link
Contributor

The size of lit-html.js and lit-core.min.js are as expected.

@e111077 e111077 force-pushed the techytacos--lit-template-analyzer branch 3 times, most recently from ca4e91b to e45c385 Compare March 5, 2025 09:24
@e111077 e111077 changed the title [analyzer] lit template analyzer fork [analyzer] lit template parser Mar 5, 2025
@e111077 e111077 force-pushed the techytacos--lit-template-analyzer branch 5 times, most recently from 1e2c6f1 to e308dad Compare March 7, 2025 08:44
@e111077 e111077 changed the title [analyzer] lit template parser [parser] lit template parser Mar 7, 2025
@e111077 e111077 force-pushed the techytacos--lit-template-analyzer branch 6 times, most recently from e2fcc3e to 40651ee Compare March 13, 2025 23:14
@e111077 e111077 force-pushed the techytacos--lit-template-analyzer branch from 40651ee to 42fcb6d Compare April 11, 2025 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy