Skip to content

Commit fea52a8

Browse files
authored
add yaml language support (ast-grep#73)
1 parent 27550fe commit fea52a8

File tree

7 files changed

+280
-0
lines changed

7 files changed

+280
-0
lines changed

packages/yaml/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ast-grep napi language for yaml
2+
3+
## Installation
4+
5+
In a pnpm project, run:
6+
7+
```bash
8+
pnpm install @ast-grep/lang-yaml
9+
pnpm install @ast-grep/napi
10+
# install the tree-sitter-cli if no prebuild is available
11+
pnpm install @tree-sitter/cli --save-dev
12+
```
13+
14+
## Usage
15+
16+
```js
17+
import yaml from '@ast-grep/lang-yaml'
18+
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
19+
20+
registerDynamicLanguage({ yaml })
21+
22+
const sg = parse('yaml', `your code`)
23+
sg.root().kind()
24+
```

packages/yaml/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type LanguageRegistration = {
2+
libraryPath: string
3+
extensions: string[]
4+
languageSymbol?: string
5+
metaVarChar?: string
6+
expandoChar?: string
7+
}
8+
9+
declare const registration: LanguageRegistration
10+
export default registration

packages/yaml/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('node:path')
2+
const libPath = path.join(__dirname, 'parser.so')
3+
4+
module.exports = {
5+
libraryPath: libPath,
6+
extensions: ['yaml', 'yml'],
7+
languageSymbol: 'tree_sitter_yaml',
8+
expandoChar: '$',
9+
}

packages/yaml/nursery.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { setup } = require('@ast-grep/nursery')
2+
const assert = require('node:assert')
3+
const languageRegistration = require('./index')
4+
5+
setup({
6+
dirname: __dirname,
7+
name: 'yaml',
8+
treeSitterPackage: '@tree-sitter-grammars/tree-sitter-yaml',
9+
languageRegistration,
10+
testRunner: parse => {
11+
// Based on test_yaml_str
12+
let sg = parse('123')
13+
let node = sg.root().find('123')
14+
assert.ok(node, 'should match simple scalar')
15+
node = sg.root().find("'123'")
16+
assert.ok(!node, 'should not match quoted scalar')
17+
18+
// Based on test_yaml_pattern
19+
sg = parse('foo: 123')
20+
node = sg.root().find('foo: $BAR')
21+
assert.ok(node, 'should match key-value with metavariable')
22+
let match = node.getMatch('BAR')
23+
assert.equal(match.text(), '123')
24+
25+
sg = parse('foo: [1, 2, 3]')
26+
node = sg.root().find('foo: $$$')
27+
assert.ok(node, 'should match key-list with multi-metavariable')
28+
29+
sg = parse(`foo:
30+
- a`)
31+
node = sg.root().find('foo: $BAR')
32+
assert.ok(node, 'should match key-sequence with metavariable')
33+
match = node.getMatch('BAR')
34+
assert.equal(match.kind(), 'block_node') // Updated expected kind
35+
36+
sg = parse('bar: bar')
37+
node = sg.root().find('foo: $BAR')
38+
assert.ok(!node, 'should not match incorrect key')
39+
40+
// Based on test_yaml_replace (testing find, as replace is not directly available)
41+
const SOURCE = `
42+
key: value
43+
list:
44+
- item1
45+
- item2
46+
`
47+
sg = parse(SOURCE)
48+
node = sg.root().find('$KEY: value')
49+
assert.ok(node, 'should find key-value pair for replacement test')
50+
match = node.getMatch('KEY')
51+
assert.equal(match.text(), 'key')
52+
},
53+
})

packages/yaml/package.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@ast-grep/lang-yaml",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tree-sitter build -o parser.so",
8+
"source": "node nursery.js source",
9+
"prepublishOnly": "node nursery.js source",
10+
"postinstall": "node postinstall.js",
11+
"test": "node nursery.js test"
12+
},
13+
"files": [
14+
"index.js",
15+
"index.d.ts",
16+
"type.d.ts",
17+
"postinstall.js",
18+
"src",
19+
"prebuilds"
20+
],
21+
"keywords": ["ast-grep", "ast-grep-lang"],
22+
"author": "",
23+
"license": "ISC",
24+
"dependencies": {
25+
"@ast-grep/setup-lang": "0.0.3"
26+
},
27+
"peerDependencies": {
28+
"tree-sitter-cli": "0.24.6"
29+
},
30+
"peerDependenciesMeta": {
31+
"tree-sitter-cli": {
32+
"optional": true
33+
}
34+
},
35+
"devDependencies": {
36+
"@ast-grep/nursery": "0.0.2",
37+
"@tree-sitter-grammars/tree-sitter-yaml": "0.7.0",
38+
"tree-sitter-cli": "0.24.6"
39+
},
40+
"publishConfig": {
41+
"access": "public",
42+
"registry": "https://registry.npmjs.org/"
43+
},
44+
"pnpm": {
45+
"onlyBuiltDependencies": ["@ast-grep/lang-yaml", "tree-sitter-cli"]
46+
}
47+
}

packages/yaml/postinstall.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { postinstall } = require('@ast-grep/setup-lang')
2+
postinstall({
3+
dirname: __dirname,
4+
})

0 commit comments

Comments
 (0)