File tree Expand file tree Collapse file tree 2 files changed +64
-1
lines changed Expand file tree Collapse file tree 2 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -182,10 +182,15 @@ function stringify(arg: any, quoteString?: boolean): string {
182
182
if ( isConvertableRegexp ( arg ) )
183
183
return arg ;
184
184
185
+ const v = tryEval ( arg )
186
+ if ( v !== undefined )
187
+ arg = v
188
+
185
189
if ( quoteString )
186
190
return JSON . stringify ( arg ) ;
187
191
}
188
- if ( typeof arg === 'boolean' || typeof arg === 'number' || typeof arg === 'bigint' )
192
+
193
+ if ( typeof arg === 'boolean' || typeof arg === 'number' || typeof arg === 'bigint' || arg === 'undefined' || arg === null )
189
194
return `${ arg } ` ;
190
195
191
196
return JSON . stringify ( arg ) ;
@@ -199,6 +204,16 @@ function apiArgsFromConstValueNode(value: ConstValueNode): any[] {
199
204
return [ val ] ;
200
205
}
201
206
207
+ function tryEval ( maybeValidJavaScript : string ) : any | undefined {
208
+ try {
209
+ // eslint-disable-next-line no-eval
210
+ return eval ( maybeValidJavaScript )
211
+ }
212
+ catch {
213
+ return undefined
214
+ }
215
+ }
216
+
202
217
export const exportedForTesting = {
203
218
applyArgToApiSchemaTemplate,
204
219
buildApiFromDirectiveObjectArguments,
Original file line number Diff line number Diff line change @@ -190,6 +190,38 @@ describe('format directive config', () => {
190
190
} ,
191
191
want : 'true' ,
192
192
} ,
193
+ {
194
+ name : 'eval number' ,
195
+ args : {
196
+ template : '$1' ,
197
+ apiArgs : [ '10 + 1' ] ,
198
+ } ,
199
+ want : '11' ,
200
+ } ,
201
+ {
202
+ name : 'eval boolean' ,
203
+ args : {
204
+ template : '$1' ,
205
+ apiArgs : [ '!true' ] ,
206
+ } ,
207
+ want : 'false' ,
208
+ } ,
209
+ {
210
+ name : 'eval template with number' ,
211
+ args : {
212
+ template : '$1 + 1' ,
213
+ apiArgs : [ 10 ] ,
214
+ } ,
215
+ want : '11' ,
216
+ } ,
217
+ {
218
+ name : 'eval template with boolean' ,
219
+ args : {
220
+ template : '$1 && false' ,
221
+ apiArgs : [ true ] ,
222
+ } ,
223
+ want : 'false' ,
224
+ } ,
193
225
{
194
226
name : 'array' ,
195
227
args : {
@@ -206,6 +238,22 @@ describe('format directive config', () => {
206
238
} ,
207
239
want : `{"hello":"world"}` ,
208
240
} ,
241
+ {
242
+ name : 'undefined' ,
243
+ args : {
244
+ template : '$1' ,
245
+ apiArgs : [ 'undefined' ] ,
246
+ } ,
247
+ want : 'undefined' ,
248
+ } ,
249
+ {
250
+ name : 'null' ,
251
+ args : {
252
+ template : '$1' ,
253
+ apiArgs : [ 'null' ] ,
254
+ } ,
255
+ want : 'null' ,
256
+ } ,
209
257
{
210
258
name : 'multiple' ,
211
259
args : {
You can’t perform that action at this time.
0 commit comments