Skip to content

feat: C# language #41

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged

Conversation

pelikhan
Copy link
Contributor

@pelikhan pelikhan commented Mar 27, 2025

Summary by CodeRabbit

  • Documentation

    • Introduced a detailed README with installation instructions and usage examples for enabling C# language support.
  • New Features

    • Launched a new package that integrates C# language parsing capabilities and dynamic registration.
  • Tests

    • Set up a dedicated testing environment to verify C# code parsing and syntax tree validation.
  • Chores

    • Added configuration and automation scripts to streamline building, testing, and post-installation processes.

Copy link

coderabbitai bot commented Mar 27, 2025

Walkthrough

This pull request introduces a new C# language package into the ast-grep framework. It adds a set of files including a README with installation and usage instructions, type definitions, implementation code registering the C# parser, a testing setup using the nursery package, and a post-install script for setup tasks. The changes define exported entities for language registration and configure package metadata, dependencies, and scripts for building and testing.

Changes

File(s) Change Summary
packages/csharp/README.md
packages/csharp/package.json
Added new documentation and package metadata for @ast-grep/lang-csharp including installation instructions, usage examples, scripts, and dependency declarations.
packages/csharp/index.d.ts
packages/csharp/index.js
Introduced a new type (LanguageRegistration) and its corresponding export object with properties such as libraryPath, extensions, languageSymbol, and expandoChar for C# parser integration.
packages/csharp/nursery.js
packages/csharp/postinstall.js
Added a testing setup environment using @ast-grep/nursery to validate the parser and a post-install script that invokes setup tasks using @ast-grep/setup-lang.

Sequence Diagram(s)

sequenceDiagram
    participant User as User/Dev
    participant Postinstall as Postinstall Hook
    participant Parser as C# Parser Module
    participant Nursery as Nursery Test Runner

    User->>Postinstall: Trigger package post-installation
    Postinstall->>Parser: Execute setup with current directory
    User->>Nursery: Run language tests
    Nursery->>Parser: Register language and parse sample code
    Parser-->>Nursery: Return parsed syntax tree
Loading

Possibly related PRs

  • feat: add tree-sitter-c #35: Introduces a new language package for C (similar structure and registration patterns as the C# package), relating directly to code-level changes in language support.

Poem

Hopping through code with a joyful heart,
I found a new burrow—a fresh language start.
README, types, and tests in a neat cascade,
A C# surprise in our ast-grep parade!
With every line and hop, I cheer in delight,
A rabbit’s song coding through day and night!

✨ 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.

@HerringtonDarkholme HerringtonDarkholme merged commit 21e1b9b into ast-grep:main Mar 27, 2025
1 of 2 checks passed
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: 3

🧹 Nitpick comments (3)
packages/csharp/index.js (1)

1-9: LGTM, but consider adding additional file extensions and documentation.

The code correctly sets up C# language integration for ast-grep. A few suggestions for improvement:

  1. Consider adding .csx extension to the extensions array for C# script files
  2. Adding a JSDoc comment at the top of the file would help document the module's purpose
  3. A brief comment explaining what expandoChar is used for would improve clarity
 const path = require('node:path')
 const libPath = path.join(__dirname, 'parser.so')
 
+// C# language configuration for ast-grep
 module.exports = {
   libraryPath: libPath,
-  extensions: ["cs"],
+  extensions: ["cs", "csx"],
   languageSymbol: 'tree_sitter_CSharp',
+  // Character used for AST node expansion in patterns
   expandoChar: 'µ',
 }
packages/csharp/package.json (2)

22-22: Add author information.

The author field is empty. Adding proper attribution is important for maintainability and recognition.

-  "author": "",
+  "author": "The ast-grep team",

8-9: Consider consolidating duplicate scripts.

The source and prepublishOnly scripts are identical. Consider using a shared script or documenting why both are needed.

   "scripts": {
     "build": "tree-sitter build -o parser.so",
-    "source": "node nursery.js source",
-    "prepublishOnly": "node nursery.js source",
+    "source": "node nursery.js source",
+    "prepublishOnly": "npm run source",
     "postinstall": "node postinstall.js",
     "test": "node nursery.js test"
   },
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ebe5ca2 and 3775bf1.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (6)
  • packages/csharp/README.md (1 hunks)
  • packages/csharp/index.d.ts (1 hunks)
  • packages/csharp/index.js (1 hunks)
  • packages/csharp/nursery.js (1 hunks)
  • packages/csharp/package.json (1 hunks)
  • packages/csharp/postinstall.js (1 hunks)
