File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -36,5 +36,22 @@ export function main() {
3636 expect ( f ( ) ) . toBe ( 3 ) ;
3737 } ) ;
3838 } ) ;
39+
40+ describe ( "optional params" , function ( ) {
41+ it ( "should work" , function ( ) {
42+ function optional ( a = 1 , b = 2 ) { return a + b ; }
43+
44+ expect ( optional ( ) ) . toEqual ( 3 ) ;
45+ expect ( optional ( 10 ) ) . toEqual ( 12 ) ;
46+ expect ( optional ( 10 , 20 ) ) . toEqual ( 30 ) ;
47+ } ) ;
48+
49+ it ( "should support a mix of optional and mandatory params" , function ( ) {
50+ function optional ( a , b = 2 ) { return a + b ; }
51+
52+ expect ( optional ( 1 ) ) . toEqual ( 3 ) ;
53+ expect ( optional ( 10 ) ) . toEqual ( 12 ) ;
54+ } ) ;
55+ } ) ;
3956 } ) ;
4057}
Original file line number Diff line number Diff line change @@ -3,13 +3,16 @@ import {
33 AT ,
44 CLOSE_CURLY ,
55 CLOSE_PAREN ,
6+ CLOSE_SQUARE ,
67 COLON ,
78 COMMA ,
89 EQUAL ,
910 EQUAL_EQUAL_EQUAL ,
1011 IMPORT ,
1112 OPEN_CURLY ,
1213 OPEN_PAREN ,
14+ OBJECT_PATTERN ,
15+ OPEN_SQUARE ,
1316 SEMI_COLON ,
1417 STAR ,
1518 STATIC
@@ -118,6 +121,34 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
118121 this . visitAny ( tree . body ) ;
119122 }
120123
124+ visitFormalParameterList ( tree ) {
125+ var hasPosOptionalParams = false ;
126+ var first = true ;
127+ for ( var i = 0 ; i < tree . parameters . length ; i ++ ) {
128+ var parameter = tree . parameters [ i ] ;
129+ if ( first ) {
130+ first = false ;
131+ } else {
132+ this . write_ ( COMMA ) ;
133+ this . writeSpace_ ( ) ;
134+ }
135+
136+ if ( ! hasPosOptionalParams && this . _isOptionalPositionParam ( parameter . parameter ) ) {
137+ hasPosOptionalParams = true ;
138+ this . write_ ( OPEN_SQUARE ) ;
139+ }
140+ this . visitAny ( parameter ) ;
141+ }
142+
143+ if ( hasPosOptionalParams ) {
144+ this . write_ ( CLOSE_SQUARE ) ;
145+ }
146+ }
147+
148+ _isOptionalPositionParam ( parameter ) {
149+ return parameter . initializer && parameter . binding . type !== OBJECT_PATTERN ;
150+ }
151+
121152 /**
122153 * @param {PropertyMethodAssignment } tree
123154 */
You can’t perform that action at this time.
0 commit comments