sift-light 1.0.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/CHANGELOG.md +0 -0
- package/CONTRIBUTING.md +19 -0
- package/LICENSE +661 -0
- package/README.md +177 -0
- package/README.zh-CN.md +177 -0
- package/SECURITY.md +25 -0
- package/package.json +142 -0
- package/plugins/sift-light/.claude-plugin/plugin.json +10 -0
- package/plugins/sift-light/.codex-plugin/plugin.json +20 -0
- package/plugins/sift-light/.mcp.json +24 -0
- package/plugins/sift-light/LICENSE +661 -0
- package/plugins/sift-light/concept-worker.mjs +567 -0
- package/plugins/sift-light/hooks/THIRD_PARTY_NOTICES.txt +70 -0
- package/plugins/sift-light/hooks/hooks.json +16 -0
- package/plugins/sift-light/hooks/search-policy.mjs +4113 -0
- package/plugins/sift-light/hooks/tree-sitter-bash.wasm +0 -0
- package/plugins/sift-light/hooks/tree-sitter-powershell.wasm +0 -0
- package/plugins/sift-light/hooks/tree-sitter.wasm +0 -0
- package/plugins/sift-light/kimi.plugin.json +36 -0
- package/plugins/sift-light/mcp-server.mjs +1 -0
- package/plugins/sift-light/omp-extension.mjs +20914 -0
- package/plugins/sift-light/package.json +11 -0
- package/plugins/sift-light/syntax-worker.mjs +280 -0
- package/plugins/sift-light/syntax-worker.toml +2 -0
- package/src/analysis-evidence.ts +43 -0
- package/src/analysis-limits.ts +27 -0
- package/src/analysis-store.ts +559 -0
- package/src/analysis-term-pages.ts +73 -0
- package/src/analysis-types.ts +162 -0
- package/src/audit-search.ts +391 -0
- package/src/capped-lines.ts +112 -0
- package/src/concept-embedding-cache.ts +186 -0
- package/src/concept-inference.ts +218 -0
- package/src/concept-model.ts +96 -0
- package/src/concept-search.ts +577 -0
- package/src/concept-setup.ts +75 -0
- package/src/concept-source-generation.ts +268 -0
- package/src/concept-worker.mjs +567 -0
- package/src/concept-worker.ts +210 -0
- package/src/config-reader.ts +283 -0
- package/src/config.ts +37 -0
- package/src/context-budget.ts +26 -0
- package/src/discovery-errors.ts +2 -0
- package/src/errors.ts +95 -0
- package/src/evidence-candidates.ts +344 -0
- package/src/evidence-ranking.ts +48 -0
- package/src/evidence-service.ts +1047 -0
- package/src/evidence-validation.ts +466 -0
- package/src/evidence-validity.ts +33 -0
- package/src/excerpt.ts +53 -0
- package/src/file-discovery.ts +150 -0
- package/src/file-metadata-filter.ts +46 -0
- package/src/format.ts +407 -0
- package/src/git-diff.ts +195 -0
- package/src/git-process.ts +189 -0
- package/src/git-repository.ts +367 -0
- package/src/git-source.ts +385 -0
- package/src/historical-paths.ts +205 -0
- package/src/hybrid-search.ts +340 -0
- package/src/import-model.ts +611 -0
- package/src/import-navigation.ts +100 -0
- package/src/import-resolution.ts +240 -0
- package/src/index.ts +97 -0
- package/src/inspect-batch.ts +265 -0
- package/src/inspect.ts +271 -0
- package/src/language-capabilities.ts +157 -0
- package/src/language-capability-definitions.ts +204 -0
- package/src/literal-search.ts +25 -0
- package/src/mcp-cli.ts +21 -0
- package/src/mcp-model-output.ts +120 -0
- package/src/mcp-output.ts +9 -0
- package/src/mcp-semantic-judge.ts +39 -0
- package/src/mcp-server.mjs +14064 -0
- package/src/mcp-server.ts +157 -0
- package/src/mcp-stdio.ts +114 -0
- package/src/mcp.ts +631 -0
- package/src/model-error.ts +120 -0
- package/src/multi-term-search.ts +122 -0
- package/src/omp-index.ts +277 -0
- package/src/omp-marketplace-entry.ts +7 -0
- package/src/operation-lifecycle.ts +413 -0
- package/src/operation-output.ts +100 -0
- package/src/owned-parallel.ts +22 -0
- package/src/owned-process.ts +102 -0
- package/src/owned-task-queue.ts +38 -0
- package/src/package-version.ts +3 -0
- package/src/path-policy.ts +203 -0
- package/src/pi-search-policy.ts +33 -0
- package/src/prompt-guidelines.ts +54 -0
- package/src/python-outline.ts +231 -0
- package/src/record-value.ts +3 -0
- package/src/redaction.ts +95 -0
- package/src/request-contract-catalog.ts +215 -0
- package/src/request-contract-error.ts +96 -0
- package/src/request-contract.ts +481 -0
- package/src/request.ts +132 -0
- package/src/result-statistics.ts +178 -0
- package/src/rg.ts +574 -0
- package/src/ripgrep-diagnostics.ts +122 -0
- package/src/ripgrep-executable.ts +43 -0
- package/src/runtime.ts +53 -0
- package/src/scan-revisions.ts +148 -0
- package/src/script-runtime.ts +11 -0
- package/src/search-policy-commands.ts +170 -0
- package/src/search-policy-hook.ts +61 -0
- package/src/search-policy-recovery.ts +388 -0
- package/src/search-policy-shell.ts +280 -0
- package/src/search-policy.ts +102 -0
- package/src/search-retention.ts +77 -0
- package/src/semantic-judge-batches.ts +135 -0
- package/src/semantic-judge.ts +519 -0
- package/src/service.ts +945 -0
- package/src/session-summary.ts +100 -0
- package/src/snapshot-store.ts +203 -0
- package/src/source-access.ts +288 -0
- package/src/source-continuations.ts +168 -0
- package/src/source-document.ts +277 -0
- package/src/source-inspection.ts +731 -0
- package/src/source-pages.ts +112 -0
- package/src/source.ts +286 -0
- package/src/structural-search.ts +125 -0
- package/src/structure.ts +259 -0
- package/src/summary-previews.ts +96 -0
- package/src/summary.ts +79 -0
- package/src/syntax-facts.ts +464 -0
- package/src/syntax-fields.ts +161 -0
- package/src/syntax-search.ts +361 -0
- package/src/syntax-tree.ts +30 -0
- package/src/syntax-types.ts +72 -0
- package/src/syntax-worker.mjs +280 -0
- package/src/syntax-worker.toml +2 -0
- package/src/syntax-worker.ts +143 -0
- package/src/syntax.ts +259 -0
- package/src/test-navigation-facts.ts +261 -0
- package/src/test-navigation.ts +543 -0
- package/src/tool-schema.ts +321 -0
- package/src/tui/dashboard.ts +107 -0
- package/src/tui/layout.ts +291 -0
- package/src/tui/palette.ts +73 -0
- package/src/tui/presentation.ts +240 -0
- package/src/tui/renderers.ts +106 -0
- package/src/types.ts +368 -0
- package/src/validation-output.ts +148 -0
- package/src/validation-types.ts +48 -0
- package/src/workspace-files.ts +142 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { syntaxField, syntaxFields, syntaxText } from "./syntax-tree.js";
|
|
2
|
+
import type {
|
|
3
|
+
SyntaxAnalysis,
|
|
4
|
+
SyntaxLanguage,
|
|
5
|
+
SyntaxNode,
|
|
6
|
+
SyntaxRole,
|
|
7
|
+
SyntaxRoleName,
|
|
8
|
+
SyntaxSymbol,
|
|
9
|
+
} from "./syntax-types.js";
|
|
10
|
+
|
|
11
|
+
const IMPLEMENTATIONS = new Set([
|
|
12
|
+
"function_declaration",
|
|
13
|
+
"generator_function_declaration",
|
|
14
|
+
"function_expression",
|
|
15
|
+
"generator_function",
|
|
16
|
+
"arrow_function",
|
|
17
|
+
"method_definition",
|
|
18
|
+
"method_declaration",
|
|
19
|
+
"func_literal",
|
|
20
|
+
]);
|
|
21
|
+
const SIGNATURES = new Set([
|
|
22
|
+
"function_signature",
|
|
23
|
+
"method_signature",
|
|
24
|
+
"abstract_method_signature",
|
|
25
|
+
"construct_signature",
|
|
26
|
+
"call_signature",
|
|
27
|
+
"method_elem",
|
|
28
|
+
]);
|
|
29
|
+
const CONTAINERS = new Set([
|
|
30
|
+
"class_declaration",
|
|
31
|
+
"abstract_class_declaration",
|
|
32
|
+
"class",
|
|
33
|
+
"interface_declaration",
|
|
34
|
+
]);
|
|
35
|
+
const TYPE_SYMBOLS = new Set([
|
|
36
|
+
"type_alias_declaration",
|
|
37
|
+
"enum_declaration",
|
|
38
|
+
"type_spec",
|
|
39
|
+
"type_alias",
|
|
40
|
+
]);
|
|
41
|
+
const BINDING_IDENTIFIERS = new Set([
|
|
42
|
+
"identifier",
|
|
43
|
+
"shorthand_property_identifier_pattern",
|
|
44
|
+
"private_property_identifier",
|
|
45
|
+
"property_identifier",
|
|
46
|
+
"type_identifier",
|
|
47
|
+
"field_identifier",
|
|
48
|
+
]);
|
|
49
|
+
const STRINGS = new Set([
|
|
50
|
+
"string",
|
|
51
|
+
"interpreted_string_literal",
|
|
52
|
+
"raw_string_literal",
|
|
53
|
+
"rune_literal",
|
|
54
|
+
]);
|
|
55
|
+
const TYPE_AREAS = new Set([
|
|
56
|
+
"type_annotation",
|
|
57
|
+
"type_arguments",
|
|
58
|
+
"type_parameters",
|
|
59
|
+
"type_identifier",
|
|
60
|
+
"predefined_type",
|
|
61
|
+
"type_alias_declaration",
|
|
62
|
+
"interface_body",
|
|
63
|
+
"extends_type_clause",
|
|
64
|
+
"implements_clause",
|
|
65
|
+
"array_type",
|
|
66
|
+
"conditional_type",
|
|
67
|
+
"constructor_type",
|
|
68
|
+
"existential_type",
|
|
69
|
+
"flow_maybe_type",
|
|
70
|
+
"function_type",
|
|
71
|
+
"generic_type",
|
|
72
|
+
"index_type_query",
|
|
73
|
+
"infer_type",
|
|
74
|
+
"intersection_type",
|
|
75
|
+
"literal_type",
|
|
76
|
+
"lookup_type",
|
|
77
|
+
"nested_type_identifier",
|
|
78
|
+
"object_type",
|
|
79
|
+
"parenthesized_type",
|
|
80
|
+
"readonly_type",
|
|
81
|
+
"template_literal_type",
|
|
82
|
+
"this_type",
|
|
83
|
+
"tuple_type",
|
|
84
|
+
"type_query",
|
|
85
|
+
"union_type",
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
interface Interval {
|
|
89
|
+
start: number;
|
|
90
|
+
end: number;
|
|
91
|
+
}
|
|
92
|
+
type Tree = Pick<SyntaxAnalysis, "nodes" | "children">;
|
|
93
|
+
|
|
94
|
+
function mergeIntervals(intervals: Interval[]): Interval[] {
|
|
95
|
+
const merged: Interval[] = [];
|
|
96
|
+
intervals.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
97
|
+
for (const interval of intervals) {
|
|
98
|
+
const previous = merged.at(-1);
|
|
99
|
+
if (previous && interval.start <= previous.end)
|
|
100
|
+
previous.end = Math.max(previous.end, interval.end);
|
|
101
|
+
else merged.push({ ...interval });
|
|
102
|
+
}
|
|
103
|
+
return merged;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function subtract(range: Interval, excluded: readonly Interval[]): Interval[] {
|
|
107
|
+
const results: Interval[] = [];
|
|
108
|
+
let low = 0,
|
|
109
|
+
high = excluded.length;
|
|
110
|
+
while (low < high) {
|
|
111
|
+
const middle = (low + high) >>> 1;
|
|
112
|
+
if ((excluded[middle]?.end ?? Infinity) <= range.start) low = middle + 1;
|
|
113
|
+
else high = middle;
|
|
114
|
+
}
|
|
115
|
+
let start = range.start;
|
|
116
|
+
for (let i = low; i < excluded.length; i++) {
|
|
117
|
+
const gap = excluded[i];
|
|
118
|
+
if (!gap || gap.start >= range.end) break;
|
|
119
|
+
if (gap.start > start) results.push({ start, end: gap.start });
|
|
120
|
+
start = Math.max(start, gap.end);
|
|
121
|
+
}
|
|
122
|
+
if (start < range.end) results.push({ start, end: range.end });
|
|
123
|
+
return results;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function containedIntervals(range: Interval, sorted: readonly Interval[]): Interval[] {
|
|
127
|
+
let low = 0,
|
|
128
|
+
high = sorted.length;
|
|
129
|
+
while (low < high) {
|
|
130
|
+
const middle = (low + high) >>> 1;
|
|
131
|
+
if ((sorted[middle]?.start ?? Infinity) < range.start) low = middle + 1;
|
|
132
|
+
else high = middle;
|
|
133
|
+
}
|
|
134
|
+
const contained: Interval[] = [];
|
|
135
|
+
for (let i = low; i < sorted.length; i++) {
|
|
136
|
+
const item = sorted[i];
|
|
137
|
+
if (!item || item.start >= range.end) break;
|
|
138
|
+
if (item.end <= range.end) contained.push(item);
|
|
139
|
+
}
|
|
140
|
+
return mergeIntervals(contained);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function bindingNodes(tree: Tree, index: number): number[] {
|
|
144
|
+
const results: number[] = [];
|
|
145
|
+
const stack = [index];
|
|
146
|
+
while (stack.length) {
|
|
147
|
+
const id = stack.pop();
|
|
148
|
+
if (id === undefined) break;
|
|
149
|
+
const node = tree.nodes[id];
|
|
150
|
+
if (!node) continue;
|
|
151
|
+
if (BINDING_IDENTIFIERS.has(node.kind)) {
|
|
152
|
+
results.push(id);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (node.kind === "pair_pattern") {
|
|
156
|
+
const value = syntaxField(tree, id, "value");
|
|
157
|
+
if (value !== undefined) stack.push(value);
|
|
158
|
+
} else if (node.kind === "assignment_pattern" || node.kind === "object_assignment_pattern") {
|
|
159
|
+
const left = syntaxField(tree, id, "left");
|
|
160
|
+
if (left !== undefined) stack.push(left);
|
|
161
|
+
} else {
|
|
162
|
+
for (const child of tree.children[id] ?? []) {
|
|
163
|
+
const value = tree.nodes[child];
|
|
164
|
+
if (value?.named && value.field !== "type" && value.field !== "value") stack.push(child);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return results;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function attachedName(tree: Tree, index: number, text: string): string {
|
|
172
|
+
const node = tree.nodes[index];
|
|
173
|
+
if (!node) return "<anonymous>";
|
|
174
|
+
const ownName = syntaxField(tree, index, "name");
|
|
175
|
+
const own = ownName === undefined ? undefined : tree.nodes[ownName];
|
|
176
|
+
if (own) return syntaxText(own, text);
|
|
177
|
+
const parent = node.parent === null ? undefined : tree.nodes[node.parent];
|
|
178
|
+
if (parent && node.parent !== null) {
|
|
179
|
+
const binding =
|
|
180
|
+
syntaxField(tree, node.parent, parent.kind === "pair" ? "key" : "name") ??
|
|
181
|
+
(parent.kind === "assignment_expression"
|
|
182
|
+
? syntaxField(tree, node.parent, "left")
|
|
183
|
+
: undefined);
|
|
184
|
+
const target = binding === undefined ? undefined : tree.nodes[binding];
|
|
185
|
+
if (target) return syntaxText(target, text);
|
|
186
|
+
if (parent.kind === "export_statement") return "default";
|
|
187
|
+
}
|
|
188
|
+
return `<anonymous@${node.start}>`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function directlyExported(tree: Tree, index: number): boolean {
|
|
192
|
+
let current: number | null = tree.nodes[index]?.parent ?? null;
|
|
193
|
+
while (current !== null) {
|
|
194
|
+
const node: SyntaxNode | undefined = tree.nodes[current];
|
|
195
|
+
if (!node) return false;
|
|
196
|
+
if (node.kind === "export_statement") return true;
|
|
197
|
+
if (
|
|
198
|
+
![
|
|
199
|
+
"variable_declarator",
|
|
200
|
+
"lexical_declaration",
|
|
201
|
+
"variable_declaration",
|
|
202
|
+
"ambient_declaration",
|
|
203
|
+
].includes(node.kind)
|
|
204
|
+
)
|
|
205
|
+
return false;
|
|
206
|
+
current = node.parent;
|
|
207
|
+
}
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function goExported(tree: Tree, index: number, name: string, inFunction: boolean): boolean {
|
|
212
|
+
if (!/^\p{Lu}/u.test(name)) return false;
|
|
213
|
+
const kind = tree.nodes[index]?.kind;
|
|
214
|
+
if (kind === "method_declaration" || kind === "field_declaration" || kind === "method_elem")
|
|
215
|
+
return true;
|
|
216
|
+
return (
|
|
217
|
+
!inFunction &&
|
|
218
|
+
["function_declaration", "type_spec", "type_alias", "var_spec", "const_spec"].includes(
|
|
219
|
+
kind ?? "",
|
|
220
|
+
)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Derive bounded facts once; no downstream consumer needs another native parse. */
|
|
225
|
+
export function deriveSyntaxFacts(
|
|
226
|
+
tree: Tree,
|
|
227
|
+
language: SyntaxLanguage,
|
|
228
|
+
text: string,
|
|
229
|
+
): { symbols: SyntaxSymbol[]; roles: SyntaxRole[] } {
|
|
230
|
+
const { nodes, children } = tree;
|
|
231
|
+
const roles: SyntaxRole[] = [];
|
|
232
|
+
const symbols: SyntaxSymbol[] = [];
|
|
233
|
+
const lexical: Interval[] = [];
|
|
234
|
+
const comments: Interval[] = [];
|
|
235
|
+
const nestedCallContent: Interval[] = [];
|
|
236
|
+
const scopes: (string | undefined)[] = [];
|
|
237
|
+
const functionScopes: boolean[] = [];
|
|
238
|
+
const semanticCalls: { node: number; range: SyntaxNode; subkind: string; candidate: boolean }[] =
|
|
239
|
+
[];
|
|
240
|
+
const semanticImports: { node: number; range: SyntaxNode; role: "import" | "export" }[] = [];
|
|
241
|
+
const add = (id: number, role: SyntaxRoleName, subkind?: string, candidate = false) => {
|
|
242
|
+
const node = nodes[id];
|
|
243
|
+
if (!node || node.start === node.end) return;
|
|
244
|
+
roles.push({
|
|
245
|
+
start: node.start,
|
|
246
|
+
end: node.end,
|
|
247
|
+
role,
|
|
248
|
+
certainty: candidate ? "candidate" : "syntax",
|
|
249
|
+
node: id,
|
|
250
|
+
...(subkind ? { subkind } : {}),
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
for (let index = 0; index < nodes.length; index++) {
|
|
255
|
+
const node = nodes[index];
|
|
256
|
+
if (!node) continue;
|
|
257
|
+
const parent = node.parent === null ? undefined : nodes[node.parent];
|
|
258
|
+
const outerScope = node.parent === null ? undefined : scopes[node.parent];
|
|
259
|
+
const inFunction = node.parent === null ? false : (functionScopes[node.parent] ?? false);
|
|
260
|
+
scopes[index] = outerScope;
|
|
261
|
+
functionScopes[index] = inFunction || IMPLEMENTATIONS.has(node.kind);
|
|
262
|
+
|
|
263
|
+
if (node.kind === "comment") {
|
|
264
|
+
add(index, "comment");
|
|
265
|
+
lexical.push(node);
|
|
266
|
+
comments.push(node);
|
|
267
|
+
} else if (node.named && STRINGS.has(node.kind)) {
|
|
268
|
+
add(index, "string", node.kind);
|
|
269
|
+
lexical.push(node);
|
|
270
|
+
} else if (node.kind === "template_string") {
|
|
271
|
+
const substitutions = (children[index] ?? [])
|
|
272
|
+
.map((child) => nodes[child])
|
|
273
|
+
.filter((child): child is SyntaxNode => child?.kind === "template_substitution");
|
|
274
|
+
for (const range of subtract(node, mergeIntervals(substitutions))) {
|
|
275
|
+
roles.push({
|
|
276
|
+
...range,
|
|
277
|
+
role: "string",
|
|
278
|
+
certainty: "syntax",
|
|
279
|
+
subkind: "template-static",
|
|
280
|
+
node: index,
|
|
281
|
+
});
|
|
282
|
+
lexical.push(range);
|
|
283
|
+
}
|
|
284
|
+
} else if (node.kind === "jsx_text") {
|
|
285
|
+
add(index, "jsx-text");
|
|
286
|
+
lexical.push(node);
|
|
287
|
+
} else if (node.kind === "regex" || node.kind === "regex_pattern") {
|
|
288
|
+
add(index, "unknown", "regex-literal");
|
|
289
|
+
lexical.push(node);
|
|
290
|
+
} else if (language !== "go" && TYPE_AREAS.has(node.kind)) {
|
|
291
|
+
add(index, "unknown", "type");
|
|
292
|
+
lexical.push(node);
|
|
293
|
+
}
|
|
294
|
+
if (node.kind === "as_expression" || node.kind === "satisfies_expression") {
|
|
295
|
+
let afterOperator = false;
|
|
296
|
+
for (const child of children[index] ?? []) {
|
|
297
|
+
const target = nodes[child];
|
|
298
|
+
if (!target) continue;
|
|
299
|
+
if (target.kind === "as" || target.kind === "satisfies") afterOperator = true;
|
|
300
|
+
else if (afterOperator) {
|
|
301
|
+
add(child, "unknown", "type");
|
|
302
|
+
lexical.push(target);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (["arguments", "argument_list", "formal_parameters", "parameter_list"].includes(node.kind)) {
|
|
308
|
+
nestedCallContent.push(node);
|
|
309
|
+
}
|
|
310
|
+
if (IMPLEMENTATIONS.has(node.kind)) {
|
|
311
|
+
const bodyId = syntaxField(tree, index, "body");
|
|
312
|
+
const body = bodyId === undefined ? undefined : nodes[bodyId];
|
|
313
|
+
if (body) nestedCallContent.push(body);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const isImplementation = IMPLEMENTATIONS.has(node.kind);
|
|
317
|
+
const isStructure =
|
|
318
|
+
node.named &&
|
|
319
|
+
(isImplementation ||
|
|
320
|
+
SIGNATURES.has(node.kind) ||
|
|
321
|
+
CONTAINERS.has(node.kind) ||
|
|
322
|
+
TYPE_SYMBOLS.has(node.kind));
|
|
323
|
+
const isVariable =
|
|
324
|
+
node.named && ["variable_declarator", "var_spec", "const_spec"].includes(node.kind);
|
|
325
|
+
const isField =
|
|
326
|
+
node.named &&
|
|
327
|
+
[
|
|
328
|
+
"public_field_definition",
|
|
329
|
+
"field_definition",
|
|
330
|
+
"field_declaration",
|
|
331
|
+
"property_signature",
|
|
332
|
+
].includes(node.kind);
|
|
333
|
+
if (isStructure || isVariable || isField) {
|
|
334
|
+
const nameIds = syntaxFields(tree, index, "name").flatMap((name) => bindingNodes(tree, name));
|
|
335
|
+
for (const id of nameIds) {
|
|
336
|
+
if (syntaxText(nodes[id]!, text) !== "_") {
|
|
337
|
+
add(id, "declaration", node.kind);
|
|
338
|
+
const name = syntaxText(nodes[id]!, text);
|
|
339
|
+
if (
|
|
340
|
+
language === "go"
|
|
341
|
+
? goExported(tree, index, name, inFunction)
|
|
342
|
+
: directlyExported(tree, index)
|
|
343
|
+
) {
|
|
344
|
+
add(id, "export", language === "go" ? "exported-identifier" : "exported-declaration");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const valueId = syntaxField(tree, index, "value");
|
|
349
|
+
const value = valueId === undefined ? undefined : nodes[valueId];
|
|
350
|
+
const variableHasOwnImplementation = value && IMPLEMENTATIONS.has(value.kind);
|
|
351
|
+
if (isStructure || (isVariable && !inFunction && !variableHasOwnImplementation) || isField) {
|
|
352
|
+
const name = attachedName(tree, index, text);
|
|
353
|
+
const bodyId = syntaxField(tree, index, "body");
|
|
354
|
+
const body = bodyId === undefined ? undefined : nodes[bodyId];
|
|
355
|
+
const hasBody = isImplementation && body !== undefined;
|
|
356
|
+
const symbol: SyntaxSymbol = {
|
|
357
|
+
name,
|
|
358
|
+
kind: node.kind,
|
|
359
|
+
start: node.start,
|
|
360
|
+
end: node.end,
|
|
361
|
+
hasBody,
|
|
362
|
+
exported:
|
|
363
|
+
language === "go"
|
|
364
|
+
? goExported(tree, index, name, inFunction)
|
|
365
|
+
: directlyExported(tree, index),
|
|
366
|
+
node: index,
|
|
367
|
+
...(outerScope ? { scope: outerScope } : {}),
|
|
368
|
+
...(hasBody && body ? { bodyStart: body.start, bodyEnd: body.end } : {}),
|
|
369
|
+
};
|
|
370
|
+
symbols.push(symbol);
|
|
371
|
+
if (isImplementation || CONTAINERS.has(node.kind)) scopes[index] = name;
|
|
372
|
+
}
|
|
373
|
+
} else if (node.kind === "object" && parent?.kind === "variable_declarator") {
|
|
374
|
+
scopes[index] = attachedName(tree, index, text);
|
|
375
|
+
} else if (language === "go" && node.kind === "short_var_declaration") {
|
|
376
|
+
const left = syntaxField(tree, index, "left");
|
|
377
|
+
if (left !== undefined) {
|
|
378
|
+
for (const id of bindingNodes(tree, left)) {
|
|
379
|
+
if (syntaxText(nodes[id]!, text) !== "_")
|
|
380
|
+
add(id, "declaration", "short-variable-candidate", true);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (node.kind === "call_expression" || node.kind === "new_expression") {
|
|
386
|
+
const field = node.kind === "new_expression" ? "constructor" : "function";
|
|
387
|
+
const calleeId = syntaxField(tree, index, field);
|
|
388
|
+
const callee = calleeId === undefined ? undefined : nodes[calleeId];
|
|
389
|
+
if (callee) {
|
|
390
|
+
const optional = (children[index] ?? []).some(
|
|
391
|
+
(child) => nodes[child]?.kind === "optional_chain",
|
|
392
|
+
);
|
|
393
|
+
semanticCalls.push({
|
|
394
|
+
node: index,
|
|
395
|
+
range: callee,
|
|
396
|
+
subkind:
|
|
397
|
+
language === "go"
|
|
398
|
+
? callee.kind === "func_literal"
|
|
399
|
+
? "call"
|
|
400
|
+
: "call-or-conversion"
|
|
401
|
+
: node.kind === "new_expression"
|
|
402
|
+
? "constructor"
|
|
403
|
+
: optional
|
|
404
|
+
? "optional-call"
|
|
405
|
+
: "call",
|
|
406
|
+
candidate: language === "go" && callee.kind !== "func_literal",
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (["import_statement", "import_spec", "export_statement"].includes(node.kind)) {
|
|
411
|
+
const role = node.kind === "export_statement" ? "export" : "import";
|
|
412
|
+
for (const child of children[index] ?? []) {
|
|
413
|
+
const target = nodes[child];
|
|
414
|
+
if (
|
|
415
|
+
target &&
|
|
416
|
+
(["source", "path", "name"].includes(target.field ?? "") ||
|
|
417
|
+
["import_clause", "export_clause", "import", "export", "default"].includes(target.kind))
|
|
418
|
+
)
|
|
419
|
+
semanticImports.push({ node: child, range: target, role });
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (language === "go" && node.kind === "import") add(index, "import", "import-keyword");
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const excluded = mergeIntervals(lexical);
|
|
426
|
+
nestedCallContent.sort((a, b) => a.start - b.start);
|
|
427
|
+
const commentExcluded = mergeIntervals(comments);
|
|
428
|
+
for (const call of semanticCalls) {
|
|
429
|
+
const callExcluded = containedIntervals(call.range, nestedCallContent);
|
|
430
|
+
for (const lexicalRange of subtract(call.range, excluded)) {
|
|
431
|
+
for (const range of subtract(lexicalRange, callExcluded)) {
|
|
432
|
+
roles.push({
|
|
433
|
+
...range,
|
|
434
|
+
role: "call",
|
|
435
|
+
certainty: call.candidate ? "candidate" : "syntax",
|
|
436
|
+
subkind: call.subkind,
|
|
437
|
+
node: call.node,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
for (const item of semanticImports) {
|
|
443
|
+
for (const range of subtract(item.range, commentExcluded)) {
|
|
444
|
+
roles.push({ ...range, role: item.role, certainty: "syntax", node: item.node });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
for (const range of subtract({ start: 0, end: text.length }, excluded)) {
|
|
448
|
+
roles.push({ ...range, role: "code", certainty: "syntax", node: 0 });
|
|
449
|
+
}
|
|
450
|
+
roles.sort((a, b) => a.start - b.start || a.end - b.end || a.role.localeCompare(b.role));
|
|
451
|
+
return { symbols, roles };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Whole-occurrence classification; crossing a role boundary is not a role match. */
|
|
455
|
+
export function classifySyntaxRange(
|
|
456
|
+
analysis: SyntaxAnalysis,
|
|
457
|
+
start: number,
|
|
458
|
+
end: number,
|
|
459
|
+
): SyntaxRole[] {
|
|
460
|
+
if (analysis.status !== "ok" || start < 0 || end < start) return [];
|
|
461
|
+
return analysis.roles.filter(
|
|
462
|
+
(role) => role.start <= start && end <= role.end && start < role.end,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Field-name metadata derived from @ast-grep/napi 0.45.2
|
|
3
|
+
* (tree-sitter-javascript 0.25.0, tree-sitter-typescript/tsx 0.23.2)
|
|
4
|
+
* and @ast-grep/lang-go 0.0.6 (tree-sitter-go 0.25.0).
|
|
5
|
+
*
|
|
6
|
+
* MIT License
|
|
7
|
+
* Copyright (c) 2020 N-API for Rust
|
|
8
|
+
* Copyright (c) 2025 ast-grep
|
|
9
|
+
*
|
|
10
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
* in the Software without restriction, including without limitation the rights
|
|
13
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
* furnished to do so, subject to the following conditions:
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in
|
|
17
|
+
* all copies or substantial portions of the Software.
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24
|
+
* THE SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export const JAVASCRIPT_FIELDS: Readonly<Record<string, readonly string[]>> = {
|
|
28
|
+
arrow_function: ["body", "parameter", "parameters", "return_type", "type_parameters"],
|
|
29
|
+
assignment_expression: ["left", "right"],
|
|
30
|
+
assignment_pattern: ["left", "right"],
|
|
31
|
+
augmented_assignment_expression: ["left", "operator", "right"],
|
|
32
|
+
binary_expression: ["left", "operator", "right"],
|
|
33
|
+
break_statement: ["label"],
|
|
34
|
+
call_expression: ["arguments", "function", "optional_chain", "type_arguments"],
|
|
35
|
+
catch_clause: ["body", "parameter", "type"],
|
|
36
|
+
class: ["body", "decorator", "name", "type_parameters"],
|
|
37
|
+
class_body: ["decorator", "member"],
|
|
38
|
+
class_declaration: ["body", "decorator", "name", "type_parameters"],
|
|
39
|
+
class_static_block: ["body"],
|
|
40
|
+
continue_statement: ["label"],
|
|
41
|
+
do_statement: ["body", "condition"],
|
|
42
|
+
export_specifier: ["alias", "name"],
|
|
43
|
+
export_statement: ["declaration", "decorator", "source", "value"],
|
|
44
|
+
field_definition: ["decorator", "property", "value"],
|
|
45
|
+
finally_clause: ["body"],
|
|
46
|
+
for_in_statement: ["body", "kind", "left", "operator", "right", "value"],
|
|
47
|
+
for_statement: ["body", "condition", "increment", "initializer"],
|
|
48
|
+
function_declaration: ["body", "name", "parameters", "return_type", "type_parameters"],
|
|
49
|
+
function_expression: ["body", "name", "parameters", "return_type", "type_parameters"],
|
|
50
|
+
generator_function: ["body", "name", "parameters", "return_type", "type_parameters"],
|
|
51
|
+
generator_function_declaration: ["body", "name", "parameters", "return_type", "type_parameters"],
|
|
52
|
+
if_statement: ["alternative", "condition", "consequence"],
|
|
53
|
+
import_specifier: ["alias", "name"],
|
|
54
|
+
import_statement: ["source"],
|
|
55
|
+
jsx_closing_element: ["name"],
|
|
56
|
+
jsx_element: ["close_tag", "open_tag"],
|
|
57
|
+
jsx_opening_element: ["attribute", "name", "type_arguments"],
|
|
58
|
+
jsx_self_closing_element: ["attribute", "name", "type_arguments"],
|
|
59
|
+
labeled_statement: ["body", "label"],
|
|
60
|
+
lexical_declaration: ["kind"],
|
|
61
|
+
member_expression: ["object", "optional_chain", "property"],
|
|
62
|
+
method_definition: ["body", "decorator", "name", "parameters", "return_type", "type_parameters"],
|
|
63
|
+
new_expression: ["arguments", "constructor", "type_arguments"],
|
|
64
|
+
object_assignment_pattern: ["left", "right"],
|
|
65
|
+
pair: ["key", "value"],
|
|
66
|
+
pair_pattern: ["key", "value"],
|
|
67
|
+
regex: ["flags", "pattern"],
|
|
68
|
+
subscript_expression: ["index", "object", "optional_chain"],
|
|
69
|
+
switch_case: ["body", "value"],
|
|
70
|
+
switch_default: ["body"],
|
|
71
|
+
switch_statement: ["body", "value"],
|
|
72
|
+
ternary_expression: ["alternative", "condition", "consequence"],
|
|
73
|
+
try_statement: ["body", "finalizer", "handler"],
|
|
74
|
+
unary_expression: ["argument", "operator"],
|
|
75
|
+
update_expression: ["argument", "operator"],
|
|
76
|
+
using_declaration: ["kind"],
|
|
77
|
+
variable_declarator: ["name", "type", "value"],
|
|
78
|
+
while_statement: ["body", "condition"],
|
|
79
|
+
with_statement: ["body", "object"],
|
|
80
|
+
abstract_class_declaration: ["body", "decorator", "name", "type_parameters"],
|
|
81
|
+
abstract_method_signature: ["name", "parameters", "return_type", "type_parameters"],
|
|
82
|
+
call_signature: ["parameters", "return_type", "type_parameters"],
|
|
83
|
+
conditional_type: ["alternative", "consequence", "left", "right"],
|
|
84
|
+
construct_signature: ["parameters", "type", "type_parameters"],
|
|
85
|
+
constructor_type: ["parameters", "type", "type_parameters"],
|
|
86
|
+
enum_assignment: ["name", "value"],
|
|
87
|
+
enum_body: ["name"],
|
|
88
|
+
enum_declaration: ["body", "name"],
|
|
89
|
+
extends_clause: ["type_arguments", "value"],
|
|
90
|
+
extends_type_clause: ["type"],
|
|
91
|
+
function_signature: ["name", "parameters", "return_type", "type_parameters"],
|
|
92
|
+
function_type: ["parameters", "return_type", "type_parameters"],
|
|
93
|
+
generic_type: ["name", "type_arguments"],
|
|
94
|
+
import_require_clause: ["source"],
|
|
95
|
+
index_signature: ["index_type", "name", "sign", "type"],
|
|
96
|
+
instantiation_expression: ["function", "type_arguments"],
|
|
97
|
+
interface_declaration: ["body", "name", "type_parameters"],
|
|
98
|
+
internal_module: ["body", "name"],
|
|
99
|
+
mapped_type_clause: ["alias", "name", "type"],
|
|
100
|
+
method_signature: ["name", "parameters", "return_type", "type_parameters"],
|
|
101
|
+
module: ["body", "name"],
|
|
102
|
+
nested_identifier: ["object", "property"],
|
|
103
|
+
nested_type_identifier: ["module", "name"],
|
|
104
|
+
optional_parameter: ["decorator", "name", "pattern", "type", "value"],
|
|
105
|
+
parenthesized_expression: ["type"],
|
|
106
|
+
property_signature: ["name", "type"],
|
|
107
|
+
public_field_definition: ["decorator", "name", "type", "value"],
|
|
108
|
+
required_parameter: ["decorator", "name", "pattern", "type", "value"],
|
|
109
|
+
type_alias_declaration: ["name", "type_parameters", "value"],
|
|
110
|
+
type_parameter: ["constraint", "name", "value"],
|
|
111
|
+
type_predicate: ["name", "type"],
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const GO_FIELDS: Readonly<Record<string, readonly string[]>> = {
|
|
115
|
+
array_type: ["element", "length"],
|
|
116
|
+
assignment_statement: ["left", "operator", "right"],
|
|
117
|
+
binary_expression: ["left", "operator", "right"],
|
|
118
|
+
call_expression: ["arguments", "function", "type_arguments"],
|
|
119
|
+
channel_type: ["value"],
|
|
120
|
+
communication_case: ["communication"],
|
|
121
|
+
composite_literal: ["body", "type"],
|
|
122
|
+
const_spec: ["name", "type", "value"],
|
|
123
|
+
expression_case: ["value"],
|
|
124
|
+
expression_switch_statement: ["initializer", "value"],
|
|
125
|
+
field_declaration: ["name", "tag", "type"],
|
|
126
|
+
for_clause: ["condition", "initializer", "update"],
|
|
127
|
+
for_statement: ["body"],
|
|
128
|
+
func_literal: ["body", "parameters", "result"],
|
|
129
|
+
function_declaration: ["body", "name", "parameters", "result", "type_parameters"],
|
|
130
|
+
function_type: ["parameters", "result"],
|
|
131
|
+
generic_type: ["type", "type_arguments"],
|
|
132
|
+
if_statement: ["alternative", "condition", "consequence", "initializer"],
|
|
133
|
+
implicit_length_array_type: ["element"],
|
|
134
|
+
import_spec: ["name", "path"],
|
|
135
|
+
index_expression: ["index", "operand"],
|
|
136
|
+
keyed_element: ["key", "value"],
|
|
137
|
+
labeled_statement: ["label"],
|
|
138
|
+
map_type: ["key", "value"],
|
|
139
|
+
method_declaration: ["body", "name", "parameters", "receiver", "result"],
|
|
140
|
+
method_elem: ["name", "parameters", "result"],
|
|
141
|
+
parameter_declaration: ["name", "type"],
|
|
142
|
+
qualified_type: ["name", "package"],
|
|
143
|
+
range_clause: ["left", "right"],
|
|
144
|
+
receive_statement: ["left", "right"],
|
|
145
|
+
selector_expression: ["field", "operand"],
|
|
146
|
+
send_statement: ["channel", "value"],
|
|
147
|
+
short_var_declaration: ["left", "right"],
|
|
148
|
+
slice_expression: ["capacity", "end", "operand", "start"],
|
|
149
|
+
slice_type: ["element"],
|
|
150
|
+
type_alias: ["name", "type", "type_parameters"],
|
|
151
|
+
type_assertion_expression: ["operand", "type"],
|
|
152
|
+
type_case: ["type"],
|
|
153
|
+
type_conversion_expression: ["operand", "type"],
|
|
154
|
+
type_instantiation_expression: ["type"],
|
|
155
|
+
type_parameter_declaration: ["name", "type"],
|
|
156
|
+
type_spec: ["name", "type", "type_parameters"],
|
|
157
|
+
type_switch_statement: ["alias", "initializer", "value"],
|
|
158
|
+
unary_expression: ["operand", "operator"],
|
|
159
|
+
var_spec: ["name", "type", "value"],
|
|
160
|
+
variadic_parameter_declaration: ["name", "type"],
|
|
161
|
+
};
|