-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule no-class-prototype-attributes
[closes #2]
- Loading branch information
1 parent
def535e
commit 67847e2
Showing
3 changed files
with
96 additions
and
0 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 |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
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,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"] | ||
}] | ||
}); |