Skip to content

Commit 0ec372f

Browse files
Fix parsing for return statements
1 parent 0ddceff commit 0ec372f

File tree

1 file changed

+4
-55
lines changed

1 file changed

+4
-55
lines changed

index.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,6 @@ function getReturnStatement(node) {
4545
);
4646
}
4747

48-
function isInNestedReturnStatement(ancestors, returnStatement) {
49-
const nestedReturnStatementIndex = ancestors.findIndex(
50-
(ancestor, index) =>
51-
(ancestor.type === 'ReturnStatement' ||
52-
(ancestor.type === 'JSXElement' &&
53-
ancestors[index + 1]?.type === 'ArrowFunctionExpression')) &&
54-
ancestor !== returnStatement,
55-
);
56-
57-
const originalReturnStatementIndex = ancestors.findIndex(
58-
(ancestor) => ancestor === returnStatement,
59-
);
60-
61-
return (
62-
nestedReturnStatementIndex !== -1 &&
63-
originalReturnStatementIndex !== -1 &&
64-
originalReturnStatementIndex > nestedReturnStatementIndex
65-
);
66-
}
67-
68-
function isInNestedFunction(ancestors, returnStatement) {
69-
const nestedFunctionIndex = ancestors.findIndex(
70-
(ancestor) =>
71-
ancestor.type === 'ArrowFunctionExpression' ||
72-
ancestor.type === 'FunctionDeclaration',
73-
);
74-
75-
const originalReturnStatementIndex = ancestors.findIndex(
76-
(ancestor) => ancestor === returnStatement,
77-
);
78-
79-
return (
80-
nestedFunctionIndex !== -1 &&
81-
originalReturnStatementIndex !== -1 &&
82-
originalReturnStatementIndex > nestedFunctionIndex
83-
);
84-
}
85-
8648
const rules = {
8749
'data-component': {
8850
meta: {
@@ -135,7 +97,7 @@ const rules = {
13597
traverseTree(
13698
getReturnStatement(child),
13799
visitorKeys,
138-
(current, ancestors) => {
100+
(current) => {
139101
if (
140102
current.type === 'JSXElement' &&
141103
current.openingElement.attributes.find(
@@ -157,12 +119,7 @@ const rules = {
157119
!current.openingElement.attributes.find(
158120
(attributeNode) =>
159121
attributeNode.name?.name === 'data-component',
160-
) &&
161-
(isInNestedReturnStatement(
162-
ancestors,
163-
getReturnStatement(child),
164-
) ||
165-
!isInNestedFunction(ancestors, getReturnStatement(child)))
122+
)
166123
) {
167124
flag = true;
168125

@@ -202,7 +159,7 @@ const rules = {
202159
traverseTree(
203160
getReturnStatement(componentNode),
204161
visitorKeys,
205-
(current, ancestors) => {
162+
(current) => {
206163
if (
207164
current.type === 'JSXElement' &&
208165
current.openingElement.attributes.find(
@@ -224,15 +181,7 @@ const rules = {
224181
!current.openingElement.attributes.find(
225182
(attributeNode) =>
226183
attributeNode.name?.name === 'data-component',
227-
) &&
228-
(isInNestedReturnStatement(
229-
ancestors,
230-
getReturnStatement(componentNode),
231-
) ||
232-
!isInNestedFunction(
233-
ancestors,
234-
getReturnStatement(componentNode),
235-
))
184+
)
236185
) {
237186
fixNode = current.openingElement;
238187

0 commit comments

Comments
 (0)