Skip to content

🎉 @ast-grep/all-langs #25

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/green-turtles-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ast-grep/all-langs": patch
---

New package: @ast-grep/all-langs
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: ">= 21"
- name: Install dependencies
run: pnpm install

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: ">= 21"
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: pnpm install
Expand Down Expand Up @@ -74,6 +75,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: ">= 21"
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: pnpm install
Expand Down
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["biomejs.biome"]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[javascript][json][jsonc][typescript]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.minimap.maxColumn": 80
}
}
1 change: 1 addition & 0 deletions packages/all/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!src
71 changes: 71 additions & 0 deletions packages/all/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@ast-grep/all-langs",
"version": "0.0.0",
"description": "Exports all officially supported languages.",
"keywords": ["ast-grep"],
"homepage": "https://github.com/ast-grep/langs/tree/main/packages/all",
"bugs": {
"url": "https://github.com/ast-grep/langs/issues"
},
"license": "ISC",
"author": {
"name": "CodeRabbit",
"url": "https://www.coderabbit.ai"
},
"files": ["dist", "!dist/**/*.test.*"],
"main": "dist/index.js",
"repository": "github:ast-grep/langs",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist docs node_modules tsconfig.tsbuildinfo",
"format": "biome format --write",
"format:ci": "biome format",
"lint": "biome lint --fix",
"lint:ci": "biome lint",
"pretest": "tsc",
"test": "node --test './dist/**/*.test.js'"
},
"dependencies": {
"@ast-grep/lang-angular": "workspace:*",
"@ast-grep/lang-bash": "workspace:*",
"@ast-grep/lang-c": "workspace:*",
"@ast-grep/lang-cpp": "workspace:*",
"@ast-grep/lang-csharp": "workspace:*",
"@ast-grep/lang-css": "workspace:*",
"@ast-grep/lang-dart": "workspace:*",
"@ast-grep/lang-elixir": "workspace:*",
"@ast-grep/lang-go": "workspace:*",
"@ast-grep/lang-haskell": "workspace:*",
"@ast-grep/lang-html": "workspace:*",
"@ast-grep/lang-java": "workspace:*",
"@ast-grep/lang-javascript": "workspace:*",
"@ast-grep/lang-json": "workspace:*",
"@ast-grep/lang-kotlin": "workspace:*",
"@ast-grep/lang-lua": "workspace:*",
"@ast-grep/lang-php": "workspace:*",
"@ast-grep/lang-python": "workspace:*",
"@ast-grep/lang-ruby": "workspace:*",
"@ast-grep/lang-rust": "workspace:*",
"@ast-grep/lang-scala": "workspace:*",
"@ast-grep/lang-sql": "workspace:*",
"@ast-grep/lang-swift": "workspace:*",
"@ast-grep/lang-toml": "workspace:*",
"@ast-grep/lang-tsx": "workspace:*",
"@ast-grep/lang-typescript": "workspace:*",
"@ast-grep/lang-yaml": "workspace:*",
"@ast-grep/napi": "^0.37.0"
},
"devDependencies": {
"@types/node": "22.14.1",
"typescript": "^5.8.3"
},
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"types": "dist/index.d.ts",
"module": "dist/index.js"
}
2 changes: 2 additions & 0 deletions packages/all/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./lang.ts"
export * from "./langs.ts"
41 changes: 41 additions & 0 deletions packages/all/src/lang.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Lang as LangNapi } from "@ast-grep/napi"
import test, { describe } from "node:test"
import { Lang } from "./lang.ts"

/** Languages supported in `@ast-grep/[email protected]`. */
const previous = Object.freeze({
Html: "Html",
JavaScript: "JavaScript",
Tsx: "Tsx",
Css: "Css",
TypeScript: "TypeScript",
Bash: "Bash",
C: "C",
Cpp: "Cpp",
CSharp: "CSharp",
Go: "Go",
Elixir: "Elixir",
Haskell: "Haskell",
Java: "Java",
Json: "Json",
Kotlin: "Kotlin",
Lua: "Lua",
Php: "Php",
Python: "Python",
Ruby: "Ruby",
Rust: "Rust",
Scala: "Scala",
Sql: "Sql",
Swift: "Swift",
Yaml: 'Yaml',
})

describe("Lang", () => {
test("The new enum is compatible with the old one", ({ assert }) => {
for (const lang of Object.values(previous)) assert.equal(Lang[lang], lang)
})

test("The new enum is compatible with the built-in ones", ({ assert }) => {
for (const lang of Object.values(LangNapi)) assert.equal(Lang[lang], lang)
})
})
37 changes: 37 additions & 0 deletions packages/all/src/lang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* An enum of all languages supported by `@ast-grep/langs`.
*/
export const Lang = {
Angular: "Angular",
Bash: "Bash",
C: "C",
Cpp: "Cpp",
CSharp: "CSharp",
Css: "Css",
Dart: "Dart",
Elixir: "Elixir",
Go: "Go",
Haskell: "Haskell",
Html: "Html",
Java: "Java",
JavaScript: "JavaScript",
Json: "Json",
Kotlin: "Kotlin",
Lua: "Lua",
Php: "Php",
Python: "Python",
Ruby: "Ruby",
Rust: "Rust",
Scala: "Scala",
Sql: "Sql",
Swift: "Swift",
Toml: "Toml",
Tsx: "Tsx",
TypeScript: "TypeScript",
Yaml: "Yaml",
} as const

