Skip to content

Commit 5e5707f

Browse files
sruenwgHarelM
andauthored
Fix migrate function to allow transition properties (#1207)
* fix migrate script to allow transition properties * reduce usage of `any` type in visit.ts --------- Co-authored-by: Harel M <[email protected]>
1 parent 90e69ec commit 5e5707f

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/migrate.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as spec from '.';
33
import v8 from './reference/v8.json' with {type: 'json'};
44
import {validateStyle} from './validate_style';
55
import {describe, test, expect} from 'vitest';
6+
import type {StyleSpecification} from './types.g';
67

78
describe('migrate', () => {
89
test('does not migrate from version 5', () => {
@@ -140,4 +141,25 @@ describe('migrate', () => {
140141
});
141142
});
142143

144+
test('migrates successfully when style contains transition properties', () => {
145+
const style: StyleSpecification = {
146+
version: 8,
147+
sources: {},
148+
layers: [{
149+
id: 'layer-with-transition',
150+
type: 'symbol',
151+
source: 'vector-source',
152+
paint: {
153+
'icon-color': 'hsl(100,0.3,.2)',
154+
'icon-opacity-transition': {duration: 0},
155+
},
156+
}],
157+
};
158+
159+
expect(() => migrate(style)).not.toThrow();
160+
expect(style.layers[0].paint).toEqual({
161+
'icon-color': 'hsl(100,30%,20%)',
162+
'icon-opacity-transition': {duration: 0},
163+
});
164+
});
143165
});

src/migrate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function migrate(style: StyleSpecification): StyleSpecification {
3030
}
3131

3232
eachProperty(style, {paint: true, layout: true}, ({value, reference, set}) => {
33-
if (reference.type === 'color') {
33+
if (reference?.type === 'color') {
3434
set(migrateColors(value));
3535
}
3636
});

src/migrate/expressions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export function expressions(style: StyleSpecification) {
2121
}
2222
});
2323

24-
eachProperty(style, {paint: true, layout: true}, ({path, value, reference, set}) => {
25-
if (isExpression(value)) return;
24+
eachProperty(style, {paint: true, layout: true}, ({path, key, value, reference, set}) => {
25+
if (isExpression(value) || key.endsWith('-transition') || reference === null) return;
2626
if (typeof value === 'object' && !Array.isArray(value)) {
2727
set(convertFunction(value, reference));
2828
converted.push(path.join('.'));

src/visit.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import type {
88
DataDrivenPropertyValueSpecification
99
} from './types.g';
1010

11-
function getPropertyReference(propertyName): StylePropertySpecification {
11+
function getPropertyReference(propertyName: string): StylePropertySpecification | null {
1212
for (let i = 0; i < Reference.layout.length; i++) {
1313
for (const key in Reference[Reference.layout[i]]) {
14-
if (key === propertyName) return Reference[Reference.layout[i]][key] as any;
14+
if (key === propertyName) return Reference[Reference.layout[i]][key];
1515
}
1616
}
1717
for (let i = 0; i < Reference.paint.length; i++) {
1818
for (const key in Reference[Reference.paint[i]]) {
19-
if (key === propertyName) return Reference[Reference.paint[i]][key] as any;
19+
if (key === propertyName) return Reference[Reference.paint[i]][key];
2020
}
2121
}
2222

@@ -40,7 +40,7 @@ type PropertyCallback = (
4040
path: [string, 'paint' | 'layout', string]; // [layerid, paint/layout, property key],
4141
key: string;
4242
value: PropertyValueSpecification<unknown> | DataDrivenPropertyValueSpecification<unknown>;
43-
reference: StylePropertySpecification;
43+
reference: StylePropertySpecification | null;
4444
set: (
4545
a: PropertyValueSpecification<unknown> | DataDrivenPropertyValueSpecification<unknown>
4646
) => void;
@@ -55,8 +55,8 @@ export function eachProperty(
5555
},
5656
callback: PropertyCallback
5757
) {
58-
function inner(layer, propertyType: 'paint' | 'layout') {
59-
const properties = (layer[propertyType] as any);
58+
function inner(layer: LayerSpecification, propertyType: 'paint' | 'layout') {
59+
const properties = layer[propertyType];
6060
if (!properties) return;
6161
Object.keys(properties).forEach((key) => {
6262
callback({

0 commit comments

Comments
 (0)