Skip to content
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/cyan-crabs-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ast-grep/lang-tsx": patch
---

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

## Installation

In a pnpm project, run:

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

registerDynamicLanguage({ tsx })

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

setup({
dirname: __dirname,
name: 'tsx',
treeSitterPackage: 'tree-sitter-typescript',
src: path.join('tsx', 'src'),
languageRegistration,
testRunner: parse => {
const sg = parse('<Button onClick={() => foo()}>Click me</Button>')
const root = sg.root()
const node = root.find('<$ELEMENT onClick={$HANDLER}>$$$</$ELEMENT>')
assert.equal(node.kind(), 'jsx_element')
},
})
47 changes: 47 additions & 0 deletions packages/tsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "",
"main": "index.js",
Comment on lines +1 to +5
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add a meaningful package description.

The description field is currently empty, which isn't helpful for users discovering this package.

{
  "name": "@ast-grep/lang-tsx",
  "version": "0.0.1",
-  "description": "",
+  "description": "TypeScript JSX (TSX) language support for ast-grep",
  "main": "index.js",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "",
"main": "index.js",
{
"name": "@ast-grep/lang-tsx",
"version": "0.0.1",
"description": "TypeScript JSX (TSX) language support for ast-grep",
"main": "index.js",

"scripts": {
"build": "tree-sitter build -o parser.so ./node_modules/tree-sitter-typescript/tsx",
"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.3",
"tree-sitter-cli": "0.24.6",
"tree-sitter-typescript": "^0.23.2"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"pnpm": {
"onlyBuiltDependencies": ["@ast-grep/lang-tsx", "tree-sitter-cli"]
}
}
4 changes: 4 additions & 0 deletions packages/tsx/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,
})
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

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