@@ -204,12 +204,6 @@ const sampleCodes = Object.entries<[(definedName: string, referredName: string)
204204 <: ${ referredName } :f()
205205 ` , NUM ( 1 ) ] ,
206206
207- prop : [ ( definedName , referredName ) =>
208- `
209- let x = { ${ definedName } : 1 }
210- x.${ referredName }
211- ` , NUM ( 1 ) ] ,
212-
213207 meta : [ ( definedName ) =>
214208 `
215209 ### ${ definedName } 1
@@ -303,6 +297,67 @@ describe.each(
303297 } ) ;
304298} ) ;
305299
300+ describe ( 'identifier validation on obj key' , ( ) => {
301+ const codes : [ string , ( definedName : string , referredName : string ) => string ] [ ] = [
302+ [ 'literal' , ( definedName : string , referredName : string ) => `
303+ let x = { ${ definedName } : 1 }
304+ <: x["${ referredName } "]
305+ ` ] ,
306+
307+ [ 'prop' , ( definedName : string , referredName : string ) => `
308+ let x = {}
309+ x.${ definedName } = 1
310+ <: x.${ referredName }
311+ ` ] ,
312+ ]
313+
314+ describe . each ( codes ) ( '%s' , ( _ , code ) => {
315+ test . concurrent . each (
316+ reservedWords
317+ ) ( 'reserved word %s must be allowed' , async ( word ) => {
318+ const res = await exe ( code ( word , word ) ) ;
319+ eq ( res , NUM ( 1 ) ) ;
320+ } ) ;
321+
322+ test . concurrent . each (
323+ identifierCases
324+ ) ( '%s is allowed: %s' , async ( word , allowed ) => {
325+ expect . hasAssertions ( ) ;
326+ if ( allowed ) {
327+ const res = await exe ( code ( word , word ) ) ;
328+ eq ( res , NUM ( 1 ) ) ;
329+ } else {
330+ expect ( ( ) => parser . parse ( code ( word , word ) ) ) . toThrow ( AiScriptSyntaxError ) ;
331+ await Promise . resolve ( ) ; // https://github.com/vitest-dev/vitest/issues/4750
332+ }
333+ } ) ;
334+ } ) ;
335+ } ) ;
336+
337+ describe ( 'reserved word validation on string obj key' , ( ) => {
338+ const codes : [ string , ( definedName : string , referredName : string ) => string ] [ ] = [
339+ [ 'literal' , ( definedName : string , referredName : string ) => `
340+ let x = { "${ definedName } ": 1 }
341+ <: x["${ referredName } "]
342+ ` ] ,
343+
344+ [ 'prop' , ( definedName : string , referredName : string ) => `
345+ let x = {}
346+ x."${ definedName } " = 1
347+ <: x."${ referredName } "
348+ ` ] ,
349+ ]
350+
351+ describe . each ( codes ) ( '%s' , ( _ , code ) => {
352+ test . concurrent . each (
353+ reservedWords
354+ ) ( 'reserved word %s must be allowed' , async ( word ) => {
355+ const res = await exe ( code ( word , word ) ) ;
356+ eq ( res , NUM ( 1 ) ) ;
357+ } ) ;
358+ } ) ;
359+ } ) ;
360+
306361test . concurrent ( 'Keyword cannot contain escape characters' , async ( ) => {
307362 await expect ( async ( ) => await exe ( `
308363 \\u0069\\u0066 true {
0 commit comments