Skip to content

Commit 8e54747

Browse files
author
Nico Sammito
authored
Merge pull request #17 from code0-tech/feat/#16
Value suggestion should also work with dataTypes
2 parents a67e601 + 2371364 commit 8e54747

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/suggestion/getValueSuggestions.ts

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

55
/**
66
* Extracts possible literal values from a type string to provide suggestions.
77
*/
8-
export const getValueSuggestions = (type: string): LiteralValue[] => {
8+
export const getValueSuggestions = (
9+
type?: string,
10+
dataTypes?: DataType[]
11+
): LiteralValue[] => {
912
if (!type) return [];
1013

11-
const sourceCode = `type T = ${type}; const val: T = {} as any;`;
14+
const sourceCode = `
15+
${getSharedTypeDeclarations(dataTypes)}
16+
type T = ${type}; const val: T = {} as any;
17+
`;
1218
const fileName = "index.ts";
1319
const host = createCompilerHost(fileName, sourceCode);
1420
const sourceFile = host.getSourceFile(fileName)!;

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ export const DEFAULT_COMPILER_OPTIONS: ts.CompilerOptions = {
7575
/**
7676
* Extracts and returns common type and generic declarations from DATA_TYPES.
7777
*/
78-
export function getSharedTypeDeclarations(dataTypes: DataType[]): string {
79-
const genericDeclarations = Array.from(new Set(dataTypes.flatMap(dt => dt.genericKeys || [])))
78+
export function getSharedTypeDeclarations(dataTypes?: DataType[]): string {
79+
const genericDeclarations = Array.from(new Set(dataTypes?.flatMap(dt => dt.genericKeys || [])))
8080
.map(g => `type ${g} = any;`)
8181
.join("\n");
8282

83-
const typeAliasDeclarations = dataTypes.map(dt =>
83+
const typeAliasDeclarations = dataTypes?.map(dt =>
8484
`type ${dt.identifier}${dt.genericKeys ? `<${dt.genericKeys.join(",")}>` : ""} = ${dt.type};`
8585
).join("\n");
8686

0 commit comments

Comments
 (0)