forked from colbymchenry/codegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluau.ts
More file actions
36 lines (32 loc) · 1.69 KB
/
luau.ts
File metadata and controls
36 lines (32 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { getNodeText, getChildByField } from '../tree-sitter-helpers';
import type { LanguageExtractor } from '../tree-sitter-types';
import { luaExtractor } from './lua';
// Luau (https://luau.org) is a gradually-typed superset of Lua. The
// tree-sitter-luau grammar reuses the same node names as the vendored Lua
// grammar (function_declaration, variable_declaration, function_call,
// dot/method_index_expression, …), so the Luau extractor extends the Lua one
// and adds the type-system pieces Luau introduces:
// - `type X = ...` / `export type X = ...` → type_definition (type_alias)
// - typed parameters and return types → richer signatures
//
// require detection, receiver-splitting (t.f / t:m → methods), and local
// variable extraction are inherited unchanged from luaExtractor. The shared
// `extractVariable` core branch is gated on `lua` || `luau`.
export const luauExtractor: LanguageExtractor = {
...luaExtractor,
// `type X = ...` and `export type X = ...`
typeAliasTypes: ['type_definition'],
// Only Luau `export type` is exported; the keyword leads the node.
isExported: (node, source) => source.slice(node.startIndex, node.startIndex + 7) === 'export ',
// Params + Luau return type (the named child after `parameters`, before the body).
getSignature: (node, source) => {
const params = getChildByField(node, 'parameters');
if (!params) return undefined;
let sig = getNodeText(params, source);
const kids = node.namedChildren;
const idx = kids.findIndex((c) => c.startIndex === params.startIndex);
const ret = idx >= 0 ? kids[idx + 1] : null;
if (ret && ret.type !== 'block') sig += `: ${getNodeText(ret, source)}`;
return sig;
},
};