-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from shmuga/require-render-return
Add require-render-return rule
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
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,55 @@ | ||
/** | ||
* @fileoverview Enforce ES5 or ES6 class for returning value in render function. | ||
* @author Mark Orel | ||
*/ | ||
'use strict'; | ||
|
||
var Components = require('../util/Components'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = Components.detect(function(context) { | ||
/** | ||
* Helper for checking if parent node is render method. | ||
* @param {ASTNode} node to check | ||
* @returns {boolean} If previous node is method and name is render returns true, otherwise false; | ||
*/ | ||
function checkParent(node) { | ||
return (node.parent.type === 'Property' || node.parent.type === 'MethodDefinition') | ||
&& node.parent.key | ||
&& node.parent.key.name === 'render'; | ||
} | ||
|
||
/** | ||
* Helper for checking if child node exists and if it's ReturnStatement | ||
* @param {ASTNode} node to check | ||
* @returns {boolean} True if ReturnStatement exists, otherwise false | ||
*/ | ||
function checkReturnStatementExistence(node) { | ||
if (!node.body && !node.body.body && !node.body.body.length) { | ||
return false; | ||
} | ||
|
||
var hasReturnStatement = node.body.body.some(function(elem) { | ||
return elem.type === 'ReturnStatement'; | ||
}); | ||
|
||
return hasReturnStatement; | ||
} | ||
|
||
|
||
return { | ||
FunctionExpression: function(node) { | ||
if (checkParent(node) && !checkReturnStatementExistence(node)) { | ||
context.report({ | ||
node: node, | ||
message: 'Your render method should have return statement' | ||
}); | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
module.exports.schema = [{}]; |
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,73 @@ | ||
/** | ||
* @fileoverview Enforce ES5 or ES6 class for returning value in render function. | ||
* @author Mark Orel | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/require-render-return'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
require('babel-eslint'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('require-render-return', rule, { | ||
|
||
valid: [{ | ||
code: [ | ||
'class Hello extends React.Component {', | ||
' render() {', | ||
' return <div>Hello {this.props.name}</div>;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' displayName: \'Hello\',', | ||
' render: function() {', | ||
' return <div></div>', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' displayName: \'Hello\',', | ||
' render: function() {}', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Your render method should have return statement' | ||
}] | ||
}, { | ||
code: [ | ||
'class Hello extends React.Component {', | ||
' render() {} ', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Your render method should have return statement' | ||
}] | ||
} | ||
]}); |