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

Improve filter specification typings #1390

Merged
merged 14 commits into from
Aug 5, 2022
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
### ✨ Features and improvements

- *...Add new stuff here...*
- Update `icon-padding` symbol layout property to support asymmetric padding [#1289](https://github.com/maplibre/maplibre-gl-js/pull/1289)
- Update `icon-padding` symbol layout property to support asymmetric padding (#1289)
- Added `cooperativeGestures` option when instantiating map to prevent inadvertent scrolling/panning when navigating a page where map is embedded inline (#234)
- Improve filter specification typings (#1390)

### 🐞 Bug fixes

- *...Add new stuff here...*
- Fix compact attribution style when using global CSS that sets `box-sizing: border-box;` ([#1250](https://github.com/maplibre/maplibre-gl-js/pull/1250))
- Fix compact attribution style when using global CSS that sets `box-sizing: border-box;` (#1250)

## 2.2.0-pre.3

Expand Down
182 changes: 148 additions & 34 deletions build/generate-style-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function propertyType(property) {
} else if (properties.supportsZoomExpression(property)) {
return `PropertyValueSpecification<${baseType}>`;
} else if (property.expression) {
return 'ExpressionSpecificationArray';
return 'ExpressionSpecification';
} else {
return baseType;
}
Expand Down Expand Up @@ -128,36 +128,152 @@ export type ResolvedImageSpecification = string;

export type PromoteIdSpecification = {[_: string]: string} | string;

export type FilterSpecificationInputType = string | number | boolean;
export type FilterSpecification =
// Lookup
| ['at', number, (number |string)[]]
| ['get', string, Record<string, unknown>?]
| ['has', string, Record<string, unknown>?]
| ['in', ...FilterSpecificationInputType[], FilterSpecificationInputType | FilterSpecificationInputType[]]
| ['index-of', FilterSpecificationInputType, FilterSpecificationInputType | FilterSpecificationInputType[]]
| ['length', string | string[]]
| ['slice', string | string[], number]
export type ExpressionInputType = string | number | boolean;

export type CollatorExpressionSpecification =
['collator', {
'case-sensitive'?: boolean | ExpressionSpecification,
'diacritic-sensitive'?: boolean | ExpressionSpecification,
locale?: string | ExpressionSpecification}
]; // collator

export type InterpolationSpecification =
| ['linear']
| ['exponential', number | ExpressionSpecification]
| ['cubic-bezier', number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification]

export type ExpressionSpecification =
// types
| ['array', unknown | ExpressionSpecification] // array
| ['array', ExpressionInputType | ExpressionSpecification, unknown | ExpressionSpecification] // array
| ['array', ExpressionInputType | ExpressionSpecification, number | ExpressionSpecification, unknown | ExpressionSpecification] // array
| ['boolean', ...(unknown | ExpressionSpecification)[], unknown | ExpressionSpecification] // boolean
| CollatorExpressionSpecification
| ['format', ...(string | ['image', ExpressionSpecification] | ExpressionSpecification | {'font-scale'?: number | ExpressionSpecification, 'text-font'?: string[] | ExpressionSpecification, 'text-color': ColorSpecification | ExpressionSpecification})[]] // string
| ['image', unknown | ExpressionSpecification] // image
| ['literal', unknown]
| ['number', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // number
| ['number-format', number | ExpressionSpecification, {'locale'?: string | ExpressionSpecification, 'currency'?: string | ExpressionSpecification, 'min-fraction-digits'?: number | ExpressionSpecification, 'max-fraction-digits'?: number | ExpressionSpecification}] // string
| ['object', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // object
| ['string', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // string
| ['to-boolean', unknown | ExpressionSpecification] // boolean
| ['to-color', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // color
| ['to-number', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // number
| ['to-string', unknown | ExpressionSpecification] // string
// feature data
| ['accumulated']
| ['feature-state', string]
| ['geometry-type'] // string
| ['id']
| ['line-progress'] // number
| ['properties'] // object
// lookup
| ['at', number | ExpressionSpecification, ExpressionSpecification]
| ['get', string | ExpressionSpecification, (Record<string, unknown> | ExpressionSpecification)?]
| ['has', string | ExpressionSpecification, (Record<string, unknown> | ExpressionSpecification)?]
| ['in', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification]
| ['index-of', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification] // number
| ['length', string | ExpressionSpecification]
| ['slice', string | ExpressionSpecification, number | ExpressionSpecification]
// Decision
| ['!', FilterSpecification]
| ['!=', string | FilterSpecification, FilterSpecificationInputType]
| ['<', string | FilterSpecification, FilterSpecificationInputType]
| ['<=', string | FilterSpecification, FilterSpecificationInputType]
| ['==', string | FilterSpecification, FilterSpecificationInputType]
| ['>', string | FilterSpecification, FilterSpecificationInputType]
| ['>=', string | FilterSpecification, FilterSpecificationInputType]
| ["all", ...FilterSpecification[], FilterSpecificationInputType]
| ["any", ...FilterSpecification[], FilterSpecificationInputType]
| ["case", ...FilterSpecification[], FilterSpecificationInputType]
| ["coalesce", ...FilterSpecification[], FilterSpecificationInputType]
| ["match", ...FilterSpecification[], FilterSpecificationInputType]
| ["within", ...FilterSpecification[], FilterSpecificationInputType]
// Used in convert.ts
| ["!in", ...FilterSpecification[], FilterSpecificationInputType]
| ["!has", ...FilterSpecification[], FilterSpecificationInputType]
| ["none", ...FilterSpecification[], FilterSpecificationInputType]
// Fallbak for others
| Array<string | FilterSpecification>
| ['!', boolean | ExpressionSpecification] // boolean
| ['!=', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['<', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['<=', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['==', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['>', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['>=', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification, CollatorExpressionSpecification?] // boolean
| ['all', ...(boolean | ExpressionSpecification)[]] // boolean
| ['any', ...(boolean | ExpressionSpecification)[]] // boolean
| ['case', boolean | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(boolean | ExpressionInputType | ExpressionSpecification)[], ExpressionInputType | ExpressionSpecification]
| ['coalesce', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(ExpressionInputType | ExpressionSpecification)[]]
| ['match', ExpressionInputType | ExpressionSpecification,
ExpressionInputType | ExpressionInputType[], ExpressionInputType | ExpressionSpecification,
...(ExpressionInputType | ExpressionInputType[] | ExpressionSpecification)[], // repeated as above
ExpressionInputType]
| ['within', unknown | ExpressionSpecification]
// Ramps, scales, curves
| ['interpolate', InterpolationSpecification,
number | ExpressionSpecification, number | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(number | ExpressionInputType | ExpressionSpecification)[]]
| ['interpolate-hcl', InterpolationSpecification,
number | ExpressionSpecification, number | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(number | ColorSpecification | ExpressionSpecification)[]]
| ['interpolate-lab', InterpolationSpecification,
number | ExpressionSpecification, number | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(number | ColorSpecification | ExpressionSpecification)[]]
| ['step', number | ExpressionSpecification, number | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(number | ExpressionInputType | ExpressionSpecification)[]]
// Variable binding
| ['let', string, ExpressionInputType | ExpressionSpecification, ...(string | ExpressionInputType | ExpressionSpecification)[]]
| ['var', string]
// String
| ['concat', ExpressionInputType | ExpressionSpecification, ExpressionInputType | ExpressionSpecification,
...(ExpressionInputType | ExpressionSpecification)[]] // string
| ['downcase', string | ExpressionSpecification] // string
| ['is-supported-script', string | ExpressionSpecification] // boolean
| ['resolved-locale', CollatorExpressionSpecification] // string
| ['upcase', string | ExpressionSpecification] // string
// Color
| ['rgb', number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification] // color
| ['rgba', number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification, number | ExpressionSpecification]
| ['to-rgba', ColorSpecification | ExpressionSpecification]
// Math
| ['-', number | ExpressionSpecification, (number | ExpressionSpecification)?] // number
| ['*', number | ExpressionSpecification, number | ExpressionSpecification, ...(number | ExpressionSpecification)[]] // number
| ['/', number | ExpressionSpecification, number | ExpressionSpecification] // number
| ['%', number | ExpressionSpecification, number | ExpressionSpecification] // number
| ['^', number | ExpressionSpecification, number | ExpressionSpecification] // number
| ['+', number | ExpressionSpecification, number | ExpressionSpecification, ...(number | ExpressionSpecification)[]] // number
| ['abs', number | ExpressionSpecification] // number
| ['acos', number | ExpressionSpecification] // number
| ['asin', number | ExpressionSpecification] // number
| ['atan', number | ExpressionSpecification] // number
| ['ceil', number | ExpressionSpecification] // number
| ['cos', number | ExpressionSpecification] // number
| ['distance', Record<string, unknown> | ExpressionSpecification] // number
| ['ExpressionSpecification'] // number
| ['floor', number | ExpressionSpecification] // number
| ['ln', number | ExpressionSpecification] // number
| ['ln2'] // number
| ['log10', number | ExpressionSpecification] // number
| ['log2', number | ExpressionSpecification] // number
| ['max', number | ExpressionSpecification, ...(number | ExpressionSpecification)[]] // number
| ['min', number | ExpressionSpecification, ...(number | ExpressionSpecification)[]] // number
| ['pi'] // number
| ['round', number | ExpressionSpecification] // number
| ['sin', number | ExpressionSpecification] // number
| ['sqrt', number | ExpressionSpecification] // number
| ['tan', number | ExpressionSpecification] // number
// Zoom
| ['zoom'] // number
// Heatmap
| ['heatmap-density'] // number

export type ExpressionFilterSpecification = boolean | ExpressionSpecification

export type LegacyFilterSpecification =
// Existential
| ['has', string]
| ['!has', string]
// Comparison
| ['==', string, string | number | boolean]
| ['!=', string, string | number | boolean]
| ['>', string, string | number | boolean]
| ['>=', string, string | number | boolean]
| ['<', string, string | number | boolean]
| ['<=', string, string | number | boolean]
// Set membership
| ['in', string, ...(string | number | boolean)[]]
| ['!in', string, ...(string | number | boolean)[]]
// Combining
| ['all', ...LegacyFilterSpecification[]]
| ['any', ...LegacyFilterSpecification[]]
| ['none', ...LegacyFilterSpecification[]]

export type FilterSpecification = ExpressionFilterSpecification | LegacyFilterSpecification

export type TransitionSpecification = {
duration?: number,
Expand All @@ -181,19 +297,17 @@ export type CompositeFunctionSpecification<T> =
| { type: 'interval', stops: Array<[{zoom: number, value: number}, T]>, property: string, default?: T }
| { type: 'categorical', stops: Array<[{zoom: number, value: string | number | boolean}, T]>, property: string, default?: T };

export type ExpressionSpecificationArray = Array<unknown>;

export type PropertyValueSpecification<T> =
T
| CameraFunctionSpecification<T>
| ExpressionSpecificationArray;
| ExpressionSpecification;

export type DataDrivenPropertyValueSpecification<T> =
T
| CameraFunctionSpecification<T>
| SourceFunctionSpecification<T>
| CompositeFunctionSpecification<T>
| ExpressionSpecificationArray;
| ExpressionSpecification;

${objectDeclaration('StyleSpecification', spec.$root)}

Expand Down
1 change: 1 addition & 0 deletions src/data/bucket/fill_bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ test('FillBucket segmentation', () => {
id: 'test',
type: 'fill',
layout: {},
source: 'source',
paint: {
'fill-color': ['to-color', ['get', 'foo'], '#000']
}
Expand Down
Loading