@opensip-cli/tree-sitter 0.1.0
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 +202 -0
- package/NOTICE +8 -0
- package/README.md +31 -0
- package/dist/__tests__/lifecycle.test.d.ts +2 -0
- package/dist/__tests__/lifecycle.test.d.ts.map +1 -0
- package/dist/__tests__/lifecycle.test.js +26 -0
- package/dist/__tests__/lifecycle.test.js.map +1 -0
- package/dist/__tests__/nodes.test.d.ts +2 -0
- package/dist/__tests__/nodes.test.d.ts.map +1 -0
- package/dist/__tests__/nodes.test.js +57 -0
- package/dist/__tests__/nodes.test.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/lifecycle.d.ts +36 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +48 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/nodes.d.ts +45 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +70 -0
- package/dist/nodes.js.map +1 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
OpenSIP CLI
|
|
2
|
+
Copyright 2026 opensip-ai
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
5
|
+
this software except in compliance with the License. A copy of the License is
|
|
6
|
+
included in the LICENSE file and is also available at:
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<!-- Generated by scripts/build-package-readmes.mjs — do not edit by hand.
|
|
2
|
+
Change the package's "description" (or the generator template) and run
|
|
3
|
+
`pnpm docs:readmes`. CI enforces sync via `pnpm docs:readmes:check`. -->
|
|
4
|
+
|
|
5
|
+
# @opensip-cli/tree-sitter
|
|
6
|
+
|
|
7
|
+
> Grammar-agnostic tree-sitter substrate (parser lifecycle + node accessors) shared by lang-* and the graph adapters
|
|
8
|
+
|
|
9
|
+
This is an **internal library** of the opensip-cli toolkit. It is published so the CLI and tools can depend on it; most users will not install it directly.
|
|
10
|
+
|
|
11
|
+
Part of [**opensip-cli**](https://github.com/opensip-ai/opensip-cli) — an open-source codebase-analysis toolkit: fitness checks (`fit`), static call-graph analysis (`graph`), and simulation (`sim`).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
Most users install the CLI, which bundles the first-party tools:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
curl -fsSL https://opensip.ai/cli/install.sh | bash
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This package is published for the CLI and advanced plugin authors; most users should not install `@opensip-cli/tree-sitter` directly.
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
- 📚 Project docs: https://opensip.ai/docs/opensip-cli/
|
|
26
|
+
- 🧭 Package catalog (what every package does): https://github.com/opensip-ai/opensip-cli/blob/v0.1.0/docs/public/70-reference/02-package-catalog.md
|
|
27
|
+
- 📦 Source: https://github.com/opensip-ai/opensip-cli/tree/v0.1.0/packages/tree-sitter
|
|
28
|
+
|
|
29
|
+
## License
|
|
30
|
+
|
|
31
|
+
Apache-2.0 © opensip-ai
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/lifecycle.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { beforeAll, describe, expect, it } from 'vitest';
|
|
3
|
+
import { loadGrammar, createParser, parseToTree } from '../index.js';
|
|
4
|
+
// The substrate is grammar-agnostic, so its tests parse with *some* grammar:
|
|
5
|
+
// the vendored python wasm from the sibling lang-python package, referenced by
|
|
6
|
+
// filesystem path (not a package import — that would add a forbidden lang-*
|
|
7
|
+
// dependency and is unnecessary for a file the test only needs to read).
|
|
8
|
+
const WASM = fileURLToPath(new URL('../../../languages/lang-python/wasm/tree-sitter-python.wasm', import.meta.url));
|
|
9
|
+
let parser;
|
|
10
|
+
beforeAll(async () => {
|
|
11
|
+
parser = createParser(await loadGrammar(WASM));
|
|
12
|
+
});
|
|
13
|
+
describe('parseToTree', () => {
|
|
14
|
+
it('returns a tree for valid source', () => {
|
|
15
|
+
const tree = parseToTree(parser, 'x = 1\n');
|
|
16
|
+
expect(tree).not.toBeNull();
|
|
17
|
+
expect(tree?.rootNode.type).toBe('module');
|
|
18
|
+
expect(tree?.rootNode.hasError).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
it('returns a partial (non-null) tree with hasError for malformed source', () => {
|
|
21
|
+
const tree = parseToTree(parser, 'def (:\n');
|
|
22
|
+
expect(tree).not.toBeNull();
|
|
23
|
+
expect(tree?.rootNode.hasError).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=lifecycle.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.test.js","sourceRoot":"","sources":["../../src/__tests__/lifecycle.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;A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AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/nodes.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { beforeAll, describe, expect, it } from 'vitest';
|
|
3
|
+
import { loadGrammar, createParser, parseToTree, nameOf, childrenOf, namedChildrenOf, nodeText, getLineNumber, getColumn, walkNodes, findEnclosing, } from '../index.js';
|
|
4
|
+
const WASM = fileURLToPath(new URL('../../../languages/lang-python/wasm/tree-sitter-python.wasm', import.meta.url));
|
|
5
|
+
let parse;
|
|
6
|
+
beforeAll(async () => {
|
|
7
|
+
const parser = createParser(await loadGrammar(WASM));
|
|
8
|
+
parse = (src) => {
|
|
9
|
+
const tree = parseToTree(parser, src);
|
|
10
|
+
if (!tree)
|
|
11
|
+
throw new Error('no tree');
|
|
12
|
+
return tree;
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
describe('node accessors', () => {
|
|
16
|
+
it('nameOf reads the name field, null when absent', () => {
|
|
17
|
+
const root = parse('def foo():\n pass\n').rootNode;
|
|
18
|
+
const fn = namedChildrenOf(root)[0];
|
|
19
|
+
expect(fn.type).toBe('function_definition');
|
|
20
|
+
expect(nameOf(fn)).toBe('foo');
|
|
21
|
+
expect(nameOf(root)).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
it('childrenOf / namedChildrenOf return only non-null nodes', () => {
|
|
24
|
+
const root = parse('x = 1\n').rootNode;
|
|
25
|
+
expect(childrenOf(root).every((n) => n !== null)).toBe(true);
|
|
26
|
+
expect(namedChildrenOf(root).length).toBeGreaterThan(0);
|
|
27
|
+
});
|
|
28
|
+
it('getLineNumber is 1-based, getColumn 0-based, nodeText returns source', () => {
|
|
29
|
+
const root = parse('a = 1\nb = 2\n').rootNode;
|
|
30
|
+
const second = namedChildrenOf(root)[1];
|
|
31
|
+
expect(getLineNumber(second)).toBe(2);
|
|
32
|
+
expect(getColumn(second)).toBe(0);
|
|
33
|
+
expect(nodeText(second)).toContain('b = 2');
|
|
34
|
+
});
|
|
35
|
+
it('walkNodes visits named descendants (function + nested return)', () => {
|
|
36
|
+
const root = parse('def f():\n return 1\n').rootNode;
|
|
37
|
+
const types = [];
|
|
38
|
+
walkNodes(root, (n) => types.push(n.type));
|
|
39
|
+
expect(types).toContain('function_definition');
|
|
40
|
+
expect(types).toContain('return_statement');
|
|
41
|
+
});
|
|
42
|
+
it('findEnclosing returns the nearest matching ancestor, null at root', () => {
|
|
43
|
+
const root = parse('def f():\n return 1\n').rootNode;
|
|
44
|
+
const returns = [];
|
|
45
|
+
walkNodes(root, (n) => {
|
|
46
|
+
if (n.type === 'return_statement')
|
|
47
|
+
returns.push(n);
|
|
48
|
+
});
|
|
49
|
+
expect(returns[0]).toBeDefined();
|
|
50
|
+
const fn = findEnclosing(returns[0], (n) => n.type === 'function_definition');
|
|
51
|
+
expect(fn).not.toBeNull();
|
|
52
|
+
expect(nameOf(fn)).toBe('f');
|
|
53
|
+
// root has no parent → no enclosing match
|
|
54
|
+
expect(findEnclosing(root, () => true)).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=nodes.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.test.js","sourceRoot":"","sources":["../../src/__tests__/nodes.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAEzD,OAAO,EACL,WAAW,EACX,YAAY,EACZ,WAAW,EACX,MAAM,EACN,UAAU,EACV,eAAe,EACf,QAAQ,EACR,aAAa,EACb,SAAS,EACT,SAAS,EACT,aAAa,GACd,MAAM,aAAa,CAAC;AAIrB,MAAM,I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AAC,CAAC,EAAE,EAAE;YACpB,IAAI,CAAC,CAAC,IAAI,KAAK,kBAAkB;gBAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QACjC,MAAM,EAAE,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,qBAAqB,CAAC,CAAC;QAC9E,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QAC1B,MAAM,CAAC,MAAM,CAAC,EAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,0CAA0C;QAC1C,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @opensip-cli/tree-sitter — the grammar-agnostic tree-sitter substrate
|
|
3
|
+
* (ADR-0010). Sits between `@opensip-cli/core` and the `lang-*` / graph
|
|
4
|
+
* adapters; depends on `web-tree-sitter` only, never on `@opensip-cli/graph`.
|
|
5
|
+
*
|
|
6
|
+
* core → tree-sitter → { lang-*, graph-adapter-common }
|
|
7
|
+
*
|
|
8
|
+
* Provides: the parser lifecycle (`Parser.init` once, `loadGrammar`,
|
|
9
|
+
* `createParser`, `parseToTree`), the neutral `ParsedFile` shape, and the
|
|
10
|
+
* generic node accessors / AST helpers. Per-language grammars + node-kind
|
|
11
|
+
* predicates live in each `lang-*` package.
|
|
12
|
+
*/
|
|
13
|
+
export * from './types.js';
|
|
14
|
+
export * from './lifecycle.js';
|
|
15
|
+
export * from './nodes.js';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @opensip-cli/tree-sitter — the grammar-agnostic tree-sitter substrate
|
|
3
|
+
* (ADR-0010). Sits between `@opensip-cli/core` and the `lang-*` / graph
|
|
4
|
+
* adapters; depends on `web-tree-sitter` only, never on `@opensip-cli/graph`.
|
|
5
|
+
*
|
|
6
|
+
* core → tree-sitter → { lang-*, graph-adapter-common }
|
|
7
|
+
*
|
|
8
|
+
* Provides: the parser lifecycle (`Parser.init` once, `loadGrammar`,
|
|
9
|
+
* `createParser`, `parseToTree`), the neutral `ParsedFile` shape, and the
|
|
10
|
+
* generic node accessors / AST helpers. Per-language grammars + node-kind
|
|
11
|
+
* predicates live in each `lang-*` package.
|
|
12
|
+
*/
|
|
13
|
+
export * from './types.js';
|
|
14
|
+
export * from './lifecycle.js';
|
|
15
|
+
export * from './nodes.js';
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* web-tree-sitter lifecycle — the grammar-agnostic half lifted out of
|
|
3
|
+
* `graph-adapter-common/parse.ts` (ADR-0010). No `@opensip-cli/graph`
|
|
4
|
+
* coupling: this layer knows nothing of `ParseInput`/`ParseOutput`. The
|
|
5
|
+
* per-project parse driver (which owns those graph types) stays in
|
|
6
|
+
* graph-adapter-common; `lang-*` adapters call `parseToTree` per file.
|
|
7
|
+
*
|
|
8
|
+
* ## Sync parse over an async runtime (the load-bearing seam)
|
|
9
|
+
*
|
|
10
|
+
* `web-tree-sitter` needs a one-time async init (`Parser.init()`) and a
|
|
11
|
+
* one-time async grammar load (`Language.load(<wasm>)`), but
|
|
12
|
+
* `parser.parse(source)` is synchronous once a language is set. We confine
|
|
13
|
+
* the async to module top-level `await Parser.init()` here — every consumer
|
|
14
|
+
* statically imports this module, so the WASM runtime is ready before any
|
|
15
|
+
* adapter's own top-level `await loadGrammar(<wasm>)`.
|
|
16
|
+
*/
|
|
17
|
+
import { Parser, Language, type Tree } from 'web-tree-sitter';
|
|
18
|
+
/** Load a tree-sitter grammar from a `.wasm` path (adapter module top level). */
|
|
19
|
+
export declare function loadGrammar(wasmPath: string): Promise<Language>;
|
|
20
|
+
/**
|
|
21
|
+
* Create a parser bound to `grammar`. Adapters create one per grammar at
|
|
22
|
+
* module load and reuse it across every `parseToTree` call — tree-sitter
|
|
23
|
+
* parsers are reusable and stateless per parse, so reuse yields identical
|
|
24
|
+
* trees with no per-call allocation.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createParser(grammar: Language): Parser;
|
|
27
|
+
/**
|
|
28
|
+
* Parse `source` into a tree. Returns the tree (possibly with
|
|
29
|
+
* `rootNode.hasError` — tree-sitter recovers with MISSING nodes; the partial
|
|
30
|
+
* tree is retained, matching the graph parse contract). Returns `null` only
|
|
31
|
+
* when web-tree-sitter yields no tree (no language set / aborted callback —
|
|
32
|
+
* neither applies here). Never swallows a thrown error: a throwing parse
|
|
33
|
+
* propagates to the caller's project loop, which records a `ParseError`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseToTree(parser: Parser, source: string): Tree | null;
|
|
36
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAO9D,iFAAiF;AACjF,wBAAsB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAErE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,QAAQ,GAAG,MAAM,CAItD;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAEvE"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* web-tree-sitter lifecycle — the grammar-agnostic half lifted out of
|
|
3
|
+
* `graph-adapter-common/parse.ts` (ADR-0010). No `@opensip-cli/graph`
|
|
4
|
+
* coupling: this layer knows nothing of `ParseInput`/`ParseOutput`. The
|
|
5
|
+
* per-project parse driver (which owns those graph types) stays in
|
|
6
|
+
* graph-adapter-common; `lang-*` adapters call `parseToTree` per file.
|
|
7
|
+
*
|
|
8
|
+
* ## Sync parse over an async runtime (the load-bearing seam)
|
|
9
|
+
*
|
|
10
|
+
* `web-tree-sitter` needs a one-time async init (`Parser.init()`) and a
|
|
11
|
+
* one-time async grammar load (`Language.load(<wasm>)`), but
|
|
12
|
+
* `parser.parse(source)` is synchronous once a language is set. We confine
|
|
13
|
+
* the async to module top-level `await Parser.init()` here — every consumer
|
|
14
|
+
* statically imports this module, so the WASM runtime is ready before any
|
|
15
|
+
* adapter's own top-level `await loadGrammar(<wasm>)`.
|
|
16
|
+
*/
|
|
17
|
+
import { Parser, Language } from 'web-tree-sitter';
|
|
18
|
+
// One-time WASM runtime init. Top-level await — every consumer statically
|
|
19
|
+
// imports this module — guarantees the runtime is ready before any adapter's
|
|
20
|
+
// `loadGrammar(<wasm>)` (which also runs at module top level).
|
|
21
|
+
await Parser.init();
|
|
22
|
+
/** Load a tree-sitter grammar from a `.wasm` path (adapter module top level). */
|
|
23
|
+
export async function loadGrammar(wasmPath) {
|
|
24
|
+
return Language.load(wasmPath);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a parser bound to `grammar`. Adapters create one per grammar at
|
|
28
|
+
* module load and reuse it across every `parseToTree` call — tree-sitter
|
|
29
|
+
* parsers are reusable and stateless per parse, so reuse yields identical
|
|
30
|
+
* trees with no per-call allocation.
|
|
31
|
+
*/
|
|
32
|
+
export function createParser(grammar) {
|
|
33
|
+
const parser = new Parser();
|
|
34
|
+
parser.setLanguage(grammar);
|
|
35
|
+
return parser;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse `source` into a tree. Returns the tree (possibly with
|
|
39
|
+
* `rootNode.hasError` — tree-sitter recovers with MISSING nodes; the partial
|
|
40
|
+
* tree is retained, matching the graph parse contract). Returns `null` only
|
|
41
|
+
* when web-tree-sitter yields no tree (no language set / aborted callback —
|
|
42
|
+
* neither applies here). Never swallows a thrown error: a throwing parse
|
|
43
|
+
* propagates to the caller's project loop, which records a `ParseError`.
|
|
44
|
+
*/
|
|
45
|
+
export function parseToTree(parser, source) {
|
|
46
|
+
return parser.parse(source);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=lifecycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAa,MAAM,iBAAiB,CAAC;AAE9D,0EAA0E;AAC1E,6EAA6E;AAC7E,+DAA+D;AAC/D,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;AAEpB,iFAAiF;AACjF,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,QAAgB;IAChD,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,OAAiB;IAC5C,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAC5B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,MAAc;IACxD,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC"}
|
package/dist/nodes.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic, grammar-agnostic node accessors + AST helpers (ADR-0010 v1
|
|
3
|
+
* vocabulary). `nameOf`/`childrenOf`/`namedChildrenOf` are lifted verbatim
|
|
4
|
+
* from `graph-adapter-common/walk.ts` (they operate on `Node` only, no graph
|
|
5
|
+
* types). `walkNodes`/`findEnclosing` mirror the `lang-typescript` precedents
|
|
6
|
+
* (`ast-utilities.walkNodes`, `function-scope.findEnclosingFunction`) over
|
|
7
|
+
* tree-sitter nodes. Per-language node-kind predicates layer on top in each
|
|
8
|
+
* `lang-*` package.
|
|
9
|
+
*/
|
|
10
|
+
import type { Node } from 'web-tree-sitter';
|
|
11
|
+
/**
|
|
12
|
+
* Read the text of a node's `name` field, or `null` when absent. Every
|
|
13
|
+
* grammar exposes the declared name via a `name` field on function / method /
|
|
14
|
+
* class / impl nodes.
|
|
15
|
+
*/
|
|
16
|
+
export declare function nameOf(node: Node): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* `node.children` with the nulls removed. web-tree-sitter types `.children`
|
|
19
|
+
* as `(Node | null)[]` (a null slot is theoretically possible mid-iteration
|
|
20
|
+
* during an incremental re-parse); for our one-shot parse the slots are
|
|
21
|
+
* always populated, but we filter once here so consumers get a clean `Node[]`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function childrenOf(node: Node): Node[];
|
|
24
|
+
/** `node.namedChildren` with the nulls removed. See {@link childrenOf}. */
|
|
25
|
+
export declare function namedChildrenOf(node: Node): Node[];
|
|
26
|
+
/** The node's source text. */
|
|
27
|
+
export declare function nodeText(node: Node): string;
|
|
28
|
+
/** 1-based line number of the node's start. */
|
|
29
|
+
export declare function getLineNumber(node: Node): number;
|
|
30
|
+
/** 0-based column of the node's start. */
|
|
31
|
+
export declare function getColumn(node: Node): number;
|
|
32
|
+
/**
|
|
33
|
+
* Pre-order visit of every named descendant of `root` (not `root` itself) —
|
|
34
|
+
* the tree-sitter analog of `lang-typescript`'s `walkNodes`. Named children
|
|
35
|
+
* are the meaningful AST nodes (punctuation/anonymous tokens excluded).
|
|
36
|
+
*/
|
|
37
|
+
export declare function walkNodes(root: Node, visitor: (node: Node) => void): void;
|
|
38
|
+
/**
|
|
39
|
+
* Walk ancestors of `node` (excluding `node`) and return the nearest one
|
|
40
|
+
* matching `predicate`, or `null` at the root — the tree-sitter analog of
|
|
41
|
+
* `lang-typescript`'s `findEnclosingFunction`. Per-language helpers compose
|
|
42
|
+
* this with a node-kind predicate.
|
|
43
|
+
*/
|
|
44
|
+
export declare function findEnclosing(node: Node, predicate: (n: Node) => boolean): Node | null;
|
|
45
|
+
//# sourceMappingURL=nodes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.d.ts","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAGhD;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,CAE7C;AAED,2EAA2E;AAC3E,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,CAElD;AAED,8BAA8B;AAC9B,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE3C;AAED,+CAA+C;AAC/C,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAEhD;AAED,0CAA0C;AAC1C,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE5C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,GAAG,IAAI,CAKzE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,EAAE,IAAI,KAAK,OAAO,GAAG,IAAI,GAAG,IAAI,CAOtF"}
|
package/dist/nodes.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic, grammar-agnostic node accessors + AST helpers (ADR-0010 v1
|
|
3
|
+
* vocabulary). `nameOf`/`childrenOf`/`namedChildrenOf` are lifted verbatim
|
|
4
|
+
* from `graph-adapter-common/walk.ts` (they operate on `Node` only, no graph
|
|
5
|
+
* types). `walkNodes`/`findEnclosing` mirror the `lang-typescript` precedents
|
|
6
|
+
* (`ast-utilities.walkNodes`, `function-scope.findEnclosingFunction`) over
|
|
7
|
+
* tree-sitter nodes. Per-language node-kind predicates layer on top in each
|
|
8
|
+
* `lang-*` package.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Read the text of a node's `name` field, or `null` when absent. Every
|
|
12
|
+
* grammar exposes the declared name via a `name` field on function / method /
|
|
13
|
+
* class / impl nodes.
|
|
14
|
+
*/
|
|
15
|
+
export function nameOf(node) {
|
|
16
|
+
const name = node.childForFieldName('name');
|
|
17
|
+
return name ? name.text : null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `node.children` with the nulls removed. web-tree-sitter types `.children`
|
|
21
|
+
* as `(Node | null)[]` (a null slot is theoretically possible mid-iteration
|
|
22
|
+
* during an incremental re-parse); for our one-shot parse the slots are
|
|
23
|
+
* always populated, but we filter once here so consumers get a clean `Node[]`.
|
|
24
|
+
*/
|
|
25
|
+
export function childrenOf(node) {
|
|
26
|
+
return node.children.filter((c) => c !== null);
|
|
27
|
+
}
|
|
28
|
+
/** `node.namedChildren` with the nulls removed. See {@link childrenOf}. */
|
|
29
|
+
export function namedChildrenOf(node) {
|
|
30
|
+
return node.namedChildren.filter((c) => c !== null);
|
|
31
|
+
}
|
|
32
|
+
/** The node's source text. */
|
|
33
|
+
export function nodeText(node) {
|
|
34
|
+
return node.text;
|
|
35
|
+
}
|
|
36
|
+
/** 1-based line number of the node's start. */
|
|
37
|
+
export function getLineNumber(node) {
|
|
38
|
+
return node.startPosition.row + 1;
|
|
39
|
+
}
|
|
40
|
+
/** 0-based column of the node's start. */
|
|
41
|
+
export function getColumn(node) {
|
|
42
|
+
return node.startPosition.column;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Pre-order visit of every named descendant of `root` (not `root` itself) —
|
|
46
|
+
* the tree-sitter analog of `lang-typescript`'s `walkNodes`. Named children
|
|
47
|
+
* are the meaningful AST nodes (punctuation/anonymous tokens excluded).
|
|
48
|
+
*/
|
|
49
|
+
export function walkNodes(root, visitor) {
|
|
50
|
+
for (const child of namedChildrenOf(root)) {
|
|
51
|
+
visitor(child);
|
|
52
|
+
walkNodes(child, visitor);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Walk ancestors of `node` (excluding `node`) and return the nearest one
|
|
57
|
+
* matching `predicate`, or `null` at the root — the tree-sitter analog of
|
|
58
|
+
* `lang-typescript`'s `findEnclosingFunction`. Per-language helpers compose
|
|
59
|
+
* this with a node-kind predicate.
|
|
60
|
+
*/
|
|
61
|
+
export function findEnclosing(node, predicate) {
|
|
62
|
+
let current = node.parent;
|
|
63
|
+
while (current) {
|
|
64
|
+
if (predicate(current))
|
|
65
|
+
return current;
|
|
66
|
+
current = current.parent;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=nodes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,IAAU;IAC/B,MAAM,IAAI,GAAG,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,IAAU;IACnC,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAa,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;AAC5D,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,eAAe,CAAC,IAAU;IACxC,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,EAAa,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;AACjE,CAAC;AAED,8BAA8B;AAC9B,MAAM,UAAU,QAAQ,CAAC,IAAU;IACjC,OAAO,IAAI,CAAC,IAAI,CAAC;AACnB,CAAC;AAED,+CAA+C;AAC/C,MAAM,UAAU,aAAa,CAAC,IAAU;IACtC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,GAAG,CAAC,CAAC;AACpC,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,SAAS,CAAC,IAAU;IAClC,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,IAAU,EAAE,OAA6B;IACjE,KAAK,MAAM,KAAK,IAAI,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1C,OAAO,CAAC,KAAK,CAAC,CAAC;QACf,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC5B,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,IAAU,EAAE,SAA+B;IACvE,IAAI,OAAO,GAAgB,IAAI,CAAC,MAAM,CAAC;IACvC,OAAO,OAAO,EAAE,CAAC;QACf,IAAI,SAAS,CAAC,OAAO,CAAC;YAAE,OAAO,OAAO,CAAC;QACvC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAC3B,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neutral tree-sitter types — the grammar-agnostic surface both `lang-*`
|
|
3
|
+
* adapters and the graph adapters consume. Deliberately free of any
|
|
4
|
+
* `@opensip-cli/graph` coupling (the reason this package exists, ADR-0010).
|
|
5
|
+
*/
|
|
6
|
+
import type { Tree } from 'web-tree-sitter';
|
|
7
|
+
export type { Node, Tree, Language } from 'web-tree-sitter';
|
|
8
|
+
/**
|
|
9
|
+
* The parsed-file shape every tree-sitter consumer uses: the parse tree
|
|
10
|
+
* plus the original source text (held so body slices can be extracted for
|
|
11
|
+
* hashing without re-parsing). Matches the graph adapters' historical
|
|
12
|
+
* `{ tree, source }` record exactly.
|
|
13
|
+
*/
|
|
14
|
+
export interface ParsedFile {
|
|
15
|
+
readonly tree: Tree;
|
|
16
|
+
readonly source: string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neutral tree-sitter types — the grammar-agnostic surface both `lang-*`
|
|
3
|
+
* adapters and the graph adapters consume. Deliberately free of any
|
|
4
|
+
* `@opensip-cli/graph` coupling (the reason this package exists, ADR-0010).
|
|
5
|
+
*/
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opensip-cli/tree-sitter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Grammar-agnostic tree-sitter substrate (parser lifecycle + node accessors) shared by lang-* and the graph adapters",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"opensip-cli",
|
|
8
|
+
"static-analysis",
|
|
9
|
+
"code-quality",
|
|
10
|
+
"tree-sitter",
|
|
11
|
+
"ast",
|
|
12
|
+
"parser"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/opensip-ai/opensip-cli.git",
|
|
17
|
+
"directory": "packages/tree-sitter"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/opensip-ai/opensip-cli",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/opensip-ai/opensip-cli/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"NOTICE"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"web-tree-sitter": "0.26.9"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^24.13.2",
|
|
39
|
+
"vitest": "^4.1.8"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc",
|
|
43
|
+
"test": "vitest run --passWithNoTests",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"clean": "rm -rf dist"
|
|
46
|
+
}
|
|
47
|
+
}
|