@@ -211,14 +211,15 @@ function stringArrayV3(ast) {
211
211
let ob_func_str = [ ]
212
212
let ob_dec_name = [ ]
213
213
let ob_string_func_name = null
214
- // **Prefer** Find the string list func ("func1") by matching its feature :
214
+ // Normally, the string array func ("func1") follows the template below :
215
215
// function aaa() {
216
216
// const bbb = [...]
217
217
// aaa = function () {
218
218
// return bbb;
219
219
// };
220
220
// return aaa();
221
221
// }
222
+ // In some cases (lint), the assignment is merged into the ReturnStatement
222
223
// After finding the possible func1, this method will check all the binding
223
224
// references and put the child encode function into list.
224
225
function find_string_array_function ( path ) {
@@ -228,29 +229,33 @@ function stringArrayV3(ast) {
228
229
if (
229
230
! t . isIdentifier ( path . node . id ) ||
230
231
path . node . params . length ||
231
- ! t . isBlockStatement ( path . node . body ) ||
232
- path . node . body . body . length != 3
232
+ ! t . isBlockStatement ( path . node . body )
233
233
) {
234
234
return
235
235
}
236
+ const body = path . node . body . body
237
+ if ( body . length < 2 || body . length > 3 ) {
238
+ return
239
+ }
236
240
const name_func = path . node . id . name
237
241
let string_var = - 1
238
- const body = path . node . body . body
239
242
try {
240
243
if (
241
244
body [ 0 ] . declarations . length != 1 ||
242
245
! ( string_var = body [ 0 ] . declarations [ 0 ] . id . name ) ||
243
- ! t . isArrayExpression ( body [ 0 ] . declarations [ 0 ] . init ) ||
244
- name_func != body [ 1 ] . expression . left . name ||
245
- body [ 1 ] . expression . right . params . length ||
246
- string_var != body [ 1 ] . expression . right . body . body [ 0 ] . argument . name ||
247
- body [ 2 ] . argument . arguments . length ||
248
- name_func != body [ 2 ] . argument . callee . name
246
+ ! t . isArrayExpression ( body [ 0 ] . declarations [ 0 ] . init )
249
247
) {
250
248
return
251
249
}
250
+ const nodes = [ ...body ]
251
+ nodes . shift ( )
252
+ const code = generator ( t . BlockStatement ( nodes ) ) . code
253
+ const fp = `${ name_func } =function(){return${ string_var } }${ name_func } ()`
254
+ if ( ! checkPattern ( code , fp ) ) {
255
+ return
256
+ }
252
257
} catch {
253
- //
258
+ return
254
259
}
255
260
const binding = path . scope . getBinding ( name_func )
256
261
if ( ! binding . referencePaths ) {
0 commit comments