Skip to content

Commit 8d96346

Browse files
committed
♻️ Convert to TypeScript
1 parent c298021 commit 8d96346

17 files changed

+278
-223
lines changed

packages/all/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
!src

packages/all/index.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/all/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/all/lang.d.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/all/lang.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/all/lang.test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/all/langs.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/all/langs.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/all/langs.test.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/all/package.json

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
},
1010
"license": "ISC",
1111
"author": {
12-
"name": "Nato Boram",
13-
"url": "https://github.com/NatoBoram"
12+
"name": "CodeRabbit",
13+
"url": "https://www.coderabbit.ai"
1414
},
15-
"files": ["./**/*.js", "./**/*.d.ts", "!./**/*.test.*"],
16-
"main": "index.js",
15+
"files": ["dist", "!dist/**/*.test.*"],
16+
"main": "dist/index.js",
1717
"repository": "github:ast-grep/langs",
1818
"scripts": {
19+
"build": "tsc",
20+
"clean": "rm -rf dist docs node_modules tsconfig.tsbuildinfo",
1921
"format": "biome format --write",
20-
"test": "node --test './**/*.test.js'"
22+
"format:ci": "biome format",
23+
"lint": "biome lint --fix",
24+
"lint:ci": "biome lint",
25+
"pretest": "tsc",
26+
"test": "node --test './dist/**/*.test.js'"
2127
},
2228
"dependencies": {
2329
"@ast-grep/lang-angular": "workspace:*",
@@ -46,15 +52,21 @@
4652
"@ast-grep/lang-toml": "workspace:*",
4753
"@ast-grep/lang-tsx": "workspace:*",
4854
"@ast-grep/lang-typescript": "workspace:*",
55+
"@ast-grep/lang-yaml": "workspace:*",
4956
"@ast-grep/napi": "^0.37.0"
5057
},
58+
"devDependencies": {
59+
"@biomejs/biome": "1.9.4",
60+
"@types/node": "22.14.1",
61+
"typescript": "^5.8.3"
62+
},
5163
"type": "module",
5264
"exports": {
5365
".": {
54-
"types": "./index.d.ts",
55-
"default": "./index.js"
66+
"types": "./dist/index.d.ts",
67+
"default": "./dist/index.js"
5668
}
5769
},
58-
"types": "./index.d.ts",
59-
"module": "./index.js"
70+
"types": "dist/index.d.ts",
71+
"module": "dist/index.js"
6072
}

packages/all/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./lang.ts"
2+
export * from "./langs.ts"

packages/all/src/lang.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Lang as LangNapi } from "@ast-grep/napi"
2+
import test, { describe } from "node:test"
3+
import { Lang } from "./lang.ts"
4+
5+
/** Languages supported in `@ast-grep/napi@0.33.1`. */
6+
const previous = Object.freeze({
7+
Html: "Html",
8+
JavaScript: "JavaScript",
9+
Tsx: "Tsx",
10+
Css: "Css",
11+
TypeScript: "TypeScript",
12+
Bash: "Bash",
13+
C: "C",
14+
Cpp: "Cpp",
15+
CSharp: "CSharp",
16+
Go: "Go",
17+
Elixir: "Elixir",
18+
Haskell: "Haskell",
19+
Java: "Java",
20+
Json: "Json",
21+
Kotlin: "Kotlin",
22+
Lua: "Lua",
23+
Php: "Php",
24+
Python: "Python",
25+
Ruby: "Ruby",
26+
Rust: "Rust",
27+
Scala: "Scala",
28+
Sql: "Sql",
29+
Swift: "Swift",
30+
Yaml: 'Yaml',
31+
})
32+
33+
describe("Lang", () => {
34+
test("The new enum is compatible with the old one", ({ assert }) => {
35+
for (const lang of Object.values(previous)) assert.equal(Lang[lang], lang)
36+
})
37+
38+
test("The new enum is compatible with the built-in ones", ({ assert }) => {
39+
for (const lang of Object.values(LangNapi)) assert.equal(Lang[lang], lang)
40+
})
41+
})

packages/all/src/lang.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* An enum of all languages supported by `@ast-grep/langs`.
3+
*/
4+
export const Lang = {
5+
Angular: "Angular",
6+
Bash: "Bash",
7+
C: "C",
8+
Cpp: "Cpp",
9+
CSharp: "CSharp",
10+
Css: "Css",
11+
Dart: "Dart",
12+
Elixir: "Elixir",
13+
Go: "Go",
14+
Haskell: "Haskell",
15+
Html: "Html",
16+
Java: "Java",
17+
JavaScript: "JavaScript",
18+
Json: "Json",
19+
Kotlin: "Kotlin",
20+
Lua: "Lua",
21+
Php: "Php",
22+
Python: "Python",
23+
Ruby: "Ruby",
24+
Rust: "Rust",
25+
Scala: "Scala",
26+
Sql: "Sql",
27+
Swift: "Swift",
28+
Toml: "Toml",
29+
Tsx: "Tsx",
30+
TypeScript: "TypeScript",
31+
Yaml: "Yaml",
32+
} as const
33+
34+
/**
35+
* An enum of all languages supported by `@ast-grep/langs`.
36+
*/
37+
export type Lang = (typeof Lang)[keyof typeof Lang]

packages/all/src/langs.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { parse, registerDynamicLanguage } from "@ast-grep/napi"
2+
import { before, describe, test } from "node:test"
3+
import { Lang } from "./lang.js"
4+
import { langs } from "./langs.ts"
5+
6+
describe("langs", () => {
7+
// @ts-expect-error Type `StaticLangRegistration` is missing the following
8+
// properties from type `LangRegistration`: `libraryPath`, `extensions`.
9+
before(() => registerDynamicLanguage(langs))
10+
11+
// A newly supported language
12+
test(Lang.Dart, ({ assert }) => {
13+
const sg = parse(Lang.Dart, 'var x = "Hello, world!";"')
14+
const kind = sg.root().kind()
15+
assert.equal(kind, "program")
16+
})
17+
18+
// A previously supported language
19+
test(Lang.Go, ({ assert }) => {
20+
const sg = parse(Lang.Go, 'x := "Hello, world!"')
21+
const kind = sg.root().kind()
22+
assert.equal(kind, "source_file")
23+
})
24+
25+
// A built-in language
26+
test(Lang.TypeScript, ({ assert }) => {
27+
const sg = parse(Lang.TypeScript, 'const x = "Hello, world!"')
28+
const kind = sg.root().kind()
29+
assert.equal(kind, "program")
30+
})
31+
})

0 commit comments

Comments
 (0)