Skip to content

feat(compiler-core): add openTagLoc and closeTagLoc to element ast node #12928

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

KazariEX
Copy link
Member

@KazariEX KazariEX commented Feb 22, 2025

Nowadays, language tools rely on string search to determine the offset of tags, which is not reliable and may cause some issues, like:

<img v-bind="imgAttrs">
        <!-- ^^^ is recognized as the close tag -->
<div></Div>
  <!-- ^^^ is not recognized as the close tag -->

Summary by CodeRabbit

  • New Features

    • Added an option to record the precise source location of opening and closing tags in parsed elements.
    • Introduced a parser setting to enable or disable recording of tag locations.
  • Tests

    • Enhanced test coverage to verify accurate recording of tag location information in parsed elements.

Copy link

github-actions bot commented Feb 22, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB (+114 B) 58.5 kB (+39 B) 52 kB (+41 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.4 kB 22.8 kB 20.8 kB
overall 68.5 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Feb 22, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@12928

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@12928

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@12928

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@12928

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@12928

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@12928

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@12928

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@12928

@vue/shared

npm i https://pkg.pr.new/@vue/shared@12928

vue

npm i https://pkg.pr.new/vue@12928

@vue/compat

npm i https://pkg.pr.new/@vue/compat@12928

commit: fb731b8

@KazariEX KazariEX marked this pull request as ready for review February 22, 2025 17:50
@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: compiler language-tools related to vue language-tools labels Feb 23, 2025
Copy link

coderabbitai bot commented Jun 18, 2025

Walkthrough

The changes introduce new options and properties to enable and handle precise source location tracking for opening and closing tags in the parser's AST nodes. Updates include AST type extensions, parser logic to populate these locations when enabled, parser options to control the feature, and corresponding test coverage.

Changes

File(s) Change Summary
packages/compiler-core/src/ast.ts Extended BaseElementNode interface with optional openTagLoc and closeTagLoc properties.
packages/compiler-core/src/options.ts Added optional tagLocations boolean property to ParserOptions interface.
packages/compiler-core/src/parser.ts Updated parser logic and options to support tagLocations, recording tag locations in AST nodes.
packages/compiler-dom/tests/parse.spec.ts Enhanced test to assert presence and correctness of tagLoc in parsed AST for a void <img> element.

Sequence Diagram(s)

sequenceDiagram
    participant SourceCode
    participant Parser
    participant ASTNode

    SourceCode->>Parser: Provide HTML/XML source
    Parser->>Parser: Check if tagLocations option is enabled
    alt tagLocations enabled
        Parser->>ASTNode: Attach openTagLoc during open tag parse
        Parser->>ASTNode: Attach closeTagLoc during close tag parse
    end
    Parser->>ASTNode: Build AST with tag location info (if enabled)
Loading

Poem

🐇
With tags now tracked from open to close,
The parser knows just where it goes.
Locations marked with careful care,
In ASTs, precision’s there!
A hop, a skip, a test that sings—
The code now maps all taggy things!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@KazariEX KazariEX changed the title feat(compiler-core): add tagLoc and closeTagLoc to element ast node feat(compiler-core): add openTagLoc and closeTagLoc to element ast node Jun 18, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
packages/compiler-core/src/options.ts (1)

100-104: Minor doc tweak

The comment says “open tag and close tag of each node” – consider clarifying that it records the tag-name locations, not the whole tag range, to avoid ambiguity.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 604d087 and b4566c2.

📒 Files selected for processing (4)
  • packages/compiler-core/src/ast.ts (1 hunks)
  • packages/compiler-core/src/options.ts (1 hunks)
  • packages/compiler-core/src/parser.ts (3 hunks)
  • packages/compiler-dom/__tests__/parse.spec.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test / unit-test-windows
🔇 Additional comments (1)
packages/compiler-core/src/parser.ts (1)

81-82: Performance toggle added – ensure downstream opts in

tagLocations defaults to false, yet the DOM-parser tests rely on it being true.
If parserOptions in packages/compiler-dom/src/parserOptions.ts is not explicitly enabling this flag, the new fields will never be set and tests will still fail even after the naming fix.
Please verify & enable where needed.

Comment on lines +133 to 135
openTagLoc?: SourceLocation
closeTagLoc?: SourceLocation
tagType: ElementTypes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Mismatch with test expectations – property should be tagLoc or an alias must be added

The interface now exposes openTagLoc / closeTagLoc, but the updated unit-test asserts against a tagLoc field.
Unless an alias is added or the tests are updated, the type checker and the runtime equality check (toStrictEqual) will fail.

   tag: string
-  openTagLoc?: SourceLocation
-  closeTagLoc?: SourceLocation
+  // location of `<tag>` name
+  openTagLoc?: SourceLocation
+  // location of `</tag>` name
+  closeTagLoc?: SourceLocation
+  /** @deprecated – kept for backwards-compatibility with existing tooling/tests */
+  tagLoc?: SourceLocation

Alternatively, rename the tests to use openTagLoc. Keep one consistent public field, or expose an alias – but make it deliberate.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
openTagLoc?: SourceLocation
closeTagLoc?: SourceLocation
tagType: ElementTypes
tag: string
// location of `<tag>` name
openTagLoc?: SourceLocation
// location of `</tag>` name
closeTagLoc?: SourceLocation
/** @deprecated – kept for backwards-compatibility with existing tooling/tests */
tagLoc?: SourceLocation
tagType: ElementTypes
🤖 Prompt for AI Agents
In packages/compiler-core/src/ast.ts around lines 133 to 135, the interface uses
separate properties openTagLoc and closeTagLoc, but the unit tests expect a
single tagLoc property. To fix this, either rename the interface property to
tagLoc to match the tests, add a tagLoc alias that combines or references
openTagLoc, or update the tests to use openTagLoc consistently. Ensure the
property naming is consistent between the interface and tests to avoid type
checker and runtime equality failures.

Comment on lines +150 to +152
if (currentOptions.tagLocations) {
currentOpenTag.openTagLoc = getLoc(start, end)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Set legacy alias for forward compatibility

When the option is active you attach openTagLoc, but not tagLoc.
If you decide to keep the test’s tagLoc, add the alias at the same time:

 if (currentOptions.tagLocations) {
   currentOpenTag.openTagLoc = getLoc(start, end)
+  // backwards compat
+  ;(currentOpenTag as any).tagLoc = currentOpenTag.openTagLoc
 }

This avoids breaking userland tools that already consume tagLoc.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (currentOptions.tagLocations) {
currentOpenTag.openTagLoc = getLoc(start, end)
}
if (currentOptions.tagLocations) {
currentOpenTag.openTagLoc = getLoc(start, end)
// backwards compat
;(currentOpenTag as any).tagLoc = currentOpenTag.openTagLoc
}
🤖 Prompt for AI Agents
In packages/compiler-core/src/parser.ts around lines 150 to 152, the code sets
currentOpenTag.openTagLoc when currentOptions.tagLocations is true but does not
set the legacy alias currentOpenTag.tagLoc. To fix this, add an assignment
setting currentOpenTag.tagLoc equal to currentOpenTag.openTagLoc within the same
conditional block to maintain backward compatibility with userland tools relying
on tagLoc.

Comment on lines 171 to +174
const el = stack.shift()!
if (j === i && currentOptions.tagLocations) {
el.closeTagLoc = getLoc(start, end)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Same alias needed on close tag

 if (j === i && currentOptions.tagLocations) {
   el.closeTagLoc = getLoc(start, end)
+  ;(el as any).tagLoc = el.openTagLoc // keep alias symmetrical
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const el = stack.shift()!
if (j === i && currentOptions.tagLocations) {
el.closeTagLoc = getLoc(start, end)
}
const el = stack.shift()!
if (j === i && currentOptions.tagLocations) {
el.closeTagLoc = getLoc(start, end)
;(el as any).tagLoc = el.openTagLoc // keep alias symmetrical
}
🤖 Prompt for AI Agents
In packages/compiler-core/src/parser.ts around lines 171 to 174, the close tag
element lacks the same alias as the opening tag. To fix this, ensure that the
close tag element uses the same alias as the corresponding opening tag by
assigning the alias property from the opening tag to the close tag element
within the conditional block where closeTagLoc is set.

Comment on lines 312 to 316
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'img',
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Assertion uses tagLoc which parser no longer emits

With the AST now exposing openTagLoc/closeTagLoc, the expectation below will fail:

-  tagLoc: {
+  openTagLoc: {
     ...
   },

Either update the test or keep a tagLoc alias in the AST node (see previous comments).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'img',
},
openTagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'img',
},
🤖 Prompt for AI Agents
In packages/compiler-dom/__tests__/parse.spec.ts around lines 312 to 316, the
test is using the outdated `tagLoc` property which the parser no longer emits,
having replaced it with `openTagLoc` and `closeTagLoc`. To fix this, update the
test assertions to use `openTagLoc` and `closeTagLoc` properties instead of
`tagLoc`, reflecting the current AST structure. Alternatively, if preferred,
modify the AST node to include a `tagLoc` alias that combines `openTagLoc` and
`closeTagLoc` as per previous comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language-tools related to vue language-tools ready to merge The PR is ready to be merged. scope: compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants