Skip to content

Commit 5a60432

Browse files
golopotljharb
authored andcommitted
[Fix] require-render-return: more accurate report location
Previously the reported node is the whole component. Now the reported node is the render method.
1 parent b37a504 commit 5a60432

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

lib/rules/require-render-return.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ module.exports = {
3535
}
3636

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

5349
return {
@@ -80,14 +76,14 @@ module.exports = {
8076
const list = components.list();
8177
Object.keys(list).forEach(component => {
8278
if (
83-
!hasRenderMethod(list[component].node) ||
79+
!findRenderMethod(list[component].node) ||
8480
list[component].hasReturnStatement ||
8581
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
8682
) {
8783
return;
8884
}
8985
context.report({
90-
node: list[component].node,
86+
node: findRenderMethod(list[component].node),
9187
message: 'Your render method should have return statement'
9288
});
9389
});

tests/lib/rules/require-render-return.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ ruleTester.run('require-render-return', rule, {
147147
});
148148
`,
149149
errors: [{
150-
message: 'Your render method should have return statement'
150+
message: 'Your render method should have return statement',
151+
line: 4
151152
}]
152153
}, {
153154
// Missing return in ES6 class
@@ -171,7 +172,8 @@ ruleTester.run('require-render-return', rule, {
171172
}
172173
`,
173174
errors: [{
174-
message: 'Your render method should have return statement'
175+
message: 'Your render method should have return statement',
176+
line: 3
175177
}]
176178
}, {
177179
// Missing return ES6 class render property
@@ -184,7 +186,8 @@ ruleTester.run('require-render-return', rule, {
184186
`,
185187
parser: 'babel-eslint',
186188
errors: [{
187-
message: 'Your render method should have return statement'
189+
message: 'Your render method should have return statement',
190+
type: 'ClassProperty'
188191
}]
189192
}]
190193
});

0 commit comments

Comments
 (0)