1
1
import { AST , Rule , Scope , SourceCode } from 'eslint' ;
2
2
import * as ESTree from 'estree' ;
3
- import { isFunction , isPageMethod } from '../utils/ast' ;
3
+ import { getStringValue , isFunction , isPageMethod } from '../utils/ast' ;
4
4
import { getSourceCode , truthy } from '../utils/misc' ;
5
5
6
6
/** Collect all variable references in the parent scopes recursively. */
@@ -30,15 +30,21 @@ function addArgument(
30
30
31
31
// If there are at least two arguments, we can add the references after the
32
32
// last element of the second argument, which should be an array.
33
- const arr = node . arguments . at ( - 1 ) ;
34
- if ( ! arr || arr . type !== 'ArrayExpression' ) return ;
33
+ const arg = node . arguments . at ( - 1 ) ;
34
+ if ( ! arg ) return ;
35
+
36
+ // If the second argument is not an array, we have to replace it with an array
37
+ // containing the existing argument and the new references.
38
+ if ( arg . type !== 'ArrayExpression' ) {
39
+ return fixer . replaceText ( arg , `[${ getStringValue ( arg ) } , ${ refs } ]` ) ;
40
+ }
35
41
36
42
// It's possible the array is provided but empty, in which case we can just
37
43
// replace it with the references.
38
- const lastItem = arr . elements . at ( - 1 ) ;
44
+ const lastItem = arg . elements . at ( - 1 ) ;
39
45
return lastItem
40
46
? fixer . insertTextAfter ( lastItem , `, ${ refs } ` )
41
- : fixer . replaceText ( arr , `[${ refs } ]` ) ;
47
+ : fixer . replaceText ( arg , `[${ refs } ]` ) ;
42
48
}
43
49
44
50
/** Get the opening parenthesis of the function. */
@@ -62,7 +68,12 @@ function addParam(
62
68
// If the function has params, add the reference after the last one
63
69
const lastParam = node . params . at ( - 1 ) ;
64
70
if ( lastParam ) {
65
- return fixer . insertTextAfter ( lastParam , `, ${ refs } ` ) ;
71
+ // If the last param is an array, add the reference after the last element
72
+ // otherwise, replace the last param with an array containing the existing
73
+ // param and the references.
74
+ return lastParam . type === 'ArrayPattern'
75
+ ? fixer . insertTextAfter ( lastParam . elements . at ( - 1 ) ! , `, ${ refs } ` )
76
+ : fixer . replaceText ( lastParam , `[${ getStringValue ( lastParam ) } , ${ refs } ]` ) ;
66
77
}
67
78
68
79
// If the function has no params, add the reference after the opening parenthesis
0 commit comments