-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve typing for ==, != expressions #5840
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,76 @@ | ||
// @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 error-coalescing coercion expressions "to-number", | ||
* "to-color". Since these coercions can fail at runtime, they accept multiple | ||
* arguments, only evaluating one at a time until one succeeds. | ||
* | ||
* @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 this.lhs.possibleOutputs().concat(this.rhs.possibleOutputs()); | ||
} | ||
} | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
return [true, false]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️