From 8e19a5b057f5ea67c73641da3fbeb5293abd4c3f Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 2 May 2017 21:24:35 -0700 Subject: [PATCH] Update param scope values when expanding parameters. --- .../src/index.js | 9 +++++++-- .../src/params.js | 4 +++- .../src/rest.js | 6 ++++-- .../src/path/inference/inferers.js | 20 +++++++++++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-parameters/src/index.js b/packages/babel-plugin-transform-es2015-parameters/src/index.js index f3ffba2755..267c5f13b6 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/index.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/index.js @@ -15,8 +15,13 @@ export default function() { path.arrowFunctionToExpression(); } - convertFunctionRest(path); - convertFunctionParams(path); + const convertedRest = convertFunctionRest(path); + const convertedParams = convertFunctionParams(path); + + if (convertedRest || convertedParams) { + // Manually reprocess this scope to ensure that the moved params are updated. + path.scope.crawl(); + } }, }, }; diff --git a/packages/babel-plugin-transform-es2015-parameters/src/params.js b/packages/babel-plugin-transform-es2015-parameters/src/params.js index f6cf4b1e31..99e80cc7f6 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/params.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/params.js @@ -96,7 +96,7 @@ export default function convertFunctionParams(path) { } } - if (body.length === 0) return; + if (body.length === 0) return false; // we need to cut off all trailing parameters if (firstOptionalIndex !== null) { @@ -112,4 +112,6 @@ export default function convertFunctionParams(path) { } else { path.get("body").unshiftContainer("body", body); } + + return true; } diff --git a/packages/babel-plugin-transform-es2015-parameters/src/rest.js b/packages/babel-plugin-transform-es2015-parameters/src/rest.js index c9f7f2906c..5463633259 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/rest.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/rest.js @@ -222,7 +222,7 @@ function optimiseLengthGetter(path, argsId, offset) { export default function convertFunctionRest(path) { const { node, scope } = path; - if (!hasRest(node)) return; + if (!hasRest(node)) return false; const rest = node.params.pop().argument; @@ -273,7 +273,7 @@ export default function convertFunctionRest(path) { path.replaceWith(argsId); } } - return; + return true; } state.references = state.references.concat( @@ -338,4 +338,6 @@ export default function convertFunctionRest(path) { target.insertBefore(loop); } + + return true; } diff --git a/packages/babel-traverse/src/path/inference/inferers.js b/packages/babel-traverse/src/path/inference/inferers.js index 740f3f07c8..aa2ecec29f 100644 --- a/packages/babel-traverse/src/path/inference/inferers.js +++ b/packages/babel-traverse/src/path/inference/inferers.js @@ -5,11 +5,23 @@ export { default as Identifier } from "./inferer-reference"; export function VariableDeclarator() { const id = this.get("id"); - if (id.isIdentifier()) { - return this.get("init").getTypeAnnotation(); - } else { - return; + if (!id.isIdentifier()) return; + const init = this.get("init"); + + let type = init.getTypeAnnotation(); + + if (type && type.type === "AnyTypeAnnotation") { + // Detect "var foo = Array()" calls so we can optimize for arrays vs iterables. + if ( + init.isCallExpression() && + init.get("callee").isIdentifier({ name: "Array" }) && + !init.scope.hasBinding("Array", true /* noGlobals */) + ) { + type = ArrayExpression(); + } } + + return type; } export function TypeCastExpression(node) {