Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(Issue #325): inference right self invoked functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
JSMonk committed Feb 14, 2021
1 parent 80ae92a commit c7f73e9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
16 changes: 5 additions & 11 deletions packages/core/src/inference/function-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ export function inferenceFunctionTypeByScope(
isUserDefined: false,
});
genericArg.root = newRoot;
newRoot.root = root;
if (alreadyCreated === undefined) {
created.set(root, newRoot);
}
Expand Down Expand Up @@ -837,6 +838,7 @@ export function inferenceFunctionTypeByScope(
if (newReturnType instanceof TypeVar) {
newGenericArguments.add(newReturnType);
}
const GENERIC_RETURN_TYPE = typeGraph.findVariable({ name: "return" }).type;
const shouldBeCleaned = [];
for (const { calls } of nestedScopes) {
for (let i = 0; i < calls.length; i++) {
Expand Down Expand Up @@ -865,17 +867,9 @@ export function inferenceFunctionTypeByScope(
}
} else if (copy !== undefined) {
args[j] = copy;
if (
call.targetName === "return" &&
call.target instanceof FunctionType
) {
// $FlowIssue
call.target = targetType.changeAll(
[argumentType],
[copy],
typeScope
);
}
}
if (call.targetName === "return" && call.target instanceof FunctionType && newReturnType !== call.target.returnType) {
call.target = GENERIC_RETURN_TYPE;
}
}
if (targetType instanceof GenericType) {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/type-graph/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import HegelError from "../utils/errors";
import { Meta } from "./meta/meta";
import { Type } from "./types/type";
import { $Keys } from "./types/keys-type";
import { $Values } from "./types/values-type";
import { TypeVar } from "./types/type-var";
import { CallMeta } from "./meta/call-meta";
import { TupleType } from "./types/tuple-type";
Expand Down Expand Up @@ -536,7 +535,10 @@ export function addCallToTypeGraph(
arg.result instanceof VariableInfo ? arg.result.type : arg.result;
if (argType instanceof $Refinemented) {
if (!argType.isSafe()) {
throw new HegelError("You try to return unsafly refinemented object, which mean that somebody could change prooved property type outside the function.", node.loc);
throw new HegelError(
"You try to return unsafly refinemented object, which mean that somebody could change prooved property type outside the function.",
node.loc
);
}
argType = argType.refinemented;
}
Expand Down Expand Up @@ -902,7 +904,7 @@ export function addCallToTypeGraph(
args = node.arguments.map((n, i) => {
argsLocations.push(n.loc);
// $FlowIssue
const defaultArguments = (fnType.argumentsTypes || []);
const defaultArguments = fnType.argumentsTypes || [];
const defaultArg = defaultArguments[i];
if (
n.type === NODE.FUNCTION_EXPRESSION ||
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/type-graph/types/generic-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class GenericType<T: Type> extends Type {
subordinateType: T;
localTypeScope: TypeScope;
nestedRestriction: Type | void;
inferencedArguments: Set<TypeVar> = new Set();

constructor(
name: string,
Expand Down Expand Up @@ -518,14 +519,22 @@ export class GenericType<T: Type> extends Type {

asUserDefined() {
this.genericArguments.forEach((t) => {
if (this.inferencedArguments.has(t)) return;
t._isUserDefined = true;
t.root = undefined;
});
this.inferencedArguments = new Set();
return this;
}

asNotUserDefined() {
this.genericArguments.forEach((t) => (t._isUserDefined = false));
this.inferencedArguments = new Set();
this.genericArguments.forEach((t) => {
if (!t._isUserDefined) {
this.inferencedArguments.add(t);
}
t._isUserDefined = false;
});
return this;
}
}
6 changes: 2 additions & 4 deletions packages/core/src/type-graph/types/object-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,8 @@ export class ObjectType extends Type {
name: newName,
});
}
const currentSelf = TypeVar.createSelf(
this.getChangedName(sourceTypes, targetTypes),
this.parent
);
const currentSelf = TypeVar.createSelf("this", this.parent);
targetTypes = targetTypes.map(t => t === this ? currentSelf : t);
if (
this._changeStack !== null &&
this._changeStack.find((a) => a.equalsTo(currentSelf))
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/type-graph/types/type-var.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export class TypeVar extends Type {
typeScope: TypeScope
): Type {
const indexOfNewRootType = sourceTypes.findIndex(
// $FlowIssue
(a) => a.equalsTo(this, true, true)
(a) =>
// $FlowIssue
a.equalsTo(this, true, true) || this.equalsTo(a, true, true)
);
if (indexOfNewRootType !== -1) {
return targetTypes[indexOfNewRootType];
Expand Down

0 comments on commit c7f73e9

Please sign in to comment.