@@ -9,24 +9,32 @@ const ParserWithTS = acorn.Parser.extend(tsPlugin());
99 * @param {FunctionExpression | FunctionDeclaration } node
1010 * @param {Context<any, any> } context
1111 */
12- function remove_this_param (
12+ function remove_this_param_and_optional (
1313 node : acorn . FunctionExpression | acorn . FunctionDeclaration ,
1414 context : Context < any , any >
1515) {
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+ }
3038 }
3139 }
3240 return context . next ( ) ;
@@ -207,8 +215,8 @@ const visitors: Visitors<any, { ms: MagicString }> = {
207215 ts_blank_space ( context , { start : node . start , end : node . expression . start } ) ;
208216 context . visit ( node . expression ) ;
209217 } ,
210- FunctionExpression : remove_this_param ,
211- FunctionDeclaration : remove_this_param ,
218+ FunctionExpression : remove_this_param_and_optional ,
219+ FunctionDeclaration : remove_this_param_and_optional ,
212220 TSDeclareFunction ( node , context ) {
213221 ts_blank_space ( context , node ) ;
214222 return empty ;
@@ -245,6 +253,16 @@ const visitors: Visitors<any, { ms: MagicString }> = {
245253 }
246254 context . next ( ) ;
247255 } ,
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+ } ,
248266 TSModuleDeclaration ( node , context ) {
249267 if ( ! node . body ) {
250268 ts_blank_space ( context , node ) ;
0 commit comments