@@ -9,24 +9,32 @@ const ParserWithTS = acorn.Parser.extend(tsPlugin());
9
9
* @param {FunctionExpression | FunctionDeclaration } node
10
10
* @param {Context<any, any> } context
11
11
*/
12
- function remove_this_param (
12
+ function remove_this_param_and_optional (
13
13
node : acorn . FunctionExpression | acorn . FunctionDeclaration ,
14
14
context : Context < any , any >
15
15
) {
16
- const param = node . params [ 0 ] as any ;
17
- if ( param ?. type === 'Identifier' && param . name === 'this' ) {
18
- if ( param . typeAnnotation ) {
19
- // the type annotation is blanked by another visitor, do it in two parts to prevent an overwrite error
20
- ts_blank_space ( context , { start : param . start , end : param . typeAnnotation . start } ) ;
21
- ts_blank_space ( context , {
22
- start : param . typeAnnotation . end ,
23
- end : node . params [ 1 ] ?. start || param . end
24
- } ) ;
25
- } else {
26
- ts_blank_space ( context , {
27
- start : param . start ,
28
- end : node . params [ 1 ] ?. start || param . end
29
- } ) ;
16
+ for ( const param of node . params as Array < acorn . Pattern & Record < string , any > > ) {
17
+ if ( param ?. type === 'Identifier' ) {
18
+ if ( param . name === 'this' ) {
19
+ if ( param . typeAnnotation ) {
20
+ // the type annotation is blanked by another visitor, do it in two parts to prevent an overwrite error
21
+ ts_blank_space ( context , { start : param . start , end : param . typeAnnotation . start } ) ;
22
+ ts_blank_space ( context , {
23
+ start : param . typeAnnotation . end ,
24
+ end : node . params [ 1 ] ?. start || param . end
25
+ } ) ;
26
+ } else {
27
+ ts_blank_space ( context , {
28
+ start : param . start ,
29
+ end : node . params [ 1 ] ?. start || param . end
30
+ } ) ;
31
+ }
32
+ } else if ( param . optional ) {
33
+ const question_start = context . state . ms . original . indexOf ( '?' , param . start ) ;
34
+ if ( question_start !== - 1 && question_start < param . end ) {
35
+ ts_blank_space ( context , { start : question_start , end : question_start + 1 } ) ;
36
+ }
37
+ }
30
38
}
31
39
}
32
40
return context . next ( ) ;
@@ -207,8 +215,8 @@ const visitors: Visitors<any, { ms: MagicString }> = {
207
215
ts_blank_space ( context , { start : node . start , end : node . expression . start } ) ;
208
216
context . visit ( node . expression ) ;
209
217
} ,
210
- FunctionExpression : remove_this_param ,
211
- FunctionDeclaration : remove_this_param ,
218
+ FunctionExpression : remove_this_param_and_optional ,
219
+ FunctionDeclaration : remove_this_param_and_optional ,
212
220
TSDeclareFunction ( node , context ) {
213
221
ts_blank_space ( context , node ) ;
214
222
return empty ;
@@ -245,6 +253,16 @@ const visitors: Visitors<any, { ms: MagicString }> = {
245
253
}
246
254
context . next ( ) ;
247
255
} ,
256
+ VariableDeclarator ( node , context ) {
257
+ if ( node . definite && node . id . type === 'Identifier' ) {
258
+ const definite_start = context . state . ms . original . indexOf (
259
+ '!' ,
260
+ node . id . start + node . id . name . length
261
+ ) ;
262
+ ts_blank_space ( context , { start : definite_start , end : definite_start + 1 } ) ;
263
+ }
264
+ context . next ( ) ;
265
+ } ,
248
266
TSModuleDeclaration ( node , context ) {
249
267
if ( ! node . body ) {
250
268
ts_blank_space ( context , node ) ;
0 commit comments