Skip to content

Commit

Permalink
Merge pull request #1 from ilyavolodin/FixNestedBackbone
Browse files Browse the repository at this point in the history
Fixing edge case with nested models or collections
  • Loading branch information
ilyavolodin committed Sep 11, 2014
2 parents 5bbcbb4 + 72aaa3a commit dd2cf53
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
14 changes: 9 additions & 5 deletions backbone-helper.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
function isBackboneBase(node) {
"use strict";
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.property.name === "extend";
}

function isBackboneModel(node) {
"use strict";
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "Model";
return isBackboneBase(node) && node.callee.object.property.name === "Model";
}

function isBackboneView(node) {
"use strict";
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "View";
return isBackboneBase(node) && node.callee.object.property.name === "View";
}

function isBackboneCollection(node) {
"use strict";
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "Collection";
return isBackboneBase(node) && node.callee.object.property.name === "Collection";
}

function isBackboneAny(node) {
"use strict";
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" &&
(node.callee.object.property.name === "Collection" || node.callee.object.property.name === "View" || node.callee.object.property.name === "Model");
return isBackboneBase(node) && (node.callee.object.property.name === "Collection" || node.callee.object.property.name === "View" || node.callee.object.property.name === "Model");
}

exports.isBackboneAny = isBackboneAny;
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/collection-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var helper = require("../../backbone-helper.js");

module.exports = function(context) {

var backboneCollection = false;
var backboneCollection = [];
var foundModel = false;

//--------------------------------------------------------------------------
Expand All @@ -21,8 +21,8 @@ module.exports = function(context) {

return {
"CallExpression": function(node) {
backboneCollection = backboneCollection || helper.isBackboneCollection(node);
if (backboneCollection) {
backboneCollection.push(backboneCollection[backboneCollection.length - 1] || helper.isBackboneCollection(node));
if (backboneCollection[backboneCollection.length - 1]) {
foundModel = false;
}
},
Expand All @@ -31,11 +31,11 @@ module.exports = function(context) {
if (!foundModel) {
context.report(node, "All collections should have model declared");
}
backboneCollection = false;
backboneCollection.pop();
}
},
"Identifier": function(node) {
if (backboneCollection && node.name === "model") {
if (backboneCollection[backboneCollection.length - 1] && node.name === "model") {
if (helper.checkIfPropertyInBackboneCollection(node)) {
foundModel = true;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/model-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var helper = require("../../backbone-helper.js");

module.exports = function(context) {

var backboneModel = false;
var backboneModel = [];
var foundDefaults = false;

//--------------------------------------------------------------------------
Expand All @@ -21,8 +21,8 @@ module.exports = function(context) {

return {
"CallExpression": function(node) {
backboneModel = backboneModel || helper.isBackboneModel(node);
if (backboneModel) {
backboneModel.push(backboneModel[backboneModel.length - 1] || helper.isBackboneModel(node));
if (backboneModel[backboneModel.length - 1]) {
foundDefaults = false;
}
},
Expand All @@ -31,11 +31,11 @@ module.exports = function(context) {
if (!foundDefaults) {
context.report(node, "All models should have defaults declared");
}
backboneModel = false;
backboneModel.pop();
}
},
"Identifier": function(node) {
if (backboneModel && node.name === "defaults") {
if (backboneModel[backboneModel.length - 1] && node.name === "defaults") {
if (helper.checkIfPropertyInBackboneModel(node)) {
foundDefaults = true;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-native-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ var helper = require("../../backbone-helper.js");

module.exports = function(context) {

var backboneView = false;
var backboneView = [];

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
"CallExpression": function(node) {
backboneView = backboneView || helper.isBackboneView(node);
backboneView.push(backboneView[backboneView.length - 1] || helper.isBackboneView(node));
},
"CallExpression:exit": function(node) {
if (helper.isBackboneView(node)) {
backboneView = false;
backboneView.pop();
}
},
"Identifier": function(node) {
if (backboneView && node.name === "$") {
if (backboneView[backboneView.length - 1] && node.name === "$") {
var ancestors = context.getAncestors(node), parent = ancestors.pop();

if (parent.type === "CallExpression") {
Expand Down
10 changes: 8 additions & 2 deletions tests/lib/rules/collection-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ eslintTester.addRuleTest("lib/rules/collection-model", {
"Backbone.Collection.extend({ model: {}});",
"Backbone.Collection.extend({ initialize: function() { }, model: {} });",
"var a = Backbone.Collection.extend({ model: {} }); var b = Backbone.Collection.extend({ model: {} });",
"Backbone.Models.extend();",
"Backbone.Collection.extend({ constructor: function() { Backbone.Collection.apply(this, arguments); }, model: {} });",
"Backbone.Collection.extend({ initialize: function() { var a = Backbone.Collection.extend({ model: {} });}, model: {} });",
"Backbone.Model.extend();",
"var a=6 * 7;"
],

Expand All @@ -42,6 +44,10 @@ eslintTester.addRuleTest("lib/rules/collection-model", {
{
code: "Backbone.Collection.extend({ initialize: function() { var a = { model: {} }; } });",
errors: 1
}
},
{
code: "Backbone.Collection.extend({ initialize: function() { var a = Backbone.Collection.extend({});}, model: {} });",
errors: 1
}
]
});
6 changes: 6 additions & 0 deletions tests/lib/rules/model-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ eslintTester.addRuleTest("lib/rules/model-defaults", {
"Backbone.Model.extend({ defaults: {}});",
"Backbone.Model.extend({ initialize: function() { }, defaults: {} });",
"var a = Backbone.Model.extend({ defaults: {} }); var b = Backbone.Models.extend({ defaults: {} });",
"Backbone.Model.extend({ constructor: function() { Backbone.Model.apply(this, arguments); }, defaults: {} });",
"Backbone.Model.extend({ initialize: function() { var a = Backbone.Model.extend({ defaults: {} });}, defaults: {} });",
"Backbone.Models.extend();",
"var a=6 * 7;"
],
Expand All @@ -38,6 +40,10 @@ eslintTester.addRuleTest("lib/rules/model-defaults", {
{
code: "Backbone.Model.extend({ initialize: function() { var a = { defaults: {} }; } });",
errors: 1
},
{
code: "Backbone.Model.extend({ initialize: function() { var a = Backbone.Model.extend({});}, defaults: {} });",
errors: 1
}
]
});
4 changes: 4 additions & 0 deletions tests/lib/rules/no-native-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ eslintTester.addRuleTest("lib/rules/no-native-jquery", {
{
code: "var a = Backbone.View.extend({ render: function() { var a = $('.item').offset(); } }); var b = Backbone.View.extend({ render: function() { var a = this.$('.item').offset() } });",
errors: 1
},
{
code: "Backbone.View.extend({ initialize: function() { Backbone.View.apply(this, arguments); var a = $('.item').offset(); } });",
errors: 1
}
]
});

0 comments on commit dd2cf53

Please sign in to comment.