@@ -7,10 +7,13 @@ import { namedTypes as N } from "../gen/namedTypes";
7
7
export default function ( fork : Fork ) {
8
8
fork . use ( esProposalsDef ) ;
9
9
10
- var types = fork . use ( typesPlugin ) ;
11
- var defaults = fork . use ( sharedPlugin ) . defaults ;
12
- var def = types . Type . def ;
13
- var or = types . Type . or ;
10
+ const types = fork . use ( typesPlugin ) ;
11
+ const defaults = fork . use ( sharedPlugin ) . defaults ;
12
+ const def = types . Type . def ;
13
+ const or = types . Type . or ;
14
+ const {
15
+ undefined : isUndefined ,
16
+ } = types . builtInTypes ;
14
17
15
18
def ( "Noop" )
16
19
. bases ( "Statement" )
@@ -78,58 +81,60 @@ export default function (fork: Fork) {
78
81
. field ( "directives" , [ def ( "Directive" ) ] , defaults . emptyArray )
79
82
. field ( "interpreter" , or ( def ( "InterpreterDirective" ) , null ) , defaults [ "null" ] ) ;
80
83
84
+ function makeLiteralExtra <
85
+ // Allowing N.RegExpLiteral explicitly here is important because the
86
+ // node.value field of RegExpLiteral nodes can be undefined, which is not
87
+ // allowed for other Literal subtypes.
88
+ TNode extends Omit < N . Literal , "type" > | N . RegExpLiteral
89
+ > (
90
+ rawValueType : any = String ,
91
+ toRaw ?: ( value : any ) => string ,
92
+ ) : Parameters < import ( "../lib/types" ) . Def [ "field" ] > {
93
+ return [
94
+ "extra" ,
95
+ {
96
+ rawValue : rawValueType ,
97
+ raw : String ,
98
+ } ,
99
+ function getDefault ( this : TNode ) {
100
+ const value = types . getFieldValue ( this , "value" ) ;
101
+ return {
102
+ rawValue : value ,
103
+ raw : toRaw ? toRaw ( value ) : String ( value ) ,
104
+ } ;
105
+ } ,
106
+ ] ;
107
+ }
108
+
81
109
// Split Literal
82
110
def ( "StringLiteral" )
83
111
. bases ( "Literal" )
84
112
. build ( "value" )
85
- . field ( "value" , String ) ;
113
+ . field ( "value" , String )
114
+ . field ( ...makeLiteralExtra < N . StringLiteral > ( String , val => JSON . stringify ( val ) ) ) ;
86
115
87
116
def ( "NumericLiteral" )
88
117
. bases ( "Literal" )
89
118
. build ( "value" )
90
119
. field ( "value" , Number )
91
120
. field ( "raw" , or ( String , null ) , defaults [ "null" ] )
92
- . field ( "extra" , {
93
- rawValue : Number ,
94
- raw : String
95
- } , function getDefault ( this : N . NumericLiteral ) {
96
- return {
97
- rawValue : this . value ,
98
- raw : this . value + ""
99
- }
100
- } ) ;
121
+ . field ( ...makeLiteralExtra < N . NumericLiteral > ( Number ) ) ;
101
122
102
123
def ( "BigIntLiteral" )
103
124
. bases ( "Literal" )
104
125
. build ( "value" )
105
126
// Only String really seems appropriate here, since BigInt values
106
127
// often exceed the limits of JS numbers.
107
128
. field ( "value" , or ( String , Number ) )
108
- . field ( "extra" , {
109
- rawValue : String ,
110
- raw : String
111
- } , function getDefault ( this : N . BigIntLiteral ) {
112
- return {
113
- rawValue : String ( this . value ) ,
114
- raw : this . value + "n" ,
115
- } ;
116
- } ) ;
129
+ . field ( ...makeLiteralExtra < N . BigIntLiteral > ( String , val => val + "n" ) ) ;
117
130
118
131
// https://github.com/tc39/proposal-decimal
119
132
// https://github.com/babel/babel/pull/11640
120
133
def ( "DecimalLiteral" )
121
134
. bases ( "Literal" )
122
135
. build ( "value" )
123
136
. field ( "value" , String )
124
- . field ( "extra" , {
125
- rawValue : String ,
126
- raw : String
127
- } , function getDefault ( this : N . DecimalLiteral ) {
128
- return {
129
- rawValue : String ( this . value ) ,
130
- raw : this . value + "m" ,
131
- } ;
132
- } ) ;
137
+ . field ( ...makeLiteralExtra < N . DecimalLiteral > ( String , val => val + "m" ) ) ;
133
138
134
139
def ( "NullLiteral" )
135
140
. bases ( "Literal" )
@@ -148,6 +153,21 @@ export default function (fork: Fork) {
148
153
. field ( "flags" , String )
149
154
. field ( "value" , RegExp , function ( this : N . RegExpLiteral ) {
150
155
return new RegExp ( this . pattern , this . flags ) ;
156
+ } )
157
+ . field ( ...makeLiteralExtra < N . RegExpLiteral > (
158
+ or ( RegExp , isUndefined ) ,
159
+ exp => `/${ exp . pattern } /${ exp . flags || "" } ` ,
160
+ ) )
161
+ // I'm not sure why this field exists, but it's "specified" by estree:
162
+ // https://github.com/estree/estree/blob/master/es5.md#regexpliteral
163
+ . field ( "regex" , {
164
+ pattern : String ,
165
+ flags : String
166
+ } , function ( this : N . RegExpLiteral ) {
167
+ return {
168
+ pattern : this . pattern ,
169
+ flags : this . flags ,
170
+ } ;
151
171
} ) ;
152
172
153
173
var ObjectExpressionProperty = or (
0 commit comments