funkscript 0.2.6 → 0.2.7
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/README.md +1 -1
- package/dist/cli/build-watch.d.ts.map +1 -1
- package/dist/cli/build-watch.js +4 -24
- package/dist/cli/build-watch.js.map +1 -1
- package/dist/cli/build.d.ts.map +1 -1
- package/dist/cli/build.js +3 -58
- package/dist/cli/build.js.map +1 -1
- package/dist/cli/check-policy.d.ts +9 -0
- package/dist/cli/check-policy.d.ts.map +1 -0
- package/dist/cli/check-policy.js +39 -0
- package/dist/cli/check-policy.js.map +1 -0
- package/dist/cli/check.d.ts.map +1 -1
- package/dist/cli/check.js +16 -30
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +11 -0
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +2 -6
- package/dist/cli/doctor.js.map +1 -1
- package/dist/cli/index.js +3 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/query.d.ts +3 -0
- package/dist/cli/query.d.ts.map +1 -0
- package/dist/cli/query.js +152 -0
- package/dist/cli/query.js.map +1 -0
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +2 -29
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/tsc.d.ts.map +1 -1
- package/dist/cli/tsc.js +2 -6
- package/dist/cli/tsc.js.map +1 -1
- package/dist/compiler/check.d.ts.map +1 -1
- package/dist/compiler/check.js +18 -28
- package/dist/compiler/check.js.map +1 -1
- package/dist/compiler/diagnostics-json.d.ts +73 -0
- package/dist/compiler/diagnostics-json.d.ts.map +1 -0
- package/dist/compiler/diagnostics-json.js +957 -0
- package/dist/compiler/diagnostics-json.js.map +1 -0
- package/dist/compiler/escape-audit.d.ts +46 -0
- package/dist/compiler/escape-audit.d.ts.map +1 -0
- package/dist/compiler/escape-audit.js +682 -0
- package/dist/compiler/escape-audit.js.map +1 -0
- package/dist/compiler/modules/output-plan.d.ts.map +1 -1
- package/dist/compiler/modules/output-plan.js +10 -5
- package/dist/compiler/modules/output-plan.js.map +1 -1
- package/dist/compiler/modules/source-files.d.ts +2 -0
- package/dist/compiler/modules/source-files.d.ts.map +1 -1
- package/dist/compiler/modules/source-files.js +8 -0
- package/dist/compiler/modules/source-files.js.map +1 -1
- package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
- package/dist/compiler/numeric-comparisons.js +13 -7
- package/dist/compiler/numeric-comparisons.js.map +1 -1
- package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
- package/dist/compiler/pipelines/resolution.js +24 -23
- package/dist/compiler/pipelines/resolution.js.map +1 -1
- package/dist/compiler/query.d.ts +53 -0
- package/dist/compiler/query.d.ts.map +1 -0
- package/dist/compiler/query.js +781 -0
- package/dist/compiler/query.js.map +1 -0
- package/dist/compiler/source-map.d.ts.map +1 -1
- package/dist/compiler/source-map.js +2 -7
- package/dist/compiler/source-map.js.map +1 -1
- package/dist/compiler/type-view-paths.d.ts +2 -0
- package/dist/compiler/type-view-paths.d.ts.map +1 -1
- package/dist/compiler/type-view-paths.js +8 -0
- package/dist/compiler/type-view-paths.js.map +1 -1
- package/dist/compiler/typescript-context.d.ts.map +1 -1
- package/dist/compiler/typescript-context.js +5 -7
- package/dist/compiler/typescript-context.js.map +1 -1
- package/dist/emitter/assemblyscript.d.ts.map +1 -1
- package/dist/emitter/assemblyscript.js +120 -136
- package/dist/emitter/assemblyscript.js.map +1 -1
- package/dist/formatter/index.d.ts +9 -0
- package/dist/formatter/index.d.ts.map +1 -1
- package/dist/formatter/index.js +158 -2
- package/dist/formatter/index.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lsp/completion.d.ts +25 -0
- package/dist/lsp/completion.d.ts.map +1 -1
- package/dist/lsp/completion.js +1114 -51
- package/dist/lsp/completion.js.map +1 -1
- package/dist/lsp/definition.d.ts +2 -0
- package/dist/lsp/definition.d.ts.map +1 -1
- package/dist/lsp/definition.js +52 -58
- package/dist/lsp/definition.js.map +1 -1
- package/dist/lsp/hover.d.ts +5 -1
- package/dist/lsp/hover.d.ts.map +1 -1
- package/dist/lsp/hover.js +1000 -41
- package/dist/lsp/hover.js.map +1 -1
- package/dist/lsp/imports.d.ts +2 -0
- package/dist/lsp/imports.d.ts.map +1 -1
- package/dist/lsp/imports.js +35 -20
- package/dist/lsp/imports.js.map +1 -1
- package/dist/lsp/navigation.d.ts +1 -1
- package/dist/lsp/navigation.d.ts.map +1 -1
- package/dist/lsp/navigation.js +30 -12
- package/dist/lsp/navigation.js.map +1 -1
- package/dist/lsp/range-format.d.ts +4 -0
- package/dist/lsp/range-format.d.ts.map +1 -0
- package/dist/lsp/range-format.js +88 -0
- package/dist/lsp/range-format.js.map +1 -0
- package/dist/lsp/schema-import-hover.d.ts +8 -0
- package/dist/lsp/schema-import-hover.d.ts.map +1 -0
- package/dist/lsp/schema-import-hover.js +52 -0
- package/dist/lsp/schema-import-hover.js.map +1 -0
- package/dist/lsp/server.d.ts +1 -0
- package/dist/lsp/server.d.ts.map +1 -1
- package/dist/lsp/server.js +53 -48
- package/dist/lsp/server.js.map +1 -1
- package/dist/lsp/workspace.d.ts +3 -1
- package/dist/lsp/workspace.d.ts.map +1 -1
- package/dist/lsp/workspace.js +66 -10
- package/dist/lsp/workspace.js.map +1 -1
- package/dist/plugins/bun.d.ts +2 -18
- package/dist/plugins/bun.d.ts.map +1 -1
- package/dist/plugins/bun.js +5 -30
- package/dist/plugins/bun.js.map +1 -1
- package/dist/plugins/esbuild.d.ts +1 -1
- package/dist/plugins/esbuild.d.ts.map +1 -1
- package/dist/plugins/esbuild.js +10 -36
- package/dist/plugins/esbuild.js.map +1 -1
- package/dist/plugins/server-boundary.d.ts +14 -0
- package/dist/plugins/server-boundary.d.ts.map +1 -0
- package/dist/plugins/server-boundary.js +25 -0
- package/dist/plugins/server-boundary.js.map +1 -0
- package/dist/plugins/vite.d.ts +2 -18
- package/dist/plugins/vite.d.ts.map +1 -1
- package/dist/plugins/vite.js +4 -29
- package/dist/plugins/vite.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/build-watch.ts +4 -26
- package/src/cli/build.ts +9 -60
- package/src/cli/check-policy.ts +54 -0
- package/src/cli/check.ts +19 -31
- package/src/cli/commands.ts +13 -0
- package/src/cli/doctor.ts +2 -5
- package/src/cli/index.ts +3 -0
- package/src/cli/query.ts +191 -0
- package/src/cli/run.ts +5 -30
- package/src/cli/tsc.ts +2 -5
- package/src/compiler/check.ts +28 -48
- package/src/compiler/diagnostics-json.ts +1205 -0
- package/src/compiler/escape-audit.ts +913 -0
- package/src/compiler/modules/output-plan.ts +11 -9
- package/src/compiler/modules/source-files.ts +9 -0
- package/src/compiler/numeric-comparisons.ts +14 -8
- package/src/compiler/pipelines/resolution.ts +30 -26
- package/src/compiler/query.ts +1177 -0
- package/src/compiler/source-map.ts +3 -7
- package/src/compiler/type-view-paths.ts +7 -0
- package/src/compiler/typescript-context.ts +6 -7
- package/src/emitter/assemblyscript.ts +101 -147
- package/src/formatter/index.ts +229 -2
- package/src/index.ts +3 -0
- package/src/lsp/completion.ts +1452 -66
- package/src/lsp/definition.ts +50 -58
- package/src/lsp/hover.ts +1442 -40
- package/src/lsp/imports.ts +55 -21
- package/src/lsp/navigation.ts +41 -22
- package/src/lsp/range-format.ts +102 -0
- package/src/lsp/schema-import-hover.ts +76 -0
- package/src/lsp/server.ts +69 -69
- package/src/lsp/workspace.ts +63 -9
- package/src/plugins/bun.ts +14 -66
- package/src/plugins/esbuild.ts +16 -55
- package/src/plugins/server-boundary.ts +41 -0
- package/src/plugins/vite.ts +12 -62
|
@@ -0,0 +1,781 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import { compileString, CompilerError } from "../compiler.js";
|
|
6
|
+
import { toErrorMessage } from "../errors.js";
|
|
7
|
+
import { DEFAULT_COMPILER_OPTIONS } from "./check.js";
|
|
8
|
+
import { resolveFunModule } from "./modules/resolution.js";
|
|
9
|
+
import { JSX_INTRINSICS_DECLARATION, TS_RESET_DECLARATION, TYPE_FEST_DECLARATION, } from "./typescript-context.js";
|
|
10
|
+
function getCompilerOptions(cwd) {
|
|
11
|
+
let baseOptions = { ...DEFAULT_COMPILER_OPTIONS };
|
|
12
|
+
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, "tsconfig.json");
|
|
13
|
+
if (configPath) {
|
|
14
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
15
|
+
if (!configFile.error && configFile.config) {
|
|
16
|
+
const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath));
|
|
17
|
+
baseOptions = { ...baseOptions, ...parsed.options };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...baseOptions,
|
|
22
|
+
noEmit: true,
|
|
23
|
+
allowImportingTsExtensions: true,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function toVirtualTsPath(fullFunPath) {
|
|
27
|
+
const ext = fullFunPath.endsWith(".funx") ? ".tsx" : ".ts";
|
|
28
|
+
return `${fullFunPath}${ext}`;
|
|
29
|
+
}
|
|
30
|
+
function cleanTypeString(typeText) {
|
|
31
|
+
return typeText.replace(/\b([A-Z][A-Za-z0-9_]*)\$Class\b/g, "$1");
|
|
32
|
+
}
|
|
33
|
+
function formatType(checker, type, enclosingNode) {
|
|
34
|
+
return cleanTypeString(checker.typeToString(type, enclosingNode, ts.TypeFormatFlags.NoTruncation));
|
|
35
|
+
}
|
|
36
|
+
function extractStringLiteralValue(lit) {
|
|
37
|
+
const fromParts = lit.parts
|
|
38
|
+
.map((p) => (p.kind === "StringRawPart" ? p.value : ""))
|
|
39
|
+
.join("");
|
|
40
|
+
if (fromParts.length > 0)
|
|
41
|
+
return fromParts;
|
|
42
|
+
if ((lit.raw.startsWith('"') && lit.raw.endsWith('"')) ||
|
|
43
|
+
(lit.raw.startsWith("'") && lit.raw.endsWith("'"))) {
|
|
44
|
+
return lit.raw.slice(1, -1);
|
|
45
|
+
}
|
|
46
|
+
return lit.raw;
|
|
47
|
+
}
|
|
48
|
+
function collectImports(ast) {
|
|
49
|
+
const imports = [];
|
|
50
|
+
for (const item of ast.body) {
|
|
51
|
+
if (item.kind === "ImportDeclaration") {
|
|
52
|
+
imports.push(extractStringLiteralValue(item.source));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return imports;
|
|
56
|
+
}
|
|
57
|
+
function collectTopLevelDeclarations(ast) {
|
|
58
|
+
const entries = [];
|
|
59
|
+
for (const item of ast.body) {
|
|
60
|
+
if (item.kind === "ExportDeclaration") {
|
|
61
|
+
entries.push({ exported: true, decl: item.declaration });
|
|
62
|
+
}
|
|
63
|
+
else if (item.kind === "SchemaDeclaration" ||
|
|
64
|
+
item.kind === "ErrorDeclaration" ||
|
|
65
|
+
item.kind === "ServiceDeclaration" ||
|
|
66
|
+
item.kind === "BindingDeclaration") {
|
|
67
|
+
entries.push({ exported: false, decl: item });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return entries;
|
|
71
|
+
}
|
|
72
|
+
function collectBindingTargetLocations(target) {
|
|
73
|
+
switch (target.kind) {
|
|
74
|
+
case "Identifier":
|
|
75
|
+
case "UpperIdentifier":
|
|
76
|
+
return [{ name: target.name, span: target.span }];
|
|
77
|
+
case "VariablePattern":
|
|
78
|
+
return [{ name: target.name.name, span: target.name.span }];
|
|
79
|
+
case "RestPattern":
|
|
80
|
+
return collectBindingTargetLocations(target.argument);
|
|
81
|
+
case "RecordPattern":
|
|
82
|
+
return target.fields.flatMap((field) => field.pattern
|
|
83
|
+
? collectBindingTargetLocations(field.pattern)
|
|
84
|
+
: [{ name: field.key.name, span: field.key.span }]);
|
|
85
|
+
case "ArrayPattern":
|
|
86
|
+
return target.elements.flatMap((elem) => collectBindingTargetLocations(elem));
|
|
87
|
+
case "OrPattern":
|
|
88
|
+
return target.patterns.flatMap((part) => collectBindingTargetLocations(part));
|
|
89
|
+
case "WildcardPattern":
|
|
90
|
+
case "LiteralPattern":
|
|
91
|
+
case "ConstructorPattern":
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function isEffectType(type, checker) {
|
|
96
|
+
const symbol = type.symbol ?? type.aliasSymbol;
|
|
97
|
+
if (!symbol || symbol.getName() !== "Effect") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const typeArgs = checker.getTypeArguments(type);
|
|
101
|
+
if (typeArgs.length !== 3) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
const decls = symbol.declarations;
|
|
105
|
+
if (!decls || decls.length === 0) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return decls.some((d) => {
|
|
109
|
+
const fileName = d.getSourceFile().fileName;
|
|
110
|
+
return fileName.includes("effect") || Boolean(type.getProperty("pipe"));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function collectUnionMembers(type, checker, enclosingNode) {
|
|
114
|
+
const out = [];
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
const visit = (t) => {
|
|
117
|
+
if ((t.flags & ts.TypeFlags.Never) !== 0) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (t.isUnion()) {
|
|
121
|
+
for (const member of t.types) {
|
|
122
|
+
visit(member);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const formatted = formatType(checker, t, enclosingNode);
|
|
127
|
+
if (formatted === "never" || seen.has(formatted)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
seen.add(formatted);
|
|
131
|
+
out.push(formatted);
|
|
132
|
+
};
|
|
133
|
+
visit(type);
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
function decomposeEffectType(type, checker, enclosingNode) {
|
|
137
|
+
if (isEffectType(type, checker)) {
|
|
138
|
+
const [successType, errorType, reqType] = checker.getTypeArguments(type);
|
|
139
|
+
return {
|
|
140
|
+
success: successType
|
|
141
|
+
? formatType(checker, successType, enclosingNode)
|
|
142
|
+
: "unknown",
|
|
143
|
+
error: errorType
|
|
144
|
+
? formatType(checker, errorType, enclosingNode)
|
|
145
|
+
: "never",
|
|
146
|
+
requirements: reqType
|
|
147
|
+
? collectUnionMembers(reqType, checker, enclosingNode)
|
|
148
|
+
: [],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const callSignatures = type.getCallSignatures();
|
|
152
|
+
if (callSignatures.length > 0 && type.getConstructSignatures().length === 0) {
|
|
153
|
+
const returnType = callSignatures[0].getReturnType();
|
|
154
|
+
if (isEffectType(returnType, checker)) {
|
|
155
|
+
return decomposeEffectType(returnType, checker, enclosingNode);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
function collectBindingNamesFromTsNode(nameNode, decl, variables) {
|
|
161
|
+
if (ts.isIdentifier(nameNode)) {
|
|
162
|
+
variables.set(nameNode.text, decl);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
for (const element of nameNode.elements) {
|
|
166
|
+
if (ts.isBindingElement(element)) {
|
|
167
|
+
collectBindingNamesFromTsNode(element.name, decl, variables);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function findTopLevelDeclarationsInTs(sourceFile) {
|
|
172
|
+
const variables = new Map();
|
|
173
|
+
const typeAliases = new Map();
|
|
174
|
+
const classes = new Map();
|
|
175
|
+
for (const stmt of sourceFile.statements) {
|
|
176
|
+
if (ts.isVariableStatement(stmt)) {
|
|
177
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
178
|
+
collectBindingNamesFromTsNode(decl.name, decl, variables);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else if (ts.isTypeAliasDeclaration(stmt)) {
|
|
182
|
+
typeAliases.set(stmt.name.text, stmt);
|
|
183
|
+
}
|
|
184
|
+
else if (ts.isClassDeclaration(stmt) && stmt.name) {
|
|
185
|
+
classes.set(stmt.name.text, stmt);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return { variables, typeAliases, classes };
|
|
189
|
+
}
|
|
190
|
+
function resolveSchemaSymbol(entry, funPath, checker, tsDecls) {
|
|
191
|
+
const name = entry.decl.id.name;
|
|
192
|
+
const kind = entry.decl.body.kind === "TypeSignatureSchema" ? "type" : "schema";
|
|
193
|
+
const typeAlias = tsDecls.typeAliases.get(name);
|
|
194
|
+
const varDecl = tsDecls.variables.get(name);
|
|
195
|
+
const node = typeAlias ?? varDecl;
|
|
196
|
+
let resolvedType;
|
|
197
|
+
if (typeAlias) {
|
|
198
|
+
resolvedType =
|
|
199
|
+
kind === "type"
|
|
200
|
+
? checker.getTypeFromTypeNode(typeAlias.type)
|
|
201
|
+
: checker.getTypeAtLocation(typeAlias);
|
|
202
|
+
}
|
|
203
|
+
else if (varDecl) {
|
|
204
|
+
resolvedType = checker.getTypeAtLocation(varDecl);
|
|
205
|
+
}
|
|
206
|
+
let typeStr = resolvedType
|
|
207
|
+
? formatType(checker, resolvedType, node)
|
|
208
|
+
: "unknown";
|
|
209
|
+
const effect = resolvedType && kind === "type"
|
|
210
|
+
? decomposeEffectType(resolvedType, checker, node)
|
|
211
|
+
: undefined;
|
|
212
|
+
if (kind === "type" && resolvedType && typeStr === name) {
|
|
213
|
+
if (effect) {
|
|
214
|
+
const reqs = effect.requirements.length > 0
|
|
215
|
+
? effect.requirements.join(" | ")
|
|
216
|
+
: "never";
|
|
217
|
+
typeStr = `Effect<${effect.success}, ${effect.error}, ${reqs}>`;
|
|
218
|
+
}
|
|
219
|
+
else if (resolvedType.getCallSignatures().length > 0 &&
|
|
220
|
+
resolvedType.getConstructSignatures().length === 0) {
|
|
221
|
+
typeStr = cleanTypeString(checker.signatureToString(resolvedType.getCallSignatures()[0], node, ts.TypeFormatFlags.NoTruncation |
|
|
222
|
+
ts.TypeFormatFlags.WriteArrowStyleSignature));
|
|
223
|
+
}
|
|
224
|
+
else if (typeAlias) {
|
|
225
|
+
typeStr = cleanTypeString(typeAlias.type.getText());
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
name,
|
|
230
|
+
kind,
|
|
231
|
+
exported: entry.exported,
|
|
232
|
+
file: funPath,
|
|
233
|
+
line: entry.decl.id.span.start.line,
|
|
234
|
+
column: entry.decl.id.span.start.column,
|
|
235
|
+
type: typeStr,
|
|
236
|
+
...(effect ? { effect } : {}),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function resolveErrorSymbol(entry, funPath, checker, tsDecls) {
|
|
240
|
+
const name = entry.decl.id.name;
|
|
241
|
+
const classDecl = tsDecls.classes.get(`${name}$Class`);
|
|
242
|
+
const typeAlias = tsDecls.typeAliases.get(name);
|
|
243
|
+
let typeStr = name;
|
|
244
|
+
if (classDecl) {
|
|
245
|
+
const classType = checker.getTypeAtLocation(classDecl);
|
|
246
|
+
const baseTypes = classType.getBaseTypes();
|
|
247
|
+
if (baseTypes && baseTypes.length > 0) {
|
|
248
|
+
typeStr = formatType(checker, baseTypes[0], classDecl);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
typeStr = formatType(checker, classType, classDecl);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
else if (typeAlias) {
|
|
255
|
+
const aliasType = checker.getTypeAtLocation(typeAlias);
|
|
256
|
+
typeStr = formatType(checker, aliasType, typeAlias);
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
name,
|
|
260
|
+
kind: "error",
|
|
261
|
+
exported: entry.exported,
|
|
262
|
+
file: funPath,
|
|
263
|
+
line: entry.decl.id.span.start.line,
|
|
264
|
+
column: entry.decl.id.span.start.column,
|
|
265
|
+
type: typeStr,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function resolveServiceSymbol(entry, funPath, checker, tsDecls) {
|
|
269
|
+
const name = entry.decl.id.name;
|
|
270
|
+
const classDecl = tsDecls.classes.get(name);
|
|
271
|
+
let typeStr = name;
|
|
272
|
+
if (classDecl) {
|
|
273
|
+
const classType = checker.getTypeAtLocation(classDecl);
|
|
274
|
+
const baseTypes = classType.getBaseTypes();
|
|
275
|
+
if (baseTypes && baseTypes.length > 0) {
|
|
276
|
+
const shapeArgs = checker.getTypeArguments(baseTypes[0]);
|
|
277
|
+
if (shapeArgs.length >= 2) {
|
|
278
|
+
typeStr = formatType(checker, shapeArgs[1], classDecl);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
typeStr = formatType(checker, baseTypes[0], classDecl);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
typeStr = formatType(checker, classType, classDecl);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
name,
|
|
290
|
+
kind: "service",
|
|
291
|
+
exported: entry.exported,
|
|
292
|
+
file: funPath,
|
|
293
|
+
line: entry.decl.id.span.start.line,
|
|
294
|
+
column: entry.decl.id.span.start.column,
|
|
295
|
+
type: typeStr,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function resolveBindingSymbols(entry, funPath, checker, tsDecls) {
|
|
299
|
+
const targets = collectBindingTargetLocations(entry.decl.id);
|
|
300
|
+
const isSimpleTarget = entry.decl.id.kind === "Identifier" ||
|
|
301
|
+
entry.decl.id.kind === "UpperIdentifier";
|
|
302
|
+
return targets.map(({ name, span }) => {
|
|
303
|
+
const varDecl = tsDecls.variables.get(name);
|
|
304
|
+
let resolvedType;
|
|
305
|
+
if (varDecl) {
|
|
306
|
+
if (ts.isIdentifier(varDecl.name)) {
|
|
307
|
+
resolvedType = checker.getTypeAtLocation(varDecl);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
const symbol = checker.getSymbolsInScope(varDecl, ts.SymbolFlags.BlockScopedVariable);
|
|
311
|
+
const match = symbol.find((sym) => sym.getName() === name);
|
|
312
|
+
if (match) {
|
|
313
|
+
resolvedType = checker.getTypeOfSymbolAtLocation(match, varDecl);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const isFuncFromAst = isSimpleTarget &&
|
|
318
|
+
(entry.decl.init.kind === "FunctionExpression" ||
|
|
319
|
+
entry.decl.typeAnnotation?.kind === "FunctionTypeNode");
|
|
320
|
+
const isFuncFromTs = resolvedType !== undefined &&
|
|
321
|
+
!isEffectType(resolvedType, checker) &&
|
|
322
|
+
resolvedType.getCallSignatures().length > 0 &&
|
|
323
|
+
resolvedType.getConstructSignatures().length === 0;
|
|
324
|
+
const kind = isFuncFromAst || isFuncFromTs ? "function" : "binding";
|
|
325
|
+
const typeStr = resolvedType
|
|
326
|
+
? formatType(checker, resolvedType, varDecl)
|
|
327
|
+
: "unknown";
|
|
328
|
+
const effect = resolvedType
|
|
329
|
+
? decomposeEffectType(resolvedType, checker, varDecl)
|
|
330
|
+
: undefined;
|
|
331
|
+
return {
|
|
332
|
+
name,
|
|
333
|
+
kind,
|
|
334
|
+
exported: entry.exported,
|
|
335
|
+
file: funPath,
|
|
336
|
+
line: span.start.line,
|
|
337
|
+
column: span.start.column,
|
|
338
|
+
type: typeStr,
|
|
339
|
+
...(effect ? { effect } : {}),
|
|
340
|
+
};
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function collectAstTypedHoles(ast) {
|
|
344
|
+
const holes = [];
|
|
345
|
+
const visited = new Set();
|
|
346
|
+
const walk = (node) => {
|
|
347
|
+
if (!node || typeof node !== "object")
|
|
348
|
+
return;
|
|
349
|
+
if (visited.has(node))
|
|
350
|
+
return;
|
|
351
|
+
visited.add(node);
|
|
352
|
+
if (Array.isArray(node)) {
|
|
353
|
+
for (const item of node)
|
|
354
|
+
walk(item);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const record = node;
|
|
358
|
+
if (record.kind === "TypedHole") {
|
|
359
|
+
holes.push(record);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
for (const [key, value] of Object.entries(record)) {
|
|
363
|
+
if (key === "span")
|
|
364
|
+
continue;
|
|
365
|
+
walk(value);
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
walk(ast);
|
|
369
|
+
return holes;
|
|
370
|
+
}
|
|
371
|
+
function extractTypedHoleCallMessage(node) {
|
|
372
|
+
if (!ts.isCallExpression(node) || node.arguments.length !== 0) {
|
|
373
|
+
return undefined;
|
|
374
|
+
}
|
|
375
|
+
if (!ts.isParenthesizedExpression(node.expression)) {
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
const arrow = node.expression.expression;
|
|
379
|
+
if (!ts.isArrowFunction(arrow) || arrow.parameters.length !== 0) {
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
if (!ts.isBlock(arrow.body) || arrow.body.statements.length !== 1) {
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
const stmt = arrow.body.statements[0];
|
|
386
|
+
if (!ts.isThrowStatement(stmt) || !stmt.expression) {
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
if (!ts.isNewExpression(stmt.expression)) {
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
const ctor = stmt.expression.expression;
|
|
393
|
+
if (!ts.isIdentifier(ctor) || ctor.text !== "Error") {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
const args = stmt.expression.arguments;
|
|
397
|
+
if (!args || args.length !== 1 || !ts.isStringLiteral(args[0])) {
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
const prefix = "Typed hole reached: ";
|
|
401
|
+
if (!args[0].text.startsWith(prefix)) {
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
return args[0].text.slice(prefix.length);
|
|
405
|
+
}
|
|
406
|
+
function collectTsHoleCalls(sourceFile) {
|
|
407
|
+
const matches = [];
|
|
408
|
+
const visit = (node) => {
|
|
409
|
+
const rawMessage = extractTypedHoleCallMessage(node);
|
|
410
|
+
if (rawMessage !== undefined) {
|
|
411
|
+
matches.push({ call: node, rawMessage });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
ts.forEachChild(node, visit);
|
|
415
|
+
};
|
|
416
|
+
visit(sourceFile);
|
|
417
|
+
return matches;
|
|
418
|
+
}
|
|
419
|
+
function findEnclosingOuterFunctionReturnType(node, checker) {
|
|
420
|
+
let cur = node.parent;
|
|
421
|
+
while (cur) {
|
|
422
|
+
if (ts.isFunctionLike(cur) && cur.type) {
|
|
423
|
+
return checker.getTypeFromTypeNode(cur.type);
|
|
424
|
+
}
|
|
425
|
+
if (ts.isVariableDeclaration(cur) && cur.type) {
|
|
426
|
+
return checker.getTypeFromTypeNode(cur.type);
|
|
427
|
+
}
|
|
428
|
+
cur = cur.parent;
|
|
429
|
+
}
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
function unwrapEffectSuccessFromOuterCall(effectCall, checker) {
|
|
433
|
+
let outerCall = effectCall;
|
|
434
|
+
if (ts.isCallExpression(outerCall.parent) &&
|
|
435
|
+
outerCall.parent.expression.getText().endsWith("Effect.scoped")) {
|
|
436
|
+
outerCall = outerCall.parent;
|
|
437
|
+
}
|
|
438
|
+
const contextual = checker.getContextualType(outerCall);
|
|
439
|
+
const candidate = contextual ?? findEnclosingOuterFunctionReturnType(outerCall, checker);
|
|
440
|
+
if (candidate && isEffectType(candidate, checker)) {
|
|
441
|
+
const args = checker.getTypeArguments(candidate);
|
|
442
|
+
return args[0];
|
|
443
|
+
}
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
function unwrapEffectSuccessIfReturnInEffectGen(holeCall, checker) {
|
|
447
|
+
if (ts.isArrowFunction(holeCall.parent)) {
|
|
448
|
+
const parentCall = holeCall.parent.parent;
|
|
449
|
+
if (ts.isCallExpression(parentCall) &&
|
|
450
|
+
parentCall.expression.getText().endsWith("Effect.map")) {
|
|
451
|
+
return unwrapEffectSuccessFromOuterCall(parentCall, checker);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (!ts.isReturnStatement(holeCall.parent)) {
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
let genFn = holeCall.parent.parent;
|
|
458
|
+
while (genFn && !ts.isFunctionLike(genFn)) {
|
|
459
|
+
genFn = genFn.parent;
|
|
460
|
+
}
|
|
461
|
+
if (!genFn || !ts.isCallExpression(genFn.parent)) {
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
const effectCall = genFn.parent;
|
|
465
|
+
const calleeText = effectCall.expression.getText();
|
|
466
|
+
if (!calleeText.endsWith("Effect.gen") &&
|
|
467
|
+
calleeText !== "gen" &&
|
|
468
|
+
!calleeText.endsWith("Effect.map")) {
|
|
469
|
+
return undefined;
|
|
470
|
+
}
|
|
471
|
+
return unwrapEffectSuccessFromOuterCall(effectCall, checker);
|
|
472
|
+
}
|
|
473
|
+
function unwrapMatchChainExpectedType(holeCall, checker) {
|
|
474
|
+
if (!ts.isArrowFunction(holeCall.parent)) {
|
|
475
|
+
return undefined;
|
|
476
|
+
}
|
|
477
|
+
const armArrow = holeCall.parent;
|
|
478
|
+
if (!ts.isCallExpression(armArrow.parent)) {
|
|
479
|
+
return undefined;
|
|
480
|
+
}
|
|
481
|
+
let chainCall = armArrow.parent;
|
|
482
|
+
while (ts.isPropertyAccessExpression(chainCall.parent) &&
|
|
483
|
+
ts.isCallExpression(chainCall.parent.parent)) {
|
|
484
|
+
chainCall = chainCall.parent.parent;
|
|
485
|
+
}
|
|
486
|
+
const chainCtx = checker.getContextualType(chainCall);
|
|
487
|
+
if (chainCtx && (chainCtx.flags & ts.TypeFlags.Never) === 0) {
|
|
488
|
+
return chainCtx;
|
|
489
|
+
}
|
|
490
|
+
if (ts.isReturnStatement(chainCall.parent)) {
|
|
491
|
+
return findEnclosingOuterFunctionReturnType(chainCall, checker);
|
|
492
|
+
}
|
|
493
|
+
return undefined;
|
|
494
|
+
}
|
|
495
|
+
function resolveHoleExpectedType(holeCall, checker) {
|
|
496
|
+
const contextual = checker.getContextualType(holeCall);
|
|
497
|
+
if (contextual && (contextual.flags & ts.TypeFlags.Never) === 0) {
|
|
498
|
+
return formatType(checker, contextual, holeCall);
|
|
499
|
+
}
|
|
500
|
+
const effectSuccess = unwrapEffectSuccessIfReturnInEffectGen(holeCall, checker);
|
|
501
|
+
if (effectSuccess && (effectSuccess.flags & ts.TypeFlags.Never) === 0) {
|
|
502
|
+
return formatType(checker, effectSuccess, holeCall);
|
|
503
|
+
}
|
|
504
|
+
const matchType = unwrapMatchChainExpectedType(holeCall, checker);
|
|
505
|
+
if (matchType && (matchType.flags & ts.TypeFlags.Never) === 0) {
|
|
506
|
+
return formatType(checker, matchType, holeCall);
|
|
507
|
+
}
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
function resolveFileHoles(ast, funPath, sourceFile, checker) {
|
|
511
|
+
const astHoles = collectAstTypedHoles(ast);
|
|
512
|
+
if (astHoles.length === 0) {
|
|
513
|
+
return [];
|
|
514
|
+
}
|
|
515
|
+
const tsHoles = collectTsHoleCalls(sourceFile);
|
|
516
|
+
const usedTsIndices = new Set();
|
|
517
|
+
const queriedHoles = [];
|
|
518
|
+
for (const astHole of astHoles) {
|
|
519
|
+
const targetMsg = astHole.label || "???";
|
|
520
|
+
let matchedIdx = tsHoles.findIndex((h, idx) => !usedTsIndices.has(idx) && h.rawMessage === targetMsg);
|
|
521
|
+
if (matchedIdx === -1) {
|
|
522
|
+
matchedIdx = tsHoles.findIndex((_, idx) => !usedTsIndices.has(idx));
|
|
523
|
+
}
|
|
524
|
+
let expectedType;
|
|
525
|
+
if (matchedIdx !== -1) {
|
|
526
|
+
usedTsIndices.add(matchedIdx);
|
|
527
|
+
expectedType = resolveHoleExpectedType(tsHoles[matchedIdx].call, checker);
|
|
528
|
+
}
|
|
529
|
+
queriedHoles.push({
|
|
530
|
+
file: funPath,
|
|
531
|
+
line: astHole.span.start.line,
|
|
532
|
+
column: astHole.span.start.column,
|
|
533
|
+
...(astHole.label !== undefined ? { message: astHole.label } : {}),
|
|
534
|
+
...(expectedType !== undefined ? { expectedType } : {}),
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
return queriedHoles;
|
|
538
|
+
}
|
|
539
|
+
function inspectCompiledFile(compiled, program, checker, symbolFilter) {
|
|
540
|
+
const sourceFile = program.getSourceFile(compiled.virtualTsPath);
|
|
541
|
+
const imports = collectImports(compiled.ast);
|
|
542
|
+
const declEntries = collectTopLevelDeclarations(compiled.ast);
|
|
543
|
+
let symbols = [];
|
|
544
|
+
let holes = [];
|
|
545
|
+
if (sourceFile) {
|
|
546
|
+
const tsDecls = findTopLevelDeclarationsInTs(sourceFile);
|
|
547
|
+
for (const entry of declEntries) {
|
|
548
|
+
switch (entry.decl.kind) {
|
|
549
|
+
case "SchemaDeclaration":
|
|
550
|
+
symbols.push(resolveSchemaSymbol(entry, compiled.funPath, checker, tsDecls));
|
|
551
|
+
break;
|
|
552
|
+
case "ErrorDeclaration":
|
|
553
|
+
symbols.push(resolveErrorSymbol(entry, compiled.funPath, checker, tsDecls));
|
|
554
|
+
break;
|
|
555
|
+
case "ServiceDeclaration":
|
|
556
|
+
symbols.push(resolveServiceSymbol(entry, compiled.funPath, checker, tsDecls));
|
|
557
|
+
break;
|
|
558
|
+
case "BindingDeclaration":
|
|
559
|
+
symbols.push(...resolveBindingSymbols(entry, compiled.funPath, checker, tsDecls));
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
holes = resolveFileHoles(compiled.ast, compiled.funPath, sourceFile, checker);
|
|
564
|
+
}
|
|
565
|
+
if (symbolFilter) {
|
|
566
|
+
symbols = symbols.filter((s) => s.name === symbolFilter);
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
file: compiled.funPath,
|
|
570
|
+
imports,
|
|
571
|
+
symbols,
|
|
572
|
+
holes,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Queries semantic type, Effect channel, and typed-hole information for virtual FunkScript files.
|
|
577
|
+
*/
|
|
578
|
+
export const queryVirtualFiles = (virtualInputFiles, options) => Effect.tryPromise({
|
|
579
|
+
try: async () => {
|
|
580
|
+
const cwd = options?.cwd ? path.resolve(options.cwd) : process.cwd();
|
|
581
|
+
const compilerOptions = getCompilerOptions(cwd);
|
|
582
|
+
const virtualTsFiles = new Map();
|
|
583
|
+
const compiledFiles = [];
|
|
584
|
+
const virtualSources = new Map(Object.entries(virtualInputFiles).map(([file, content]) => [
|
|
585
|
+
path.resolve(cwd, file),
|
|
586
|
+
content,
|
|
587
|
+
]));
|
|
588
|
+
for (const [filePath, source] of Object.entries(virtualInputFiles)) {
|
|
589
|
+
const fullFunPath = path.isAbsolute(filePath)
|
|
590
|
+
? path.normalize(filePath)
|
|
591
|
+
: path.normalize(path.join(cwd, filePath));
|
|
592
|
+
const virtualTsPath = toVirtualTsPath(fullFunPath);
|
|
593
|
+
let compiled;
|
|
594
|
+
try {
|
|
595
|
+
compiled = await Effect.runPromise(compileString(source, filePath, {
|
|
596
|
+
sourcemap: false,
|
|
597
|
+
allowHoles: true,
|
|
598
|
+
preserveFunImports: true,
|
|
599
|
+
sourcePath: fullFunPath,
|
|
600
|
+
virtualSources,
|
|
601
|
+
}));
|
|
602
|
+
}
|
|
603
|
+
catch (err) {
|
|
604
|
+
const compErr = err instanceof CompilerError
|
|
605
|
+
? err
|
|
606
|
+
: new CompilerError(toErrorMessage(err), err instanceof Error ? err : new Error(toErrorMessage(err)));
|
|
607
|
+
throw new Error(`Failed to compile '${filePath}': ${compErr.message}`);
|
|
608
|
+
}
|
|
609
|
+
virtualTsFiles.set(virtualTsPath, compiled.typescript);
|
|
610
|
+
compiledFiles.push({
|
|
611
|
+
funPath: filePath,
|
|
612
|
+
virtualTsPath,
|
|
613
|
+
ast: compiled.ast,
|
|
614
|
+
typescript: compiled.typescript,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
const hasTsx = Array.from(virtualTsFiles.keys()).some((fileName) => fileName.endsWith(".tsx"));
|
|
618
|
+
const hasJsxIntrinsicTypes = Array.from(virtualTsFiles.values()).some((src) => /namespace\s+JSX[\s\S]*?IntrinsicElements/.test(src));
|
|
619
|
+
if (hasTsx && !hasJsxIntrinsicTypes) {
|
|
620
|
+
const jsxTypesPath = path.join(cwd, "__funscript_jsx_intrinsics__.d.ts");
|
|
621
|
+
virtualTsFiles.set(jsxTypesPath, JSX_INTRINSICS_DECLARATION);
|
|
622
|
+
}
|
|
623
|
+
virtualTsFiles.set(path.join(cwd, "__funscript_ts_reset__.d.ts"), TS_RESET_DECLARATION);
|
|
624
|
+
virtualTsFiles.set(path.join(cwd, "__funscript_type_fest__.d.ts"), TYPE_FEST_DECLARATION);
|
|
625
|
+
const files = [];
|
|
626
|
+
if (compiledFiles.length > 0) {
|
|
627
|
+
const defaultHost = ts.createCompilerHost(compilerOptions);
|
|
628
|
+
const resolveVirtualContent = (fileName) => {
|
|
629
|
+
const norm = path.normalize(fileName);
|
|
630
|
+
const direct = virtualTsFiles.get(norm);
|
|
631
|
+
if (direct !== undefined)
|
|
632
|
+
return direct;
|
|
633
|
+
if (virtualTsFiles.has(norm + ".ts"))
|
|
634
|
+
return virtualTsFiles.get(norm + ".ts");
|
|
635
|
+
if (virtualTsFiles.has(norm + ".tsx"))
|
|
636
|
+
return virtualTsFiles.get(norm + ".tsx");
|
|
637
|
+
if (norm.endsWith(".js") &&
|
|
638
|
+
virtualTsFiles.has(norm.slice(0, -3) + ".ts")) {
|
|
639
|
+
return virtualTsFiles.get(norm.slice(0, -3) + ".ts");
|
|
640
|
+
}
|
|
641
|
+
if (norm.endsWith(".jsx") &&
|
|
642
|
+
virtualTsFiles.has(norm.slice(0, -4) + ".tsx")) {
|
|
643
|
+
return virtualTsFiles.get(norm.slice(0, -4) + ".tsx");
|
|
644
|
+
}
|
|
645
|
+
if (path.basename(fileName) === "__funscript_type_fest__.d.ts") {
|
|
646
|
+
return TYPE_FEST_DECLARATION;
|
|
647
|
+
}
|
|
648
|
+
if (path.basename(fileName) === "__funscript_ts_reset__.d.ts") {
|
|
649
|
+
return TS_RESET_DECLARATION;
|
|
650
|
+
}
|
|
651
|
+
if (path.basename(fileName) === "__funscript_jsx_intrinsics__.d.ts") {
|
|
652
|
+
return JSX_INTRINSICS_DECLARATION;
|
|
653
|
+
}
|
|
654
|
+
return undefined;
|
|
655
|
+
};
|
|
656
|
+
const host = {
|
|
657
|
+
...defaultHost,
|
|
658
|
+
getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
|
|
659
|
+
const content = resolveVirtualContent(fileName);
|
|
660
|
+
if (content !== undefined) {
|
|
661
|
+
return ts.createSourceFile(fileName, content, languageVersion, true);
|
|
662
|
+
}
|
|
663
|
+
return defaultHost.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
|
|
664
|
+
},
|
|
665
|
+
fileExists: (fileName) => {
|
|
666
|
+
if (resolveVirtualContent(fileName) !== undefined)
|
|
667
|
+
return true;
|
|
668
|
+
return defaultHost.fileExists(fileName);
|
|
669
|
+
},
|
|
670
|
+
readFile: (fileName) => {
|
|
671
|
+
const content = resolveVirtualContent(fileName);
|
|
672
|
+
if (content !== undefined)
|
|
673
|
+
return content;
|
|
674
|
+
return defaultHost.readFile(fileName);
|
|
675
|
+
},
|
|
676
|
+
writeFile: () => { },
|
|
677
|
+
};
|
|
678
|
+
host.resolveModuleNames = (names, containingFile) => names.map((name) => {
|
|
679
|
+
if (/^\.{1,2}\//.test(name)) {
|
|
680
|
+
if (name.endsWith(".js")) {
|
|
681
|
+
const regular = ts.resolveModuleName(name, containingFile, compilerOptions, defaultHost).resolvedModule;
|
|
682
|
+
if (regular)
|
|
683
|
+
return regular;
|
|
684
|
+
}
|
|
685
|
+
const source = resolveFunModule(containingFile, name, (file) => /\.funx?$/.test(file) &&
|
|
686
|
+
virtualTsFiles.has(file + (file.endsWith(".funx") ? ".tsx" : ".ts")), { directories: true });
|
|
687
|
+
if (source) {
|
|
688
|
+
return {
|
|
689
|
+
resolvedFileName: source + (source.endsWith(".funx") ? ".tsx" : ".ts"),
|
|
690
|
+
extension: source.endsWith(".funx")
|
|
691
|
+
? ts.Extension.Tsx
|
|
692
|
+
: ts.Extension.Ts,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
if (/\.funx?$/.test(name))
|
|
696
|
+
return undefined;
|
|
697
|
+
}
|
|
698
|
+
return ts.resolveModuleName(name, containingFile, compilerOptions, host).resolvedModule;
|
|
699
|
+
});
|
|
700
|
+
const program = ts.createProgram({
|
|
701
|
+
rootNames: Array.from(virtualTsFiles.keys()),
|
|
702
|
+
options: compilerOptions,
|
|
703
|
+
host,
|
|
704
|
+
});
|
|
705
|
+
const checker = program.getTypeChecker();
|
|
706
|
+
for (const compiled of compiledFiles) {
|
|
707
|
+
files.push(inspectCompiledFile(compiled, program, checker, options?.symbol));
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return {
|
|
711
|
+
schemaVersion: 1,
|
|
712
|
+
files,
|
|
713
|
+
};
|
|
714
|
+
},
|
|
715
|
+
catch: (error) => error instanceof Error
|
|
716
|
+
? error
|
|
717
|
+
: new Error(`Query failed: ${toErrorMessage(error)}`),
|
|
718
|
+
});
|
|
719
|
+
/**
|
|
720
|
+
* Reads FunkScript files from disk and queries their semantic information.
|
|
721
|
+
*/
|
|
722
|
+
export const queryFiles = (files, options) => Effect.gen(function* () {
|
|
723
|
+
const cwd = options?.cwd ? path.resolve(options.cwd) : process.cwd();
|
|
724
|
+
const virtualFiles = {};
|
|
725
|
+
for (const filePath of files) {
|
|
726
|
+
const fullPath = path.isAbsolute(filePath)
|
|
727
|
+
? filePath
|
|
728
|
+
: path.resolve(cwd, filePath);
|
|
729
|
+
const content = yield* Effect.tryPromise({
|
|
730
|
+
try: () => fs.readFile(fullPath, "utf-8"),
|
|
731
|
+
catch: (err) => new Error(`Failed to read file '${filePath}': ${toErrorMessage(err)}`),
|
|
732
|
+
});
|
|
733
|
+
virtualFiles[filePath] = content;
|
|
734
|
+
}
|
|
735
|
+
return yield* queryVirtualFiles(virtualFiles, options);
|
|
736
|
+
});
|
|
737
|
+
/**
|
|
738
|
+
* Formats a QueryReport as JSON or human-readable semantic inspection output.
|
|
739
|
+
*/
|
|
740
|
+
export function formatQueryReport(report, options) {
|
|
741
|
+
if (options?.json) {
|
|
742
|
+
return JSON.stringify(report, null, 2);
|
|
743
|
+
}
|
|
744
|
+
if (report.files.length === 0) {
|
|
745
|
+
return "No files queried.";
|
|
746
|
+
}
|
|
747
|
+
const sections = [];
|
|
748
|
+
for (const fileReport of report.files) {
|
|
749
|
+
const lines = [`File: ${fileReport.file}`];
|
|
750
|
+
if (fileReport.imports.length > 0) {
|
|
751
|
+
lines.push(` Imports: ${fileReport.imports.join(", ")}`);
|
|
752
|
+
}
|
|
753
|
+
if (fileReport.symbols.length === 0) {
|
|
754
|
+
lines.push(" Symbols: (none)");
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
lines.push(` Symbols (${fileReport.symbols.length}):`);
|
|
758
|
+
for (const sym of fileReport.symbols) {
|
|
759
|
+
const exp = sym.exported ? "export " : "";
|
|
760
|
+
lines.push(` ${exp}${sym.kind} ${sym.name}: ${sym.type} (${sym.file}:${sym.line}:${sym.column})`);
|
|
761
|
+
if (sym.effect) {
|
|
762
|
+
const reqs = sym.effect.requirements.length > 0
|
|
763
|
+
? sym.effect.requirements.join(" | ")
|
|
764
|
+
: "never";
|
|
765
|
+
lines.push(` effect: success=${sym.effect.success}, error=${sym.effect.error}, requirements=${reqs}`);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
if (fileReport.holes.length > 0) {
|
|
770
|
+
lines.push(` Holes (${fileReport.holes.length}):`);
|
|
771
|
+
for (const hole of fileReport.holes) {
|
|
772
|
+
const label = hole.message ? ` "${hole.message}"` : "";
|
|
773
|
+
const expected = hole.expectedType ?? "unknown";
|
|
774
|
+
lines.push(` ???${label} -> expected ${expected} (${hole.file}:${hole.line}:${hole.column})`);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
sections.push(lines.join("\n"));
|
|
778
|
+
}
|
|
779
|
+
return sections.join("\n\n");
|
|
780
|
+
}
|
|
781
|
+
//# sourceMappingURL=query.js.map
|