@fabersep/atlas 0.0.0-stage → 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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/README.md +139 -2
- package/dist/packs/csharp/calls.d.ts +4 -0
- package/dist/packs/csharp/calls.js +167 -0
- package/dist/packs/csharp/declarations.d.ts +4 -0
- package/dist/packs/csharp/declarations.js +142 -0
- package/dist/packs/csharp/pack.d.ts +2 -0
- package/dist/packs/csharp/pack.js +39 -0
- package/dist/packs/csharp/resolve.d.ts +2 -0
- package/dist/packs/csharp/resolve.js +35 -0
- package/dist/packs/csharp/signals.d.ts +7 -0
- package/dist/packs/csharp/signals.js +143 -0
- package/dist/packs/dart/calls.d.ts +4 -0
- package/dist/packs/dart/calls.js +130 -0
- package/dist/packs/dart/declarations.d.ts +11 -0
- package/dist/packs/dart/declarations.js +79 -0
- package/dist/packs/dart/members.d.ts +4 -0
- package/dist/packs/dart/members.js +62 -0
- package/dist/packs/dart/pack.d.ts +2 -0
- package/dist/packs/dart/pack.js +37 -0
- package/dist/packs/dart/resolve.d.ts +2 -0
- package/dist/packs/dart/resolve.js +32 -0
- package/dist/packs/index.d.ts +3 -0
- package/dist/packs/index.js +8 -0
- package/dist/packs/json/pack.d.ts +2 -0
- package/dist/packs/json/pack.js +27 -0
- package/dist/packs/msbuild/pack.d.ts +2 -0
- package/dist/packs/msbuild/pack.js +75 -0
- package/dist/packs/msbuild/xml.d.ts +16 -0
- package/dist/packs/msbuild/xml.js +123 -0
- package/dist/packs/typescript/calls.d.ts +4 -0
- package/dist/packs/typescript/calls.js +201 -0
- package/dist/packs/typescript/declarations.d.ts +9 -0
- package/dist/packs/typescript/declarations.js +74 -0
- package/dist/packs/typescript/members.d.ts +4 -0
- package/dist/packs/typescript/members.js +92 -0
- package/dist/packs/typescript/pack.d.ts +2 -0
- package/dist/packs/typescript/pack.js +53 -0
- package/dist/packs/typescript/resolve.d.ts +2 -0
- package/dist/packs/typescript/resolve.js +68 -0
- package/dist/packs/typescript/signals.d.ts +7 -0
- package/dist/packs/typescript/signals.js +186 -0
- package/dist/packs/yaml/pack.d.ts +2 -0
- package/dist/packs/yaml/pack.js +35 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +371 -0
- package/dist/src/graph/build.d.ts +17 -0
- package/dist/src/graph/build.js +41 -0
- package/dist/src/graph/metrics.d.ts +12 -0
- package/dist/src/graph/metrics.js +75 -0
- package/dist/src/graph/topology.d.ts +5 -0
- package/dist/src/graph/topology.js +54 -0
- package/dist/src/health/compare.d.ts +2 -0
- package/dist/src/health/compare.js +38 -0
- package/dist/src/health/duplicates.d.ts +11 -0
- package/dist/src/health/duplicates.js +66 -0
- package/dist/src/health/health.d.ts +12 -0
- package/dist/src/health/health.js +94 -0
- package/dist/src/health/types.d.ts +101 -0
- package/dist/src/health/types.js +5 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +20 -0
- package/dist/src/init/init.d.ts +73 -0
- package/dist/src/init/init.js +305 -0
- package/dist/src/init/scout.d.ts +45 -0
- package/dist/src/init/scout.js +208 -0
- package/dist/src/jsonc.d.ts +5 -0
- package/dist/src/jsonc.js +34 -0
- package/dist/src/mcp/server.d.ts +266 -0
- package/dist/src/mcp/server.js +414 -0
- package/dist/src/metrics/doctor.d.ts +15 -0
- package/dist/src/metrics/doctor.js +125 -0
- package/dist/src/metrics/log.d.ts +43 -0
- package/dist/src/metrics/log.js +101 -0
- package/dist/src/metrics/stats.d.ts +43 -0
- package/dist/src/metrics/stats.js +172 -0
- package/dist/src/model.d.ts +178 -0
- package/dist/src/model.js +2 -0
- package/dist/src/outputs.d.ts +16 -0
- package/dist/src/outputs.js +48 -0
- package/dist/src/packs/calls.d.ts +9 -0
- package/dist/src/packs/calls.js +119 -0
- package/dist/src/packs/dataShape.d.ts +6 -0
- package/dist/src/packs/dataShape.js +54 -0
- package/dist/src/packs/pack.d.ts +51 -0
- package/dist/src/packs/pack.js +3 -0
- package/dist/src/packs/packFiles.d.ts +5 -0
- package/dist/src/packs/packFiles.js +12 -0
- package/dist/src/packs/symbols.d.ts +39 -0
- package/dist/src/packs/symbols.js +98 -0
- package/dist/src/packs/treeSitter.d.ts +8 -0
- package/dist/src/packs/treeSitter.js +107 -0
- package/dist/src/queries/flow.d.ts +42 -0
- package/dist/src/queries/flow.js +391 -0
- package/dist/src/queries/map.d.ts +28 -0
- package/dist/src/queries/map.js +102 -0
- package/dist/src/queries/neighborhood.d.ts +35 -0
- package/dist/src/queries/neighborhood.js +60 -0
- package/dist/src/scan/audit.d.ts +2 -0
- package/dist/src/scan/audit.js +41 -0
- package/dist/src/scan/records.d.ts +12 -0
- package/dist/src/scan/records.js +42 -0
- package/dist/src/scan/resolveImports.d.ts +3 -0
- package/dist/src/scan/resolveImports.js +47 -0
- package/dist/src/scan/roles.d.ts +5 -0
- package/dist/src/scan/roles.js +87 -0
- package/dist/src/scan/scan.d.ts +24 -0
- package/dist/src/scan/scan.js +72 -0
- package/dist/src/text/briefs.d.ts +13 -0
- package/dist/src/text/briefs.js +43 -0
- package/dist/src/text/collapse.d.ts +10 -0
- package/dist/src/text/collapse.js +58 -0
- package/dist/src/text/declarations.d.ts +12 -0
- package/dist/src/text/declarations.js +120 -0
- package/dist/src/text/structure.d.ts +2 -0
- package/dist/src/text/structure.js +15 -0
- package/dist/src/text/tree.d.ts +20 -0
- package/dist/src/text/tree.js +49 -0
- package/dist/src/tokens.d.ts +4 -0
- package/dist/src/tokens.js +9 -0
- package/dist/src/version.d.ts +10 -0
- package/dist/src/version.js +11 -0
- package/dist/src/walk/ignoreRules.d.ts +17 -0
- package/dist/src/walk/ignoreRules.js +72 -0
- package/dist/src/walk/walk.d.ts +11 -0
- package/dist/src/walk/walk.js +29 -0
- package/grammars/LICENSE-tree-sitter-dart +20 -0
- package/grammars/README.md +11 -0
- package/grammars/tree-sitter-dart.wasm +0 -0
- package/package.json +68 -4
- package/packs/csharp/README.md +9 -0
- package/packs/csharp/imports.scm +3 -0
- package/packs/csharp/symbols.scm +17 -0
- package/packs/dart/imports.scm +5 -0
- package/packs/dart/symbols.scm +13 -0
- package/packs/msbuild/README.md +11 -0
- package/packs/typescript/exports.scm +16 -0
- package/packs/typescript/imports.scm +9 -0
- package/packs/typescript/symbols.scm +19 -0
- package/schema/ast_code_map.schema.json +144 -0
- package/schema/schema-lock.json +7 -0
- package/schema/topology.schema.json +87 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { oneLine } from '../../src/tokens.js';
|
|
2
|
+
import { childOfType, declared, header, splitList } from '../../src/packs/symbols.js';
|
|
3
|
+
/** Top-level TypeScript and JavaScript declarations. */
|
|
4
|
+
const MAX_TYPE_ALIAS = 200;
|
|
5
|
+
const CONTAINERS = {
|
|
6
|
+
class_declaration: 'class',
|
|
7
|
+
abstract_class_declaration: 'class',
|
|
8
|
+
interface_declaration: 'interface',
|
|
9
|
+
enum_declaration: 'enum',
|
|
10
|
+
};
|
|
11
|
+
function isExported(node, name, ctx) {
|
|
12
|
+
return node.parent?.type === 'export_statement' || ctx.exportedNames.has(name);
|
|
13
|
+
}
|
|
14
|
+
function topLevel(node, ctx, kind, signature, extra = {}) {
|
|
15
|
+
const name = node.childForFieldName('name')?.text ?? 'default';
|
|
16
|
+
const exported = isExported(node, name, ctx);
|
|
17
|
+
return declared(node, { kind, name, signature, exported, visibility: exported ? 'public' : 'private', ...extra });
|
|
18
|
+
}
|
|
19
|
+
/** `extends A, B<C>` and `implements D` of a class or interface. */
|
|
20
|
+
function heritage(node) {
|
|
21
|
+
const clause = (n, keyword) => (n ? splitList(oneLine(n.text).replace(new RegExp(`^${keyword}\\s+`), '')) : undefined);
|
|
22
|
+
const classHeritage = childOfType(node, 'class_heritage');
|
|
23
|
+
const ext = clause(childOfType(classHeritage ?? node, 'extends_clause', 'extends_type_clause'), 'extends');
|
|
24
|
+
const impl = clause(classHeritage && childOfType(classHeritage, 'implements_clause'), 'implements');
|
|
25
|
+
return { ...(ext ? { extends: ext } : {}), ...(impl ? { implements: impl } : {}) };
|
|
26
|
+
}
|
|
27
|
+
function returnType(node) {
|
|
28
|
+
const type = node.childForFieldName('return_type');
|
|
29
|
+
return type ? oneLine(type.text).replace(/^:\s*/, '') : undefined;
|
|
30
|
+
}
|
|
31
|
+
function container(node, ctx) {
|
|
32
|
+
return topLevel(node, ctx, CONTAINERS[node.type], header(ctx.source, node, node.childForFieldName('body')), heritage(node));
|
|
33
|
+
}
|
|
34
|
+
function typeAlias(node, ctx) {
|
|
35
|
+
const text = oneLine(node.text).replace(/;$/, '');
|
|
36
|
+
return topLevel(node, ctx, 'type', text.length > MAX_TYPE_ALIAS ? `${text.slice(0, MAX_TYPE_ALIAS)}…` : text);
|
|
37
|
+
}
|
|
38
|
+
function functionDeclaration(node, ctx) {
|
|
39
|
+
const signature = header(ctx.source, node, node.childForFieldName('body'));
|
|
40
|
+
const type = returnType(node);
|
|
41
|
+
return topLevel(node, ctx, 'function', signature, type ? { type } : {});
|
|
42
|
+
}
|
|
43
|
+
const FUNCTION_VALUES = new Set(['arrow_function', 'function_expression', 'function', 'generator_function']);
|
|
44
|
+
/** `const f = (a: A): B => ...` is a function; any other declarator is a variable. A destructuring pattern declares nothing to list. */
|
|
45
|
+
function variable(node, ctx) {
|
|
46
|
+
const nameNode = node.childForFieldName('name');
|
|
47
|
+
if (nameNode?.type !== 'identifier')
|
|
48
|
+
return undefined;
|
|
49
|
+
const keyword = node.parent?.children[0]?.text ?? 'const';
|
|
50
|
+
const name = nameNode.text;
|
|
51
|
+
const annotation = node.childForFieldName('type');
|
|
52
|
+
const value = node.childForFieldName('value');
|
|
53
|
+
const exported = node.parent?.parent?.type === 'export_statement' || ctx.exportedNames.has(name);
|
|
54
|
+
const base = { name, exported, visibility: exported ? 'public' : 'private' };
|
|
55
|
+
if (value && FUNCTION_VALUES.has(value.type)) {
|
|
56
|
+
const type = returnType(value);
|
|
57
|
+
const signature = `${keyword} ${name} = ${header(ctx.source, value, value.childForFieldName('body'))}`;
|
|
58
|
+
return declared(node, { ...base, kind: 'function', signature, ...(type ? { type } : {}) });
|
|
59
|
+
}
|
|
60
|
+
const type = annotation ? oneLine(annotation.text).replace(/^:\s*/, '') : undefined;
|
|
61
|
+
return declared(node, { ...base, kind: 'variable', signature: `${keyword} ${name}${type ? `: ${type}` : ''}`, ...(type ? { type } : {}) });
|
|
62
|
+
}
|
|
63
|
+
/** A top-level declaration, or undefined for a node this module does not handle. */
|
|
64
|
+
export function describeTopLevel(node, ctx) {
|
|
65
|
+
if (node.type in CONTAINERS)
|
|
66
|
+
return container(node, ctx);
|
|
67
|
+
if (node.type === 'type_alias_declaration')
|
|
68
|
+
return typeAlias(node, ctx);
|
|
69
|
+
if (node.type === 'function_declaration' || node.type === 'generator_function_declaration')
|
|
70
|
+
return functionDeclaration(node, ctx);
|
|
71
|
+
if (node.type === 'variable_declarator')
|
|
72
|
+
return variable(node, ctx);
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Node } from 'web-tree-sitter';
|
|
2
|
+
import { type Declared } from '../../src/packs/symbols.js';
|
|
3
|
+
/** A member declaration, or undefined for a node this module does not handle. */
|
|
4
|
+
export declare function describeMember(node: Node, source: string): Declared[] | undefined;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { oneLine } from '../../src/tokens.js';
|
|
2
|
+
import { childOfType, declared, hasToken, header } from '../../src/packs/symbols.js';
|
|
3
|
+
/** Members of TypeScript and JavaScript classes, interfaces and enums. */
|
|
4
|
+
/** Framework callbacks: React class components and Angular lifecycle hooks. */
|
|
5
|
+
const LIFECYCLE = new Set([
|
|
6
|
+
'render', 'componentDidMount', 'componentDidUpdate', 'componentWillUnmount', 'shouldComponentUpdate',
|
|
7
|
+
'getSnapshotBeforeUpdate', 'componentDidCatch', 'getDerivedStateFromProps',
|
|
8
|
+
'ngOnInit', 'ngOnChanges', 'ngDoCheck', 'ngAfterContentInit', 'ngAfterContentChecked', 'ngAfterViewInit',
|
|
9
|
+
'ngAfterViewChecked', 'ngOnDestroy',
|
|
10
|
+
]);
|
|
11
|
+
function visibility(node, name) {
|
|
12
|
+
if (name.startsWith('#'))
|
|
13
|
+
return 'private';
|
|
14
|
+
const modifier = childOfType(node, 'accessibility_modifier')?.text;
|
|
15
|
+
return modifier === 'private' || modifier === 'protected' ? modifier : 'public';
|
|
16
|
+
}
|
|
17
|
+
function typeOf(node) {
|
|
18
|
+
const annotation = node.childForFieldName('type') ?? childOfType(node, 'type_annotation');
|
|
19
|
+
return annotation ? oneLine(annotation.text).replace(/^\??:\s*/, '') : undefined;
|
|
20
|
+
}
|
|
21
|
+
function member(node, kind, signature, type) {
|
|
22
|
+
const name = node.childForFieldName('name')?.text ?? '';
|
|
23
|
+
const vis = visibility(node, name);
|
|
24
|
+
return declared(node, {
|
|
25
|
+
kind, name, signature, exported: vis === 'public', visibility: vis,
|
|
26
|
+
...(type ? { type } : {}),
|
|
27
|
+
...(hasToken(node, 'static') ? { static: true } : {}),
|
|
28
|
+
...(LIFECYCLE.has(name) ? { lifecycle: true } : {}),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function methodKind(node, name) {
|
|
32
|
+
if (name === 'constructor')
|
|
33
|
+
return 'constructor';
|
|
34
|
+
if (hasToken(node, 'get'))
|
|
35
|
+
return 'getter';
|
|
36
|
+
return hasToken(node, 'set') ? 'setter' : 'method';
|
|
37
|
+
}
|
|
38
|
+
/** `constructor(private readonly repo: Repo)` declares the field `repo`. */
|
|
39
|
+
function parameterProperties(ctor, source) {
|
|
40
|
+
const params = ctor.childForFieldName('parameters')?.namedChildren ?? [];
|
|
41
|
+
return params
|
|
42
|
+
.filter((p) => !!p && (childOfType(p, 'accessibility_modifier') !== undefined || hasToken(p, 'readonly')))
|
|
43
|
+
.map((p) => {
|
|
44
|
+
const name = p.childForFieldName('pattern')?.text ?? '';
|
|
45
|
+
const vis = visibility(p, name);
|
|
46
|
+
const value = p.childForFieldName('value');
|
|
47
|
+
const signature = value ? oneLine(source.slice(p.startIndex, value.startIndex)).replace(/\s*=$/, '') : oneLine(p.text);
|
|
48
|
+
const type = typeOf(p);
|
|
49
|
+
return declared(p, { kind: 'field', name, signature, exported: vis === 'public', visibility: vis, ...(type ? { type } : {}) });
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function method(node, source) {
|
|
53
|
+
const name = node.childForFieldName('name')?.text ?? '';
|
|
54
|
+
const kind = methodKind(node, name);
|
|
55
|
+
const returns = node.childForFieldName('return_type');
|
|
56
|
+
const self = member(node, kind, header(source, node, node.childForFieldName('body')), returns ? oneLine(returns.text).replace(/^:\s*/, '') : undefined);
|
|
57
|
+
return kind === 'constructor' ? [self, ...parameterProperties(node, source)] : [self];
|
|
58
|
+
}
|
|
59
|
+
const FUNCTION_VALUES = new Set(['arrow_function', 'function_expression']);
|
|
60
|
+
/** A field; one that holds an arrow function (`increment = (): void => ...`) is a method. */
|
|
61
|
+
function field(node, source) {
|
|
62
|
+
const value = node.childForFieldName('value');
|
|
63
|
+
const head = header(source, node, value);
|
|
64
|
+
if (!value || !FUNCTION_VALUES.has(value.type))
|
|
65
|
+
return member(node, 'field', head, typeOf(node));
|
|
66
|
+
const returns = value.childForFieldName('return_type');
|
|
67
|
+
return member(node, 'method', `${head} = ${header(source, value, value.childForFieldName('body'))}`, returns ? oneLine(returns.text).replace(/^:\s*/, '') : undefined);
|
|
68
|
+
}
|
|
69
|
+
/** An interface member or an abstract method: its whole text is the signature. */
|
|
70
|
+
function signatureOnly(node) {
|
|
71
|
+
const kind = node.type === 'property_signature' ? 'field' : 'method';
|
|
72
|
+
const returns = node.childForFieldName('return_type');
|
|
73
|
+
const type = kind === 'field' ? typeOf(node) : returns && oneLine(returns.text).replace(/^:\s*/, '');
|
|
74
|
+
return member(node, kind, oneLine(node.text).replace(/[;,]$/, ''), type || undefined);
|
|
75
|
+
}
|
|
76
|
+
function enumMember(node) {
|
|
77
|
+
const name = node.type === 'enum_assignment' ? node.childForFieldName('name')?.text ?? '' : node.text;
|
|
78
|
+
return declared(node, { kind: 'enumMember', name, signature: oneLine(node.text), exported: true, visibility: 'public' });
|
|
79
|
+
}
|
|
80
|
+
/** A member declaration, or undefined for a node this module does not handle. */
|
|
81
|
+
export function describeMember(node, source) {
|
|
82
|
+
switch (node.type) {
|
|
83
|
+
case 'method_definition': return method(node, source);
|
|
84
|
+
case 'public_field_definition': return [field(node, source)];
|
|
85
|
+
case 'abstract_method_signature':
|
|
86
|
+
case 'method_signature':
|
|
87
|
+
case 'property_signature': return [signatureOnly(node)];
|
|
88
|
+
case 'property_identifier':
|
|
89
|
+
case 'enum_assignment': return [enumMember(node)];
|
|
90
|
+
default: return undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { nest, withNesting } from '../../src/packs/symbols.js';
|
|
3
|
+
import { query, withTree } from '../../src/packs/treeSitter.js';
|
|
4
|
+
import { readPackFile } from '../../src/packs/packFiles.js';
|
|
5
|
+
import { withTypeScriptCalls } from './calls.js';
|
|
6
|
+
import { describeTopLevel } from './declarations.js';
|
|
7
|
+
import { describeMember } from './members.js';
|
|
8
|
+
import { resolveTypeScript } from './resolve.js';
|
|
9
|
+
import { typeScriptAstRoles, withTypeScriptSignals } from './signals.js';
|
|
10
|
+
/** TypeScript and JavaScript, including TSX and JSX (Tree-sitter grammars `typescript` and `tsx`). */
|
|
11
|
+
const QUERIES = {
|
|
12
|
+
symbols: readPackFile(import.meta.url, 'symbols.scm'),
|
|
13
|
+
imports: readPackFile(import.meta.url, 'imports.scm'),
|
|
14
|
+
exports: readPackFile(import.meta.url, 'exports.scm'),
|
|
15
|
+
};
|
|
16
|
+
/** The statements that open a nesting level (code health, FR-07). */
|
|
17
|
+
const BLOCKS = new Set(['if_statement', 'for_statement', 'for_in_statement', 'while_statement', 'do_statement', 'switch_statement', 'try_statement']);
|
|
18
|
+
const GRAMMAR = {
|
|
19
|
+
'.ts': 'typescript', '.mts': 'typescript', '.cts': 'typescript',
|
|
20
|
+
'.tsx': 'tsx', '.js': 'tsx', '.jsx': 'tsx', '.mjs': 'tsx', '.cjs': 'tsx',
|
|
21
|
+
};
|
|
22
|
+
function captures(language, key, root, name) {
|
|
23
|
+
return query(language, `ts-${key}`, QUERIES[key]).captures(root).filter((c) => c.name === name).map((c) => c.node);
|
|
24
|
+
}
|
|
25
|
+
function imports(language, root) {
|
|
26
|
+
return captures(language, 'imports', root, 'source').map((node) => {
|
|
27
|
+
const statement = node.parent?.parent;
|
|
28
|
+
const typeOnly = statement?.type === 'import_statement' && statement.children.some((c) => c?.type === 'type');
|
|
29
|
+
return { specifier: node.text, line: node.startPosition.row + 1, ...(typeOnly ? { typeOnly } : {}) };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function symbols(language, root, source) {
|
|
33
|
+
const ctx = { source, exportedNames: new Set(captures(language, 'exports', root, 'name').map((n) => n.text)) };
|
|
34
|
+
const found = [];
|
|
35
|
+
for (const node of captures(language, 'symbols', root, 'symbol')) {
|
|
36
|
+
const top = describeTopLevel(node, ctx);
|
|
37
|
+
found.push(...(top ? [top] : describeMember(node, source) ?? []));
|
|
38
|
+
}
|
|
39
|
+
return nest(withTypeScriptCalls(withTypeScriptSignals(withNesting(found, root, BLOCKS), root), root));
|
|
40
|
+
}
|
|
41
|
+
export const typescriptPack = {
|
|
42
|
+
id: 'typescript',
|
|
43
|
+
claims: (file) => path.posix.extname(file) in GRAMMAR,
|
|
44
|
+
parse: ({ path: file, content }) => withTree(GRAMMAR[path.posix.extname(file)], content, (root, language) => {
|
|
45
|
+
const roles = typeScriptAstRoles(root);
|
|
46
|
+
return {
|
|
47
|
+
symbols: symbols(language, root, content),
|
|
48
|
+
imports: imports(language, root),
|
|
49
|
+
...(roles.length ? { roles } : {}),
|
|
50
|
+
};
|
|
51
|
+
}),
|
|
52
|
+
resolve: resolveTypeScript,
|
|
53
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { builtinModules } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseJsonc } from '../../src/jsonc.js';
|
|
4
|
+
/** Resolves a module specifier to a project file, a package or a Node.js built-in. */
|
|
5
|
+
const EXTENSIONS = ['.ts', '.tsx', '.d.ts', '.js', '.jsx', '.mjs', '.cjs', '.mts', '.cts'];
|
|
6
|
+
/** NodeNext projects import `./a.js` for the source file `./a.ts`. */
|
|
7
|
+
const SOURCE_FOR = { '.js': ['.ts', '.tsx'], '.jsx': ['.tsx'], '.mjs': ['.mts'], '.cjs': ['.cts'] };
|
|
8
|
+
const BUILTINS = new Set(builtinModules);
|
|
9
|
+
const aliasCache = new WeakMap();
|
|
10
|
+
/** `compilerOptions.baseUrl` and `paths` of the root tsconfig.json or jsconfig.json. */
|
|
11
|
+
function aliases(project) {
|
|
12
|
+
const cached = aliasCache.get(project);
|
|
13
|
+
if (cached)
|
|
14
|
+
return cached;
|
|
15
|
+
const text = project.read('tsconfig.json') ?? project.read('jsconfig.json');
|
|
16
|
+
const options = text ? (parseJsonc(text)?.compilerOptions ?? {}) : {};
|
|
17
|
+
const baseUrl = typeof options.baseUrl === 'string' ? path.posix.normalize(options.baseUrl) : undefined;
|
|
18
|
+
const paths = Object.entries((options.paths ?? {})).map(([pattern, targets]) => {
|
|
19
|
+
const [prefix, suffix = ''] = pattern.split('*');
|
|
20
|
+
return { prefix, suffix, targets };
|
|
21
|
+
});
|
|
22
|
+
const found = { baseUrl, paths };
|
|
23
|
+
aliasCache.set(project, found);
|
|
24
|
+
return found;
|
|
25
|
+
}
|
|
26
|
+
/** The project file a path stands for: the path itself, its source file, or the path with an extension or /index. */
|
|
27
|
+
function findFile(base, project) {
|
|
28
|
+
const clean = path.posix.normalize(base).replace(/^\.\//, '');
|
|
29
|
+
const ext = path.posix.extname(clean);
|
|
30
|
+
const candidates = [
|
|
31
|
+
clean,
|
|
32
|
+
...(SOURCE_FOR[ext] ?? []).map((e) => clean.slice(0, -ext.length) + e),
|
|
33
|
+
...EXTENSIONS.map((e) => clean + e),
|
|
34
|
+
...EXTENSIONS.map((e) => `${clean}/index${e}`),
|
|
35
|
+
];
|
|
36
|
+
return candidates.find((c) => !c.startsWith('..') && project.has(c));
|
|
37
|
+
}
|
|
38
|
+
/** A file through `paths` or `baseUrl`; `null` when a `paths` alias matches but names no project file. */
|
|
39
|
+
function viaAliases(spec, project) {
|
|
40
|
+
const { baseUrl, paths } = aliases(project);
|
|
41
|
+
const matching = paths.filter((a) => spec.startsWith(a.prefix) && spec.endsWith(a.suffix));
|
|
42
|
+
for (const alias of matching) {
|
|
43
|
+
const middle = spec.slice(alias.prefix.length, spec.length - alias.suffix.length);
|
|
44
|
+
const found = alias.targets.map((t) => findFile(path.posix.join(baseUrl ?? '.', t.replace('*', middle)), project)).find(Boolean);
|
|
45
|
+
if (found)
|
|
46
|
+
return found;
|
|
47
|
+
}
|
|
48
|
+
if (matching.length)
|
|
49
|
+
return null;
|
|
50
|
+
return baseUrl ? findFile(path.posix.join(baseUrl, spec), project) : undefined;
|
|
51
|
+
}
|
|
52
|
+
function packageName(spec) {
|
|
53
|
+
const parts = spec.split('/');
|
|
54
|
+
return spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
55
|
+
}
|
|
56
|
+
export function resolveTypeScript({ from, raw, project }) {
|
|
57
|
+
const spec = raw.specifier;
|
|
58
|
+
if (spec.startsWith('node:') || BUILTINS.has(spec))
|
|
59
|
+
return { kind: 'builtin' };
|
|
60
|
+
if (spec.startsWith('.') || spec.startsWith('/')) {
|
|
61
|
+
const target = findFile(path.posix.join(path.posix.dirname(from), spec), project);
|
|
62
|
+
return target ? { kind: 'file', target } : { kind: 'unresolved' };
|
|
63
|
+
}
|
|
64
|
+
const aliased = viaAliases(spec, project);
|
|
65
|
+
if (aliased === null)
|
|
66
|
+
return { kind: 'unresolved' };
|
|
67
|
+
return aliased ? { kind: 'file', target: aliased } : { kind: 'package', package: packageName(spec) };
|
|
68
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Node } from 'web-tree-sitter';
|
|
2
|
+
import type { RoleTag } from '../../src/model.js';
|
|
3
|
+
import { type Declared } from '../../src/packs/symbols.js';
|
|
4
|
+
/** Attaches `entrypoints` and `throws` signals to declared TypeScript symbols. */
|
|
5
|
+
export declare function withTypeScriptSignals(items: Declared[], root: Node): Declared[];
|
|
6
|
+
/** Extracts file-level AST role hints (decorators and JSX) from a TypeScript/TSX tree. */
|
|
7
|
+
export declare function typeScriptAstRoles(root: Node): RoleTag[];
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { FUNCTION_KINDS } from '../../src/packs/symbols.js';
|
|
2
|
+
/** Deterministic AST signals for TypeScript/JavaScript: decorator entrypoints, thrown exceptions, and AST role hints. */
|
|
3
|
+
const HTTP_METHOD_DECORATORS = {
|
|
4
|
+
Get: 'GET', Post: 'POST', Put: 'PUT', Patch: 'PATCH', Delete: 'DELETE',
|
|
5
|
+
Head: 'HEAD', Options: 'OPTIONS', All: 'ALL',
|
|
6
|
+
};
|
|
7
|
+
const EVENT_DECORATORS = new Set(['OnEvent', 'EventPattern', 'SubscribeMessage', 'MessagePattern']);
|
|
8
|
+
const JOB_DECORATORS = new Set(['OnJob', 'Process', 'Processor']);
|
|
9
|
+
const CRON_DECORATORS = new Set(['Cron', 'Interval', 'Timeout']);
|
|
10
|
+
const AUTH_DECORATORS = new Set(['UseGuards', 'Auth', 'Authenticated', 'AdminRoute', 'Roles', 'PublicRoute', 'SharedLinkRoute', 'Permission']);
|
|
11
|
+
const DB_DECORATORS = new Set(['Entity', 'InjectRepository', 'InjectDataSource', 'InjectModel', 'Table']);
|
|
12
|
+
function unquote(text) {
|
|
13
|
+
return text.replace(/^['"`]|['"`]$/g, '').trim();
|
|
14
|
+
}
|
|
15
|
+
/** Extracts a concise string/identifier value from a decorator's first argument (string, enum member, or `{ name: ... }`). */
|
|
16
|
+
function firstDecoratorArg(call) {
|
|
17
|
+
const args = call.childForFieldName('arguments')?.namedChildren ?? [];
|
|
18
|
+
const first = args[0];
|
|
19
|
+
if (!first)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (first.type === 'string' || first.type === 'template_string')
|
|
22
|
+
return unquote(first.text);
|
|
23
|
+
if (first.type === 'identifier' || first.type === 'member_expression')
|
|
24
|
+
return first.text;
|
|
25
|
+
if (first.type === 'object') {
|
|
26
|
+
for (const pair of first.namedChildren) {
|
|
27
|
+
if (pair?.type !== 'pair')
|
|
28
|
+
continue;
|
|
29
|
+
const key = pair.childForFieldName('key')?.text;
|
|
30
|
+
if (key === 'name' || key === 'event' || key === 'path' || key === 'queue') {
|
|
31
|
+
const val = pair.childForFieldName('value');
|
|
32
|
+
if (val)
|
|
33
|
+
return unquote(val.text);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
function parseDecorator(node) {
|
|
40
|
+
const expr = node.namedChildren[0];
|
|
41
|
+
if (!expr)
|
|
42
|
+
return undefined;
|
|
43
|
+
if (expr.type === 'call_expression') {
|
|
44
|
+
const fn = expr.childForFieldName('function');
|
|
45
|
+
const name = fn?.type === 'member_expression' ? fn.childForFieldName('property')?.text : fn?.text;
|
|
46
|
+
return name ? { name, arg: firstDecoratorArg(expr) } : undefined;
|
|
47
|
+
}
|
|
48
|
+
if (expr.type === 'identifier')
|
|
49
|
+
return { name: expr.text };
|
|
50
|
+
if (expr.type === 'member_expression') {
|
|
51
|
+
const prop = expr.childForFieldName('property')?.text;
|
|
52
|
+
return prop ? { name: prop } : undefined;
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
/** Collects decorators attached to a class/method node (either as children, parent export_statement children, or preceding class_body siblings). */
|
|
57
|
+
function decoratorsOf(node) {
|
|
58
|
+
const raw = [];
|
|
59
|
+
if (node.parent?.type === 'export_statement') {
|
|
60
|
+
for (const c of node.parent.children) {
|
|
61
|
+
if (c?.type === 'decorator')
|
|
62
|
+
raw.push(c);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
for (const c of node.children) {
|
|
66
|
+
if (c?.type === 'decorator')
|
|
67
|
+
raw.push(c);
|
|
68
|
+
}
|
|
69
|
+
if (node.parent?.type === 'class_body') {
|
|
70
|
+
const preceding = [];
|
|
71
|
+
let prev = node.previousSibling;
|
|
72
|
+
while (prev && (prev.type === 'decorator' || prev.type === 'comment')) {
|
|
73
|
+
if (prev.type === 'decorator')
|
|
74
|
+
preceding.push(prev);
|
|
75
|
+
prev = prev.previousSibling;
|
|
76
|
+
}
|
|
77
|
+
raw.push(...preceding.reverse());
|
|
78
|
+
}
|
|
79
|
+
return raw.map(parseDecorator).filter((d) => !!d);
|
|
80
|
+
}
|
|
81
|
+
function entrypointsFromDecorators(decs) {
|
|
82
|
+
const out = [];
|
|
83
|
+
for (const d of decs) {
|
|
84
|
+
if (d.name in HTTP_METHOD_DECORATORS) {
|
|
85
|
+
const verb = HTTP_METHOD_DECORATORS[d.name];
|
|
86
|
+
const route = d.arg ? (d.arg.startsWith('/') ? d.arg : `/${d.arg}`) : '/';
|
|
87
|
+
out.push(`${verb} ${route}`);
|
|
88
|
+
}
|
|
89
|
+
else if (d.name === 'Controller') {
|
|
90
|
+
out.push(d.arg ? `route:${d.arg}` : 'route:/');
|
|
91
|
+
}
|
|
92
|
+
else if (EVENT_DECORATORS.has(d.name)) {
|
|
93
|
+
out.push(d.arg ? `event:${d.arg}` : `event:${d.name}`);
|
|
94
|
+
}
|
|
95
|
+
else if (JOB_DECORATORS.has(d.name)) {
|
|
96
|
+
out.push(d.arg ? `job:${d.arg}` : `job:${d.name}`);
|
|
97
|
+
}
|
|
98
|
+
else if (CRON_DECORATORS.has(d.name)) {
|
|
99
|
+
out.push(d.arg ? `cron:${d.arg}` : `cron:${d.name}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return [...new Set(out)];
|
|
103
|
+
}
|
|
104
|
+
/** Collects exception/error names from `throw new X(...)` statements inside a node's range. */
|
|
105
|
+
function throwsIn(root, start, end) {
|
|
106
|
+
const found = new Set();
|
|
107
|
+
const walk = (node) => {
|
|
108
|
+
if (node.endIndex <= start || node.startIndex >= end)
|
|
109
|
+
return;
|
|
110
|
+
if (node.type === 'throw_statement' && node.startIndex >= start && node.endIndex <= end) {
|
|
111
|
+
const expr = node.namedChildren[0];
|
|
112
|
+
if (expr?.type === 'new_expression') {
|
|
113
|
+
const ctor = expr.childForFieldName('constructor');
|
|
114
|
+
if (ctor) {
|
|
115
|
+
const name = ctor.type === 'member_expression' ? ctor.childForFieldName('property')?.text : ctor.text;
|
|
116
|
+
if (name)
|
|
117
|
+
found.add(name);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
for (const c of node.children)
|
|
123
|
+
if (c)
|
|
124
|
+
walk(c);
|
|
125
|
+
};
|
|
126
|
+
walk(root);
|
|
127
|
+
return [...found];
|
|
128
|
+
}
|
|
129
|
+
/** Index declaration AST nodes by their byte range so we can inspect their decorators. */
|
|
130
|
+
function indexNodesByRange(root) {
|
|
131
|
+
const byRange = new Map();
|
|
132
|
+
const walk = (node) => {
|
|
133
|
+
if (node.type === 'class_declaration' || node.type === 'abstract_class_declaration' ||
|
|
134
|
+
node.type === 'method_definition' || node.type === 'public_field_definition') {
|
|
135
|
+
byRange.set(`${node.startIndex}:${node.endIndex}`, node);
|
|
136
|
+
}
|
|
137
|
+
for (const c of node.children)
|
|
138
|
+
if (c)
|
|
139
|
+
walk(c);
|
|
140
|
+
};
|
|
141
|
+
walk(root);
|
|
142
|
+
return byRange;
|
|
143
|
+
}
|
|
144
|
+
/** Attaches `entrypoints` and `throws` signals to declared TypeScript symbols. */
|
|
145
|
+
export function withTypeScriptSignals(items, root) {
|
|
146
|
+
const byRange = indexNodesByRange(root);
|
|
147
|
+
return items.map((item) => {
|
|
148
|
+
const astNode = byRange.get(`${item.start}:${item.end}`);
|
|
149
|
+
const entrypoints = astNode ? entrypointsFromDecorators(decoratorsOf(astNode)) : [];
|
|
150
|
+
const throws = FUNCTION_KINDS.has(item.kind) ? throwsIn(root, item.start, item.end) : [];
|
|
151
|
+
return {
|
|
152
|
+
...item,
|
|
153
|
+
...(entrypoints.length ? { entrypoints } : {}),
|
|
154
|
+
...(throws.length ? { throws } : {}),
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/** Extracts file-level AST role hints (decorators and JSX) from a TypeScript/TSX tree. */
|
|
159
|
+
export function typeScriptAstRoles(root) {
|
|
160
|
+
const roles = new Set();
|
|
161
|
+
const walk = (node) => {
|
|
162
|
+
if (node.type === 'jsx_element' || node.type === 'jsx_self_closing_element') {
|
|
163
|
+
roles.add('ui');
|
|
164
|
+
}
|
|
165
|
+
else if (node.type === 'decorator') {
|
|
166
|
+
const dec = parseDecorator(node);
|
|
167
|
+
if (dec) {
|
|
168
|
+
if (dec.name in HTTP_METHOD_DECORATORS || dec.name === 'Controller')
|
|
169
|
+
roles.add('http');
|
|
170
|
+
else if (EVENT_DECORATORS.has(dec.name))
|
|
171
|
+
roles.add('event');
|
|
172
|
+
else if (JOB_DECORATORS.has(dec.name) || CRON_DECORATORS.has(dec.name))
|
|
173
|
+
roles.add('job');
|
|
174
|
+
else if (AUTH_DECORATORS.has(dec.name))
|
|
175
|
+
roles.add('auth');
|
|
176
|
+
else if (DB_DECORATORS.has(dec.name))
|
|
177
|
+
roles.add('db');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
for (const c of node.children)
|
|
181
|
+
if (c)
|
|
182
|
+
walk(c);
|
|
183
|
+
};
|
|
184
|
+
walk(root);
|
|
185
|
+
return [...roles];
|
|
186
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import YAML from 'yaml';
|
|
3
|
+
import { dataSkeleton } from '../../src/packs/dataShape.js';
|
|
4
|
+
import { ParseError } from '../../src/packs/pack.js';
|
|
5
|
+
/** YAML files, and the dependencies a pubspec.yaml declares. */
|
|
6
|
+
/** `http: 1.2.2`, `flutter: { sdk: flutter }`, `local: { path: ../local }`. */
|
|
7
|
+
function dependency(name, spec, dev) {
|
|
8
|
+
if (spec && typeof spec === 'object') {
|
|
9
|
+
const entries = spec;
|
|
10
|
+
if ('sdk' in entries)
|
|
11
|
+
return { name, version: `sdk: ${entries.sdk}`, dev, sdk: true };
|
|
12
|
+
return { name, version: Object.entries(entries).map(([k, v]) => `${k}: ${typeof v === 'object' ? JSON.stringify(v) : v}`).join(', '), dev };
|
|
13
|
+
}
|
|
14
|
+
return { name, version: spec === null || spec === undefined ? 'any' : String(spec), dev };
|
|
15
|
+
}
|
|
16
|
+
function pubspecDependencies(doc) {
|
|
17
|
+
const section = (key, dev) => Object.entries((doc?.[key] ?? {})).map(([n, s]) => dependency(n, s, dev));
|
|
18
|
+
return [...section('dependencies', false), ...section('dev_dependencies', true)];
|
|
19
|
+
}
|
|
20
|
+
export const yamlPack = {
|
|
21
|
+
id: 'yaml',
|
|
22
|
+
data: true,
|
|
23
|
+
claims: (file) => file.endsWith('.yaml') || file.endsWith('.yml'),
|
|
24
|
+
parse: async ({ path: file, content }) => {
|
|
25
|
+
let parsed;
|
|
26
|
+
try {
|
|
27
|
+
parsed = YAML.parse(content);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
throw new ParseError(`Invalid YAML: ${err.message.split('\n')[0]}`);
|
|
31
|
+
}
|
|
32
|
+
const declares = path.posix.basename(file) === 'pubspec.yaml' ? pubspecDependencies(parsed) : undefined;
|
|
33
|
+
return { symbols: [], imports: [], skeleton: dataSkeleton('YAML', parsed), ...(declares ? { declares } : {}) };
|
|
34
|
+
},
|
|
35
|
+
};
|