Skip to content

Commit

Permalink
fix: improve flow typings
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Oct 31, 2018
1 parent 655213d commit ca1a3e8
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 32 deletions.
11 changes: 5 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Check https://circleci.com/docs/2.0/language-javascript/ for more details

version: 2

jobs:
build:
docker:
Expand All @@ -15,9 +16,9 @@ jobs:
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: yarn install

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

# run tests!
- run: yarn lint
- run: yarn typecheck
- run: yarn flow
- run: yarn test
- run: yarn test:coverage
# - run: yarn coverage
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[libs]
./flow-typed
./node_modules/mapbox-gl/flow-typed
./node_modules/mapbox-gl/dist/mapbox-gl.js.flow

[options]
esproposal.class_static_fields=enable
Expand Down
36 changes: 25 additions & 11 deletions flow-typed/types.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
// @flow

import Immutable from 'immutable';
import type Map from 'mapbox-gl/src/ui/map';
import type Popup from 'mapbox-gl/src/ui/popup';
import type Marker from 'mapbox-gl/src/ui/marker';
import type { Map as ImmutableMap } from 'immutable';
import type { MapMouseEvent, MapTouchEvent } from 'mapbox-gl/src/ui/events';

import type {
Map,
StyleSpecification,
SourceSpecification,
LayerSpecification,
MapMouseEvent,
MapTouchEvent
} from 'mapbox-gl';
StyleSpecification,
SourceSpecification
} from 'mapbox-gl/src/style-spec/types';

import type { LngLatBoundsLike } from 'mapbox-gl/src/geo/lng_lat_bounds';

declare type MapboxMap = Map;

declare type MapboxLayer = LayerSpecification;
declare type MapboxPopup = Popup;

declare type MapboxMarker = Marker;

declare type MapboxLngLatBoundsLike = LngLatBoundsLike;

declare type MapboxLayerSpecification = LayerSpecification;

declare type MapboxStyleSpecification = StyleSpecification;

declare type MapboxSourceSpecification = SourceSpecification;

declare type MapStyle = {
toJS: () => StyleSpecification
} & Immutable.Map<string, any>;
} & ImmutableMap<string, any>;

declare type MapSource = {
toJS: () => SourceSpecification
} & Immutable.Map<string, any>;
} & ImmutableMap<string, any>;

declare type MapLayer = {
toJS: () => LayerSpecification
} & Immutable.Map<string, any>;
} & ImmutableMap<string, any>;

declare type Viewport = {
latitude: number,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"scripts": {
"start": "npm run styleguide",
"lint": "eslint src test",
"typecheck": "flow check src",
"typecheck:coverage": "flow-coverage-report -i 'src/**/*.js' -t html",
"flow": "flow check",
"flow:coverage": "flow-coverage-report -i 'src/**/*.js' -t html",
"test": "jest",
"test:coverage": "jest --coverage && codecov",
"build": "rollup -c",
Expand Down
4 changes: 2 additions & 2 deletions src/components/Layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class Layer extends PureComponent<Props> {
}

componentDidMount() {
const map = this._map;
const map: MapboxMap = this._map;
const { layer, before } = this.props;

const mapboxLayer: MapboxLayer = layer.toJS();
const mapboxLayer: MapboxLayerSpecification = layer.toJS();
if (before && map.getLayer(before)) {
map.addLayer(mapboxLayer, before);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/components/MapContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow

import createContext from 'create-react-context';
import type { Context } from 'create-react-context';

Expand Down
2 changes: 2 additions & 0 deletions src/components/MapGL/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

const events = [
'resize',
'remove',
Expand Down
7 changes: 4 additions & 3 deletions src/components/MapGL/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Children, PureComponent, createElement, cloneElement } from 'react';
import { isImmutable } from 'immutable';
import type { Node } from 'react';

import type { EventProps } from './eventProps';

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

/** If set, the map will be constrained to the given bounds. */
maxBounds?: mapboxgl.LngLatBoundsLike,
maxBounds?: MapboxLngLatBoundsLike,

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

const viewportChanged =
newProps.latitude !== center.lat ||
Expand Down
6 changes: 4 additions & 2 deletions src/components/Marker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { render, unmountComponentAtNode } from 'react-dom';
import { PureComponent, createElement } from 'react';
import type { Element } from 'react';
import type { PointLike } from '@mapbox/point-geometry';

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

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

_container: HTMLDivElement;

_marker: mapboxgl.Marker;
_marker: MapboxMarker;

static displayName = 'Marker';

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

// $FlowFixMe
const marker = new mapboxgl.Marker(this._container, { offset });
marker.setLngLat([longitude, latitude]).addTo(this._map);
this._marker = marker;
Expand Down
5 changes: 3 additions & 2 deletions src/components/Popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ type Props = {
* The offset in pixels as a `PointLike` object to apply
* relative to the element's center. Negatives indicate left and up.
*/
offset?: mapboxgl.LngLatLike
offset?: MapboxLngLatBoundsLike
};

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

_popup: mapboxgl.Popup;
_popup: MapboxPopup;

static displayName = 'Popup';

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

// $FlowFixMe
const popup = new mapboxgl.Popup({ offset, closeButton, closeOnClick, anchor });
popup.setLngLat([longitude, latitude]).addTo(this._map);
popup.setDOMContent(div);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

export { default } from './components/MapGL';
export { default as Layer } from './components/Layer';
export { default as Source } from './components/Source';
Expand Down
4 changes: 3 additions & 1 deletion src/utils/capitalizeFirstLetter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function capitalizeFirstLetter(string) {
// @flow

function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/generateEventProps.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

const fs = require('fs');
const path = require('path');
const events = require('../components/MapGL/events');
Expand Down
2 changes: 2 additions & 0 deletions src/utils/mapbox-gl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

const isBrowser = !(
Object.prototype.toString.call(global.process) === '[object process]' && !global.process.browser
);
Expand Down
4 changes: 1 addition & 3 deletions src/utils/queryRenderedFeatures.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @flow

import mapboxgl from './mapbox-gl';

const queryRenderedFeatures = (
map: mapboxgl.Map,
map: MapboxMap,
layerId: string,
position: [number, number],
radius: number
Expand Down

0 comments on commit ca1a3e8

Please sign in to comment.