-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5681e60
commit 5bbcbb4
Showing
11 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
exports.isBackboneModel = function(node) { | ||
return node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "Model"; | ||
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"; | ||
} | ||
|
||
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"; | ||
} | ||
|
||
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"; | ||
} | ||
|
||
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"); | ||
} | ||
|
||
exports.isBackboneAny = isBackboneAny; | ||
exports.isBackboneModel = isBackboneModel; | ||
exports.isBackboneView = isBackboneView; | ||
exports.isBackboneCollection = isBackboneCollection; | ||
|
||
exports.checkIfPropertyInBackbone = function(node) { | ||
var parent = node.parent, grandparent = parent.parent, greatgrandparent = grandparent.parent; | ||
return isBackboneAny(greatgrandparent); | ||
}; | ||
|
||
exports.isBackboneView = function(node) { | ||
return node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "View"; | ||
exports.checkIfPropertyInBackboneModel = function(node) { | ||
var parent = node.parent, grandparent = parent.parent, greatgrandparent = grandparent.parent; | ||
return isBackboneModel(greatgrandparent); | ||
}; | ||
|
||
exports.isBackboneCollection = function(node) { | ||
return node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression" && node.callee.object.object.name === "Backbone" && node.callee.object.property.name === "Collection"; | ||
}; | ||
exports.checkIfPropertyInBackboneCollection = function(node) { | ||
var parent = node.parent, grandparent = parent.parent, greatgrandparent = grandparent.parent; | ||
return isBackboneCollection(greatgrandparent); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Prevent overloading of constructor (no-constructor) | ||
|
||
Backbone Models, Views and Collections use `constructor` method to instantiate themselves. Overloading it might lead to unexpected results. When overloading `constructor`, you should always call base. In most cases it's easier to overload `initialize` method, which will be executed when Model, View or Collection is created. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
Backbone.Model.extend({ | ||
constructor: function() { | ||
... | ||
} | ||
}); | ||
|
||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
|
||
Backbone.Model.extend({ | ||
initialize: function() { | ||
... | ||
} | ||
}); | ||
|
||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are creating your own wrapper around Model, View or Collection, it's probably better to overload `constructor`. So this rule should be disabled for those files. | ||
|
||
## Further Reading | ||
|
||
[BackboneJS Documentation](http://backbonejs.org/#Model-constructor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @fileoverview Prevent overloading of constructor | ||
* @author Ilya Volodin | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
var helper = require("../../backbone-helper.js"); | ||
|
||
module.exports = function(context) { | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
"Identifier": function(node) { | ||
if (node.name === "constructor") { | ||
if (helper.checkIfPropertyInBackbone(node)) { | ||
context.report(node, "Overload initialize instead of constructor"); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @fileoverview Prevent overloading of constructor | ||
* @author Ilya Volodin | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslint = require("eslint").linter, | ||
ESLintTester = require("eslint-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest("lib/rules/no-constructor", { | ||
|
||
valid: [ | ||
"Backbone.Model.extend({ initialize: function() { } });", | ||
"Backbone.View.extend({ initialize: function() { } });", | ||
"Backbone.Collection.extend({ initialize: function() { } });", | ||
"Backbone.Model.extend({ initialize: function() { var a = { constructor: function() { } }; } });" | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "Backbone.Model.extend({ constructor: function() {} });", | ||
errors: 1 | ||
}, | ||
{ | ||
code: "Backbone.View.extend({ constructor: function() {} });", | ||
errors: 1 | ||
}, | ||
{ | ||
code: "Backbone.Collection.extend({ constructor: function() {} });", | ||
errors: 1 | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters