Skip to content

Commit ca1a3e8

Browse files
committed
fix: improve flow typings
1 parent 655213d commit ca1a3e8

File tree

15 files changed

+59
-32
lines changed

15 files changed

+59
-32
lines changed

.circleci/config.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
33

44
version: 2
5+
56
jobs:
67
build:
78
docker:
@@ -15,9 +16,9 @@ jobs:
1516
# Download and cache dependencies
1617
- restore_cache:
1718
keys:
18-
- v1-dependencies-{{ checksum "package.json" }}
19-
# fallback to using the latest cache if no exact match is found
20-
- v1-dependencies-
19+
- v1-dependencies-{{ checksum "package.json" }}
20+
# fallback to using the latest cache if no exact match is found
21+
- v1-dependencies-
2122

2223
- run: yarn install
2324

@@ -26,9 +27,7 @@ jobs:
2627
- node_modules
2728
key: v1-dependencies-{{ checksum "package.json" }}
2829

29-
# run tests!
3030
- run: yarn lint
31-
- run: yarn typecheck
31+
- run: yarn flow
3232
- run: yarn test
3333
- run: yarn test:coverage
34-
# - run: yarn coverage

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[libs]
88
./flow-typed
99
./node_modules/mapbox-gl/flow-typed
10+
./node_modules/mapbox-gl/dist/mapbox-gl.js.flow
1011

1112
[options]
1213
esproposal.class_static_fields=enable

flow-typed/types.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
// @flow
22

3-
import Immutable from 'immutable';
3+
import type Map from 'mapbox-gl/src/ui/map';
4+
import type Popup from 'mapbox-gl/src/ui/popup';
5+
import type Marker from 'mapbox-gl/src/ui/marker';
6+
import type { Map as ImmutableMap } from 'immutable';
7+
import type { MapMouseEvent, MapTouchEvent } from 'mapbox-gl/src/ui/events';
8+
49
import type {
5-
Map,
6-
StyleSpecification,
7-
SourceSpecification,
810
LayerSpecification,
9-
MapMouseEvent,
10-
MapTouchEvent
11-
} from 'mapbox-gl';
11+
StyleSpecification,
12+
SourceSpecification
13+
} from 'mapbox-gl/src/style-spec/types';
14+
15+
import type { LngLatBoundsLike } from 'mapbox-gl/src/geo/lng_lat_bounds';
1216

1317
declare type MapboxMap = Map;
1418

15-
declare type MapboxLayer = LayerSpecification;
19+
declare type MapboxPopup = Popup;
20+
21+
declare type MapboxMarker = Marker;
22+
23+
declare type MapboxLngLatBoundsLike = LngLatBoundsLike;
24+
25+
declare type MapboxLayerSpecification = LayerSpecification;
26+
27+
declare type MapboxStyleSpecification = StyleSpecification;
28+
29+
declare type MapboxSourceSpecification = SourceSpecification;
1630

1731
declare type MapStyle = {
1832
toJS: () => StyleSpecification
19-
} & Immutable.Map<string, any>;
33+
} & ImmutableMap<string, any>;
2034

2135
declare type MapSource = {
2236
toJS: () => SourceSpecification
23-
} & Immutable.Map<string, any>;
37+
} & ImmutableMap<string, any>;
2438

2539
declare type MapLayer = {
2640
toJS: () => LayerSpecification
27-
} & Immutable.Map<string, any>;
41+
} & ImmutableMap<string, any>;
2842