🔇 Additional comments (7)
packages/csharp/postinstall.js (1)

1-4: LGTM: Well-structured postinstall script.

The script correctly imports and uses the postinstall function from the @ast-grep/setup-lang package, providing the current directory as needed for proper language setup.

packages/csharp/index.d.ts (1)

1-7: LGTM: Clear type definition for language registration.

The LanguageRegistration type is well-defined with appropriate properties for language integration.

packages/csharp/nursery.js (1)

1-3: LGTM: Good setup for testing environment.

The imports correctly set up the nursery testing framework and language registration.

packages/csharp/README.md (1)

1-25: LGTM: Well-structured documentation.

The README provides clear installation and usage instructions, covering all necessary steps and dependencies. The example code is concise and demonstrates the core functionality effectively.

packages/csharp/package.json (3)

13-20: LGTM for package files configuration.

The files list includes all necessary components for the package to function correctly.


24-47: LGTM for dependency configuration and publishing setup.

The dependency configuration is appropriate, with proper versioning and optional peer dependencies. The publishing configuration is also properly set up for npm.


23-23:

✅ Verification successful

Verify license compatibility.

Ensure the ISC license is consistent with the licensing approach used in the rest of the ast-grep project.


🏁 Script executed:

#!/bin/bash
# Check license consistency across ast-grep packages
echo "Checking licenses in other ast-grep packages..."
find packages -name package.json -not -path "*/node_modules/*" | xargs grep -l "license" | xargs grep "license"

Length of output: 450


License Consistency Confirmed
The ISC license is used uniformly across all ast-grep packages, including in packages/csharp/package.json (line 23). No changes are needed.

Comment on lines +9 to +10
declare const registratoin: LanguageRegistration
export default registratoin
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in variable name.

There's a typo in the variable name registratoin (should be registration). This impacts not only this file but also any imports that use this package.

-declare const registratoin: LanguageRegistration
-export default registratoin
+declare const registration: LanguageRegistration
+export default registration
📝 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
declare const registratoin: LanguageRegistration
export default registratoin
declare const registration: LanguageRegistration
export default registration

Comment on lines +4 to +15
setup({
dirname: __dirname,
name: 'CSharp',
treeSitterPackage: 'tree-sitter-c-sharp',
languageRegistration,
testRunner: (parse) => {
const sg = parse('var a = 123;')
const root = sg.root()
const node = root.find('var $A = 123')
assert.equal(node.kind(), 'variable_declaration')
}
})
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Missing import for assert.

The test runner uses assert but doesn't import it. Add the import statement to fix this issue.

const { setup } = require('@ast-grep/nursery')
const languageRegistration = require('./index')
+const assert = require('assert')

setup({
  dirname: __dirname,
  name: 'CSharp',
  treeSitterPackage: 'tree-sitter-c-sharp',
  languageRegistration,
  testRunner: (parse) => {
    const sg = parse('var a = 123;')
    const root = sg.root()
    const node = root.find('var $A = 123')
    assert.equal(node.kind(), 'variable_declaration')
  }
})
📝 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
setup({
dirname: __dirname,
name: 'CSharp',
treeSitterPackage: 'tree-sitter-c-sharp',
languageRegistration,
testRunner: (parse) => {
const sg = parse('var a = 123;')
const root = sg.root()
const node = root.find('var $A = 123')
assert.equal(node.kind(), 'variable_declaration')
}
})
const { setup } = require('@ast-grep/nursery')
const languageRegistration = require('./index')
const assert = require('assert')
setup({
dirname: __dirname,
name: 'CSharp',
treeSitterPackage: 'tree-sitter-c-sharp',
languageRegistration,
testRunner: (parse) => {
const sg = parse('var a = 123;')
const root = sg.root()
const node = root.find('var $A = 123')
assert.equal(node.kind(), 'variable_declaration')
}
})

{
"name": "@ast-grep/lang-csharp",
"version": "0.0.1",
"description": "",
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 empty. Adding a clear description will improve package discoverability and help users understand its purpose.

-  "description": "",
+  "description": "C# language support for ast-grep",
📝 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
"description": "",
"description": "C# language support for ast-grep",

@HerringtonDarkholme
Copy link
Member

Csharp released

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