Skip to content
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

Correctly detect expressions and legacy filters for the new in expression #9374

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/style-spec/feature_filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function isExpressionFilter(filter: any) {
return filter.length >= 2 && filter[1] !== '$id' && filter[1] !== '$type';

case 'in':
return filter.length >= 3 && Array.isArray(filter[2]);
return filter.length >= 3 && (typeof filter[1] !== 'string' || Array.isArray(filter[2]));

case '!in':
case '!has':
case 'none':
Expand Down
33 changes: 32 additions & 1 deletion test/unit/style-spec/feature_filter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {test} from '../../util/test';
import createFilter from '../../../src/style-spec/feature_filter';
import {default as createFilter, isExpressionFilter} from '../../../src/style-spec/feature_filter';

import convertFilter from '../../../src/style-spec/feature_filter/convert';

test('filter', t => {
Expand Down Expand Up @@ -71,6 +72,36 @@ test('filter', t => {
t.end();
});

test('legacy filter detection', t => {
t.test('definitely legacy filters', t => {
// Expressions with more than two arguments.
t.notOk(isExpressionFilter(["in", "color", "red", "blue"]));

// Expressions where the second argument is not a string or array.
t.notOk(isExpressionFilter(["in", "value", 42]));
t.notOk(isExpressionFilter(["in", "value", true]));
t.end();
});

t.test('ambiguous value', t => {
// Should err on the side of reporting as a legacy filter. Style authors can force filters
// by using a literal expression as the first argument.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workaround might be worth mentioning in the style specification documentation.

t.notOk(isExpressionFilter(["in", "color", "red"]));
t.end();
});

t.test('definitely expressions', t => {
t.ok(isExpressionFilter(["in", ["get", "color"], "reddish"]));
t.ok(isExpressionFilter(["in", ["get", "color"], ["red", "blue"]]));
t.ok(isExpressionFilter(["in", 42, 42]));
t.ok(isExpressionFilter(["in", true, true]));
t.ok(isExpressionFilter(["in", "red", ["get", "colors"]]));
t.end();
});

t.end();
});

test('convert legacy filters to expressions', t => {
t.beforeEach(done => {
t.stub(console, 'warn');
Expand Down