File tree 3 files changed +150
-3
lines changed 3 files changed +150
-3
lines changed Original file line number Diff line number Diff line change @@ -234,8 +234,18 @@ module.exports = {
234
234
this . next ( ) ;
235
235
}
236
236
237
- const [ nullable , type ] =
238
- this . version >= 803 ? this . read_optional_type ( ) : [ false , null ] ;
237
+ let type = null ;
238
+ let nullable = false ;
239
+ if ( this . version >= 803 ) {
240
+ [ nullable , type ] = this . read_optional_type ( ) ;
241
+ }
242
+ let name = null ;
243
+
244
+ // read_optional_type can return a "type" even if the constant does not actually have a type.
245
+ if ( type && type . kind === "name" && this . token === "=" ) {
246
+ name = type . name ;
247
+ type = null ;
248
+ }
239
249
240
250
const result = this . node ( "classconstant" ) ;
241
251
const items = this . read_list (
@@ -256,9 +266,14 @@ module.exports = {
256
266
( this . version >= 700 && this . is ( "IDENTIFIER" ) )
257
267
) {
258
268
constName = this . node ( "identifier" ) ;
259
- const name = this . text ( ) ;
269
+ name = this . text ( ) ;
260
270
this . next ( ) ;
261
271
constName = constName ( name ) ;
272
+ }
273
+ // read_optional_type is not always returning just a type but
274
+ else if ( name ) {
275
+ constName = this . node ( "identifier" ) ( name ) ;
276
+ name = null ;
262
277
} else {
263
278
this . expect ( "IDENTIFIER" ) ;
264
279
}
Original file line number Diff line number Diff line change @@ -112,6 +112,69 @@ Program {
112
112
}
113
113
` ;
114
114
115
+ exports [` classconstant multiple 8.3 1` ] = `
116
+ Program {
117
+ " children" : [
118
+ Class {
119
+ " attrGroups" : [],
120
+ " body" : [
121
+ ClassConstant {
122
+ " attrGroups" : [],
123
+ " constants" : [
124
+ Constant {
125
+ " kind" : " constant" ,
126
+ " name" : Identifier {
127
+ " kind" : " identifier" ,
128
+ " name" : " NAME_1" ,
129
+ },
130
+ " value" : String {
131
+ " isDoubleQuote" : true ,
132
+ " kind" : " string" ,
133
+ " raw" : " " Hello world ! " " ,
134
+ " unicode" : false ,
135
+ " value" : " Hello world!" ,
136
+ },
137
+ },
138
+ Constant {
139
+ " kind" : " constant" ,
140
+ " name" : Identifier {
141
+ " kind" : " identifier" ,
142
+ " name" : " NAME_2" ,
143
+ },
144
+ " value" : String {
145
+ " isDoubleQuote" : true ,
146
+ " kind" : " string" ,
147
+ " raw" : " " Other hello world ! " " ,
148
+ " unicode" : false ,
149
+ " value" : " Other hello world!" ,
150
+ },
151
+ },
152
+ ],
153
+ " final" : false ,
154
+ " kind" : " classconstant" ,
155
+ " nullable" : false ,
156
+ " type" : null ,
157
+ " visibility" : " " ,
158
+ },
159
+ ],
160
+ " extends" : null ,
161
+ " implements" : null ,
162
+ " isAbstract" : false ,
163
+ " isAnonymous" : false ,
164
+ " isFinal" : false ,
165
+ " isReadonly" : false ,
166
+ " kind" : " class" ,
167
+ " name" : Identifier {
168
+ " kind" : " identifier" ,
169
+ " name" : " Foo" ,
170
+ },
171
+ },
172
+ ],
173
+ " errors" : [],
174
+ " kind" : " program" ,
175
+ }
176
+ ` ;
177
+
115
178
exports [` classconstant private 1` ] = `
116
179
Program {
117
180
" children" : [
@@ -308,6 +371,55 @@ Program {
308
371
}
309
372
` ;
310
373
374
+ exports [` classconstant simple using 8.3 1` ] = `
375
+ Program {
376
+ " children" : [
377
+ Class {
378
+ " attrGroups" : [],
379
+ " body" : [
380
+ ClassConstant {
381
+ " attrGroups" : [],
382
+ " constants" : [
383
+ Constant {
384
+ " kind" : " constant" ,
385
+ " name" : Identifier {
386
+ " kind" : " identifier" ,
387
+ " name" : " CONSTANT" ,
388
+ },
389
+ " value" : String {
390
+ " isDoubleQuote" : true ,
391
+ " kind" : " string" ,
392
+ " raw" : " " Hello world ! " " ,
393
+ " unicode" : false ,
394
+ " value" : " Hello world!" ,
395
+ },
396
+ },
397
+ ],
398
+ " final" : false ,
399
+ " kind" : " classconstant" ,
400
+ " nullable" : false ,
401
+ " type" : null ,
402
+ " visibility" : " " ,
403
+ },
404
+ ],
405
+ " extends" : null ,
406
+ " implements" : null ,
407
+ " isAbstract" : false ,
408
+ " isAnonymous" : false ,
409
+ " isFinal" : false ,
410
+ " isReadonly" : false ,
411
+ " kind" : " class" ,
412
+ " name" : Identifier {
413
+ " kind" : " identifier" ,
414
+ " name" : " Foo" ,
415
+ },
416
+ },
417
+ ],
418
+ " errors" : [],
419
+ " kind" : " program" ,
420
+ }
421
+ ` ;
422
+
311
423
exports [` classconstant type hinted (supported) 1` ] = `
312
424
Program {
313
425
" children" : [
Original file line number Diff line number Diff line change @@ -6,13 +6,33 @@ describe("classconstant", () => {
6
6
parser . parseEval ( 'class Foo { const CONSTANT = "Hello world!"; }' ) ,
7
7
) . toMatchSnapshot ( ) ;
8
8
} ) ;
9
+ it ( "simple using 8.3" , ( ) => {
10
+ expect (
11
+ parser . parseEval ( `class Foo { const CONSTANT = "Hello world!"; }` , {
12
+ parser : { version : 803 } ,
13
+ } ) ,
14
+ ) . toMatchSnapshot ( ) ;
15
+ } ) ;
16
+
9
17
it ( "multiple" , ( ) => {
10
18
expect (
11
19
parser . parseEval (
12
20
'class Foo { const CONSTANT = "Hello world!", OTHER_CONSTANT = "Other hello world!"; }' ,
13
21
) ,
14
22
) . toMatchSnapshot ( ) ;
15
23
} ) ;
24
+
25
+ it ( "multiple 8.3" , ( ) => {
26
+ expect (
27
+ parser . parseEval (
28
+ 'class Foo { const NAME_1 = "Hello world!", NAME_2 = "Other hello world!"; }' ,
29
+ {
30
+ parser : { version : 803 } ,
31
+ } ,
32
+ ) ,
33
+ ) . toMatchSnapshot ( ) ;
34
+ } ) ;
35
+
16
36
it ( "public" , ( ) => {
17
37
expect (
18
38
parser . parseEval ( 'class Foo { public const CONSTANT = "Hello world!"; }' ) ,
You can’t perform that action at this time.
0 commit comments