|
3 | 3 | */ |
4 | 4 | 'use strict'; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Find a return statment in the current node |
| 8 | + * |
| 9 | + * @param {ASTNode} ASTnode The AST node being checked |
| 10 | + */ |
| 11 | +function findReturnStatement(node) { |
| 12 | + if ( |
| 13 | + (!node.value || !node.value.body || !node.value.body.body) && |
| 14 | + (!node.body || !node.body.body) |
| 15 | + ) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + |
| 19 | + const bodyNodes = (node.value ? node.value.body.body : node.body.body); |
| 20 | + |
| 21 | + let i = bodyNodes.length - 1; |
| 22 | + for (; i >= 0; i--) { |
| 23 | + if (bodyNodes[i].type === 'ReturnStatement') { |
| 24 | + return bodyNodes[i]; |
| 25 | + } |
| 26 | + } |
| 27 | + return false; |
| 28 | +} |
| 29 | + |
6 | 30 | /** |
7 | 31 | * Get properties name |
8 | 32 | * @param {Object} node - Property. |
@@ -36,31 +60,34 @@ function getComponentProperties(node) { |
36 | 60 | } |
37 | 61 |
|
38 | 62 | /** |
39 | | - * Find a return statment in the current node |
40 | | - * |
41 | | - * @param {ASTNode} ASTnode The AST node being checked |
42 | | - */ |
43 | | -function findReturnStatement(node) { |
44 | | - if ( |
45 | | - (!node.value || !node.value.body || !node.value.body.body) && |
46 | | - (!node.body || !node.body.body) |
47 | | - ) { |
48 | | - return false; |
49 | | - } |
50 | | - |
51 | | - const bodyNodes = (node.value ? node.value.body.body : node.body.body); |
| 63 | + * Checks if the node is the first in its line, excluding whitespace. |
| 64 | + * @param {Object} context The node to check |
| 65 | + * @param {ASTNode} node The node to check |
| 66 | + * @return {Boolean} true if its the first node in its line |
| 67 | + */ |
| 68 | +function isNodeFirstInLine(context, node) { |
| 69 | + const sourceCode = context.getSourceCode(); |
| 70 | + let token = node; |
| 71 | + let lines; |
| 72 | + do { |
| 73 | + token = sourceCode.getTokenBefore(token); |
| 74 | + lines = token.type === 'JSXText' |
| 75 | + ? token.value.split('\n') |
| 76 | + : null; |
| 77 | + } while ( |
| 78 | + token.type === 'JSXText' && |
| 79 | + /^\s*$/.test(lines[lines.length - 1]) |
| 80 | + ); |
52 | 81 |
|
53 | | - let i = bodyNodes.length - 1; |
54 | | - for (; i >= 0; i--) { |
55 | | - if (bodyNodes[i].type === 'ReturnStatement') { |
56 | | - return bodyNodes[i]; |
57 | | - } |
58 | | - } |
59 | | - return false; |
| 82 | + const startLine = node.loc.start.line; |
| 83 | + const endLine = token ? token.loc.end.line : -1; |
| 84 | + return startLine !== endLine; |
60 | 85 | } |
61 | 86 |
|
| 87 | + |
62 | 88 | module.exports = { |
| 89 | + findReturnStatement: findReturnStatement, |
63 | 90 | getPropertyName: getPropertyName, |
64 | 91 | getComponentProperties: getComponentProperties, |
65 | | - findReturnStatement: findReturnStatement |
| 92 | + isNodeFirstInLine: isNodeFirstInLine |
66 | 93 | }; |
0 commit comments