Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yarn Dedup & rename hasMoreThanOneBinding to shouldStoreRHSInTemporar…
…yVariable
- Loading branch information
Showing
6 changed files
with
38 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
packages/babel-plugin-proposal-object-rest-spread/src/hasMoreThanOneBinding.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/babel-plugin-proposal-object-rest-spread/src/shouldStoreRHSInTemporaryVariable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { types as t } from "@babel/core"; | ||
|
||
/** | ||
* This is a helper function to determine if we should create an intermediate variable | ||
* such that the RHS of an assignment is not duplicated. | ||
* | ||
* See https://github.com/babel/babel/pull/13711#issuecomment-914388382 for discussion | ||
* on further optimizations. | ||
*/ | ||
export default function shouldStoreRHSInTemporaryVariable(node) { | ||
if (t.isArrayPattern(node)) { | ||
const nonNullElements = node.elements.filter(element => element !== null); | ||
if (nonNullElements.length > 1) return true; | ||
else return shouldStoreRHSInTemporaryVariable(nonNullElements[0]); | ||
} else if (t.isObjectPattern(node)) { | ||
if (node.properties.length > 1) return true; | ||
else if (node.properties.length === 0) return false; | ||
else return shouldStoreRHSInTemporaryVariable(node.properties[0]); | ||
} else if (t.isObjectProperty(node)) { | ||
return shouldStoreRHSInTemporaryVariable(node.value); | ||
} else if (t.isAssignmentPattern(node)) { | ||
return shouldStoreRHSInTemporaryVariable(node.left); | ||
} else if (t.isRestElement(node)) { | ||
if (t.isIdentifier(node.argument)) return true; | ||
return shouldStoreRHSInTemporaryVariable(node.argument); | ||
} else { | ||
// node is Identifier or MemberExpression | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters