Skip to content

add yaml language support #73

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
Apr 22, 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
24 changes: 24 additions & 0 deletions packages/yaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ast-grep napi language for yaml

## Installation

In a pnpm project, run:

```bash
pnpm install @ast-grep/lang-yaml
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 yaml from '@ast-grep/lang-yaml'
import { registerDynamicLanguage, parse } from '@ast-grep/napi'

registerDynamicLanguage({ yaml })

const sg = parse('yaml', `your code`)
sg.root().kind()
```
10 changes: 10 additions & 0 deletions packages/yaml/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/yaml/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: ['yaml', 'yml'],
languageSymbol: 'tree_sitter_yaml',
expandoChar: '$',
}
53 changes: 53 additions & 0 deletions packages/yaml/nursery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { setup } = require('@ast-grep/nursery')
const assert = require('node:assert')
const languageRegistration = require('./index')

setup({
dirname: __dirname,
name: 'yaml',
treeSitterPackage: '@tree-sitter-grammars/tree-sitter-yaml',
languageRegistration,
testRunner: parse => {
// Based on test_yaml_str
let sg = parse('123')
let node = sg.root().find('123')
assert.ok(node, 'should match simple scalar')
node = sg.root().find("'123'")
assert.ok(!node, 'should not match quoted scalar')

// Based on test_yaml_pattern
sg = parse('foo: 123')
node = sg.root().find('foo: $BAR')
assert.ok(node, 'should match key-value with metavariable')
let match = node.getMatch('BAR')
assert.equal(match.text(), '123')

sg = parse('foo: [1, 2, 3]')
node = sg.root().find('foo: $$$')
assert.ok(node, 'should match key-list with multi-metavariable')

sg = parse(`foo:
- a`)
node = sg.root().find('foo: $BAR')
assert.ok(node, 'should match key-sequence with metavariable')
match = node.getMatch('BAR')
assert.equal(match.kind(), 'block_node') // Updated expected kind

sg = parse('bar: bar')
node = sg.root().find('foo: $BAR')
assert.ok(!node, 'should not match incorrect key')

// Based on test_yaml_replace (testing find, as replace is not directly available)
const SOURCE = `
key: value
list:
- item1
- item2
`
sg = parse(SOURCE)
node = sg.root().find('$KEY: value')
assert.ok(node, 'should find key-value pair for replacement test')
match = node.getMatch('KEY')
assert.equal(match.text(), 'key')
},
})
47 changes: 47 additions & 0 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@ast-grep/lang-yaml",
"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", "ast-grep-lang"],
"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-grammars/tree-sitter-yaml": "0.7.0",
"tree-sitter-cli": "0.24.6"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"pnpm": {
"onlyBuiltDependencies": ["@ast-grep/lang-yaml", "tree-sitter-cli"]
}
}
4 changes: 4 additions & 0 deletions packages/yaml/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,
})
Loading