@@ -7,10 +7,13 @@ import { namedTypes as N } from "../gen/namedTypes";
77export default function ( fork : Fork ) {
88 fork . use ( esProposalsDef ) ;
99
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 ;
1417
1518 def ( "Noop" )
1619 . bases ( "Statement" )
@@ -78,58 +81,60 @@ export default function (fork: Fork) {
7881 . field ( "directives" , [ def ( "Directive" ) ] , defaults . emptyArray )
7982 . field ( "interpreter" , or ( def ( "InterpreterDirective" ) , null ) , defaults [ "null" ] ) ;
8083
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+
81109 // Split Literal
82110 def ( "StringLiteral" )
83111 . bases ( "Literal" )
84112 . build ( "value" )
85- . field ( "value" , String ) ;
113+ . field ( "value" , String )
114+ . field ( ...makeLiteralExtra < N . StringLiteral > ( String , val => JSON . stringify ( val ) ) ) ;
86115
87116 def ( "NumericLiteral" )
88117 . bases ( "Literal" )
89118 . build ( "value" )
90119 . field ( "value" , Number )
91120 . 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 ) ) ;
101122
102123 def ( "BigIntLiteral" )
103124 . bases ( "Literal" )
104125 . build ( "value" )
105126 // Only String really seems appropriate here, since BigInt values
106127 // often exceed the limits of JS numbers.
107128 . 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" ) ) ;
117130
118131 // https://github.com/tc39/proposal-decimal
119132 // https://github.com/babel/babel/pull/11640
120133 def ( "DecimalLiteral" )
121134 . bases ( "Literal" )
122135 . build ( "value" )
123136 . 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" ) ) ;
133138
134139 def ( "NullLiteral" )
135140 . bases ( "Literal" )
@@ -148,6 +153,21 @@ export default function (fork: Fork) {
148153 . field ( "flags" , String )
149154 . field ( "value" , RegExp , function ( this : N . RegExpLiteral ) {
150155 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+ } ;
151171 } ) ;
152172
153173 var ObjectExpressionProperty = or (
0 commit comments