/**
* An enum of all languages supported by `@ast-grep/langs`.
*/
export type Lang = (typeof Lang)[keyof typeof Lang]
31 changes: 31 additions & 0 deletions packages/all/src/langs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { parse, registerDynamicLanguage } from "@ast-grep/napi"
import { before, describe, test } from "node:test"
import { Lang } from "./lang.ts"
import { langs } from "./langs.ts"

describe("langs", () => {
// @ts-expect-error Type `StaticLangRegistration` is missing the following
// properties from type `LangRegistration`: `libraryPath`, `extensions`.
before(() => registerDynamicLanguage(langs))

// A newly supported language
test(Lang.Dart, ({ assert }) => {
const sg = parse(Lang.Dart, 'var x = "Hello, world!";"')
const kind = sg.root().kind()
assert.equal(kind, "program")
})

// A previously supported language
test(Lang.Go, ({ assert }) => {
const sg = parse(Lang.Go, 'x := "Hello, world!"')
const kind = sg.root().kind()
assert.equal(kind, "source_file")
})

// A built-in language
test(Lang.TypeScript, ({ assert }) => {
const sg = parse(Lang.TypeScript, 'const x = "Hello, world!"')
const kind = sg.root().kind()
assert.equal(kind, "program")
})
})
128 changes: 128 additions & 0 deletions packages/all/src/langs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import angular from "@ast-grep/lang-angular"
import bash from "@ast-grep/lang-bash"
import c from "@ast-grep/lang-c"
import cpp from "@ast-grep/lang-cpp"
import csharp from "@ast-grep/lang-csharp"
import css from "@ast-grep/lang-css"
import dart from "@ast-grep/lang-dart"
import elixir from "@ast-grep/lang-elixir"
import go from "@ast-grep/lang-go"
import haskell from "@ast-grep/lang-haskell"
import html from "@ast-grep/lang-html"
import java from "@ast-grep/lang-java"
import javascript from "@ast-grep/lang-javascript"
import json from "@ast-grep/lang-json"
import kotlin from "@ast-grep/lang-kotlin"
import lua from "@ast-grep/lang-lua"
import php from "@ast-grep/lang-php"
import python from "@ast-grep/lang-python"
import ruby from "@ast-grep/lang-ruby"
import rust from "@ast-grep/lang-rust"
import scala from "@ast-grep/lang-scala"
import sql from "@ast-grep/lang-sql"
import swift from "@ast-grep/lang-swift"
import toml from "@ast-grep/lang-toml"
import tsx from "@ast-grep/lang-tsx"
import typescript from "@ast-grep/lang-typescript"
import yaml from "@ast-grep/lang-yaml"
import type {
DynamicLangRegistrations,
LangRegistration,
} from "@ast-grep/napi/types/registerDynamicLang.js"
import { Lang } from "./lang.ts"

type StaticLangRegistration =
| typeof angular
| typeof bash
| typeof c
| typeof cpp
| typeof csharp
| typeof css
| typeof dart
| typeof elixir
| typeof go
| typeof haskell
| typeof html
| typeof java
| typeof javascript
| typeof json
| typeof kotlin
| typeof lua
| typeof php
| typeof python
| typeof ruby
| typeof rust
| typeof scala
| typeof sql
| typeof swift
| typeof toml
| typeof tsx
| typeof typescript
| typeof yaml
| LangRegistration

interface StaticLangRegistrations {
Angular: typeof angular
Bash: typeof bash
C: typeof c
Cpp: typeof cpp
CSharp: typeof csharp
Css: typeof css
Dart: typeof dart
Elixir: typeof elixir
Go: typeof go
Haskell: typeof haskell
Html: typeof html
Java: typeof java
JavaScript: typeof javascript
Json: typeof json
Kotlin: typeof kotlin
Lua: typeof lua
Php: typeof php
Python: typeof python
Ruby: typeof ruby
Rust: typeof rust
Scala: typeof scala
Sql: typeof sql
Swift: typeof swift
Toml: typeof toml
Tsx: typeof tsx
TypeScript: typeof typescript
Yaml: typeof yaml

/** To satisfy {@link DynamicLangRegistrations}. */
[langName: string]: StaticLangRegistration
}

/**
* Built-in dynamic languages supported by `@ast-grep/langs`.
*/
export const langs: StaticLangRegistrations = {
[Lang.Angular]: angular,
[Lang.Bash]: bash,
[Lang.C]: c,
[Lang.Cpp]: cpp,
[Lang.CSharp]: csharp,
[Lang.Css]: css,
[Lang.Dart]: dart,
[Lang.Elixir]: elixir,
[Lang.Go]: go,
[Lang.Haskell]: haskell,
[Lang.Html]: html,
[Lang.Java]: java,
[Lang.JavaScript]: javascript,
[Lang.Json]: json,
[Lang.Kotlin]: kotlin,
[Lang.Lua]: lua,
[Lang.Php]: php,
[Lang.Python]: python,
[Lang.Ruby]: ruby,
[Lang.Rust]: rust,
[Lang.Scala]: scala,
[Lang.Sql]: sql,
[Lang.Swift]: swift,
[Lang.Toml]: toml,
[Lang.Tsx]: tsx,
[Lang.TypeScript]: typescript,
[Lang.Yaml]: yaml,
}
Loading