Skip to content

Commit

Permalink
Add rule no-class-prototype-attributes
Browse files Browse the repository at this point in the history
[closes #2]
  • Loading branch information
mitchlloyd committed Mar 2, 2016
1 parent def535e commit 67847e2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ Then configure the rules you want to use under the rules section.
}.property('firstName', 'lastName')
})
```

* `no-class-prototype-attributes` - For certain Ember objects, some properties
must be set on the prototype when extending a factory (`classNames`,
`actions`). However when setting default properties for an object, in most
cases one should use the `init` hook. This is especially important when
setting values to objects or arrays that will be shared (likely inadvertently)
across instances. This rule warns when properties are set on the prototype
that should instead be initialized inside of the `init` hook.
45 changes: 45 additions & 0 deletions lib/rules/no-class-prototype-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

var prohibitedPrototypeSyntax = {
'ArrayExpression': true,
'ObjectExpression': true,
'Literal': true
}

var whitelistedProperties = {
'actions': true,
'attributeBindings': true,
'classNameBindings': true,
'classNames': true,
'concatenatedProperties': true,
'mergedProperties': true,
'queryParams': true
}

module.exports = function(context) {
function isInsideObjectExtension(node) {
return (
node.parent.type === 'ObjectExpression' &&
node.parent.parent.type === 'CallExpression' &&
node.parent.parent.callee.type === 'MemberExpression' &&
node.parent.parent.callee.property.name === 'extend'
);
}

function isWhitelistedProperty(node) {
return whitelistedProperties.hasOwnProperty(node.key.name);
}

return {
'Property': function(node) {
if (isInsideObjectExtension(node) && !isWhitelistedProperty(node)) {
if (prohibitedPrototypeSyntax.hasOwnProperty(node.value.type)) {
context.report({
node: node,
message: 'error'
});
}
}
}
};
};
43 changes: 43 additions & 0 deletions tests/lib/rules/no-class-prototype-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

var rule = require("../../../lib/rules/no-class-prototype-attributes");
var RuleTester = require("eslint").RuleTester;

var ruleTester = new RuleTester();

ruleTester.run("no-class-prototype-attributes", rule, {
valid: [
"Class.extend({});",
"{ a: [] };",
"var b = { a: [] };",
"{ a: {} };",
"var a = {};",
"Class.extend({ a: computed() })",
"Class.extend({ a: identifier })",
"Class.extend({ a: identifier })",
"Class.extend({ actions: [] })",
"Class.extend({ attributeBindings: [] })",
"Class.extend({ classNameBindings: [] })",
"Class.extend({ classNames: [] })",
"Class.extend({ concatenatedProperties: [] })",
"Class.extend({ mergedProperties: [] })",
"Class.extend({ queryParams: [] })"
],

invalid: [{
code: "Class.extend({ a: [] })",
errors: ["error"]
}, {
code: "Class.extend({ a: {} })",
errors: ["error"]
}, {
code: "Class.extend({ a: 1 })",
errors: ["error"]
}, {
code: "Class.extend({ a: true })",
errors: ["error"]
}, {
code: "Class.extend({ a: null })",
errors: ["error"]
}]
});

0 comments on commit 67847e2

Please sign in to comment.