Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 074a64f

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Arrow function body should be ObjectExpression (fixes #331) (#334)
1 parent fb66f61 commit 074a64f

File tree

4 files changed

+1376
-18
lines changed

4 files changed

+1376
-18
lines changed

lib/convert.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,18 @@ module.exports = function convert(config) {
606606

607607
case SyntaxKind.ObjectLiteralExpression: {
608608

609-
const objectAssignNode = nodeUtils.findAncestorOfKind(node, SyntaxKind.BinaryExpression);
610-
let objectIsInAssignment;
609+
const ancestorNode = nodeUtils.findFirstMatchingAncestor(
610+
node,
611+
parentNode =>
612+
(parentNode.kind === SyntaxKind.BinaryExpression || parentNode.kind === SyntaxKind.ArrowFunction)
613+
);
614+
const objectAssignNode = (
615+
ancestorNode &&
616+
ancestorNode.kind === SyntaxKind.BinaryExpression &&
617+
ancestorNode.operatorToken.kind === SyntaxKind.FirstAssignment
618+
) ? ancestorNode : null;
619+
620+
let objectIsInAssignment = false;
611621

612622
if (objectAssignNode) {
613623
if (objectAssignNode.left === node) {

lib/node-utils.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,6 @@ function findFirstMatchingChild(node, sourceFile, predicate) {
121121
return undefined;
122122
}
123123

124-
/**
125-
* Find the first matching ancestor based on the given predicate function.
126-
* @param {TSNode} node The current TSNode
127-
* @param {Function} predicate The predicate function to apply to each checked ancestor
128-
* @returns {TSNode|undefined} a matching parent TSNode
129-
*/
130-
function findFirstMatchingAncestor(node, predicate) {
131-
while (node) {
132-
if (predicate(node)) {
133-
return node;
134-
}
135-
node = node.parent;
136-
}
137-
return undefined;
138-
}
139-
140124
/**
141125
* Returns true if the given TSNode is a let variable declaration
142126
* @param {TSNode} node The TSNode
@@ -187,6 +171,7 @@ module.exports = {
187171
hasStaticModifierFlag,
188172
findNextToken,
189173
findChildOfKind,
174+
findFirstMatchingAncestor,
190175
findAncestorOfKind,
191176
hasJSXAncestor,
192177
unescapeIdentifier,
@@ -417,6 +402,22 @@ function findChildOfKind(node, kind, sourceFile) {
417402
return findFirstMatchingChild(node, sourceFile, child => child.kind === kind);
418403
}
419404

405+
/**
406+
* Find the first matching ancestor based on the given predicate function.
407+
* @param {TSNode} node The current TSNode
408+
* @param {Function} predicate The predicate function to apply to each checked ancestor
409+
* @returns {TSNode|undefined} a matching parent TSNode
410+
*/
411+
function findFirstMatchingAncestor(node, predicate) {
412+
while (node) {
413+
if (predicate(node)) {
414+
return node;
415+
}
416+
node = node.parent;
417+
}
418+
return undefined;
419+
}
420+
420421
/**
421422
* Finds the first parent TSNode which mastches the given kind
422423
* @param {TSNode} node The current TSNode
@@ -427,6 +428,7 @@ function findAncestorOfKind(node, kind) {
427428
return findFirstMatchingAncestor(node, parent => parent.kind === kind);
428429
}
429430

431+
430432
/**
431433
* Returns true if a given TSNode has a JSX token within its hierarchy
432434
* @param {TSNode} node TSNode to be checked
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(a => ({})) + 1;
2+
(a => ({})) = 1;
3+
((a => ({})) + 1);
4+
((a => ({})) = 1);
5+

0 commit comments

Comments
 (0)