Skip to content

fix: various fix for napi build #1666

Merged
HerringtonDarkholme merged 3 commits into
mainfrom
feat-improve-napi-type
Dec 15, 2024
Merged

fix: various fix for napi build #1666
HerringtonDarkholme merged 3 commits into
mainfrom
feat-improve-napi-type

Conversation

@HerringtonDarkholme

@HerringtonDarkholme HerringtonDarkholme commented Dec 14, 2024

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features

    • Introduced a script for generating language-specific node type definitions.
  • Bug Fixes

    • Consolidated duplicate import statements to streamline code.
  • Chores

    • Updated build scripts to include a new command in the package configuration.
    • Changed reference in the type generation script to a new file.
    • Updated version of the package to 0.31.1.
  • Refactor

    • Removed unnecessary function and simplified control flow in the update script.
    • Updated URL for Lua language's node types to a version-tagged repository.

@coderabbitai

coderabbitai Bot commented Dec 14, 2024

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request focuses on refactoring the TypeScript type generation and build scripts for the @ast-grep/napi package. The changes involve consolidating import statements in the index.d.ts file, updating build scripts in package.json, and introducing a new script for generating language-specific node type definitions. Additionally, a function has been removed from the updateIndexDts.ts script to simplify its control flow. These modifications aim to streamline the type generation workflow and improve script organization.

Changes

File Change Summary
crates/napi/index.d.ts Removed duplicate import statements for FieldNames, FieldSgNode, and NodeTypesMap
crates/napi/package.json Updated build scripts to include yarn patchDts, modified typegen script, added new patchDts script
crates/napi/scripts/generateTypes.ts New script for generating language-specific node type definitions
crates/napi/scripts/updateIndexDts.ts Removed generateLangNodeTypes function, updated updateIndexDts function
crates/napi/scripts/constants.ts Updated URL for Lua language's node types in languagesNodeTypesUrls constant

Possibly related PRs

  • feat: support more languages in napi #1171: Modifies the index.d.ts file to update the FrontEndLanguage enum and introduces new functions related to parsing code in different languages, which may interact with the consolidated import statements in the main PR.
  • [RFC] feat(napi): Typed SgNode and SgRoot #1661: Enhances type definitions in index.d.ts and modifies the SgNode and SgRoot classes, which could be related to the changes in import statements and type definitions in the main PR.
  • feat: improve type generation #1662: Restructures type exports and function signatures in index.d.ts, which aligns with the changes made in the main PR regarding import statement consolidation.

Poem

🐰 Hop, hop, through the code we go,
Cleaning imports with a rhythmic flow.
Scripts dancing, types align,
Refactoring magic, simply divine!
A rabbit's touch, both swift and neat, 🌟
In the garden of code, we make it sweet!

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4f4af1d and c6c7609.

📒 Files selected for processing (2)
  • crates/napi/scripts/constants.ts (1 hunks)
  • crates/napi/scripts/generateTypes.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/napi/scripts/generateTypes.ts
🔇 Additional comments (1)
crates/napi/scripts/constants.ts (1)

81-81: LGTM! Verifying the repository and tags.

The URL update improves consistency by using the official tree-sitter-grammars organization and supports versioning through tags.

Let's verify the repository and its tags:

✅ Verification successful

Repository and tags verified successfully

The tree-sitter-lua repository in tree-sitter-grammars organization:

  • Has proper versioning with 20 tags (v0.0.1 through v0.2.0)
  • Contains the required node-types.json file at the expected path
  • Returns HTTP 200 status code confirming file accessibility
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the Lua grammar repository exists and has proper tags

# Check if the repository exists and fetch its tags
gh api \
  -H "Accept: application/vnd.github+json" \
  repos/tree-sitter-grammars/tree-sitter-lua/tags --jq '.[].name'

# Verify node-types.json exists in the expected path
curl -s -o /dev/null -w "%{http_code}" \
  "https://raw.githubusercontent.com/tree-sitter-grammars/tree-sitter-lua/master/src/node-types.json"

Length of output: 406


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @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.

@codecov

codecov Bot commented Dec 14, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.13%. Comparing base (4e88d42) to head (c6c7609).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1666   +/-   ##
=======================================
  Coverage   87.13%   87.13%           
=======================================
  Files          96       96           
  Lines       15476    15476           
=======================================
  Hits        13485    13485           
  Misses       1991     1991           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (3)
crates/napi/package.json (1)

54-54: Update @ast-grep/napi devDependency version

The package version is 0.31.1, but the devDependency @ast-grep/napi is still at 0.31.0. Consider updating the devDependency version to match.

-    "@ast-grep/napi": "0.31.0",
+    "@ast-grep/napi": "0.31.1",
crates/napi/scripts/generateTypes.ts (1)

51-54: Consider adding cleanup on error

The error handling could benefit from cleanup operations before exiting.

 generateLangNodeTypes().catch((error) => {
   console.error('Error:', error);
+  // Attempt to clean up any partially generated files
+  console.log('Cleaning up...');
   process.exit(1);
 });
crates/napi/scripts/updateIndexDts.ts (1)

93-96: Improve error message detail

The error handling could provide more context about what failed during the update process.

 updateIndexDts().catch((error) => {
-  console.error("Error in updateIndexDts:", error);
+  console.error("Failed to update index.d.ts definitions:", error);
+  console.error("Stack trace:", error.stack);
   process.exit(1);
 })
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4e88d42 and 4f4af1d.

