Refactor Babel's helpers into smaller functions.

This commit is contained in:
Logan Smyth
2018-03-04 18:22:13 -08:00
parent fc64ab5725
commit 4d76d5dddc
3 changed files with 104 additions and 72 deletions

View File

@@ -401,7 +401,7 @@ export default class Scope {
console.log(sep);
}
toArray(node: Object, i?: number) {
toArray(node: Object, i?: number | boolean) {
const file = this.hub.file;
if (t.isIdentifier(node)) {
@@ -431,14 +431,20 @@ export default class Scope {
);
}
let helperName = "toArray";
let helperName;
const args = [node];
if (i === true) {
// Used in array-spread to create an array.
helperName = "toConsumableArray";
} else if (i) {
args.push(t.numericLiteral(i));
// Used in array-rest to create an array from a subset of an iterable.
helperName = "slicedToArray";
// TODO if (this.hub.file.isLoose("es6.forOf")) helperName += "-loose";
} else {
// Used in array-rest to create an array
helperName = "toArray";
}
return t.callExpression(file.addHelper(helperName), args);
}