@ast-grep/nursery 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @ast-grep/nursery
2
+
3
+ ## 0.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 597f386: Add @ast-grep/lang-php
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 ast-grep
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import type { DynamicLangRegistrations, SgRoot } from '@ast-grep/napi';
2
+ /** Setup ast-grep/lang package's pre-release */
3
+ interface SetupConfig {
4
+ /** the root directory of the package */
5
+ dirname: string;
6
+ /** Name of the language. e.g. toml */
7
+ name: string;
8
+ /** Language registration object, usually the export of index.js */
9
+ languageRegistration: DynamicLangRegistrations[string];
10
+ /** Package name of tree-sitter package. e.g. tree-sitter-css */
11
+ treeSitterPackage: string;
12
+ /** Test cases running in CI */
13
+ testRunner: (parse: (c: string) => SgRoot) => void;
14
+ /** Path of the `src` directory inside the `tree-sitter-*` package. Useful for
15
+ * `tree-sitter-php`, `tree-sitter-typescript` and `tree-sitter-yaml`.
16
+ * @default "src" */
17
+ src?: string;
18
+ }
19
+ /** Setup ast-grep/lang package's pre-release build and test */
20
+ export declare function setup(setupConfig: SetupConfig): void;
21
+ export {};
package/index.js CHANGED
@@ -1,33 +1,89 @@
1
- const { cp } = require('fs').promises
2
- const { parse, registerDynamicLanguage } = require('@ast-grep/napi')
3
-
4
- const path = require('path')
5
-
6
- async function copySrc(packageName) {
7
- const src = path.join(process.cwd(), 'node_modules', packageName, 'src')
8
- await cp(src, 'src', { recursive: true })
9
- }
10
-
11
- function test(setupConfig) {
12
- const { name, languageRegistration, testRunner } = setupConfig
13
- registerDynamicLanguage({ [name]: languageRegistration })
14
- testRunner((code) => parse(name, code))
15
- }
16
-
17
- /** Setup ast-grep/lang package's pre-release and
18
- * @param {Object} setupConfig
19
- * @param {string} setupConfig.name
20
- * @param {string} setupConfig.packageName
21
- * @param {Function} setupConfig.testRunner
22
- * @returns {void}
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setup = setup;
7
+ const napi_1 = require("@ast-grep/napi");
8
+ const node_fs_1 = __importDefault(require("node:fs"));
9
+ const node_path_1 = __importDefault(require("node:path"));
10
+ /**
11
+ * Log to console
23
12
  */
