diff --git a/.changeset/smart-moose-sniff.md b/.changeset/smart-moose-sniff.md new file mode 100644 index 0000000..af76ba9 --- /dev/null +++ b/.changeset/smart-moose-sniff.md @@ -0,0 +1,5 @@ +--- +"@ast-grep/lang-json": patch +--- + +Add @ast-grep/lang-json diff --git a/packages/json/README.md b/packages/json/README.md new file mode 100644 index 0000000..b904e83 --- /dev/null +++ b/packages/json/README.md @@ -0,0 +1,24 @@ +# ast-grep napi language for json + +## Installation + +In a pnpm project, run: + +```bash +pnpm install @ast-grep/lang-json +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 json from '@ast-grep/lang-json' +import { registerDynamicLanguage, parse } from '@ast-grep/napi' + +registerDynamicLanguage({ json }) + +const sg = parse('json', `your code`) +sg.root().kind() +``` diff --git a/packages/json/index.d.ts b/packages/json/index.d.ts new file mode 100644 index 0000000..11140a6 --- /dev/null +++ b/packages/json/index.d.ts @@ -0,0 +1,10 @@ +type LanguageRegistration = { + libraryPath: string + extensions: string[] + languageSymbol?: string + metaVarChar?: string + expandoChar?: string +} + +declare const registration: LanguageRegistration +export default registration diff --git a/packages/json/index.js b/packages/json/index.js new file mode 100644 index 0000000..e8507b0 --- /dev/null +++ b/packages/json/index.js @@ -0,0 +1,9 @@ +const path = require('node:path') +const libPath = path.join(__dirname, 'parser.so') + +module.exports = { + libraryPath: libPath, + extensions: ['json'], + languageSymbol: 'tree_sitter_json', + expandoChar: '$', +} diff --git a/packages/json/nursery.js b/packages/json/nursery.js new file mode 100644 index 0000000..6e0890b --- /dev/null +++ b/packages/json/nursery.js @@ -0,0 +1,16 @@ +const { setup } = require('@ast-grep/nursery') +const assert = require('node:assert') +const languageRegistration = require('./index') + +setup({ + dirname: __dirname, + name: 'json', + treeSitterPackage: 'tree-sitter-json', + languageRegistration, + testRunner: parse => { + const sg = parse('{"abc": 123}') + const root = sg.root() + const node = root.find('{ $$$ }') + assert.equal(node.kind(), 'object') + }, +}) diff --git a/packages/json/package.json b/packages/json/package.json new file mode 100644 index 0000000..3737399 --- /dev/null +++ b/packages/json/package.json @@ -0,0 +1,47 @@ +{ + "name": "@ast-grep/lang-json", + "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-json": "0.24.8" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + }, + "pnpm": { + "onlyBuiltDependencies": ["@ast-grep/lang-json", "tree-sitter-cli"] + } +} diff --git a/packages/json/postinstall.js b/packages/json/postinstall.js new file mode 100644 index 0000000..b9141ff --- /dev/null +++ b/packages/json/postinstall.js @@ -0,0 +1,4 @@ +const { postinstall } = require('@ast-grep/setup-lang') +postinstall({ + dirname: __dirname, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b5ba823..1b0869f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -226,6 +226,22 @@ importers: specifier: 0.23.1 version: 0.23.1(tree-sitter@0.21.1) + packages/json: + dependencies: + '@ast-grep/setup-lang': + specifier: 0.0.3 + version: 0.0.3 + devDependencies: + '@ast-grep/nursery': + specifier: 0.0.2 + version: 0.0.2 + tree-sitter-cli: + specifier: 0.24.6 + version: 0.24.6 + tree-sitter-json: + specifier: 0.24.8 + version: 0.24.8(tree-sitter@0.21.1) + packages/python: dependencies: '@ast-grep/setup-lang': @@ -1075,6 +1091,14 @@ packages: tree-sitter: optional: true + tree-sitter-json@0.24.8: + resolution: {integrity: sha512-Tc9ZZYwHyWZ3Tt1VEw7Pa2scu1YO7/d2BCBbKTx5hXwig3UfdQjsOPkPyLpDJOn/m1UBEWYAtSdGAwCSyagBqQ==} + peerDependencies: + tree-sitter: ^0.21.1 + peerDependenciesMeta: + tree-sitter: + optional: true + tree-sitter-python@0.23.6: resolution: {integrity: sha512-yIM9z0oxKIxT7bAtPOhgoVl6gTXlmlIhue7liFT4oBPF/lha7Ha4dQBS82Av6hMMRZoVnFJI8M6mL+SwWoLD3A==} peerDependencies: @@ -1799,6 +1823,13 @@ snapshots: optionalDependencies: tree-sitter: 0.21.1 + tree-sitter-json@0.24.8(tree-sitter@0.21.1): + dependencies: + node-addon-api: 8.3.1 + node-gyp-build: 4.8.4 + optionalDependencies: + tree-sitter: 0.21.1 + tree-sitter-python@0.23.6: dependencies: node-addon-api: 8.3.0