-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '$', | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const { postinstall } = require('@ast-grep/setup-lang') | ||
postinstall({ | ||
dirname: __dirname, | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.