Skip to content

Conversation

NatoBoram
Copy link
Collaborator

@NatoBoram NatoBoram commented Apr 10, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a dedicated TSX language support package, enabling improved TSX parsing and integration within your project.
  • Documentation
    • Added comprehensive installation and usage instructions to help you get started quickly.
  • Tests
    • Implemented automated tests to ensure the TSX parsing behaves as expected.
  • Chores
    • Streamlined post-installation tasks to simplify setup and configuration.

Copy link

coderabbitai bot commented Apr 10, 2025

Walkthrough

This pull request integrates the new package @ast-grep/lang-tsx into the project. The changes include updating the changeset file, adding documentation and installation instructions in a new README, defining types and configuration for the TSX parser in both TypeScript and JavaScript files, establishing a testing environment in a nursery script, and including package metadata along with post-install setup.

Changes

Files Change Summary
.changeset/cyan-crabs-relax.md Added a patch entry to include the @ast-grep/lang-tsx package.
packages/tsx/README.md New README with installation instructions (for pnpm) and usage examples including dynamic language registration and TSX parsing.
packages/tsx/index.d.ts
packages/tsx/index.js
Introduced new type LanguageRegistration and exported configuration constants (e.g., libraryPath, extensions, languageSymbol, expandoChar) for TSX parsing.
packages/tsx/nursery.js Added a test setup using @ast-grep/nursery to parse TSX code and assert proper syntax tree node recognition.
packages/tsx/package.json
packages/tsx/postinstall.js
New package metadata and scripts for @ast-grep/lang-tsx, including a post-installation setup via @ast-grep/setup-lang.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant TSX as @ast-grep/lang-tsx
    participant NAPI as @ast-grep/napi
    participant Nursery as @ast-grep/nursery

    Dev->>TSX: Install and import package
    TSX->>NAPI: Register dynamic TSX language (registerDynamicLanguage)
    Dev->>NAPI: Call parse('tsx', code) to parse TSX code
    NAPI-->>Dev: Return syntax tree (including root node)
    Nursery->>NAPI: Execute test runner on TSX code
    NAPI-->>Nursery: Provide parsed node for assertion
Loading

Possibly related PRs

  • ✨ HTML #51: Integrates a new language package (@ast-grep/lang-html), similar in approach to the TSX integration.
  • ✨ Ruby #57: Introduces the @ast-grep/lang-ruby package, aligning with the TSX package addition.
  • ✨ Lua #56: Adds the @ast-grep/lang-lua package, reflecting comparable modifications for new language support.

Suggested reviewers

  • HerringtonDarkholme

Poem

I hop through lines of code with glee,
New TSX magic now springs free,
Registering languages with a flick of my tail,
Parsing syntax trees without fail,
A bunny’s cheer in every commit 🐇,
Hopping onward where ideas happily flit!

✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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: 1

🧹 Nitpick comments (4)
packages/tsx/nursery.js (1)

12-18: Consider enhancing the test coverage.

The test runner successfully validates that the parser can parse a JSX element and correctly identify its node kind. However, it could be strengthened with additional assertions.

  testRunner: parse => {
    const sg = parse('<Button onClick={() => foo()}>Click me</Button>')
    const root = sg.root()
    const node = root.find('<$ELEMENT onClick={$HANDLER}>$$$</$ELEMENT>')
    assert.equal(node.kind(), 'jsx_element')
+   // Verify the element name
+   const elementName = node.find('$ELEMENT')
+   assert.equal(elementName.text(), 'Button')
+   // Verify the handler exists
+   assert.ok(node.find('$HANDLER').text().includes('foo()'))
  },
packages/tsx/README.md (2)

1-2: Improve the package description.

The title is concise but could provide more context about what ast-grep is and the purpose of this language package.

-# ast-grep napi language for tsx
+# ast-grep napi language for TSX
+
+A TypeScript JSX (TSX) language package for ast-grep, enabling precise pattern matching and manipulation of TSX abstract syntax trees.

14-24: Provide a more realistic code example.

The usage example is clear but uses a placeholder for the code string. A real TSX example would be more helpful to users.

registerDynamicLanguage({ tsx })

-const sg = parse('tsx', `your code`)
+const sg = parse('tsx', `
+  function App() {
+    return (
+      <div className="app">
+        <Button onClick={() => console.log('clicked')}>
+          Click me
+        </Button>
+      </div>
+    );
+  }
+`)
sg.root().kind()
packages/tsx/package.json (1)

21-23: Complete the package metadata.

The author field is empty, and there are other standard metadata fields missing.

  "keywords": ["ast-grep"],
-  "author": "",
+  "author": "The ast-grep contributors",
  "license": "ISC",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ast-grep/ast-grep"
