Skip to content

Commit 23dd485

Browse files
author
Nico Sammito
authored
Merge pull request #11 from code0-tech/feat/#6
Update to new sagittarius types and prepare release
2 parents 718f09c + 547db68 commit 23dd485

21 files changed

Lines changed: 309 additions & 267 deletions

package-lock.json

Lines changed: 30 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929
"test": "vitest run",
3030
"build": "vite build"
3131
},
32-
"dependencies": {
33-
"typescript": "^5.9.3"
34-
},
3532
"devDependencies": {
36-
"@code0-tech/sagittarius-graphql-types": "0.0.0-experimental-2342308809-931efb40b4bf3245999c53abbdd9164cea82e82d",
33+
"@code0-tech/sagittarius-graphql-types": "0.0.0-experimental-2385560645-52d09772ef7058a833cf32edc393ce95668b8404",
3734
"@types/node": "^25.3.2",
35+
"typescript": "^5.9.3",
3836
"vite": "^7.3.1",
3937
"vite-plugin-dts": "^4.5.4",
40-
"vitest": "^4.0.18"
38+
"vitest": "^4.0.18",
39+
"@typescript/vfs": "^1.6.4"
40+
},
41+
"peerDependencies": {
42+
"@code0-tech/sagittarius-graphql-types": "0.0.0-experimental-2385560645-52d09772ef7058a833cf32edc393ce95668b8404",
43+
"typescript": "^5.9.3",
44+
"@typescript/vfs": "^1.6.4"
4145
}
4246
}