📒 Files selected for processing (4)
  • crates/napi/index.d.ts (1 hunks)
  • crates/napi/package.json (1 hunks)
  • crates/napi/scripts/generateTypes.ts (1 hunks)
  • crates/napi/scripts/updateIndexDts.ts (5 hunks)
✅ Files skipped from review due to trivial changes (1)
  • crates/napi/index.d.ts
🧰 Additional context used
📓 Learnings (1)
crates/napi/scripts/generateTypes.ts (2)
Learnt from: HerringtonDarkholme
PR: ast-grep/ast-grep#1662
File: crates/napi/scripts/generate-types.ts:128-129
Timestamp: 2024-12-14T18:57:47.685Z
Learning: In `crates/napi/scripts/generate-types.ts`, when re-exporting modules that only export types (e.g., `./types/${lang}-node-types`), use `export type { ... }` to correctly re-export the types in TypeScript.
Learnt from: HerringtonDarkholme
PR: ast-grep/ast-grep#1662
File: crates/napi/scripts/generate-types.ts:49-49
Timestamp: 2024-12-14T18:57:37.142Z
Learning: In `crates/napi/scripts/generate-types.ts`, it's expected to assign the output of `JSON.stringify(nodeTypeMap, null, 2)` directly to a TypeScript type alias using `export type` syntax.
🔇 Additional comments (3)
crates/napi/package.json (2)

50-51: LGTM! Improved organization of type generation scripts

The separation of type generation (generateTypes.ts) and type definition updates (updateIndexDts.ts) shows good separation of concerns. This should make the type generation workflow more maintainable.

✅ Verification successful

LGTM! Clean separation of type generation concerns

The scripts show a well-structured separation of concerns:

  • generateTypes.ts fetches and generates node type definitions from tree-sitter language repositories
  • updateIndexDts.ts updates type declarations in index.d.ts to handle generic type parameters

There are no circular dependencies between the scripts, and they serve distinct purposes in the type generation workflow.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the relationship between type generation scripts
# Check for any circular dependencies or incorrect imports

# Check the content of both scripts
rg -A 5 "import.*from" scripts/generateTypes.ts scripts/updateIndexDts.ts

# Look for any shared functionality between the scripts
ast-grep --pattern 'function $name($$$) { $$$ }'

Length of output: 257


Script:

#!/bin/bash
# Let's first find the actual location of these scripts
fd -t f "generateTypes.ts|updateIndexDts.ts"

# Then check the package.json to understand the script setup
fd -t f "package.json" | xargs cat

Length of output: 13183


Script:

#!/bin/bash
# Now that we found the correct paths, let's examine the content of these scripts
cat crates/napi/scripts/generateTypes.ts crates/napi/scripts/updateIndexDts.ts

# Also check for any imports between these files
rg -A 5 "import.*from" crates/napi/scripts/generateTypes.ts crates/napi/scripts/updateIndexDts.ts

Length of output: 7077


45-46: Verify the impact of post-build type modifications

The addition of yarn patchDts to both build commands suggests type definition modifications after the build. Let's verify that this doesn't affect the build artifacts unexpectedly.

✅ Verification successful

Build process changes are safe and well-implemented

The patchDts script is a necessary post-build step that updates type definitions to ensure proper TypeScript type support. After examining the implementation:

  1. updateIndexDts.ts performs controlled modifications to index.d.ts to:

    • Add required type imports
    • Update class type parameters for better type inference
    • Modify method signatures for improved type safety
  2. The script runs after both debug and release builds to maintain consistent type definitions, and its changes are deterministic and type-safe.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the changes made by patchDts script
# Check the content and structure of the type definition files

# Check if index.d.ts exists and is modified by the script
fd -e d.ts -x stat -c "%n: %y" {}

# Look for any potential error patterns in the build script
rg -A 3 "patchDts" scripts/

Length of output: 186


Script:

#!/bin/bash
# Let's try a different approach to verify the patchDts script and its impact

# First, check if the patchDts script exists in package.json
rg "patchDts" crates/napi/package.json -A 2

# Look for any TypeScript-related build or patch scripts
rg "\"scripts\":" crates/napi/package.json -A 15

# Check for any .d.ts files in the repository
fd ".d.ts$"

# Look for any TypeScript configuration
fd "tsconfig.json"

Length of output: 1196


Script:

#!/bin/bash
# Let's examine the TypeScript scripts that handle type definitions

# Check the content of the update script
rg -l "updateIndexDts.ts" crates/napi/
cat crates/napi/scripts/updateIndexDts.ts

# Check the content of the type generation script
cat crates/napi/scripts/generateTypes.ts

# Look at the current state of index.d.ts
cat crates/napi/index.d.ts

Length of output: 16484

crates/napi/scripts/updateIndexDts.ts (1)

Line range hint 32-41: Verify AST node existence before operations

The code assumes certain AST nodes exist with ! assertions. Add explicit checks to handle cases where nodes might be missing.

 const sgNodeClass = root.find({
   // @ts-expect-error temporary error
   rule: createMatchClassDeclarationRule("SgNode"),
 });
+
+if (!sgNodeClass) {
+  throw new Error("Failed to find SgNode class declaration in AST");
+}

Comment thread crates/napi/scripts/generateTypes.ts
Comment thread crates/napi/scripts/generateTypes.ts
Comment thread crates/napi/scripts/updateIndexDts.ts
Comment thread crates/napi/scripts/constants.ts
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