Skip to content

Commit

Permalink
De-deduplicate rule generation (#59)
Browse files Browse the repository at this point in the history
Creates four generators:
* createCollectionMethodRule: $(...).method(...)
* createCollectionPropertyRule: $(...).property
* createUtilMethodRule: $.method(...)
* createUtilPropertyRule: $.property

Not touched by this change are rules with more complex criteria,
and rules where the distinction hasn't been made between util
methods and collection methods e.g. $.text() and $(...).text().

Doesn't touch the tests to ensure no functionality is changed.

Part of #55
  • Loading branch information
edg2s authored and jdforrester committed Jan 23, 2019
1 parent bd4e0f4 commit c6a2ce2
Show file tree
Hide file tree
Showing 64 changed files with 338 additions and 1,315 deletions.
32 changes: 5 additions & 27 deletions rules/no-ajax.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
'use strict'

module.exports = {
meta: {
docs: {},
schema: []
},
const utils = require('./utils.js')

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.object.name !== '$') return

const name = node.callee.property.name
switch (name) {
case 'ajax':
case 'get':
case 'getJSON':
case 'getScript':
case 'post':
context.report({
node: node,
message: 'Prefer fetch to $.' + name
})
}
}
}
}
}
module.exports = utils.createUtilMethodRule(
['ajax', 'get', 'getJSON', 'getScript', 'post'],
node => `Prefer fetch to $.${node.callee.property.name}`
)
26 changes: 4 additions & 22 deletions rules/no-and-self.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'andSelf') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer $.addBack to $.andSelf'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'andSelf',
'Prefer $.addBack to $.andSelf'
)
26 changes: 4 additions & 22 deletions rules/no-animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'animate') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer CSS transitions to $.animate'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'animate',
'Prefer CSS transitions to $.animate'
)
26 changes: 4 additions & 22 deletions rules/no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'bind') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer $.on/addEventListener to bind'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'bind',
'Prefer $.on/addEventListener to bind'
)
21 changes: 2 additions & 19 deletions rules/no-box-model.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
'use strict'

module.exports = {
meta: {
docs: {},
schema: []
},
const utils = require('./utils.js')

create: function(context) {
return {
MemberExpression: function(node) {
if (node.object.name !== '$') return
if (node.property.name !== 'boxModel') return

context.report({
node: node,
message: '$.boxModel is not allowed'
})
}
}
}
}
module.exports = utils.createUtilPropertyRule('boxModel')
21 changes: 2 additions & 19 deletions rules/no-browser.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
'use strict'

module.exports = {
meta: {
docs: {},
schema: []
},
const utils = require('./utils.js')

create: function(context) {
return {
MemberExpression: function(node) {
if (node.object.name !== '$') return
if (node.property.name !== 'browser') return

context.report({
node: node,
message: '$.browser is not allowed'
})
}
}
}
}
module.exports = utils.createUtilPropertyRule('browser')
28 changes: 4 additions & 24 deletions rules/no-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
const forbidden = ['addClass', 'hasClass', 'removeClass', 'toggleClass']

return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (forbidden.indexOf(node.callee.property.name) === -1) return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer classList to ' + node.callee.property.name
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
['addClass', 'hasClass', 'removeClass', 'toggleClass'],
node => `Prefer classList to ${node.callee.property.name}`
)
26 changes: 4 additions & 22 deletions rules/no-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'clone') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer cloneNode to clone'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'clone',
'Prefer cloneNode to clone'
)
26 changes: 4 additions & 22 deletions rules/no-closest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'closest') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer closest to closest'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'closest',
'Prefer Element.closest to $.closest'
)
23 changes: 1 addition & 22 deletions rules/no-context-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,4 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
MemberExpression: function(node) {
if (node.property.name !== 'context') return
if (node.parent.callee === node) return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: '$.context is not allowed'
})
}
}
}
}
}
module.exports = utils.createCollectionPropertyRule('context')
26 changes: 4 additions & 22 deletions rules/no-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'css') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer getComputedStyle to css'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'css',
'Prefer getComputedStyle to css'
)
26 changes: 4 additions & 22 deletions rules/no-delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'delegate') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer $.on/addEventListener to delegate'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'delegate',
'Prefer $.on/addEventListener to delegate'
)
26 changes: 4 additions & 22 deletions rules/no-die.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@

const utils = require('./utils.js')

module.exports = {
meta: {
docs: {},
schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== 'MemberExpression') return
if (node.callee.property.name !== 'die') return

if (utils.isjQuery(node)) {
context.report({
node: node,
message: 'Prefer $.off/removeEventListener to die'
})
}
}
}
}
}
module.exports = utils.createCollectionMethodRule(
'die',
'Prefer $.off/removeEventListener to die'
)
Loading

0 comments on commit c6a2ce2

Please sign in to comment.