+  },
+  "homepage": "https://github.com/ast-grep/ast-grep/tree/main/packages/tsx",
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f3631de and 97d0918.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (7)
  • .changeset/cyan-crabs-relax.md (1 hunks)
  • packages/tsx/README.md (1 hunks)
  • packages/tsx/index.d.ts (1 hunks)
  • packages/tsx/index.js (1 hunks)
  • packages/tsx/nursery.js (1 hunks)
  • packages/tsx/package.json (1 hunks)
  • packages/tsx/postinstall.js (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/tsx/nursery.js (2)
packages/tsx/index.js (1)
  • path (1-1)
scripts/nursery/index.ts (1)
  • setup (38-46)
🔇 Additional comments (12)
.changeset/cyan-crabs-relax.md (1)

1-6: LGTM: Changeset correctly configured for the new TSX package.

The changeset is correctly formatted to add @ast-grep/lang-tsx as a patch release with a clear description.

packages/tsx/postinstall.js (1)

1-4: LGTM: Postinstall script is properly configured.

The postinstall script correctly imports the setup function and provides the necessary configuration. This follows the pattern used in other language packages.

packages/tsx/index.d.ts (2)

1-7: LGTM: Type definition for LanguageRegistration is well-structured.

The type definition correctly defines the structure needed for language registration with appropriate optional properties.


9-10: LGTM: Registration constant and export are properly defined.

The constant is correctly typed and exported as the default export.

packages/tsx/index.js (2)

1-2: LGTM: Parser path resolution is correct.

The code properly uses Node's path module to resolve the location of the parser library.


4-9:

✅ Verification successful

LGTM: Module exports are properly configured for TSX support.

The module exports all necessary configuration properties for the TSX language support. The extension, language symbol, and expando character are correctly specified.


🏁 Script executed:

#!/bin/bash
# Verify that parser.so will be available after installation

# Check if there's any mechanism to build or download parser.so
# This could be in package.json scripts or the setup-lang package
echo "Checking for parser.so generation mechanism..."
cat packages/tsx/package.json | grep -E "\"(pre|post)?(install|build)\""
find packages/tsx -type f -name "*.js" | xargs grep -l "parser.so"

# Check if setup-lang package downloads or builds the parser
echo "Checking setup-lang package for parser handling..."
if [ -d "packages/setup-lang" ]; then
  grep -r "parser.so" packages/setup-lang
fi

Length of output: 539


TSX configuration verified and LGTM

The build script in packages/tsx/package.json ("build": "tree-sitter build -o parser.so ./node_modules/tree-sitter-typescript/tsx") confirms that parser.so will be generated during the build process. The module exports in packages/tsx/index.js correctly configure TSX support with the appropriate extension, language symbol, and expando character.

packages/tsx/nursery.js (2)

1-5: Imports look good and follow the standard pattern.

The necessary dependencies are correctly imported for the nursery setup, including the core nursery package, assertion module, and path utilities.


6-11: Setup configuration is properly defined.

The configuration parameters match the expected format for the nursery setup, with clear specification of the language name, tree-sitter package, and source path.

packages/tsx/README.md (1)

3-12: Installation instructions are clear and complete.

The installation section properly explains how to install the necessary packages, including the optional tree-sitter CLI for cases where no prebuilt version is available.

packages/tsx/package.json (3)

6-12: Scripts look well-defined and follow expected patterns.

The scripts for building, testing, and publishing are properly configured. The use of nursery for source generation and testing is consistent with the package's purpose.


24-46: Dependencies and configuration are properly structured.

The dependency structure correctly handles required packages and makes tree-sitter-cli an optional peer dependency. The publishing configuration and pnpm build settings are also appropriate.


13-20:

❓ Verification inconclusive

File inclusion list is comprehensive.

All necessary files are included in the package distribution. Make sure the postinstall.js file exists as it's listed here but wasn't included in the files for review.


🏁 Script executed:

#!/bin/bash
# Check if postinstall.js exists in the package
fd -t f "postinstall.js" --path-regex "packages/tsx/"

Length of output: 291


Ensure the postinstall.js file exists in the package directory

The file inclusion list in packages/tsx/package.json (lines 13–20) looks comprehensive; however, we need to verify that the postinstall.js file—which is listed—is actually present. Note that the initial verification script failed due to an unrecognized --path-regex flag. Please either run a corrected check, for example:

fd -t f "postinstall.js" packages/tsx

or manually verify that packages/tsx/postinstall.js exists.

Comment on lines +1 to +5
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "",
"main": "index.js",
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add a meaningful package description.

The description field is currently empty, which isn't helpful for users discovering this package.

{
  "name": "@ast-grep/lang-tsx",
  "version": "0.0.1",
-  "description": "",
+  "description": "TypeScript JSX (TSX) language support for ast-grep",
  "main": "index.js",
📝 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
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "",
"main": "index.js",
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "TypeScript JSX (TSX) language support for ast-grep",
"main": "index.js",

@HerringtonDarkholme HerringtonDarkholme merged commit d806a88 into ast-grep:main Apr 10, 2025
3 checks passed
@NatoBoram NatoBoram deleted the feature/tsx branch April 10, 2025 20:06
@coderabbitai coderabbitai bot mentioned this pull request Apr 10, 2025
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.

2 participants