-
Notifications
You must be signed in to change notification settings - Fork 8
/
no-class-state.js
43 lines (39 loc) · 1.21 KB
/
no-class-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const utils = require( '../utils.js' );
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Disallows ' + utils.jQueryCollectionLink( 'hasClass' ) +
' and ' + utils.jQueryCollectionLink( 'toggleClass' ) +
' to discourage querying the DOM for state information. ' +
utils.jQueryCollectionLink( 'toggleClass' ) + ' may be used with a boolean argument as then it behaves like ' +
utils.jQueryCollectionLink( 'addClass' ) + '/' + utils.jQueryCollectionLink( 'removeClass' ) + '.'
},
schema: []
},
create: ( context ) => ( {
'CallExpression:exit': ( node ) => {
if ( !(
node.callee.type === 'MemberExpression' && (
node.callee.property.name === 'hasClass' ||
// toggleClass with one argument will check if the
// class is already in the DOM before deciding what to do,
// so it is equivalent to using hasClass.
(
node.callee.property.name === 'toggleClass' &&
node.arguments.length === 1
)
)
) ) {
return;
}
if ( utils.isjQuery( context, node.callee ) ) {
context.report( {
node,
message: 'Where possible, maintain application state in JS to avoid slower DOM queries'
} );
}
}
} )
};