24
- function setup(setupConfig){
25
- const arg = process.argv[2]
26
- if (arg === 'copy') {
27
- copySrc(setupConfig.packageName)
28
- } else if (arg === 'test') {
29
- test(setupConfig)
30
- }
31
- }
32
-
33
- exports.setup = setup
13
+ function log(...args) {
14
+ console.debug('@ast-grep/lang:', ...args);
15
+ }
16
+ function test(setupConfig) {
17
+ const { name, languageRegistration, testRunner } = setupConfig;
18
+ (0, napi_1.registerDynamicLanguage)({ [name]: languageRegistration });
19
+ testRunner(code => (0, napi_1.parse)(name, code));
20
+ }
21
+ /** Setup ast-grep/lang package's pre-release build and test */
22
+ function setup(setupConfig) {
23
+ const arg = process.argv[2];
24
+ if (arg === 'test') {
25
+ test(setupConfig);
26
+ }
27
+ else if (arg === 'source') {
28
+ copySrcIfNeeded(setupConfig);
29
+ generateLangNodeTypes(setupConfig);
30
+ }
31
+ }
32
+ function copySrcIfNeeded(config) {
33
+ const { dirname, treeSitterPackage } = config;
34
+ const existing = node_path_1.default.join(dirname, 'src');
35
+ const src = config.src || 'src';
36
+ const source = node_path_1.default.join(dirname, 'node_modules', treeSitterPackage, src);
37
+ if (node_fs_1.default.existsSync(existing)) {
38
+ log('src exists, skipping copy');
39
+ return;
40
+ }
41
+ log('copying tree-sitter src');
42
+ node_fs_1.default.cpSync(source, 'src', { recursive: true });
43
+ }
44
+ function filterOutUnNamedNode(node) {
45
+ if (!node.named) {
46
+ return null;
47
+ }
48
+ if (node.fields) {
49
+ for (const field of Object.keys(node.fields)) {
50
+ node.fields[field].types = node.fields[field].types.filter(n => n.named);
51
+ }
52
+ }
53
+ if (node.children) {
54
+ node.children.types = node.children.types.filter(n => n.named);
55
+ }
56
+ if (node.subtypes) {
57
+ node.subtypes = node.subtypes.filter(n => n.named);
58
+ }
59
+ return node;
60
+ }
61
+ function processNodeTypes(nodeTypes) {
62
+ const filteredNodeTypes = nodeTypes
63
+ .map(filterOutUnNamedNode)
64
+ .filter(node => !!node);
65
+ const nodeTypeMap = Object.fromEntries(filteredNodeTypes.map(node => [node.type, node]));
66
+ return nodeTypeMap;
67
+ }
68
+ function readLangNodeTypes(dirname) {
69
+ const staticNodePath = node_path_1.default.join(dirname, 'src', 'node-types.json');
70
+ const content = node_fs_1.default.readFileSync(staticNodePath, 'utf-8');
71
+ return JSON.parse(content);
72
+ }
73
+ function generateLangNodeTypes(setupConfig) {
74
+ const { name: lang, treeSitterPackage, dirname } = setupConfig;
75
+ try {
76
+ const nodeTypes = readLangNodeTypes(dirname);
77
+ const nodeTypeMap = processNodeTypes(nodeTypes);
78
+ const fileContent = `// Auto-generated from ${treeSitterPackage}` +
79
+ '\n' +
80
+ `type ${lang}Types = ${JSON.stringify(nodeTypeMap, null, 2)};` +
81
+ '\n' +
82
+ `export default ${lang}Types;`;
83
+ node_fs_1.default.writeFileSync(node_path_1.default.join(dirname, 'type.d.ts'), fileContent);
84
+ }
85
+ catch (e) {
86
+ console.error(`Error while generating node types for ${lang}`);
87
+ throw e;
88
+ }
89
+ }
package/index.ts ADDED
@@ -0,0 +1,132 @@
1
+ import type { DynamicLangRegistrations, SgRoot } from '@ast-grep/napi'
2
+ import { parse, registerDynamicLanguage } from '@ast-grep/napi'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+
6
+ /**
7
+ * Log to console
8
+ */
9
+ function log(...args: unknown[]) {
10
+ console.debug('@ast-grep/lang:', ...args)
11
+ }
12
+
13
+ /** Setup ast-grep/lang package's pre-release */
14
+ interface SetupConfig {
15
+ /** the root directory of the package */
16
+ dirname: string
17
+ /** Name of the language. e.g. toml */
18
+ name: string
19
+ /** Language registration object, usually the export of index.js */
20
+ languageRegistration: DynamicLangRegistrations[string]
21
+ /** Package name of tree-sitter package. e.g. tree-sitter-css */
22
+ treeSitterPackage: string
23
+ /** Test cases running in CI */
24
+ testRunner: (parse: (c: string) => SgRoot) => void
25
+ /** Path of the `src` directory inside the `tree-sitter-*` package. Useful for
26
+ * `tree-sitter-php`, `tree-sitter-typescript` and `tree-sitter-yaml`.
27
+ * @default "src" */
28
+ src?: string
29
+ }
30
+
31
+ function test(setupConfig: SetupConfig) {
32
+ const { name, languageRegistration, testRunner } = setupConfig
33
+ registerDynamicLanguage({ [name]: languageRegistration })
34
+ testRunner(code => parse(name, code))
35
+ }
36
+
37
+ /** Setup ast-grep/lang package's pre-release build and test */
38
+ export function setup(setupConfig: SetupConfig) {
39
+ const arg = process.argv[2]
40
+ if (arg === 'test') {
41
+ test(setupConfig)
42
+ } else if (arg === 'source') {
43
+ copySrcIfNeeded(setupConfig)
44
+ generateLangNodeTypes(setupConfig)
45
+ }
46
+ }
47
+
48
+ function copySrcIfNeeded(config: SetupConfig) {
49
+ const { dirname, treeSitterPackage } = config
50
+ const existing = path.join(dirname, 'src')
51
+ const src = config.src || 'src'
52
+ const source = path.join(dirname, 'node_modules', treeSitterPackage, src)
53
+ if (fs.existsSync(existing)) {
54
+ log('src exists, skipping copy')
55
+ return
56
+ }
57
+
58
+ log('copying tree-sitter src')
59
+ fs.cpSync(source, 'src', { recursive: true })
60
+ }
61
+
62
+ interface NodeBasicInfo {
63
+ type: string
64
+ named: boolean
65
+ }
66
+
67
+ interface NodeFieldInfo {
68
+ multiple: boolean
69
+ required: boolean
70
+ types: NodeBasicInfo[]
71
+ }
72
+
73
+ interface NodeType extends NodeBasicInfo {
74
+ root?: boolean
75
+ fields?: {
76
+ [fieldName: string]: NodeFieldInfo
77
+ }
78
+ children?: NodeFieldInfo
79
+ subtypes?: NodeBasicInfo[]
80
+ }
81
+
82
+ function filterOutUnNamedNode(node: NodeType): NodeType | null {
83
+ if (!node.named) {
84
+ return null
85
+ }
86
+ if (node.fields) {
87
+ for (const field of Object.keys(node.fields)) {
88
+ node.fields[field].types = node.fields[field].types.filter(n => n.named)
89
+ }
90
+ }
91
+ if (node.children) {
92
+ node.children.types = node.children.types.filter(n => n.named)
93
+ }
94
+ if (node.subtypes) {
95
+ node.subtypes = node.subtypes.filter(n => n.named)
96
+ }
97
+ return node
98
+ }
99
+
100
+ function processNodeTypes(nodeTypes: NodeType[]): Record<string, NodeType> {
101
+ const filteredNodeTypes = nodeTypes
102
+ .map(filterOutUnNamedNode)
103
+ .filter(node => !!node)
104
+ const nodeTypeMap = Object.fromEntries(
105
+ filteredNodeTypes.map(node => [node.type, node]),
106
+ )
107
+ return nodeTypeMap
108
+ }
109
+
110
+ function readLangNodeTypes(dirname: string): NodeType[] {
111
+ const staticNodePath = path.join(dirname, 'src', 'node-types.json')
112
+ const content = fs.readFileSync(staticNodePath, 'utf-8')
113
+ return JSON.parse(content)
114
+ }
115
+
116
+ function generateLangNodeTypes(setupConfig: SetupConfig) {
117
+ const { name: lang, treeSitterPackage, dirname } = setupConfig
118
+ try {
119
+ const nodeTypes = readLangNodeTypes(dirname)
120
+ const nodeTypeMap = processNodeTypes(nodeTypes)
121
+ const fileContent =
122
+ `// Auto-generated from ${treeSitterPackage}` +
123
+ '\n' +
124
+ `type ${lang}Types = ${JSON.stringify(nodeTypeMap, null, 2)};` +
125
+ '\n' +
126
+ `export default ${lang}Types;`
127
+ fs.writeFileSync(path.join(dirname, 'type.d.ts'), fileContent)
128
+ } catch (e) {
129
+ console.error(`Error while generating node types for ${lang}`)
130
+ throw e
131
+ }
132
+ }
package/package.json CHANGED
@@ -1,13 +1,24 @@
1
1
  {
2
2
  "name": "@ast-grep/nursery",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
+ "private": false,
4
5
  "description": "",
5
6
  "main": "index.js",
6
7
  "keywords": [],
7
8
  "author": "",
8
9
  "license": "ISC",
9
10
  "dependencies": {
10
- "tree-sitter-cli": "0.24.6",
11
- "@ast-grep/napi": "0.33.0"
11
+ "@ast-grep/napi": "0.36.2"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.7.3",
19
+ "@types/node": "22.14.0"
20
+ },
21
+ "scripts": {
22
+ "compile-ts": "tsc"
12
23
  }
13
- }
24
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json"
3
+ }