Skip to content

Commit

Permalink
[Fix] require-render-return: more accurate report location
Browse files Browse the repository at this point in the history
Previously the reported node is the whole component.
Now the reported node is the render method.
  • Loading branch information
golopot authored and ljharb committed Apr 9, 2019
1 parent b37a504 commit 5a60432
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
22 changes: 9 additions & 13 deletions lib/rules/require-render-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ module.exports = {
}

/**
* Check if a given AST node has a render method
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if there is a render method, false if not
* Find render method in a given AST node
* @param {ASTNode} node The component to find render method.
* @returns {ASTNode} Method node if found, undefined if not.
*/
function hasRenderMethod(node) {
function findRenderMethod(node) {
const properties = astUtil.getComponentProperties(node);
for (let i = 0, j = properties.length; i < j; i++) {
if (astUtil.getPropertyName(properties[i]) !== 'render' || !properties[i].value) {
continue;
}
return astUtil.isFunctionLikeExpression(properties[i].value);
}
return false;
return properties
.filter(property => astUtil.getPropertyName(property) === 'render' && property.value)
.find(property => astUtil.isFunctionLikeExpression(property.value));
}

return {
Expand Down Expand Up @@ -80,14 +76,14 @@ module.exports = {
const list = components.list();
Object.keys(list).forEach(component => {
if (
!hasRenderMethod(list[component].node) ||
!findRenderMethod(list[component].node) ||
list[component].hasReturnStatement ||
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
return;
}
context.report({
node: list[component].node,
node: findRenderMethod(list[component].node),
message: 'Your render method should have return statement'
});
});
Expand Down
9 changes: 6 additions & 3 deletions tests/lib/rules/require-render-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ ruleTester.run('require-render-return', rule, {
});
`,
errors: [{
message: 'Your render method should have return statement'
message: 'Your render method should have return statement',
line: 4
}]
}, {
// Missing return in ES6 class
Expand All @@ -171,7 +172,8 @@ ruleTester.run('require-render-return', rule, {
}
`,
errors: [{
message: 'Your render method should have return statement'
message: 'Your render method should have return statement',
line: 3
}]
}, {
// Missing return ES6 class render property
Expand All @@ -184,7 +186,8 @@ ruleTester.run('require-render-return', rule, {
`,
parser: 'babel-eslint',
errors: [{
message: 'Your render method should have return statement'
message: 'Your render method should have return statement',
type: 'ClassProperty'
}]
}]
});

0 comments on commit 5a60432

Please sign in to comment.