Skip to content

Commit

Permalink
fix: in operator branch distance (#206)
Browse files Browse the repository at this point in the history
The 'in' operator was wrongly calculated in the branch distance visitor.
  • Loading branch information
dstallenberg committed Dec 11, 2023
1 parent a66647f commit 0632913
Showing 1 changed file with 18 additions and 47 deletions.
65 changes: 18 additions & 47 deletions libraries/search-javascript/lib/criterion/BranchDistanceVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ export class BranchDistanceVisitor extends AbstractSyntaxTreeVisitor {
// the value does not exist or is not a distance
throw new IllegalArgumentError(
"Cannot get distance from unknown condition",
{ context: { condition: condition } }
{
context: {
condition: condition,
inValueMap: this._valueMap.has(condition),
isDistance:
this._isDistanceMap.has(condition) &&
this._isDistanceMap.get(condition),
availableValues: [...this._valueMap.entries()].map(
(value) => `${value[0]} -> ${String(value[1])}`
),
},
}
);
}

Expand Down Expand Up @@ -303,48 +314,6 @@ export class BranchDistanceVisitor extends AbstractSyntaxTreeVisitor {
this._isDistanceMap.set(path.toString(), false);
};

// public ObjectExpression: (path: NodePath<t.ObjectExpression>) => void = (path) => {
// this._valueMap.set(this._getNodeId(path), )
// this._isDistanceMap.set(this._getNodeId(path), false);
// }

// public Identifier: (path: NodePath<t.Identifier>) => void = (path) => {
// if (this._variables[path.node.name] === undefined) {
// // we dont know what this variable is...
// this._valueMap.set(this._getNodeId(path), undefined);
// } else {
// this._valueMap.set(
// this._getNodeId(path),
// this._variables[path.node.name]
// );
// }
// this._isDistanceMap.set(this._getNodeId(path), false);
// };

// public MemberExpression: (path: NodePath<t.MemberExpression>) => void = (
// path
// ) => {
// const result = generate(path.node);
// const value = this._variables[result.code];
// // might be undefined
// this._valueMap.set(this._getNodeId(path), value);
// this._isDistanceMap.set(this._getNodeId(path), false);
// };
// public Identifier: (path: NodePath<t.Identifier>) => void = (path) => {
// if (this._variables[path.node.name] === undefined) {
// // we dont know what this variable is...
// // should never happen??
// this._valueMap.set(path.toString(), undefined);
// throw new ImplementationError(shouldNeverHappen('BranchDistanceVisitor'))
// } else {
// this._valueMap.set(
// path.toString(),
// this._variables[path.node.name]
// );
// }
// this._isDistanceMap.set(path.toString(), false);
// };

public UpdateExpression: (path: NodePath<t.UpdateExpression>) => void = (
path
) => {
Expand Down Expand Up @@ -620,7 +589,6 @@ export class BranchDistanceVisitor extends AbstractSyntaxTreeVisitor {

// distance
case "==":
// TODO
case "===": {
if (typeof leftValue === "number" && typeof rightValue === "number") {
value = Math.abs(leftValue - rightValue);
Expand All @@ -647,7 +615,6 @@ export class BranchDistanceVisitor extends AbstractSyntaxTreeVisitor {
break;
}
case "!=":
// TODO
case "!==": {
if (operator === "!==") {
value = leftValue === rightValue ? 1 : 0;
Expand All @@ -657,8 +624,12 @@ export class BranchDistanceVisitor extends AbstractSyntaxTreeVisitor {
break;
}
case "in": {
if (rightValue === undefined || rightValue === null) {
value = 1; // TODO should this one be inverted?
if (
rightValue === undefined ||
rightValue === null ||
typeof rightValue !== "object"
) {
value = this._inverted ? Number.MAX_VALUE : 0;
} else {
if (this._inverted) {
value = leftValue in rightValue ? Number.MAX_VALUE : 0;
Expand Down

0 comments on commit 0632913

Please sign in to comment.