Update param scope values when expanding parameters.

This commit is contained in:
Logan Smyth
2017-05-02 21:24:35 -07:00
committed by Brian Ng
parent 95882d4e5a
commit 8e19a5b057
4 changed files with 30 additions and 9 deletions

View File

@@ -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();
}
},
},
};

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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) {