-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve typing for ==, != expressions (#5840)
* Improve typing for ==, != expressions Closes #5761 Closes #5835 The overloads of == and != are now effectively: - `(T1: Comparable, T2: Comparable) => boolean { T1 == T2 }` - `(Comparable, value) => boolean` - `(value, Comparable) => boolean` Where `Comparable = string | number | boolean | null`. * Simplify comparability check * Add internal docs, fix possibleValues
- Loading branch information
1 parent
c1cf5f0
commit 537e4dd
Showing
18 changed files
with
223 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// @flow | ||
|
||
const { | ||
ValueType, | ||
BooleanType, | ||
} = require('../types'); | ||
const {toString} = require('../types'); | ||
|
||
import type { Expression } from '../expression'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type { Type } from '../types'; | ||
|
||
function eq(ctx) { return this.lhs.evaluate(ctx) === this.rhs.evaluate(ctx); } | ||
function ne(ctx) { return this.lhs.evaluate(ctx) !== this.rhs.evaluate(ctx); } | ||
|
||
function isComparableType(type: Type) { | ||
return type.kind === 'string' || | ||
type.kind === 'number' || | ||
type.kind === 'boolean' || | ||
type.kind === 'null'; | ||
} | ||
|
||
/** | ||
* Special form for ==, !=, implementing the following signatures: | ||
* - (T1: Comparable, T2: Comparable) => boolean { T1 == T2 } | ||
* - (Comparable, value) => boolean | ||
* - (value, Comparable) => boolean | ||
* | ||
* Where Comparable = string | number | boolean | null. | ||
* | ||
* Evaluation semantics for the value cases are equivalent to Javascript's | ||
* strict equality (===/!==) -- i.e., when the value argument's type doesn't | ||
* match that of the Comparable argument, == evaluates to false, != to true. | ||
* | ||
* @private | ||
*/ | ||
class Equals implements Expression { | ||
type: Type; | ||
lhs: Expression; | ||
rhs: Expression; | ||
evaluate: (EvaluationContext) => any; | ||
|
||
constructor(op: '==' | '!=', lhs: Expression, rhs: Expression) { | ||
this.type = BooleanType; | ||
this.lhs = lhs; | ||
this.rhs = rhs; | ||
this.evaluate = op === '==' ? eq : ne; | ||
} | ||
|
||
static parse(args: Array<mixed>, context: ParsingContext): ?Expression { | ||
if (args.length !== 3) | ||
return context.error(`Expected two arguments.`); | ||
|
||
const op: '==' | '!=' = (args[0]: any); | ||
|
||
const lhs = context.parse(args[1], 1, ValueType); | ||
if (!lhs) return null; | ||
const rhs = context.parse(args[2], 2, ValueType); | ||
if (!rhs) return null; | ||
|
||
if (!isComparableType(lhs.type) && !isComparableType(rhs.type)) { | ||
return context.error(`Expected at least one argument to be a string, number, boolean, or null, but found (${toString(lhs.type)}, ${toString(rhs.type)}) instead.`); | ||
} | ||
|
||
if (lhs.type.kind !== rhs.type.kind && lhs.type.kind !== 'value' && rhs.type.kind !== 'value') { | ||
return context.error(`Cannot compare ${toString(lhs.type)} and ${toString(rhs.type)}.`); | ||
} | ||
|
||
return new Equals(op, lhs, rhs); | ||
} | ||
|
||
eachChild(fn: (Expression) => void) { | ||
fn(this.lhs); | ||
fn(this.rhs); | ||
} | ||
|
||
possibleOutputs() { | ||
return [true, false]; | ||
} | ||
} | ||
|
||
module.exports = Equals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"expression": ["==", ["get", "x"], ["literal", [1]]], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "Expected at least one argument to be a string, number, boolean, or null, but found (value, array<number, 1>) instead." | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"expression": ["==", ["get", "x"], ["to-color", "red"]], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "Expected at least one argument to be a string, number, boolean, or null, but found (value, color) instead." | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/integration/expression-tests/equal/null-lhs/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"expression": ["==", null, ["get", "x"]], | ||
"inputs": [[{}, {"properties": {}}], [{}, {"properties": {"x": 1}}]], | ||
"expected": { | ||
"outputs": [true, false], | ||
"compiled": { | ||
"result": "success", | ||
"isZoomConstant": true, | ||
"isFeatureConstant": false, | ||
"type": "boolean" | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/integration/expression-tests/equal/null-rhs/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"expression": ["==", null, ["get", "x"]], | ||
"inputs": [[{}, {"properties": {}}], [{}, {"properties": {"x": 1}}]], | ||
"expected": { | ||
"outputs": [true, false], | ||
"compiled": { | ||
"result": "success", | ||
"isZoomConstant": true, | ||
"isFeatureConstant": false, | ||
"type": "boolean" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"expression": ["==", ["get", "x"], ["literal", {}]], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "Expected at least one argument to be a string, number, boolean, or null, but found (value, object) instead." | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
test/integration/expression-tests/equal/untagged/test.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"expression": ["==", ["get", "x"], ["get", "y"]], | ||
"inputs": [ | ||
[{}, {"properties": {"x": 0, "y": 0}}], | ||
[{}, {"properties": {"x": "0", "y": "0"}}], | ||
[{}, {"properties": {"x": 0, "y": false}}], | ||
[{}, {"properties": {"x": 0, "y": "0"}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "Expected at least one argument to be a string, number, boolean, or null, but found (value, value) instead." | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
test/integration/expression-tests/not_equal/untagged/test.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.