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

[master] Fix style-spec isolation for within expression #9522

Merged
merged 2 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/style-spec/expression/definitions/within.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ import type {Expression} from '../expression';
import type ParsingContext from '../parsing_context';
import type EvaluationContext from '../evaluation_context';
import type {GeoJSON, GeoJSONPolygon, GeoJSONMultiPolygon} from '@mapbox/geojson-types';
import MercatorCoordinate from '../../../geo/mercator_coordinate';
import EXTENT from '../../../data/extent';
import Point from '@mapbox/point-geometry';
import type {CanonicalTileID} from '../../../source/tile_id';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care that it's also importing types from outside the style-spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, coz they are completely ignored by module resolution, its just used by flow to typecheck.


type GeoJSONPolygons =| GeoJSONPolygon | GeoJSONMultiPolygon;

// minX, minY, maxX, maxY
type BBox = [number, number, number, number];
const EXTENT = 8192;

function updateBBox(bbox: BBox, coord: Point) {
bbox[0] = Math.min(bbox[0], coord[0]);
bbox[1] = Math.min(bbox[1], coord[1]);
bbox[2] = Math.max(bbox[2], coord[0]);
bbox[3] = Math.max(bbox[3], coord[1]);
}

function mercatorXfromLng(lng: number) {
return (180 + lng) / 360;
}

function mercatorYfromLat(lat: number) {
return (180 - (180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360)))) / 360;
}

function boxWithinBox(bbox1: BBox, bbox2: BBox) {
if (bbox1[0] <= bbox2[0]) return false;
if (bbox1[2] >= bbox2[2]) return false;
Expand All @@ -32,9 +40,10 @@ function boxWithinBox(bbox1: BBox, bbox2: BBox) {
}

function getTileCoordinates(p, canonical: CanonicalTileID) {
const coord = MercatorCoordinate.fromLngLat({lng: p[0], lat: p[1]}, 0);
const x = mercatorXfromLng(p[0]);
const y = mercatorYfromLat(p[1]);
const tilesAtZoom = Math.pow(2, canonical.z);
return [Math.round(coord.x * tilesAtZoom * EXTENT), Math.round(coord.y * tilesAtZoom * EXTENT)];
return [Math.round(x * tilesAtZoom * EXTENT), Math.round(y * tilesAtZoom * EXTENT)];
}

function onBoundary(p, p1, p2) {
Expand Down
20 changes: 20 additions & 0 deletions src/style-spec/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import replace from 'rollup-plugin-replace';
import buble from 'rollup-plugin-buble';
import resolve from 'rollup-plugin-node-resolve';
Expand All @@ -14,6 +15,8 @@ const transforms = {
modules: esm ? false : undefined
};

const ROOT_DIR = __dirname;

const config = [{
input: `${__dirname}/style-spec.js`,
output: {
Expand All @@ -23,6 +26,23 @@ const config = [{
sourcemap: true
},
plugins: [
{
name: 'dep-checker',
resolveId(source, importer) {
// Some users reference modules within style-spec package directly, instead of the bundle
// This means that files within the style-spec package should NOT import files from the parent mapbox-gl-js tree.
// This check will cause the build to fail on CI allowing these issues to be caught.
if (importer && !importer.includes('node_modules')) {
const resolvedPath = path.join(importer, source);
const fromRoot = path.relative(ROOT_DIR, resolvedPath);
if (fromRoot.length > 2 && fromRoot.slice(0, 2) === '..') {
throw new Error(`Module ${importer} imports ${source} from outside the style-spec package root directory.`);
}
}

return null;
}
},
// https://github.com/zaach/jison/issues/351
replace({
include: /\/jsonlint-lines-primitives\/lib\/jsonlint.js/,
Expand Down