Skip to content

Commit ac92cd9

Browse files
committed
Refactor shared logic into withMethod and withProperty helpers
1 parent 4b539a4 commit ac92cd9

File tree

5 files changed

+60
-40
lines changed

5 files changed

+60
-40
lines changed

rules/no-ajax.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
'use strict'
22

3+
const utils = require('./utils.js')
4+
35
module.exports = function(context) {
46
return {
5-
CallExpression: function(node) {
6-
if (node.callee.type !== 'MemberExpression') return
7-
if (node.callee.object.name !== '$') return
8-
9-
const name = node.callee.property.name
10-
switch (name) {
11-
case 'ajax':
12-
case 'get':
13-
case 'getJSON':
14-
case 'getScript':
15-
case 'post':
16-
context.report({
17-
node: node,
18-
message: 'Prefer fetch to $.' + name
19-
})
7+
CallExpression: utils.withProperty(
8+
['ajax', 'get', 'getJSON', 'getScript', 'post'],
9+
function(node) {
10+
const name = node.callee.property.name
11+
context.report({
12+
node: node,
13+
message: 'Prefer fetch to $.' + name
14+
})
2015
}
21-
}
16+
)
2217
}
2318
}
2419

rules/no-css.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ const utils = require('./utils.js')
44

55
module.exports = function(context) {
66
return {
7-
CallExpression: function(node) {
8-
if (node.callee.type !== 'MemberExpression') return
9-
if (node.callee.property.name !== 'css') return
10-
11-
if (utils.isjQuery(node)) {
12-
context.report({
13-
node: node,
14-
message: 'Prefer getComputedStyle to $.css'
15-
})
16-
}
17-
}
7+
CallExpression: utils.withMethod('css', function(node) {
8+
context.report({
9+
node: node,
10+
message: 'Prefer getComputedStyle to $.css'
11+
})
12+
})
1813
}
1914
}
2015

rules/no-deferred.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict'
22

3-
module.exports = function(context) {
4-
function enforce(node) {
5-
if (node.callee.type !== 'MemberExpression') return
6-
if (node.callee.object.name !== '$') return
7-
if (node.callee.property.name !== 'Deferred') return
3+
const utils = require('./utils.js')
84

5+
module.exports = function(context) {
6+
const enforce = utils.withProperty('Deferred', function(node) {
97
context.report({
108
node: node,
119
message: 'Prefer Promise to $.Deferred'
1210
})
13-
}
11+
})
1412

1513
return {
1614
CallExpression: enforce,

rules/no-global-eval.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
'use strict'
22

3+
const utils = require('./utils.js')
4+
35
module.exports = function(context) {
46
return {
5-
CallExpression: function(node) {
6-
if (node.callee.type !== 'MemberExpression') return
7-
if (node.callee.object.name !== '$') return
8-
if (node.callee.property.name !== 'globalEval') return
9-
7+
CallExpression: utils.withProperty('globalEval', function(node) {
108
context.report({
119
node: node,
1210
message: '$.globalEval is not allowed'
1311
})
14-
}
12+
})
1513
}
1614
}
1715

rules/utils.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,41 @@ function isjQuery(node) {
3333
return id && id.name.startsWith('$')
3434
}
3535

36+
function withProperty(property, callback) {
37+
if (typeof callback !== 'function') {
38+
throw new TypeError('Must provide callback function')
39+
}
40+
41+
const properties = new Set([].concat(property))
42+
43+
return function(node) {
44+
if (node.callee.type !== 'MemberExpression') return
45+
if (node.callee.object.name !== '$') return
46+
if (!properties.has(node.callee.property.name)) return
47+
48+
callback(node)
49+
}
50+
}
51+
52+
function withMethod(method, callback) {
53+
if (typeof callback !== 'function') {
54+
throw new TypeError('Must provide callback function')
55+
}
56+
57+
const methods = new Set([].concat(method))
58+
59+
return function(node) {
60+
if (node.callee.type !== 'MemberExpression') return
61+
if (!methods.has(node.callee.property.name)) return
62+
if (isjQuery(node)) {
63+
callback(node)
64+
}
65+
}
66+
}
67+
3668
module.exports = {
3769
traverse: traverse,
38-
isjQuery: isjQuery
70+
isjQuery: isjQuery,
71+
withMethod: withMethod,
72+
withProperty: withProperty
3973
}

0 commit comments

Comments
 (0)