2943
declare type Viewport = {
3044
latitude: number,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"scripts": {
1818
"start": "npm run styleguide",
1919
"lint": "eslint src test",
20-
"typecheck": "flow check src",
21-
"typecheck:coverage": "flow-coverage-report -i 'src/**/*.js' -t html",
20+
"flow": "flow check",
21+
"flow:coverage": "flow-coverage-report -i 'src/**/*.js' -t html",
2222
"test": "jest",
2323
"test:coverage": "jest --coverage && codecov",
2424
"build": "rollup -c",

src/components/Layer/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ class Layer extends PureComponent<Props> {
9292
}
9393

9494
componentDidMount() {
95-
const map = this._map;
95+
const map: MapboxMap = this._map;
9696
const { layer, before } = this.props;
9797

98-
const mapboxLayer: MapboxLayer = layer.toJS();
98+
const mapboxLayer: MapboxLayerSpecification = layer.toJS();
9999
if (before && map.getLayer(before)) {
100100
map.addLayer(mapboxLayer, before);
101101
} else {

src/components/MapContext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @flow
2+
23
import createContext from 'create-react-context';
34
import type { Context } from 'create-react-context';
45

src/components/MapGL/events.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @flow
2+
13
const events = [
24
'resize',
35
'remove',

src/components/MapGL/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { Children, PureComponent, createElement, cloneElement } from 'react';
44
import { isImmutable } from 'immutable';
55
import type { Node } from 'react';
6+
67
import type { EventProps } from './eventProps';
78

89
import Layer from '../Layer';
@@ -102,7 +103,7 @@ type Props = EventProps & {
102103
refreshExpiredTiles?: boolean,
103104

104105
/** If set, the map will be constrained to the given bounds. */
105-
maxBounds?: mapboxgl.LngLatBoundsLike,
106+
maxBounds?: MapboxLngLatBoundsLike,
106107

107108
/** If `true`, the "scroll to zoom" interaction is enabled. */
108109
scrollZoom?: boolean | Object,
@@ -365,8 +366,8 @@ class MapGL extends PureComponent<Props, State> {
365366
* @param {Props} newProps
366367
*/
367368
_updateMapViewport(newProps: Props): void {
368-
const map: mapboxgl.Map = this._map;
369-
const center: mapboxgl.LngLat = map.getCenter();
369+
const map: MapboxMap = this._map;
370+
const center = map.getCenter();
370371

371372
const viewportChanged =
372373
newProps.latitude !== center.lat ||

src/components/Marker/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { render, unmountComponentAtNode } from 'react-dom';
44
import { PureComponent, createElement } from 'react';
55
import type { Element } from 'react';
6+
import type { PointLike } from '@mapbox/point-geometry';
67

78
import MapContext from '../MapContext';
89
import mapboxgl from '../../utils/mapbox-gl';
@@ -21,15 +22,15 @@ type Props = {
2122
* The offset in pixels as a `PointLike` object to apply
2223
* relative to the element's center. Negatives indicate left and up.
2324
*/
24-
offset?: mapboxgl.PointLike
25+
offset?: PointLike
2526
};
2627

2728
class Marker extends PureComponent<Props> {
2829
_map: MapboxMap;
2930

3031
_container: HTMLDivElement;
3132

32-
_marker: mapboxgl.Marker;
33+
_marker: MapboxMarker;
3334

3435
static displayName = 'Marker';
3536

@@ -43,6 +44,7 @@ class Marker extends PureComponent<Props> {
4344
this._container = document.createElement('div');
4445
render(element, this._container);
4546

47+
// $FlowFixMe
4648
const marker = new mapboxgl.Marker(this._container, { offset });
4749
marker.setLngLat([longitude, latitude]).addTo(this._map);
4850
this._marker = marker;

src/components/Popup/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ type Props = {
4343
* The offset in pixels as a `PointLike` object to apply
4444
* relative to the element's center. Negatives indicate left and up.
4545
*/
46-
offset?: mapboxgl.LngLatLike
46+
offset?: MapboxLngLatBoundsLike
4747
};
4848

4949
class Popup extends PureComponent<Props> {
5050
_map: MapboxMap;
5151

52-
_popup: mapboxgl.Popup;
52+
_popup: MapboxPopup;
5353

5454
static displayName = 'Popup';
5555

@@ -76,6 +76,7 @@ class Popup extends PureComponent<Props> {
7676
const div = document.createElement('div');
7777
render(element, div);
7878

79+
// $FlowFixMe
7980
const popup = new mapboxgl.Popup({ offset, closeButton, closeOnClick, anchor });
8081
popup.setLngLat([longitude, latitude]).addTo(this._map);
8182
popup.setDOMContent(div);

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @flow
2+
13
export { default } from './components/MapGL';
24
export { default as Layer } from './components/Layer';
35
export { default as Source } from './components/Source';

src/utils/capitalizeFirstLetter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function capitalizeFirstLetter(string) {
1+
// @flow
2+
3+
function capitalizeFirstLetter(string: string) {
24
return string.charAt(0).toUpperCase() + string.slice(1);
35
}
46

src/utils/generateEventProps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @flow
2+
13
const fs = require('fs');
24
const path = require('path');
35
const events = require('../components/MapGL/events');

src/utils/mapbox-gl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @flow
2+
13
const isBrowser = !(
24
Object.prototype.toString.call(global.process) === '[object process]' && !global.process.browser
35
);

src/utils/queryRenderedFeatures.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @flow
22

3-
import mapboxgl from './mapbox-gl';
4-
53
const queryRenderedFeatures = (
6-
map: mapboxgl.Map,
4+
map: MapboxMap,
75
layerId: string,
86
position: [number, number],
97
radius: number

0 commit comments

Comments
 (0)