Skip to content

Fix template literal types in TypeScript. #484

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 1 commit into
base: main
Choose a base branch
from

Conversation

bm-w
Copy link

@bm-w bm-w commented Apr 3, 2025

The stanza dealing template_literal_type syntax nodes is too liberal in its capturing of inner types, causing it to capture string_fragment syntax nodes and fail creating edges with an error like:

0: Error executing statement edge @inner.lexical_scope -> @type.lexical_scope at (5775, 3)
     src/stack-graphs.tsg:5775:3:
     5775 |   edge @inner.lexical_scope -> @type.lexical_scope
          |   ^
     in stanza
     src/stack-graphs.tsg:5773:1:
     5773 | (template_literal_type (_)@inner)@type {
          | ^
     matching (template_literal_type) node
     test/types/template-literal-type.ts:2:17:
     2 | type Greeting = `Hello, ${World}!`;
       |                 ^
1: Evaluating edge source
2: Undefined scoped variable [syntax node string_fragment (2, 18)].lexical_scope

Restricting the inner type capture to just template_type syntax node fixes the issue. As of v0.23.2 of the Tree-sitter TypeScript grammar the template_literal_type only allows string_fragment and template_type children, so this fix should be fine. Copious testing (on this repo’s TypeScript test suite and on all of microsoft/vscode) reveals no negative side effects.

@Copilot Copilot AI review requested due to automatic review settings April 3, 2025 16:39
@bm-w bm-w requested review from a team as code owners April 3, 2025 16:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the template literal type parsing by restricting the capture of inner types to just the "template_type" syntax node, addressing an issue where "string_fragment" nodes were incorrectly captured.

  • Restricts inner type capture for template literal types
  • Adds a test case to verify the change
Files not reviewed (1)
  • languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg: Language not supported

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