Skip to content

✨ Kotlin #55

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 3 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-badgers-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ast-grep/lang-kotlin": patch
---

Add @ast-grep/lang-kotlin
24 changes: 24 additions & 0 deletions packages/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ast-grep napi language for kotlin

## Installation

In a pnpm project, run:

```bash
pnpm install @ast-grep/lang-kotlin
pnpm install @ast-grep/napi
# install the tree-sitter-cli if no prebuild is available
pnpm install @tree-sitter/cli --save-dev
```

## Usage

```js
import kotlin from '@ast-grep/lang-kotlin'
import { registerDynamicLanguage, parse } from '@ast-grep/napi'

registerDynamicLanguage({ kotlin })

const sg = parse('kotlin', `your code`)
sg.root().kind()
```
10 changes: 10 additions & 0 deletions packages/kotlin/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type LanguageRegistration = {
libraryPath: string
extensions: string[]
languageSymbol?: string
metaVarChar?: string
expandoChar?: string
}

declare const registration: LanguageRegistration
export default registration
9 changes: 9 additions & 0 deletions packages/kotlin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('node:path')
const libPath = path.join(__dirname, 'parser.so')

module.exports = {
libraryPath: libPath,
extensions: ['kt', 'ktm', 'kts'],
languageSymbol: 'tree_sitter_kotlin',
expandoChar: 'µ',
}
16 changes: 16 additions & 0 deletions packages/kotlin/nursery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { setup } = require('@ast-grep/nursery')
const assert = require('node:assert')
const languageRegistration = require('./index')

setup({
dirname: __dirname,
name: 'kotlin',
treeSitterPackage: 'tree-sitter-kotlin',
languageRegistration,
testRunner: parse => {
const sg = parse('println(123)')
const root = sg.root()
const node = root.find('println($A)')
assert.equal(node.kind(), 'call_expression')
},
Comment on lines +10 to +15
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Test coverage is minimal.

The test only verifies a simple println(123) statement. While this is a good starting point, it would be more robust to test additional Kotlin language constructs like:

  • Class and function declarations
  • Control flow structures (if/else, loops)
  • Lambda expressions
  • Type declarations and generics
  • String interpolation

Expand the test suite to cover more Kotlin syntax patterns to ensure the parser is robust across various language features.

})
47 changes: 47 additions & 0 deletions packages/kotlin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@ast-grep/lang-kotlin",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"build": "tree-sitter build -o parser.so",
"source": "node nursery.js source",
"prepublishOnly": "node nursery.js source",
"postinstall": "node postinstall.js",
"test": "node nursery.js test"
},
"files": [
"index.js",
"index.d.ts",
"type.d.ts",
"postinstall.js",
"src",
"prebuilds"
],
"keywords": ["ast-grep"],
"author": "",
"license": "ISC",
"dependencies": {
"@ast-grep/setup-lang": "0.0.3"
},
"peerDependencies": {
"tree-sitter-cli": "0.24.6"
},
"peerDependenciesMeta": {
"tree-sitter-cli": {
"optional": true
}
},
"devDependencies": {
"@ast-grep/nursery": "0.0.2",
"tree-sitter-cli": "0.24.6",
"tree-sitter-kotlin": "0.3.8"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"pnpm": {
"onlyBuiltDependencies": ["@ast-grep/lang-kotlin", "tree-sitter-cli"]
}
}
Comment on lines +1 to +47
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Missing index.js and other referenced files.

The package.json references several important files that weren't included in this review:

  • index.js (main entry point)
  • index.d.ts (TypeScript definitions)
  • type.d.ts
  • postinstall.js

🏁 Script executed:

#!/bin/bash
# Check for the existence of important files
echo "Checking for required files in the packages/kotlin directory:"
for file in index.js index.d.ts type.d.ts postinstall.js
do
  if [ -f "packages/kotlin/$file" ]; then
    echo "$file exists"
  else
    echo "$file is missing"
  fi
done

Length of output: 735


Action Required: Missing File Discrepancy in Package

After verification, only type.d.ts is missing, while index.js, index.d.ts, and postinstall.js are all present. Please add the missing type.d.ts file to the packages/kotlin directory, or update the files field in package.json if this file is not needed.

4 changes: 4 additions & 0 deletions packages/kotlin/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { postinstall } = require('@ast-grep/setup-lang')
postinstall({
dirname: __dirname,
})
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.