-
Notifications
You must be signed in to change notification settings - Fork 6
✨ Ruby #57
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
✨ Ruby #57
Conversation
WalkthroughThis pull request introduces a Ruby language package for ast-grep. It adds documentation detailing installation and usage, type definitions and configuration exports for Ruby parser settings, a testing setup using the nursery package, package metadata including build and publish scripts, and a post-installation script. These changes enable dynamic registration of Ruby language support via tree-sitter and NAPI integration. Changes
Sequence Diagram(s)sequenceDiagram
participant CLI as Command Line (pnpm)
participant Setup as Postinstall Script
participant Reg as Ruby Registration
participant Parser as Tree-sitter Parser
CLI->>Setup: Execute postinstall.js
Setup->>Reg: Call setup with __dirname
Reg-->>Setup: Return registration config (libraryPath, extensions, etc.)
CLI->>Parser: Load Ruby parser via registration config
Parser-->>CLI: Ruby language support activated
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (7)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (6)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (7)
packages/ruby/postinstall.js (1)
1-4
: Looks good but could benefit from error handlingThe postinstall script correctly imports the necessary function and passes the current directory. However, it lacks error handling which could be useful for troubleshooting installation issues.
Consider adding a try-catch block to handle potential errors during installation:
const { postinstall } = require('@ast-grep/setup-lang') -postinstall({ - dirname: __dirname, -}) +try { + postinstall({ + dirname: __dirname, + }) +} catch (error) { + console.error('Failed to complete Ruby language setup:', error) + process.exit(1) +}packages/ruby/index.d.ts (1)
1-7
: Add JSDoc comments to improve documentationThe type definition is clear and well-structured, but adding JSDoc comments would make it more maintainable and help developers understand the purpose of each property.
Consider adding descriptive JSDoc comments:
+/** + * Configuration for registering a language with ast-grep + */ type LanguageRegistration = { + /** Path to the compiled tree-sitter language library */ libraryPath: string + /** File extensions associated with this language */ extensions: string[] + /** Symbol used to identify the language in tree-sitter */ languageSymbol?: string + /** Character used for meta variables in patterns */ metaVarChar?: string + /** Character used for expandable wildcards in patterns */ expandoChar?: string }packages/ruby/nursery.js (1)
10-15
: Expand test coverage for more Ruby syntax elementsThe current test case verifies basic functionality with a simple method call, which is a good start. However, adding more test cases would ensure broader syntax coverage.
Consider extending the test coverage with additional Ruby-specific syntax:
testRunner: parse => { const sg = parse('Foo::bar()') const root = sg.root() const node = root.find('Foo::$METHOD()') assert.equal(node.kind(), 'call') + + // Test Ruby class definition + const classExample = parse('class MyClass < BaseClass; def method; end; end') + const classNode = classExample.root().find('class $CLASS < $PARENT; $$$; end') + assert.equal(classNode.kind(), 'class') + + // Test Ruby blocks + const blockExample = parse('[1, 2, 3].each { |num| puts num }') + const blockNode = blockExample.root().find('$ARRAY.each { |$VAR| $$$ }') + assert.equal(blockNode.kind(), 'call') },packages/ruby/README.md (2)
14-24
: Enhance usage examples with Ruby-specific patternsThe usage example shows the basic setup, but adding examples of Ruby-specific pattern matching would be more helpful for users.
Expand the usage examples to showcase Ruby syntax patterns:
## Usage ```js import ruby from '@ast-grep/lang-ruby' import { registerDynamicLanguage, parse } from '@ast-grep/napi' registerDynamicLanguage({ ruby }) const sg = parse('ruby', `your code`) sg.root().kind() + +// Examples of Ruby-specific pattern matching + +// Find class definitions +const rubyCode = ` +class User < ApplicationRecord + has_many :posts + + def full_name + "#{first_name} #{last_name}" + end +end +` + +const sgRuby = parse('ruby', rubyCode) + +// Find class inheritance +const classNode = sgRuby.root().find('class $CLASS < $PARENT; $$$; end') +console.log(classNode.getText()) // Outputs the entire class + +// Find method definitions +const methodNode = sgRuby.root().find('def $METHOD; $$$; end') +console.log(methodNode.getText()) // Outputs the method definition
7-12
: Mention alternative package managersThe installation instructions only mention pnpm, but users might be using npm or yarn.
Include alternative package manager commands:
## Installation -In a pnpm project, run: +Install using your preferred package manager: ```bash +# Using pnpm pnpm install @ast-grep/lang-ruby pnpm install @ast-grep/napi # install the tree-sitter-cli if no prebuild is available pnpm install @tree-sitter/cli --save-dev + +# Using npm +npm install @ast-grep/lang-ruby +npm install @ast-grep/napi +# install the tree-sitter-cli if no prebuild is available +npm install @tree-sitter/cli --save-dev + +# Using yarn +yarn add @ast-grep/lang-ruby +yarn add @ast-grep/napi +# install the tree-sitter-cli if no prebuild is available +yarn add @tree-sitter/cli --devpackages/ruby/index.js (1)
4-9
: Clear Configuration and ExportsThe exported configuration object is well structured and clearly defines the properties required for parsing Ruby files. Consider adding a brief comment explaining the purpose of the
expandoChar: 'µ'
property to aid future maintainers.packages/ruby/package.json (1)
1-5
: Basic Package MetadataThe initial metadata (name, version, main) is defined correctly. For improved clarity in the package listing, consider adding a meaningful description and author information.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (6)
packages/ruby/README.md
(1 hunks)packages/ruby/index.d.ts
(1 hunks)packages/ruby/index.js
(1 hunks)packages/ruby/nursery.js
(1 hunks)packages/ruby/package.json
(1 hunks)packages/ruby/postinstall.js
(1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/ruby/postinstall.js (1)
packages/ruby/nursery.js (1)
require
(1-1)
🔇 Additional comments (8)
packages/ruby/index.js (1)
1-2
: Clean Import and Library Path CalculationThe use of the
node:path
module andpath.join(__dirname, 'parser.so')
is clear and follows modern Node.js practices.packages/ruby/package.json (7)
6-12
: Scripts ConfigurationThe defined scripts (build, source, prepublishOnly, postinstall, and test) clearly outline the package lifecycle. Ensure that all referenced scripts (such as
nursery.js
andpostinstall.js
) exist and are maintained for smooth operation.
13-20
: Files InclusionThe "files" array correctly includes the essential components for the package. Please verify that excluding files like
nursery.js
is intentional and that they are only used during development.
21-29
: Dependencies and Peer DependenciesDependencies and peer dependencies are clearly defined. Double-check that the specified versions (especially for
tree-sitter-cli
, which is referenced in both peer and dev dependencies) are compatible with your build and test processes.
30-34
: Optional Peer Dependency ConfigurationThe configuration in
peerDependenciesMeta
correctly markstree-sitter-cli
as optional, which helps avoid installation issues if it’s not present.
35-39
: Development DependenciesThe devDependencies are appropriately set for testing and development. This ensures that the necessary tools (including the nursery runner and the Ruby parser) are available during development.
40-43
: Publish ConfigurationThe publishConfig section is properly configured to ensure public access and correct registry targeting during publication.
44-46
: pnpm ConfigurationThe
pnpm
configuration specifiesonlyBuiltDependencies
, which is useful if you intend to only include certain dependencies in the build. Please verify that this option is fully supported by your current pnpm version.
6e926f6
to
0738ba8
Compare
Summary by CodeRabbit