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

Fix: Arrow function body should be ObjectExpression (fixes #331) #334

Merged
merged 1 commit into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,18 @@ module.exports = function convert(config) {

case SyntaxKind.ObjectLiteralExpression: {

const objectAssignNode = nodeUtils.findAncestorOfKind(node, SyntaxKind.BinaryExpression);
let objectIsInAssignment;
const ancestorNode = nodeUtils.findFirstMatchingAncestor(
node,
parentNode =>
(parentNode.kind === SyntaxKind.BinaryExpression || parentNode.kind === SyntaxKind.ArrowFunction)
);
const objectAssignNode = (
ancestorNode &&
ancestorNode.kind === SyntaxKind.BinaryExpression &&
ancestorNode.operatorToken.kind === SyntaxKind.FirstAssignment
) ? ancestorNode : null;

let objectIsInAssignment = false;

if (objectAssignNode) {
if (objectAssignNode.left === node) {
Expand Down
34 changes: 18 additions & 16 deletions lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,6 @@ function findFirstMatchingChild(node, sourceFile, predicate) {
return undefined;
}

/**
* Find the first matching ancestor based on the given predicate function.
* @param {TSNode} node The current TSNode
* @param {Function} predicate The predicate function to apply to each checked ancestor
* @returns {TSNode|undefined} a matching parent TSNode
*/
function findFirstMatchingAncestor(node, predicate) {
while (node) {
if (predicate(node)) {
return node;
}
node = node.parent;
}
return undefined;
}

/**
* Returns true if the given TSNode is a let variable declaration
* @param {TSNode} node The TSNode
Expand Down Expand Up @@ -187,6 +171,7 @@ module.exports = {
hasStaticModifierFlag,
findNextToken,
findChildOfKind,
findFirstMatchingAncestor,
findAncestorOfKind,
hasJSXAncestor,
unescapeIdentifier,
Expand Down Expand Up @@ -417,6 +402,22 @@ function findChildOfKind(node, kind, sourceFile) {
return findFirstMatchingChild(node, sourceFile, child => child.kind === kind);
}

/**
* Find the first matching ancestor based on the given predicate function.
* @param {TSNode} node The current TSNode
* @param {Function} predicate The predicate function to apply to each checked ancestor
* @returns {TSNode|undefined} a matching parent TSNode
*/
function findFirstMatchingAncestor(node, predicate) {
while (node) {
if (predicate(node)) {
return node;
}
node = node.parent;
}
return undefined;
}

/**
* Finds the first parent TSNode which mastches the given kind
* @param {TSNode} node The current TSNode
Expand All @@ -427,6 +428,7 @@ function findAncestorOfKind(node, kind) {
return findFirstMatchingAncestor(node, parent => parent.kind === kind);
}


/**
* Returns true if a given TSNode has a JSX token within its hierarchy
* @param {TSNode} node TSNode to be checked
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(a => ({})) + 1;
(a => ({})) = 1;
((a => ({})) + 1);
((a => ({})) = 1);

Loading