src/extraction/getTypeFromValue.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ import {LiteralValue} from "@code0-tech/sagittarius-graphql-types";
55
/**
66
* Uses the TypeScript compiler to generate a precise type string from any runtime value.
77
*/
8-
export const getTypeFromValue = (value: LiteralValue): string => {
8+
export const getTypeFromValue = (value: LiteralValue | null): string => {
99
// 1. Serialize value to a JSON string for embedding in source code.
10-
const literal = JSON.stringify(value.value);
10+
const literal = JSON.stringify(value?.value);
1111

1212
// 2. Wrap value in virtual source code.
13-
const sourceCode = `const tempValue = ${literal};`;
14-
const fileName = "temp_value.ts";
15-
const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest);
16-
17-
// 3. Setup a minimal compiler host.
18-
const host = createCompilerHost(fileName, sourceCode, sourceFile);
19-
20-
const program = ts.createProgram([fileName], {target: ts.ScriptTarget.Latest, noEmit: true}, host);
13+
const sourceCode = `const tempValue = ${literal ?? "any"};`;
14+
const fileName = "index.ts";
15+
const host = createCompilerHost(fileName, sourceCode);
16+
const sourceFile = host.getSourceFile(fileName)!;
17+
const program = host.languageService.getProgram()!;
2118
const checker = program.getTypeChecker();
2219

2320
let inferredType = "any";

src/extraction/getTypeVariant.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ts from "typescript";
2-
import {ExtendedDataType, getSharedTypeDeclarations, createCompilerHost, DEFAULT_COMPILER_OPTIONS} from "../utils";
2+
import {createCompilerHost, getSharedTypeDeclarations} from "../utils";
3+
import {DataType} from "@code0-tech/sagittarius-graphql-types";
34

45
export enum DataTypeVariant {
56
PRIMITIVE,
@@ -13,10 +14,9 @@ export enum DataTypeVariant {
1314
*/
1415
export const getTypeVariant = (
1516
type: string,
16-
dataTypes: ExtendedDataType[]
17+
dataTypes: DataType[]
1718
): DataTypeVariant => {
1819
const typeDefs = getSharedTypeDeclarations(dataTypes);
19-
const fileName = `type_probe_${Math.random().toString(36).substring(7)}.ts`;
2020

2121
// We declare a variable with the type to probe it
2222
const sourceCode = `
@@ -25,9 +25,10 @@ export const getTypeVariant = (
2525
const val: TargetType = {} as any;
2626
`;
2727

28-
const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest);
29-
const host = createCompilerHost(fileName, sourceCode, sourceFile);
30-
const program = ts.createProgram([fileName], DEFAULT_COMPILER_OPTIONS, host);
28+
const fileName = `index.ts`;
29+
const host = createCompilerHost(fileName, sourceCode);
30+
const sourceFile = host.getSourceFile(fileName)!;
31+
const program = host.languageService.getProgram()!;
3132
const checker = program.getTypeChecker();
3233

3334
let discoveredVariant: DataTypeVariant = DataTypeVariant.TYPE;

src/extraction/getTypesFromNode.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import ts from "typescript";
2-
import {Flow, NodeFunction, NodeParameter} from "@code0-tech/sagittarius-graphql-types";
3-
import {
4-
createCompilerHost,
5-
DEFAULT_COMPILER_OPTIONS,
6-
ExtendedDataType,
7-
ExtendedFunction,
8-
getParameterCode,
9-
getSharedTypeDeclarations,
10-
} from "../utils";
2+
import {DataType, Flow, FunctionDefinition, NodeFunction, NodeParameter} from "@code0-tech/sagittarius-graphql-types";
3+
import {createCompilerHost, getParameterCode, getSharedTypeDeclarations,} from "../utils";
114
import {getNodeValidation} from "../validation/getNodeValidation"; // Wieder hinzugefügt
125

136
export interface NodeTypes {
@@ -20,8 +13,8 @@ export interface NodeTypes {
2013
*/
2114
export const getTypesFromNode = (
2215
node: NodeFunction,
23-
functions: ExtendedFunction[],
24-
dataTypes: ExtendedDataType[]
16+
functions: FunctionDefinition[],
17+
dataTypes: DataType[]
2518
): NodeTypes => {
2619
const funcMap = new Map(functions.map(f => [f.identifier, f]));
2720
const funcDef = funcMap.get(node.functionDefinition?.identifier);
@@ -35,7 +28,7 @@ export const getTypesFromNode = (
3528

3629
const mockFlow: Flow = {
3730
id: "gid://sagittarius/Flow/0" as any,
38-
nodes: { __typename: "NodeFunctionConnection", nodes: [node] }
31+
nodes: {__typename: "NodeFunctionConnection", nodes: [node]}
3932
} as Flow;
4033

4134
const params = (node.parameters?.nodes as NodeParameter[]) || [];
@@ -50,11 +43,10 @@ export const getTypesFromNode = (
5043
const result = testFunc(${funcCallArgs});
5144
`;
5245

53-
const fileName = "node_types_virtual.ts";
54-
const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest);
55-
const host = createCompilerHost(fileName, sourceCode, sourceFile);
56-
57-
const program = ts.createProgram([fileName], DEFAULT_COMPILER_OPTIONS, host);
46+
const fileName = "index.ts";
47+
const host = createCompilerHost(fileName, sourceCode);
48+
const sourceFile = host.getSourceFile(fileName)!;
49+
const program = host.languageService.getProgram()!;
5850
const checker = program.getTypeChecker();
5951

6052
let inferredReturnType = "any";

src/extraction/getValueFromType.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
import ts from "typescript";
2-
import {LiteralValue} from "@code0-tech/sagittarius-graphql-types";
3-
import {createCompilerHost, DEFAULT_COMPILER_OPTIONS, ExtendedDataType, getSharedTypeDeclarations} from "../utils";
2+
import {DataType, LiteralValue} from "@code0-tech/sagittarius-graphql-types";
3+
import {createCompilerHost, getSharedTypeDeclarations} from "../utils";
44

55
/**
66
* Generates a sample LiteralValue from a TypeScript type string.
77
*/
88
export const getValueFromType = (
99
type: string,
10-
dataTypes: ExtendedDataType[]
10+
dataTypes: DataType[]
1111
): LiteralValue => {
1212
// 1. Prepare declarations.
1313
const sourceCode = `
1414
${getSharedTypeDeclarations(dataTypes)}
1515
type Target = ${type};
1616
`;
1717

18-
const fileName = "temp_type_to_value.ts";
19-
const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest, true);
20-
21-
// 2. Setup the compiler context.
22-
const host = createCompilerHost(fileName, sourceCode, sourceFile);
23-
24-
const program = ts.createProgram([fileName], DEFAULT_COMPILER_OPTIONS, host);
25-
18+
const fileName = "index.ts";
19+
const host = createCompilerHost(fileName, sourceCode);
20+
const sourceFile = host.getSourceFile(fileName)!;
21+
const program = host.languageService.getProgram()!;
2622
const checker = program.getTypeChecker();
2723

2824
// 3. Find the Target type alias.

src/suggestion/getNodeSuggestions.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
1-
import {NodeFunction} from "@code0-tech/sagittarius-graphql-types";
2-
import ts from "typescript";
3-
import {createCompilerHost, DEFAULT_COMPILER_OPTIONS, ExtendedFunction, getSharedTypeDeclarations} from "../utils";
4-
import {DATA_TYPES} from "../../test/data";
1+
import {DataType, FunctionDefinition, NodeFunction} from "@code0-tech/sagittarius-graphql-types";
2+
import {createCompilerHost, getSharedTypeDeclarations} from "../utils";
53

64
/**
75
* Suggests NodeFunctions based on a given type and a list of available FunctionDefinitions.
86
* Returns functions whose return type is compatible with the target type.
97
*/
10-
export function getNodeSuggestions(type: string, functions: ExtendedFunction[]): NodeFunction[] {
8+
export function getNodeSuggestions(type: string, functions: FunctionDefinition[], dataTypes: DataType[]): NodeFunction[] {
119
if (!type || !functions || functions.length === 0) {
1210
return [];
1311
}
1412

15-
const fileName = "suggestions.ts";
16-
17-
const sharedTypes = getSharedTypeDeclarations(DATA_TYPES);
18-
1913
function getGenericsCount(input: string): number {
2014
const match = input.match(/<([^>]+)>/);
2115
if (!match) return 0;
2216
return match[1].split(',').map(s => s.trim()).filter(Boolean).length;
2317
}
2418

19+
const sharedTypes = getSharedTypeDeclarations(dataTypes);
2520
const sourceCode = `
2621
${sharedTypes}
2722
type TargetType = ${type};
2823
${functions.map((f, i) => {
29-
24+
3025
return `
3126
declare function Fu${i}${f.signature};
32-
type F${i} = ReturnType<typeof Fu${i}${getGenericsCount(f.signature) > 0 ? `<${Array(getGenericsCount(f.signature)).fill("any").join(", ")}>` : ""}>;
27+
type F${i} = ReturnType<typeof Fu${i}${getGenericsCount(f.signature!) > 0 ? `<${Array(getGenericsCount(f.signature!)).fill("any").join(", ")}>` : ""}>;
3328
`;
3429
}).join("\n")}
3530
${functions.map((_, i) => `const check${i}: TargetType = {} as F${i};`).join("\n")}
3631
`;
3732

38-
console.log(sourceCode)
39-
40-
const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest);
41-
const host = createCompilerHost(fileName, sourceCode, sourceFile);
42-
const program = ts.createProgram([fileName], {
43-
...DEFAULT_COMPILER_OPTIONS
44-
}, host);
33+
const fileName = "index.ts";
34+
const host = createCompilerHost(fileName, sourceCode);
35+
const sourceFile = host.getSourceFile(fileName)!;
36+
const program = host.languageService.getProgram()!;
4537

4638
const diagnostics = program.getSemanticDiagnostics();
4739
const errorLines = new Set<number>();
@@ -59,7 +51,6 @@ export function getNodeSuggestions(type: string, functions: ExtendedFunction[]):
5951
const actualLine = lines.findIndex(l => l.includes(lineToMatch));
6052

6153

62-
6354
if (actualLine !== -1 && errorLines.has(actualLine)) {
6455
return null;
6556
}

0 commit comments

Comments
 (0)