1
+ import * as MemFS from 'memfs'
2
+ import { describe , expect , test } from 'vitest'
3
+ import { generate } from '../generator/generate.js'
4
+
5
+ const fs = MemFS . fs . promises as any
6
+
7
+ describe ( 'Scalar module generation' , ( ) => {
8
+ describe ( 'Issue #1354 - TypeScript reserved keywords' , ( ) => {
9
+ test ( 'should escape reserved TypeScript keywords in scalar names' , async ( ) => {
10
+ await generate ( {
11
+ fs,
12
+ schema : {
13
+ type : 'sdl' ,
14
+ sdl : `
15
+ scalar bigint
16
+ scalar boolean
17
+
18
+ type Query {
19
+ getBigint: bigint
20
+ getBoolean: boolean
21
+ }
22
+ `
23
+ }
24
+ } )
25
+
26
+ const scalarModule = MemFS . fs . readFileSync ( './graffle/modules/scalar.ts' , 'utf8' )
27
+
28
+ // The generated code should use escaped names (e.g., $bigint instead of bigint)
29
+ // to avoid TypeScript compilation errors
30
+ expect ( scalarModule ) . toContain ( 'export type $bigint =' )
31
+ expect ( scalarModule ) . toContain ( 'export type $boolean =' )
32
+
33
+ // But the scalar references should still use the original names
34
+ expect ( scalarModule ) . toContain ( 'ScalarCodecless<"bigint">' )
35
+ expect ( scalarModule ) . toContain ( 'ScalarCodecless<"boolean">' )
36
+ } )
37
+
38
+ test ( 'should escape reserved keywords for custom scalars with codecs' , async ( ) => {
39
+ // First create the custom scalars file
40
+ await fs . mkdir ( './custom-scalars' , { recursive : true } )
41
+ await fs . writeFile ( './custom-scalars/scalars.js' , `
42
+ export const bigint = {
43
+ encode: (value) => value.toString(),
44
+ decode: (value) => BigInt(value),
45
+ }
46
+ ` )
47
+
48
+ await generate ( {
49
+ fs,
50
+ schema : {
51
+ type : 'sdl' ,
52
+ sdl : `
53
+ scalar bigint
54
+ type Query {
55
+ getBigint: bigint
56
+ }
57
+ `
58
+ } ,
59
+ customScalars : './custom-scalars/scalars.js'
60
+ } )
61
+
62
+ const scalarModule = MemFS . fs . readFileSync ( './graffle/modules/scalar.ts' , 'utf8' )
63
+
64
+ // Type definitions should be escaped
65
+ expect ( scalarModule ) . toContain ( 'export type $bigint = typeof' )
66
+ expect ( scalarModule ) . toContain ( 'type $bigint_ = typeof' )
67
+ expect ( scalarModule ) . toContain ( 'export type $bigintDecoded =' )
68
+ expect ( scalarModule ) . toContain ( 'export type $bigintEncoded =' )
69
+
70
+ // But runtime references should use original names
71
+ expect ( scalarModule ) . toContain ( 'typeof CustomScalars.bigint' )
72
+ } )
73
+ } )
74
+ } )
0 commit comments