Move a few more references to plugin options to the top level.

This commit is contained in:
Logan Smyth
2017-11-06 17:26:59 -08:00
parent 6fe7f77eb4
commit e7c57ae85a
6 changed files with 73 additions and 47 deletions

View File

@@ -1,6 +1,13 @@
import { types as t } from "@babel/core";
export default function() {
export default function(api, options) {
const { loose = false } = options;
if (typeof loose !== "boolean") {
throw new Error(`.loose must be a boolean or undefined`);
}
const arrayOnlySpread = loose;
/**
* Test if a VariableDeclaration's declarations contains any Patterns.
*/
@@ -43,8 +50,9 @@ export default function() {
this.arrays = {};
this.nodes = opts.nodes || [];
this.scope = opts.scope;
this.file = opts.file;
this.kind = opts.kind;
this.arrayOnlySpread = opts.arrayOnlySpread;
this.addHelper = opts.addHelper;
}
buildVariableAssignment(id, init) {
@@ -88,7 +96,7 @@ export default function() {
toArray(node, count) {
if (
this.file.opts.loose ||
this.arrayOnlySpread ||
(t.isIdentifier(node) && this.arrays[node.name])
) {
return node;
@@ -164,7 +172,7 @@ export default function() {
//
const value = t.callExpression(
this.file.addHelper("objectWithoutProperties"),
this.addHelper("objectWithoutProperties"),
[objRef, keys],
);
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
@@ -189,7 +197,7 @@ export default function() {
if (!pattern.properties.length) {
this.nodes.push(
t.expressionStatement(
t.callExpression(this.file.addHelper("objectDestructuringEmpty"), [
t.callExpression(this.addHelper("objectDestructuringEmpty"), [
objRef,
]),
),
@@ -370,7 +378,7 @@ export default function() {
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
},
ForXStatement(path, file) {
ForXStatement(path) {
const { node, scope } = path;
const left = node.left;
@@ -406,9 +414,10 @@ export default function() {
const destructuring = new DestructuringTransformer({
kind: left.kind,
file: file,
scope: scope,
nodes: nodes,
arrayOnlySpread,
addHelper: name => this.addHelper(name),
});
destructuring.init(pattern, key);
@@ -419,7 +428,7 @@ export default function() {
block.body = nodes.concat(block.body);
},
CatchClause({ node, scope }, file) {
CatchClause({ node, scope }) {
const pattern = node.param;
if (!t.isPattern(pattern)) return;
@@ -430,16 +439,17 @@ export default function() {
const destructuring = new DestructuringTransformer({
kind: "let",
file: file,
scope: scope,
nodes: nodes,
arrayOnlySpread,
addHelper: name => this.addHelper(name),
});
destructuring.init(pattern, ref);
node.body.body = nodes.concat(node.body.body);
},
AssignmentExpression(path, file) {
AssignmentExpression(path) {
const { node, scope } = path;
if (!t.isPattern(node.left)) return;
@@ -447,9 +457,10 @@ export default function() {
const destructuring = new DestructuringTransformer({
operator: node.operator,
file: file,
scope: scope,
nodes: nodes,
arrayOnlySpread,
addHelper: name => this.addHelper(name),
});
let ref;
@@ -479,7 +490,7 @@ export default function() {
path.replaceWithMultiple(nodes);
},
VariableDeclaration(path, file) {
VariableDeclaration(path) {
const { node, scope, parent } = path;
if (t.isForXStatement(parent)) return;
if (!parent || !path.container) return; // i don't know why this is necessary - TODO
@@ -500,7 +511,8 @@ export default function() {
nodes: nodes,
scope: scope,
kind: node.kind,
file: file,
arrayOnlySpread,
addHelper: name => this.addHelper(name),
});
if (t.isPattern(pattern)) {