clean up array inferrence for #1515
This commit is contained in:
parent
e8956a8c44
commit
1baa0df948
@ -461,16 +461,6 @@ export default class File {
|
||||
this.path = TraversalPath.get(null, null, ast, ast, "program", this);
|
||||
this.scope = this.path.scope;
|
||||
this.ast = ast;
|
||||
|
||||
this.path.traverse({
|
||||
enter(node, parent, scope) {
|
||||
if (this.isScope()) {
|
||||
for (var key in scope.bindings) {
|
||||
scope.bindings[key].setTypeAnnotation();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
addAst(ast) {
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
for (var LEN = ARGUMENTS.length, ARRAY = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) {
|
||||
for (var LEN = ARGUMENTS.length, ARRAY: ARRAY_TYPE = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) {
|
||||
ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
|
||||
}
|
||||
|
||||
@ -56,7 +56,8 @@ var hasRest = function (node) {
|
||||
export function Func(node, parent, scope, file) {
|
||||
if (!hasRest(node)) return;
|
||||
|
||||
var rest = node.params.pop().argument;
|
||||
var restParam = node.params.pop();
|
||||
var rest = restParam.argument;
|
||||
|
||||
var argsId = t.identifier("arguments");
|
||||
|
||||
@ -125,13 +126,14 @@ export function Func(node, parent, scope, file) {
|
||||
}
|
||||
|
||||
var loop = util.template("rest", {
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
ARRAY_LEN: arrLen,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key,
|
||||
LEN: len
|
||||
ARRAY_TYPE: restParam.typeAnnotation,
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
ARRAY_LEN: arrLen,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key,
|
||||
LEN: len
|
||||
});
|
||||
loop._blockHoist = node.params.length + 1;
|
||||
node.body.body.unshift(loop);
|
||||
|
||||
@ -69,6 +69,15 @@ export default class Binding {
|
||||
this.references++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
dereference() {
|
||||
this.references--;
|
||||
this.referenced = !!this.references;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
@ -947,7 +947,7 @@ export default class TraversalPath {
|
||||
annotation: null
|
||||
};
|
||||
|
||||
var type = this.node.typeAnnotation;
|
||||
var type = this.node && this.node.typeAnnotation;
|
||||
|
||||
if (!type) {
|
||||
info.inferred = true;
|
||||
@ -979,7 +979,7 @@ export default class TraversalPath {
|
||||
if (!binding || !binding.constant) return;
|
||||
|
||||
// todo: take into consideration infinite recursion #1149
|
||||
return;
|
||||
//return;
|
||||
|
||||
if (binding.path === this) {
|
||||
return this;
|
||||
@ -1025,36 +1025,36 @@ export default class TraversalPath {
|
||||
path = path.resolve();
|
||||
if (!path) return;
|
||||
|
||||
if (path.isRestElement() || path.parentPath.isRestElement() || path.isArrayExpression()) {
|
||||
if (path.isPreviousType("RestElement") || path.parentPath.isPreviousType("RestElement") || path.isPreviousType("ArrayExpression")) {
|
||||
return t.genericTypeAnnotation(t.identifier("Array"));
|
||||
}
|
||||
|
||||
if (path.parentPath.isTypeCastExpression()) {
|
||||
if (path.parentPath.isPreviousType("TypeCastExpression")) {
|
||||
return path.parentPath.node.typeAnnotation;
|
||||
}
|
||||
|
||||
if (path.isTypeCastExpression()) {
|
||||
if (path.isPreviousType("TypeCastExpression")) {
|
||||
return path.node.typeAnnotation;
|
||||
}
|
||||
|
||||
if (path.isObjectExpression()) {
|
||||
if (path.isPreviousType("ObjectExpression")) {
|
||||
return t.genericTypeAnnotation(t.identifier("Object"));
|
||||
}
|
||||
|
||||
if (path.isFunction()) {
|
||||
if (path.isPreviousType("Function")) {
|
||||
return t.identifier("Function");
|
||||
}
|
||||
|
||||
if (path.isLiteral()) {
|
||||
if (path.isPreviousType("Literal")) {
|
||||
var value = path.node.value;
|
||||
if (isString(value)) return t.stringTypeAnnotation();
|
||||
if (isNumber(value)) return t.numberTypeAnnotation();
|
||||
if (isBoolean(value)) return t.booleanTypeAnnotation();
|
||||
}
|
||||
|
||||
if (path.isCallExpression()) {
|
||||
if (path.isPreviousType("CallExpression")) {
|
||||
var callee = path.get("callee").resolve();
|
||||
if (callee && callee.isFunction()) return callee.node.returnType;
|
||||
if (callee && callee.isPreviousType("Function")) return callee.node.returnType;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user