Skip to content

Commit 5010b78

Browse files
meliteleHarelM
andauthored
replace spread operator by coping of properties (#1286)
* replace spread operator by coping of properties * Update CHANGELOG to fix spread operator usage Replaced spread operator in changelog due to esbuild limitations. --------- Co-authored-by: Harel M <[email protected]>
1 parent 966f4b9 commit 5010b78

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- _...Add new stuff here..._
55

66
### 🐞 Bug fixes
7-
- _...Add new stuff here..._
7+
- Replace spread operator (`...`) due to esbuild limitations
88

99
## 24.1.0
1010

src/expression/index.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2-
import {normalizePropertyExpression} from '.'
2+
import {normalizePropertyExpression, StyleExpression} from '.'
33
import {StylePropertySpecification} from '..';
44
import {Color} from './types/color';
55
import {ColorArray} from './types/color_array';
66
import {NumberArray} from './types/number_array';
77
import {Padding} from './types/padding';
8-
import {describe, test, expect} from 'vitest';
8+
import type {Expression} from './expression';
9+
import {describe, test, expect, vi} from 'vitest';
910

1011
function stylePropertySpecification(type): StylePropertySpecification {
1112
return {
@@ -119,4 +120,28 @@ describe('normalizePropertyExpression raw values', () => {
119120
expect(expression.evaluate({zoom: 0}).values).toEqual([1,2,1,2]);
120121
})
121122

123+
});
124+
125+
describe('StyleExpressions', () => {
126+
127+
test('ignore random fields when adding global state ', () => {
128+
const expression = {
129+
evaluate: vi.fn()
130+
} as any as Expression;
131+
const styleExpression = new StyleExpression(expression, {
132+
type: null,
133+
default: 42,
134+
'property-type': 'data-driven',
135+
transition: false
136+
} as StylePropertySpecification, {x: 5} as Record<string, any>);
137+
138+
styleExpression.evaluate({zoom: 10, a: 20, b: 30} as any);
139+
expect(expression.evaluate).toHaveBeenCalled();
140+
const params = (expression.evaluate as any).mock.calls[0][0].globals;
141+
expect(params).toHaveProperty('zoom', 10);
142+
expect(params).toHaveProperty('globalState', {x: 5});
143+
expect(params).not.toHaveProperty('a');
144+
expect(params).not.toHaveProperty('b');
145+
});
146+
122147
});

src/expression/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class StyleExpression {
9393
formattedSection?: FormattedSection
9494
): any {
9595
if (this._globalState) {
96-
globals = {...globals, globalState: this._globalState};
96+
globals = addGlobalState(globals, this._globalState);
9797
}
9898
this._evaluator.globals = globals;
9999
this._evaluator.feature = feature;
@@ -114,7 +114,7 @@ export class StyleExpression {
114114
formattedSection?: FormattedSection
115115
): any {
116116
if (this._globalState) {
117-
globals = {...globals, globalState: this._globalState};
117+
globals = addGlobalState(globals, this._globalState);
118118
}
119119
this._evaluator.globals = globals;
120120
this._evaluator.feature = feature || null;
@@ -196,7 +196,7 @@ export class ZoomConstantExpression<Kind extends EvaluationKind> {
196196
formattedSection?: FormattedSection
197197
): any {
198198
if (this._globalState) {
199-
globals = {...globals, globalState: this._globalState};
199+
globals = addGlobalState(globals, this._globalState);
200200
}
201201
return this._styleExpression.evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection);
202202
}
@@ -210,7 +210,7 @@ export class ZoomConstantExpression<Kind extends EvaluationKind> {
210210
formattedSection?: FormattedSection
211211
): any {
212212
if (this._globalState) {
213-
globals = {...globals, globalState: this._globalState};
213+
globals = addGlobalState(globals, this._globalState);
214214
}
215215
return this._styleExpression.evaluate(globals, feature, featureState, canonical, availableImages, formattedSection);
216216
}
@@ -244,7 +244,7 @@ export class ZoomDependentExpression<Kind extends EvaluationKind> {
244244
formattedSection?: FormattedSection
245245
): any {
246246
if (this._globalState) {
247-
globals = {...globals, globalState: this._globalState};
247+
globals = addGlobalState(globals, this._globalState);
248248
}
249249
return this._styleExpression.evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection);
250250
}
@@ -258,7 +258,7 @@ export class ZoomDependentExpression<Kind extends EvaluationKind> {
258258
formattedSection?: FormattedSection
259259
): any {
260260
if (this._globalState) {
261-
globals = {...globals, globalState: this._globalState};
261+
globals = addGlobalState(globals, this._globalState);
262262
}
263263
return this._styleExpression.evaluate(globals, feature, featureState, canonical, availableImages, formattedSection);
264264
}
@@ -551,3 +551,23 @@ function getDefaultValue(spec: StylePropertySpecification): Value {
551551
return (spec.default === undefined ? null : spec.default);
552552
}
553553
}
554+
555+
function addGlobalState(globals: GlobalProperties, globalState: Record<string, any>): GlobalProperties {
556+
const {
557+
zoom,
558+
heatmapDensity,
559+
elevation,
560+
lineProgress,
561+
isSupportedScript,
562+
accumulated
563+
} = globals ?? {};
564+
return {
565+
zoom,
566+
heatmapDensity,
567+
elevation,
568+
lineProgress,
569+
isSupportedScript,
570+
accumulated,
571+
globalState
572+
};
573+
}

0 commit comments

Comments
 (0)