@nudojs/service 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 +14 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +523 -0
- package/package.json +16 -0
- package/src/__tests__/analyzer.test.ts +137 -0
- package/src/__tests__/dts-generator.test.ts +98 -0
- package/src/analyzer.ts +650 -0
- package/src/dts-generator.ts +85 -0
- package/src/index.ts +22 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { TypeValue } from "@nudojs/core";
|
|
2
|
+
import { typeValueToString } from "@nudojs/core";
|
|
3
|
+
import type { AnalysisResult } from "./analyzer.ts";
|
|
4
|
+
|
|
5
|
+
export function typeValueToTSType(tv: TypeValue): string {
|
|
6
|
+
switch (tv.kind) {
|
|
7
|
+
case "literal": {
|
|
8
|
+
const v = tv.value;
|
|
9
|
+
if (v === null) return "null";
|
|
10
|
+
if (v === undefined) return "undefined";
|
|
11
|
+
if (typeof v === "string") return JSON.stringify(v);
|
|
12
|
+
if (typeof v === "boolean") return String(v);
|
|
13
|
+
return String(v);
|
|
14
|
+
}
|
|
15
|
+
case "primitive":
|
|
16
|
+
return tv.type;
|
|
17
|
+
case "object": {
|
|
18
|
+
const entries = Object.entries(tv.properties);
|
|
19
|
+
if (entries.length === 0) return "{}";
|
|
20
|
+
const inner = entries
|
|
21
|
+
.map(([k, v]) => `${k}: ${typeValueToTSType(v)}`)
|
|
22
|
+
.join("; ");
|
|
23
|
+
return `{ ${inner} }`;
|
|
24
|
+
}
|
|
25
|
+
case "array":
|
|
26
|
+
return `${wrapComplexType(tv.element)}[]`;
|
|
27
|
+
case "tuple": {
|
|
28
|
+
const inner = tv.elements.map(typeValueToTSType).join(", ");
|
|
29
|
+
return `[${inner}]`;
|
|
30
|
+
}
|
|
31
|
+
case "function": {
|
|
32
|
+
const params = tv.params
|
|
33
|
+
.map((p, i) => `${p}: unknown`)
|
|
34
|
+
.join(", ");
|
|
35
|
+
return `(${params}) => unknown`;
|
|
36
|
+
}
|
|
37
|
+
case "promise":
|
|
38
|
+
return `Promise<${typeValueToTSType(tv.value)}>`;
|
|
39
|
+
case "instance":
|
|
40
|
+
return tv.className;
|
|
41
|
+
case "union":
|
|
42
|
+
return tv.members.map(typeValueToTSType).join(" | ");
|
|
43
|
+
case "never":
|
|
44
|
+
return "never";
|
|
45
|
+
case "unknown":
|
|
46
|
+
return "unknown";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function wrapComplexType(tv: TypeValue): string {
|
|
51
|
+
const ts = typeValueToTSType(tv);
|
|
52
|
+
if (tv.kind === "union") return `(${ts})`;
|
|
53
|
+
return ts;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function generateDts(result: AnalysisResult): string {
|
|
57
|
+
const lines: string[] = [];
|
|
58
|
+
|
|
59
|
+
for (const fn of result.functions) {
|
|
60
|
+
if (fn.cases.length === 0 && fn.combined) {
|
|
61
|
+
lines.push(`export declare function ${fn.name}(...args: unknown[]): ${typeValueToTSType(fn.combined)};`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (fn.cases.length === 1) {
|
|
66
|
+
const c = fn.cases[0];
|
|
67
|
+
const params = c.args
|
|
68
|
+
.map((a, i) => `arg${i}: ${typeValueToTSType(a)}`)
|
|
69
|
+
.join(", ");
|
|
70
|
+
const ret = typeValueToTSType(c.result);
|
|
71
|
+
lines.push(`export declare function ${fn.name}(${params}): ${ret};`);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (const c of fn.cases) {
|
|
76
|
+
const params = c.args
|
|
77
|
+
.map((a, i) => `arg${i}: ${typeValueToTSType(a)}`)
|
|
78
|
+
.join(", ");
|
|
79
|
+
const ret = typeValueToTSType(c.result);
|
|
80
|
+
lines.push(`export declare function ${fn.name}(${params}): ${ret};`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return lines.join("\n") + "\n";
|
|
85
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type AnalysisResult,
|
|
3
|
+
type FunctionAnalysis,
|
|
4
|
+
type CaseResult,
|
|
5
|
+
type CaseInfo,
|
|
6
|
+
type CaseHint,
|
|
7
|
+
type Diagnostic,
|
|
8
|
+
type DiagnosticSeverity,
|
|
9
|
+
type DiagnosticTag,
|
|
10
|
+
type SourceLocation,
|
|
11
|
+
type BindingInfo,
|
|
12
|
+
type CompletionItem,
|
|
13
|
+
analyzeFile,
|
|
14
|
+
getTypeAtPosition,
|
|
15
|
+
getCompletionsAtPosition,
|
|
16
|
+
getCasesForFile,
|
|
17
|
+
} from "./analyzer.ts";
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
typeValueToTSType,
|
|
21
|
+
generateDts,
|
|
22
|
+
} from "./dts-generator.ts";
|
package/tsconfig.json
ADDED