3
3
*/
4
4
'use strict' ;
5
5
6
- const { get } = require ( '../utils/get' ) ;
7
- const JQUERY_ALIASES = [ '$' , 'jQuery' ] ;
6
+ const { get, getParent } = require ( '../utils/get' ) ;
7
+ const { isJQueryCallee , isVariableAssigmentJquery } = require ( '../utils/jquery' ) ;
8
8
9
9
function getMessage ( blackListName ) {
10
10
return `The use of jQuery's ${ blackListName } method has been deprecated.` ;
11
11
}
12
12
13
- function isJQueryCaller ( node , name = null ) {
14
- const calleePropertyNameisJquery = get ( node , 'object.callee.property.name' ) === '$' ;
15
- const calleeNameIsJquery = get ( node , 'object.callee.name' ) === '$' ;
16
- const calleeNameIsLocalJquery = get ( node , 'object.callee.name' ) === name ;
17
- const ObjectNameisLocalJquery = get ( node , 'object.name' ) === name ;
18
-
19
- return calleePropertyNameisJquery
20
- || calleeNameIsJquery
21
- || calleeNameIsLocalJquery
22
- || ObjectNameisLocalJquery ;
23
- }
24
-
25
13
function getLocalImportName ( node , sourceName ) {
26
14
if ( get ( node , 'source.value' ) . includes ( sourceName ) ) {
27
15
return get ( node , 'specifiers' ) . map ( ( specifier ) => specifier . local . name ) ;
@@ -40,35 +28,35 @@ module.exports = {
40
28
} ,
41
29
create ( context ) {
42
30
const BLACKLIST = context . options [ 0 ] || [ ] ;
43
- let jQueryLocalName = null ;
31
+ let funcScopeJqueryVariables = [ ] ;
32
+ let moduleScopeJqueryVariables = [ ] ;
44
33
45
34
return {
46
35
47
36
ImportDeclaration ( node ) {
48
37
let localName = getLocalImportName ( node , 'jquery' ) ;
49
38
if ( localName . length > 0 ) {
50
- jQueryLocalName = localName [ 0 ] ;
39
+ moduleScopeJqueryVariables . push ( localName [ 0 ] ) ;
51
40
}
52
41
} ,
53
42
54
43
VariableDeclarator ( node ) {
55
- const varDecIDName = get ( node , 'id.name' ) ;
56
- // Get the name of what was assigned to the variable.
57
- const varAssignment = get ( node , 'init.callee.property.name' )
58
- || get ( node , 'init.property.name' )
59
- || get ( node , 'init.name' )
60
- || '' ;
61
- const varAssignmentIsJquery = JQUERY_ALIASES . includes ( varAssignment ) ;
44
+ if ( isVariableAssigmentJquery ( node ) ) {
45
+ const varName = get ( node , 'id.name' ) ;
46
+ const isFuncScope = ! ! getParent ( node , ( parent ) => parent . type === 'FunctionDeclaration' ) ; // Determine if variable is in function scope or global scope.
62
47
63
- if ( varAssignmentIsJquery ) {
64
- jQueryLocalName = varDecIDName ;
48
+ if ( isFuncScope ) {
49
+ funcScopeJqueryVariables . push ( varName ) ;
50
+ } else {
51
+ moduleScopeJqueryVariables . push ( varName ) ;
52
+ }
65
53
}
66
54
} ,
67
55
68
56
CallExpression ( node ) {
69
57
const calleeName = get ( node , 'callee.property.name' ) ;
70
58
const parentCalleeName = get ( node , 'callee.object.property.name' ) ;
71
- if ( BLACKLIST . includes ( calleeName ) && parentCalleeName === '$' ) {
59
+ if ( BLACKLIST . includes ( calleeName ) && ( parentCalleeName === '$' || isJQueryCallee ( node , moduleScopeJqueryVariables , funcScopeJqueryVariables ) ) ) {
72
60
context . report ( { node : node . callee . property , message : getMessage ( `${ parentCalleeName } .${ calleeName } ()` ) } ) ;
73
61
}
74
62
} ,
@@ -82,9 +70,14 @@ module.exports = {
82
70
let blackListName = BLACKLIST . includes ( propertyName ) ? propertyName : false ;
83
71
let isThisExpression = get ( node , 'object.type' ) . includes ( 'ThisExpression' ) ;
84
72
85
- if ( ! isThisExpression && blackListName && isJQueryCaller ( node , jQueryLocalName ) ) {
73
+ if ( ! isThisExpression && blackListName && isJQueryCallee ( node , moduleScopeJqueryVariables , funcScopeJqueryVariables ) ) {
86
74
context . report ( { node : node . property , message : getMessage ( blackListName ) } ) ;
87
75
}
76
+ } ,
77
+
78
+ FunctionDeclaration ( ) {
79
+ // Reset the function scope variables for each function declaration.
80
+ funcScopeJqueryVariables . length = 0 ;
88
81
}
89
82
} ;
90
83
}
0 commit comments