1
1
function convertToFunctionName ( name ) {
2
- if ( name . startsWith ( 'on' ) ) {
3
- return name . replace ( / ^ o n / , ' handle' ) ;
2
+ if ( name . startsWith ( "on" ) ) {
3
+ return name . replace ( / ^ o n / , " handle" ) ;
4
4
} else {
5
5
return `on${ name . charAt ( 0 ) . toUpperCase ( ) + name . slice ( 1 ) } ` ;
6
6
}
7
7
}
8
8
9
9
function invertKeyValues ( obj ) {
10
- return Object . fromEntries (
11
- Object . entries ( obj ) . map ( entry => entry . reverse ( ) )
12
- ) ;
10
+ return Object . fromEntries ( Object . entries ( obj ) . map ( ( entry ) => entry . reverse ( ) ) ) ;
13
11
}
14
12
15
13
new Synvert . Rewriter ( "react" , "prevent-default" , ( ) => {
@@ -58,7 +56,7 @@ new Synvert.Rewriter("react", "prevent-default", () => {
58
56
configure ( { parser : Synvert . Parser . TYPESCRIPT } ) ;
59
57
60
58
withinFiles ( Synvert . ALL_FILES , function ( ) {
61
- callHelper ( ' helpers/find-react-component' , ( ) => {
59
+ callHelper ( " helpers/find-react-component" , ( ) => {
62
60
// { functionName: 'functionNotDefined' } the function is not defined;
63
61
// { functionName: 'functionNoEventParameter' } the function is defined but not event parameter;
64
62
// { functionName: 'preventDefaultNotCalled' } the function is defined with event parameter but not call preventDefault();
@@ -67,7 +65,8 @@ new Synvert.Rewriter("react", "prevent-default", () => {
67
65
const onClickCode = { } ;
68
66
69
67
// find a elements whose href attribute is '#'
70
- findNode ( `
68
+ findNode (
69
+ `
71
70
.JsxElement
72
71
[openingElement=.JsxOpeningElement
73
72
[tagName=a]
@@ -81,39 +80,58 @@ new Synvert.Rewriter("react", "prevent-default", () => {
81
80
[closingElement=.JsxClosingElement[tagName=a]]` ,
82
81
( ) => {
83
82
findNode ( `.JsxAttribute[name=onClick][initializer=.JsxExpression[expression=.Identifier]]` , ( ) => {
84
- onClickPreventDefault [ this . currentNode . initializer . expression . escapedText ] = 'functionNotDefined' ;
85
- onClickCode [ this . currentNode . initializer . expression . escapedText ] = this . mutationAdapter . getSource ( this . currentNode . initializer . expression ) + '()' ;
83
+ onClickPreventDefault [ this . currentNode . initializer . expression . escapedText ] = "functionNotDefined" ;
84
+ onClickCode [ this . currentNode . initializer . expression . escapedText ] =
85
+ this . mutationAdapter . getSource ( this . currentNode . initializer . expression ) + "()" ;
86
86
} ) ;
87
- findNode ( `.JsxAttribute[name=onClick][initializer=.JsxExpression[expression=.ArrowFunction[body=.CallExpression]]]` , ( ) => {
88
- onClickPreventDefault [ this . currentNode . initializer . expression . body . expression . escapedText ] = 'functionNotDefined' ;
89
- onClickCode [ this . currentNode . initializer . expression . body . expression . escapedText ] = this . mutationAdapter . getSource ( this . currentNode . initializer . expression . body ) ;
90
- } ) ;
91
- findNode ( `.JsxAttribute[name=onClick][initializer=.JsxExpression[expression=.ArrowFunction[body=.Block[statements.length>0]]]]` , ( ) => {
92
- onClickPreventDefault [ this . currentNode . initializer . expression . body . statements [ 0 ] . expression . expression . escapedText ] = 'functionNotDefined' ;
93
- onClickCode [ this . currentNode . initializer . expression . body . statements [ 0 ] . expression . expression . escapedText ] = this . currentNode . initializer . expression . body . statements . map ( statement => this . mutationAdapter . getSource ( statement ) ) . join ( "\n" ) ;
94
- } ) ;
95
- }
87
+ findNode (
88
+ `.JsxAttribute[name=onClick][initializer=.JsxExpression[expression=.ArrowFunction[body=.CallExpression]]]` ,
89
+ ( ) => {
90
+ onClickPreventDefault [ this . currentNode . initializer . expression . body . expression . escapedText ] =
91
+ "functionNotDefined" ;
92
+ onClickCode [ this . currentNode . initializer . expression . body . expression . escapedText ] =
93
+ this . mutationAdapter . getSource ( this . currentNode . initializer . expression . body ) ;
94
+ } ,
95
+ ) ;
96
+ findNode (
97
+ `.JsxAttribute[name=onClick][initializer=.JsxExpression[expression=.ArrowFunction[body=.Block[statements.length>0]]]]` ,
98
+ ( ) => {
99
+ onClickPreventDefault [
100
+ this . currentNode . initializer . expression . body . statements [ 0 ] . expression . expression . escapedText
101
+ ] = "functionNotDefined" ;
102
+ onClickCode [
103
+ this . currentNode . initializer . expression . body . statements [ 0 ] . expression . expression . escapedText
104
+ ] = this . currentNode . initializer . expression . body . statements
105
+ . map ( ( statement ) => this . mutationAdapter . getSource ( statement ) )
106
+ . join ( "\n" ) ;
107
+ } ,
108
+ ) ;
109
+ } ,
96
110
) ;
97
111
98
112
for ( const functionName of Object . keys ( onClickPreventDefault ) ) {
99
113
findNode ( `.VariableDeclaration[name=${ functionName } ][initializer=.ArrowFunction]` , ( ) => {
100
114
if ( this . currentNode . initializer . parameters . length === 0 ) {
101
115
// no parameters
102
- onClickPreventDefault [ functionName ] = ' functionNoEventParameter' ;
116
+ onClickPreventDefault [ functionName ] = " functionNoEventParameter" ;
103
117
} else {
104
118
// has parameters
105
- onClickPreventDefault [ functionName ] = ' preventDefaultNotCalled' ;
119
+ onClickPreventDefault [ functionName ] = " preventDefaultNotCalled" ;
106
120
const eventVariable = this . currentNode . initializer . parameters [ 0 ] . name . escapedText ;
107
- findNode ( `.CallExpression[expression=.PropertyAccessExpression[name=preventDefault][expression=${ eventVariable } ]]` , ( ) => {
108
- onClickPreventDefault [ functionName ] = 'preventDefaultCalled' ;
109
- } ) ;
121
+ findNode (
122
+ `.CallExpression[expression=.PropertyAccessExpression[name=preventDefault][expression=${ eventVariable } ]]` ,
123
+ ( ) => {
124
+ onClickPreventDefault [ functionName ] = "preventDefaultCalled" ;
125
+ } ,
126
+ ) ;
110
127
}
111
128
} ) ;
112
129
}
113
130
114
131
for ( const [ functionName , reason ] of Object . entries ( onClickPreventDefault ) ) {
115
- if ( reason === 'functionNotDefined' ) {
116
- findNode ( `
132
+ if ( reason === "functionNotDefined" ) {
133
+ findNode (
134
+ `
117
135
.JsxElement
118
136
[openingElement=.JsxOpeningElement
119
137
[tagName=a]
@@ -126,7 +144,8 @@ new Synvert.Rewriter("react", "prevent-default", () => {
126
144
]
127
145
[closingElement=.JsxClosingElement[tagName=a]]` ,
128
146
( ) => {
129
- findNode ( `
147
+ findNode (
148
+ `
130
149
.JsxAttribute
131
150
[name=onClick]
132
151
[initializer=.JsxExpression
@@ -142,26 +161,33 @@ new Synvert.Rewriter("react", "prevent-default", () => {
142
161
[statements.0.expression.expression=${ functionName } ]
143
162
]
144
163
)]
145
- ]` , ( ) => {
146
- replace ( 'initializer' , { with : `{${ convertToFunctionName ( functionName ) } }` } ) ;
147
- } ) ;
148
- }
164
+ ]` ,
165
+ ( ) => {
166
+ replace ( "initializer" , { with : `{${ convertToFunctionName ( functionName ) } }` } ) ;
167
+ } ,
168
+ ) ;
169
+ } ,
149
170
) ;
150
171
151
172
findNode ( `.Block:first-child > .ReturnStatement` , ( ) => {
152
- insertBefore ( appendSemicolon ( `
173
+ insertBefore (
174
+ appendSemicolon (
175
+ `
153
176
const ${ convertToFunctionName ( functionName ) } = (event) => {
154
177
event.preventDefault();
155
178
156
179
${ appendSemicolon ( onClickCode [ functionName ] ) }
157
180
}
158
- ` . trim ( ) ) + "\n" , { fixIndent : true } ) ;
181
+ ` . trim ( ) ,
182
+ ) + "\n" ,
183
+ { fixIndent : true } ,
184
+ ) ;
159
185
} ) ;
160
186
}
161
187
162
- if ( reason === ' functionNoEventParameter' ) {
188
+ if ( reason === " functionNoEventParameter" ) {
163
189
findNode ( `.VariableDeclaration[name=${ functionName } ][initializer=.ArrowFunction]` , ( ) => {
164
- replace ( ' initializer.parameters' , { with : ' (event)' } ) ;
190
+ replace ( " initializer.parameters" , { with : " (event)" } ) ;
165
191
prepend ( "event.preventDefault();\n" ) ;
166
192
} ) ;
167
193
}
0 commit comments