Skip to content

Commit

Permalink
Add rules for no-context/selector-prop and include in deprecated-1.10…
Browse files Browse the repository at this point in the history
…/1.7 (#48)

Part of #32
  • Loading branch information
edg2s authored and jdforrester committed Jan 22, 2019
1 parent 87c753c commit 66263ad
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Alternatively, you can pick out rules individually:
"jquery/no-class": 2,
"jquery/no-clone": 2,
"jquery/no-closest": 2,
"jquery/no-context-prop": 2,
"jquery/no-css": 2,
"jquery/no-data": 2,
"jquery/no-deferred": 2,
Expand Down Expand Up @@ -92,6 +93,7 @@ Alternatively, you can pick out rules individually:
"jquery/no-proxy": 2,
"jquery/no-ready": 2,
"jquery/no-global-selector": 2,
"jquery/no-selector-prop": 2,
"jquery/no-serialize": 2,
"jquery/no-show": 2,
"jquery/no-size": 2,
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'no-class': require('./rules/no-class'),
'no-clone': require('./rules/no-clone'),
'no-closest': require('./rules/no-closest'),
'no-context-prop': require('./rules/no-context-prop'),
'no-css': require('./rules/no-css'),
'no-data': require('./rules/no-data'),
'no-deferred': require('./rules/no-deferred'),
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = {
'no-prop': require('./rules/no-prop'),
'no-proxy': require('./rules/no-proxy'),
'no-ready': require('./rules/no-ready'),
'no-selector-prop': require('./rules/no-selector-prop'),
'no-serialize': require('./rules/no-serialize'),
'no-global-selector': require('./rules/no-global-selector'),
'no-show': require('./rules/no-show'),
Expand Down Expand Up @@ -101,7 +103,7 @@ module.exports = {
'deprecated-1.10': {
extends: 'plugin:jquery/deprecated-1.9',
rules: {
// FIXME: `$(...).context`
'jquery/no-context-prop': 2
}
},
// Use this profile if you're writing code targetting jQuery 1.9.x environments.
Expand Down Expand Up @@ -130,9 +132,9 @@ module.exports = {
rules: {
// FIXME: `deferred.isRejected()/isResolved()`
'jquery/no-die': 2,
'jquery/no-live': 2
'jquery/no-live': 2,
// FIXME: `$.sub()`
// FIXME: `$(...).selector`
'jquery/no-selector-prop': 2
}
},
// Use this profile if you're writing code targetting jQuery 1.3.x environments.
Expand Down
26 changes: 26 additions & 0 deletions rules/no-context-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

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'
})
}
}
}
}
}
26 changes: 26 additions & 0 deletions rules/no-selector-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

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

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

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

if (utils.isjQuery(node)) {
context.report({
node: node,
message: '$.selector is not allowed'
})
}
}
}
}
}
47 changes: 47 additions & 0 deletions tests/no-context-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const rule = require('../rules/no-context-prop')
const RuleTester = require('eslint').RuleTester

const error = '$.context is not allowed'

const ruleTester = new RuleTester()
ruleTester.run('no-context-prop', rule, {
valid: [
'context',
'div.context',
'$div.prop.context',
'$div.context()',
'$div.context(arg)'
],
invalid: [
{
code: '$("div").context',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.context',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.context.prop',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.context.method()',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$("div").first().context',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: 'f($div.context)',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$("div").append($("input").context)',
errors: [{message: error, type: 'MemberExpression'}]
}
]
})
47 changes: 47 additions & 0 deletions tests/no-selector-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const rule = require('../rules/no-selector-prop')
const RuleTester = require('eslint').RuleTester

const error = '$.selector is not allowed'

const ruleTester = new RuleTester()
ruleTester.run('no-selector-prop', rule, {
valid: [
'selector',
'div.selector',
'$div.prop.selector',
'$div.selector()',
'$div.selector(arg)'
],
invalid: [
{
code: '$("div").selector',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.selector',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.selector.prop',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$div.selector.method()',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$("div").first().selector',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: 'f($div.selector)',
errors: [{message: error, type: 'MemberExpression'}]
},
{
code: '$("div").append($("input").selector)',
errors: [{message: error, type: 'MemberExpression'}]
}
]
})

0 comments on commit 66263ad

Please sign in to comment.