1
- import { AST , Rule , Scope , SourceCode } from 'eslint' ;
1
+ import { AST , Rule , Scope } from 'eslint' ;
2
2
import * as ESTree from 'estree' ;
3
3
import { getStringValue , isFunction , isPageMethod } from '../utils/ast' ;
4
- import { getSourceCode , truthy } from '../utils/misc' ;
4
+ import { truthy } from '../utils/misc' ;
5
5
6
6
/** Collect all variable references in the parent scopes recursively. */
7
7
function collectVariables ( scope : Scope . Scope | null ) : string [ ] {
@@ -48,19 +48,22 @@ function addArgument(
48
48
}
49
49
50
50
/** Get the opening parenthesis of the function. */
51
- function getParen ( sourceCode : SourceCode , node : ESTree . Node ) : AST . Token | null {
52
- let token : AST . Token | null = sourceCode . getFirstToken ( node ) ;
51
+ function getParen (
52
+ context : Rule . RuleContext ,
53
+ node : ESTree . Node ,
54
+ ) : AST . Token | null {
55
+ let token : AST . Token | null = context . sourceCode . getFirstToken ( node ) ;
53
56
54
57
while ( token && token . value !== '(' ) {
55
- token = sourceCode . getTokenAfter ( token ) ;
58
+ token = context . sourceCode . getTokenAfter ( token ) ;
56
59
}
57
60
58
61
return token ;
59
62
}
60
63
61
64
/** Add a parameter to the function. */
62
65
function addParam (
63
- sourceCode : SourceCode ,
66
+ context : Rule . RuleContext ,
64
67
fixer : Rule . RuleFixer ,
65
68
node : ESTree . ArrowFunctionExpression | ESTree . FunctionExpression ,
66
69
refs : string ,
@@ -77,7 +80,7 @@ function addParam(
77
80
}
78
81
79
82
// If the function has no params, add the reference after the opening parenthesis
80
- const token = getParen ( sourceCode , node ) ;
83
+ const token = getParen ( context , node ) ;
81
84
return token ? fixer . insertTextAfter ( token , `[${ refs } ]` ) : null ;
82
85
}
83
86
@@ -90,8 +93,7 @@ export default {
90
93
const [ fn ] = node . arguments ;
91
94
if ( ! fn || ! isFunction ( fn ) ) return ;
92
95
93
- const sourceCode = getSourceCode ( context ) ;
94
- const { through, upper } = sourceCode . getScope ( fn . body ) ;
96
+ const { through, upper } = context . sourceCode . getScope ( fn . body ) ;
95
97
const allRefs = new Set ( collectVariables ( upper ) ) ;
96
98
97
99
// This logic is confusing at first. If we find a variable within the
@@ -120,7 +122,7 @@ export default {
120
122
121
123
return [
122
124
addArgument ( fixer , node , refs ) ,
123
- addParam ( sourceCode , fixer , fn , refs ) ,
125
+ addParam ( context , fixer , fn , refs ) ,
124
126
] . filter ( truthy ) ;
125
127
} ,
126
128
} ) ;
0 commit comments