@ast-grep/nursery 0.0.1 → 0.0.2

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/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,17 @@
1
+ import { SgRoot, DynamicLangRegistrations } 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
+ }
15
+ /** Setup ast-grep/lang package's pre-release build and test */
16
+ export declare function setup(setupConfig: SetupConfig): void;
17
+ export {};
package/index.js CHANGED
@@ -1,33 +1,88 @@
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 node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const napi_1 = require("@ast-grep/napi");
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 = node_path_1.default.join(dirname, 'node_modules', treeSitterPackage, 'src');
36
+ if (node_fs_1.default.existsSync(existing)) {
37
+ log('src exists, skipping copy');
38
+ return;
39
+ }
40
+ log('copying tree-sitter src');
41
+ node_fs_1.default.cpSync(src, 'src', { recursive: true });
42
+ }
43
+ function filterOutUnNamedNode(node) {
44
+ if (!node.named) {
45
+ return null;
46
+ }
47
+ if (node.fields) {
48
+ for (const field of Object.keys(node.fields)) {
49
+ node.fields[field].types = node.fields[field].types.filter(n => n.named);
50
+ }
51
+ }
52
+ if (node.children) {
53
+ node.children.types = node.children.types.filter(n => n.named);
54
+ }
55
+ if (node.subtypes) {
56
+ node.subtypes = node.subtypes.filter(n => n.named);
57
+ }
58
+ return node;
59
+ }
60
+ function processNodeTypes(nodeTypes) {
61
+ const filteredNodeTypes = nodeTypes
62
+ .map(filterOutUnNamedNode)
63
+ .filter(node => !!node);
64
+ const nodeTypeMap = Object.fromEntries(filteredNodeTypes.map(node => [node.type, node]));
65
+ return nodeTypeMap;
66
+ }
67
+ function readLangNodeTypes(dirname) {
68
+ const staticNodePath = node_path_1.default.join(dirname, 'src', 'node-types.json');
69
+ const content = node_fs_1.default.readFileSync(staticNodePath, 'utf-8');
70
+ return JSON.parse(content);
71
+ }
72
+ function generateLangNodeTypes(setupConfig) {
73
+ const { name: lang, treeSitterPackage, dirname } = setupConfig;
74
+ try {
75
+ const nodeTypes = readLangNodeTypes(dirname);
76
+ const nodeTypeMap = processNodeTypes(nodeTypes);
77
+ const fileContent = `// Auto-generated from ${treeSitterPackage}` +
78
+ '\n' +
79
+ `type ${lang}Types = ${JSON.stringify(nodeTypeMap, null, 2)};` +
80
+ '\n' +
81
+ `export default ${lang}Types;`;
82
+ node_fs_1.default.writeFileSync(node_path_1.default.join(dirname, `type.d.ts`), fileContent);
83
+ }
84
+ catch (e) {
85
+ console.error(`Error while generating node types for ${lang}`);
86
+ throw e;
87
+ }
88
+ }
package/index.ts ADDED
@@ -0,0 +1,125 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { parse, registerDynamicLanguage, SgRoot, DynamicLangRegistrations } from '@ast-grep/napi'
4
+
5
+ /**
6
+ * Log to console
7
+ */
8
+ function log(...args: unknown[]) {
9
+ console.debug(`@ast-grep/lang:`, ...args)
10
+ }
11
+
12
+ /** Setup ast-grep/lang package's pre-release */
13
+ interface SetupConfig {
14
+ /** the root directory of the package */
15
+ dirname: string
16
+ /** Name of the language. e.g. toml */
17
+ name: string
18
+ /** Language registration object, usually the export of index.js */
19
+ languageRegistration: DynamicLangRegistrations[string]
20
+ /** Package name of tree-sitter package. e.g. tree-sitter-css */
21
+ treeSitterPackage: string
22
+ /** Test cases running in CI */
23
+ testRunner: (parse: (c: string) => SgRoot) => void
24
+ }
25
+
26
+ function test(setupConfig: SetupConfig) {
27
+ const { name, languageRegistration, testRunner } = setupConfig
28
+ registerDynamicLanguage({ [name]: languageRegistration })
29
+ testRunner((code) => parse(name, code))
30
+ }
31
+
32
+ /** Setup ast-grep/lang package's pre-release build and test */
33
+ export function setup(setupConfig: SetupConfig) {
34
+ const arg = process.argv[2]
35
+ if (arg === 'test') {
36
+ test(setupConfig)
37
+ } else if (arg === 'source') {
38
+ copySrcIfNeeded(setupConfig)
39
+ generateLangNodeTypes(setupConfig)
40
+ }
41
+ }
42
+
43
+ function copySrcIfNeeded(config: SetupConfig) {
44
+ const { dirname, treeSitterPackage } = config
45
+ const existing = path.join(dirname, 'src')
46
+ const src = path.join(dirname, 'node_modules', treeSitterPackage, 'src')
47
+ if (fs.existsSync(existing)) {
48
+ log('src exists, skipping copy')
49
+ return
50
+ }
51
+ log('copying tree-sitter src')
52
+ fs.cpSync(src, 'src', { recursive: true })
53
+ }
54
+
55
+ interface NodeBasicInfo {
56
+ type: string
57
+ named: boolean
58
+ }
59
+
60
+ interface NodeFieldInfo {
61
+ multiple: boolean
62
+ required: boolean
63
+ types: NodeBasicInfo[]
64
+ }
65
+
66
+ interface NodeType extends NodeBasicInfo {
67
+ root?: boolean
68
+ fields?: {
69
+ [fieldName: string]: NodeFieldInfo
70
+ }
71
+ children?: NodeFieldInfo
72
+ subtypes?: NodeBasicInfo[]
73
+ }
74
+
75
+ function filterOutUnNamedNode(node: NodeType): NodeType | null {
76
+ if (!node.named) {
77
+ return null
78
+ }
79
+ if (node.fields) {
80
+ for (const field of Object.keys(node.fields)) {
81
+ node.fields[field].types = node.fields[field].types.filter(n => n.named)
82
+ }
83
+ }
84
+ if (node.children) {
85
+ node.children.types = node.children.types.filter(n => n.named)
86
+ }
87
+ if (node.subtypes) {
88
+ node.subtypes = node.subtypes.filter(n => n.named)
89
+ }
90
+ return node
91
+ }
92
+
93
+ function processNodeTypes(nodeTypes: NodeType[]): Record<string, NodeType> {
94
+ const filteredNodeTypes = nodeTypes
95
+ .map(filterOutUnNamedNode)
96
+ .filter(node => !!node)
97
+ const nodeTypeMap = Object.fromEntries(
98
+ filteredNodeTypes.map(node => [node.type, node]),
99
+ )
100
+ return nodeTypeMap
101
+ }
102
+
103
+ function readLangNodeTypes(dirname: string): NodeType[] {
104
+ const staticNodePath = path.join(dirname, 'src', 'node-types.json')
105
+ const content = fs.readFileSync(staticNodePath, 'utf-8')
106
+ return JSON.parse(content)
107
+ }
108
+
109
+ function generateLangNodeTypes(setupConfig: SetupConfig) {
110
+ const { name: lang, treeSitterPackage, dirname } = setupConfig
111
+ try {
112
+ const nodeTypes = readLangNodeTypes(dirname)
113
+ const nodeTypeMap = processNodeTypes(nodeTypes)
114
+ const fileContent =
115
+ `// Auto-generated from ${treeSitterPackage}` +
116
+ '\n' +
117
+ `type ${lang}Types = ${JSON.stringify(nodeTypeMap, null, 2)};` +
118
+ '\n' +
119
+ `export default ${lang}Types;`
120
+ fs.writeFileSync(path.join(dirname, `type.d.ts`), fileContent)
121
+ } catch (e) {
122
+ console.error(`Error while generating node types for ${lang}`)
123
+ throw e
124
+ }
125
+ }
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.2",
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
11
  "@ast-grep/napi": "0.33.0"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.7.3",
19
+ "@types/node": "22.10.5"
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
+ }