Skip to content

Commit

Permalink
Merge pull request #34 from ilyavolodin/no-native-jquery
Browse files Browse the repository at this point in the history
Update no-native-jquery with options (fixes #33)
  • Loading branch information
ilyavolodin committed Feb 22, 2015
2 parents ef79415 + e13bb62 commit 5b4fe87
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/
build/
npm-debug.log
.DS_Store
.idea/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Defaults are currently set to the following:
"no-constructor": 1,
"no-el-assign": 2,
"no-model-attributes": 2,
"no-native-jquery": 0,
"no-native-jquery": [1, "selector"],
"no-silent": 1,
"no-view-collection-models": 2,
"no-view-model-attributes": 2,
Expand Down Expand Up @@ -118,4 +118,4 @@ If you are using custom models/view/collection bases you also have to specify ea
* [no-silent](docs/rules/no-silent.md)
* [no-view-collection-models](docs/rules/no-view-collection-models.md)
* [no-view-model-attributes](docs/rules/no-view-model-attributes.md)
* [render-return](docs/rules/render-return.md)
* [render-return](docs/rules/render-return.md)
7 changes: 7 additions & 0 deletions docs/rules/no-native-jquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ Backbone.View.extend({

```

## Options

This rule supports two modes `all` or `selector`. `all` is the default mode, when on, it will check for any uses of jQuery
in the view. `selector` will only warn about jQuery selectors that are using literals, as in, if you are passing a string
into jQuery's `$` function, it will warn you, but if you are passing an object, it will skip over it. `selector` mode is
useful if you are working with event handlers and need to wrap event's target into jQuery object.

## When Not To Use It

When writing a small application sometimes it's easier to operate on external elements, instead of creating a new view.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
"no-constructor": 1,
"no-el-assign": 2,
"no-model-attributes": 2,
"no-native-jquery": 0,
"no-native-jquery": [1, "selector"],
"no-silent": 1,
"no-view-collection-models": 2,
"no-view-model-attributes": 2,
Expand Down
10 changes: 10 additions & 0 deletions lib/rules/no-native-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ module.exports = function(context) {
var ancestors = context.getAncestors(node), parent = ancestors.pop();

if (parent.type === "CallExpression") {
if (context.options && context.options.length > 0) {
if (context.options[0] === "selector" && parent.arguments && parent.arguments.length > 0) {
if (parent.arguments[0].type === "Literal") {
context.report(node, "Use this.$ instead of $ in views");
return;
} else {
return;
}
}
}
context.report(node, "Use this.$ instead of $ in views");
}
}
Expand Down
11 changes: 10 additions & 1 deletion tests/lib/rules/no-native-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ eslintTester.addRuleTest("lib/rules/no-native-jquery", {
"Backbone.View.extend({ test: function() { return $.isArray(a); } });",
"Backbone.Model.extend({ initialize: function() { var a = $('.item').offset(); } });",
"var a = 6 * 7;",
"var a = $('.item').offset();"
"var a = $('.item').offset();",
{ code: "Backbone.View.extend({ render: function(element) { $(element).show(); } });", args: [1, "selector"] }
],

invalid: [
Expand All @@ -39,6 +40,14 @@ eslintTester.addRuleTest("lib/rules/no-native-jquery", {
{
code: "Backbone.View.extend({ initialize: function() { Backbone.View.apply(this, arguments); var a = $('.item').offset(); } });",
errors: [ { message: "Use this.$ instead of $ in views" } ]
},
{
code: "Backbone.View.extend({ render: function(element) { $(element).show(); } });", args: [1, "all"],
errors: [ { message: "Use this.$ instead of $ in views" } ]
},
{
code: "Backbone.View.extend({ render: function() { $('.item').show(); } });", args: [1, "selector"],
errors: [ { message: "Use this.$ instead of $ in views" } ]
}
]
});

0 comments on commit 5b4fe87

Please sign in to comment.