From b3f81b04e7342dbf31186cdfced40902e449331f Mon Sep 17 00:00:00 2001 From: YiQianYao <42212176+yiiiiiiqianyao@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:51:50 +0800 Subject: [PATCH] fix: fix code tslint err (#1436) Co-authored-by: shihui --- .../features/line/demos/greatcircleline.tsx | 8 +-- dev-demos/features/line/demos/line.tsx | 8 +-- dev-demos/features/line/demos/linearc.tsx | 8 +-- packages/layers/src/canvas/index.ts | 4 +- packages/layers/src/core/BaseLayer.ts | 7 ++- .../src/plugins/LayerAnimateStylePlugin.ts | 3 +- .../layers/src/plugins/ShaderUniformPlugin.ts | 3 +- .../src/plugins/UpdateStyleAttributePlugin.ts | 3 +- packages/renderer/src/regl/ReglModel.ts | 4 +- packages/source/src/parser/geojsonvt.ts | 49 +++++---------- packages/source/src/parser/json.ts | 15 ++--- packages/source/src/parser/raster.ts | 4 +- packages/source/src/parser/rasterRgb.ts | 4 +- packages/source/src/parser/testTile.ts | 4 +- packages/utils/__tests__/lineAtOffset.spec.ts | 60 +++++++++---------- .../utils/src/lineAtOffset/greatCircle.ts | 24 ++++---- packages/utils/src/lineAtOffset/index.ts | 16 ++--- packages/utils/src/lineAtOffset/line.ts | 24 ++++---- 18 files changed, 120 insertions(+), 128 deletions(-) diff --git a/dev-demos/features/line/demos/greatcircleline.tsx b/dev-demos/features/line/demos/greatcircleline.tsx index 9743ffa665..f212e5bab0 100644 --- a/dev-demos/features/line/demos/greatcircleline.tsx +++ b/dev-demos/features/line/demos/greatcircleline.tsx @@ -89,8 +89,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') @@ -114,8 +114,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') diff --git a/dev-demos/features/line/demos/line.tsx b/dev-demos/features/line/demos/line.tsx index 63a6242956..a905b626f5 100644 --- a/dev-demos/features/line/demos/line.tsx +++ b/dev-demos/features/line/demos/line.tsx @@ -88,8 +88,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') @@ -109,8 +109,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') diff --git a/dev-demos/features/line/demos/linearc.tsx b/dev-demos/features/line/demos/linearc.tsx index 4c2770e696..8692d713e8 100644 --- a/dev-demos/features/line/demos/linearc.tsx +++ b/dev-demos/features/line/demos/linearc.tsx @@ -88,8 +88,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') @@ -111,8 +111,8 @@ export default () => { .source(midPoints, { parser: { type: 'json', - x: '_lng', - y: '_lat', + x: 'lng', + y: 'lat', }, }) .shape('circle') diff --git a/packages/layers/src/canvas/index.ts b/packages/layers/src/canvas/index.ts index 4c67db7f5d..6edb9071fa 100644 --- a/packages/layers/src/canvas/index.ts +++ b/packages/layers/src/canvas/index.ts @@ -20,7 +20,9 @@ export default class CanvasLayer extends BaseLayer { public hide(): ILayer { // 清除画布 - this.layerModel.clearCanvas && this.layerModel?.clearCanvas(); + if (this.layerModel.clearCanvas) { + this.layerModel.clearCanvas(); + } this.updateLayerConfig({ visible: false, diff --git a/packages/layers/src/core/BaseLayer.ts b/packages/layers/src/core/BaseLayer.ts index 32e1a19c55..1d9038ad82 100644 --- a/packages/layers/src/core/BaseLayer.ts +++ b/packages/layers/src/core/BaseLayer.ts @@ -1055,8 +1055,9 @@ export default class BaseLayer if (this.coordCenter === undefined) { const layerCenter = this.layerSource.center; this.coordCenter = layerCenter; - this.mapService?.setCoordCenter && + if (this.mapService.setCoordCenter) { this.mapService.setCoordCenter(layerCenter); + } } this.sourceEvent(); }); @@ -1407,7 +1408,9 @@ export default class BaseLayer } protected reRender() { - this.inited && this.layerService.reRender(); + if (this.inited) { + this.layerService.reRender(); + } } protected splitValuesAndCallbackInAttribute( valuesOrCallback?: unknown[], diff --git a/packages/layers/src/plugins/LayerAnimateStylePlugin.ts b/packages/layers/src/plugins/LayerAnimateStylePlugin.ts index 2f59c84995..96d8109276 100644 --- a/packages/layers/src/plugins/LayerAnimateStylePlugin.ts +++ b/packages/layers/src/plugins/LayerAnimateStylePlugin.ts @@ -8,12 +8,13 @@ export default class LayerAnimateStylePlugin implements ILayerPlugin { layer.hooks.beforeRender.tap('LayerAnimateStylePlugin', () => { // @ts-ignore const animateStatus = layer.animateStatus; - animateStatus && + if (animateStatus) { layer.models.forEach((model: IModel) => { model.addUniforms({ ...layer.layerModel.getAnimateUniforms(), }); }); + } }); } } diff --git a/packages/layers/src/plugins/ShaderUniformPlugin.ts b/packages/layers/src/plugins/ShaderUniformPlugin.ts index cb9dc1661d..94e6b1c6fe 100644 --- a/packages/layers/src/plugins/ShaderUniformPlugin.ts +++ b/packages/layers/src/plugins/ShaderUniformPlugin.ts @@ -98,7 +98,8 @@ export default class ShaderUniformPlugin implements ILayerPlugin { if (layer.coordCenter === undefined) { layer.coordCenter = layer.getSource().center; } - this.mapService.setCoordCenter && + if (this.mapService.setCoordCenter) { this.mapService.setCoordCenter(layer.coordCenter); + } } } diff --git a/packages/layers/src/plugins/UpdateStyleAttributePlugin.ts b/packages/layers/src/plugins/UpdateStyleAttributePlugin.ts index e0ba5e006f..ebe456a8e1 100644 --- a/packages/layers/src/plugins/UpdateStyleAttributePlugin.ts +++ b/packages/layers/src/plugins/UpdateStyleAttributePlugin.ts @@ -26,8 +26,9 @@ export default class UpdateStyleAttributePlugin implements ILayerPlugin { ) { return; } - layer.modelLoaded && + if (layer.modelLoaded) { this.updateStyleAttribute(layer, { styleAttributeService }); + } }); } private updateStyleAttribute( diff --git a/packages/renderer/src/regl/ReglModel.ts b/packages/renderer/src/regl/ReglModel.ts index e7400190fa..a224017bae 100644 --- a/packages/renderer/src/regl/ReglModel.ts +++ b/packages/renderer/src/regl/ReglModel.ts @@ -206,7 +206,9 @@ export default class ReglModel implements IModel { if (!pick) { this.drawCommand(reglDrawProps); } else { - this.drawPickCommand && this.drawPickCommand(reglDrawProps); + if (this.drawPickCommand) { + this.drawPickCommand(reglDrawProps); + } } // this.drawCommand(reglDrawProps); // this.drawPickCommand(reglDrawProps); diff --git a/packages/source/src/parser/geojsonvt.ts b/packages/source/src/parser/geojsonvt.ts index c4202f669e..5d6ae48f91 100644 --- a/packages/source/src/parser/geojsonvt.ts +++ b/packages/source/src/parser/geojsonvt.ts @@ -87,8 +87,8 @@ function GetGeoJSON( let j; function project(line: any[]) { - for (let j = 0; j < line.length; j++) { - const p = line[j]; + for (let index = 0; index < line.length; index++) { + const p = line[index]; if (p[3]) { // 避免重复计算 break; @@ -97,7 +97,7 @@ function GetGeoJSON( const lng = ((p[0] + x0) * 360) / size - 180; const lat = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90; - line[j] = [lng, lat, 0, 1]; + line[index] = [lng, lat, 0, 1]; } } @@ -172,23 +172,26 @@ const getVectorTile = async ( ); features.push(feature); }); + type IGeojsonVT = VectorTileLayer & { + features: Feature[]; + }; + // @ts-ignore + const featureCollection: IGeojsonVT = { + features, + }; - const vectorTile = { + const vectorTile: MapboxVectorTile = { layers: { // Tip: fixed SourceLayer Name - geojsonvt: { - features, - } as VectorTileLayer & { - features: Feature[]; - }, + geojsonvt: featureCollection, }, - } as MapboxVectorTile; + }; resolve(vectorTile); }); }; -function getGeoJSONVTOptions(cfg?: ITileParserCFG) { +function getOption(cfg?: ITileParserCFG) { const defaultOptions = { // geojson-vt default options maxZoom: 14, // max zoom to preserve detail on @@ -206,27 +209,7 @@ function getGeoJSONVTOptions(cfg?: ITileParserCFG) { if (cfg === undefined || typeof cfg.geojsonvtOptions === 'undefined') { return defaultOptions; } else { - cfg.geojsonvtOptions.maxZoom && - (defaultOptions.maxZoom = cfg.geojsonvtOptions.maxZoom); - cfg.geojsonvtOptions.indexMaxZoom && - (defaultOptions.indexMaxZoom = cfg.geojsonvtOptions.indexMaxZoom); - cfg.geojsonvtOptions.indexMaxPoints && - (defaultOptions.indexMaxPoints = cfg.geojsonvtOptions.indexMaxPoints); - cfg.geojsonvtOptions.tolerance && - (defaultOptions.tolerance = cfg.geojsonvtOptions.tolerance); - cfg.geojsonvtOptions.extent && - (defaultOptions.extent = cfg.geojsonvtOptions.extent); - cfg.geojsonvtOptions.buffer && - (defaultOptions.buffer = cfg.geojsonvtOptions.buffer); - cfg.geojsonvtOptions.lineMetrics && - (defaultOptions.lineMetrics = cfg.geojsonvtOptions.lineMetrics); - cfg.geojsonvtOptions.promoteId && - (defaultOptions.promoteId = cfg.geojsonvtOptions.promoteId); - cfg.geojsonvtOptions.generateId && - (defaultOptions.generateId = cfg.geojsonvtOptions.generateId); - cfg.geojsonvtOptions.debug && - (defaultOptions.debug = cfg.geojsonvtOptions.debug); - return defaultOptions; + return { ...defaultOptions, ...cfg.geojsonvtOptions }; } } @@ -234,7 +217,7 @@ export default function geojsonVTTile( data: FeatureCollection, cfg: ITileParserCFG, ): IParserData { - const geojsonOptions = getGeoJSONVTOptions(cfg) as geojsonvt.Options & + const geojsonOptions = getOption(cfg) as geojsonvt.Options & IGeojsonvtOptions; const extent = geojsonOptions.extent || 4096; diff --git a/packages/source/src/parser/json.ts b/packages/source/src/parser/json.ts index 969c28ba35..549e122dcb 100644 --- a/packages/source/src/parser/json.ts +++ b/packages/source/src/parser/json.ts @@ -33,8 +33,7 @@ export default function json(data: IJsonData, cfg: IParserCfg): IParserData { ); }) .forEach((col, index) => { - const _geometry = { ...col[geometry] }; - const rewindGeometry = rewind(_geometry, true); + const rewindGeometry = rewind({ ...col[geometry] }, true); // multi feature 情况拆分 flattenEach( rewindGeometry, @@ -70,11 +69,13 @@ export default function json(data: IJsonData, cfg: IParserCfg): IParserData { if (Array.isArray(coordinates[0]) && !Array.isArray(coordinates[0][0])) { type = 'LineString'; } - const geometry = { - type, - coordinates: [...col[coordinates]], - }; - const rewindGeometry = rewind(geometry, true); + const rewindGeometry = rewind( + { + type, + coordinates: [...col[coordinates]], + }, + true, + ); coords = rewindGeometry.coordinates; } else if (x && y && x1 && y1) { // 起终点数据 diff --git a/packages/source/src/parser/raster.ts b/packages/source/src/parser/raster.ts index 3c5b12f1ee..9a844d48a4 100644 --- a/packages/source/src/parser/raster.ts +++ b/packages/source/src/parser/raster.ts @@ -8,7 +8,9 @@ export default function raster( cfg: IRasterCfg, ): IParserData { const { extent, width, height, min, max, format, operation } = cfg; - let bandData, rasterWidth, rasterHeight; + let bandData; + let rasterWidth; + let rasterHeight; if (format === undefined || isNumberArray(data)) { // 兼容写法 - 用户直接传入解析完的波段数据 bandData = Array.from(data as number[]); diff --git a/packages/source/src/parser/rasterRgb.ts b/packages/source/src/parser/rasterRgb.ts index cfe19f3180..40201949d7 100644 --- a/packages/source/src/parser/rasterRgb.ts +++ b/packages/source/src/parser/rasterRgb.ts @@ -8,7 +8,9 @@ export default function rasterRgb( cfg: IRasterCfg, ): IParserData { const { extent, width, height, min, max, format, operation } = cfg; - let bandData, rasterWidth, rasterHeight; + let bandData; + let rasterWidth; + let rasterHeight; if (format === undefined || isNumberArray(data)) { // 兼容写法 - 用户直接传入解析完的波段数据 bandData = Array.from(data as number[]); diff --git a/packages/source/src/parser/testTile.ts b/packages/source/src/parser/testTile.ts index eb9b10f38a..c6294f70b1 100644 --- a/packages/source/src/parser/testTile.ts +++ b/packages/source/src/parser/testTile.ts @@ -23,7 +23,7 @@ const getVectorTile = async (tile: Tile): Promise => { // | | // minLng/minLat --- maxLng/minLat - const vectorTile = { + const vectorTile: MapboxVectorTile = { layers: { // Tip: fixed SourceLayer Name testTile: ({ @@ -50,7 +50,7 @@ const getVectorTile = async (tile: Tile): Promise => { features: Feature[]; }, }, - } as MapboxVectorTile; + }; resolve(vectorTile); }); diff --git a/packages/utils/__tests__/lineAtOffset.spec.ts b/packages/utils/__tests__/lineAtOffset.spec.ts index 364faca550..234606a24a 100644 --- a/packages/utils/__tests__/lineAtOffset.spec.ts +++ b/packages/utils/__tests__/lineAtOffset.spec.ts @@ -10,7 +10,7 @@ const arcSource = { [100.72265625, 27.994401411046148], ], offset: 0.3, - _id: 0 + id: 0 } ] } @@ -31,10 +31,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 99.66757224332352, - _lat: 36.507847734146466, - _height: 0 + id: 0, + lng: 99.66757224332352, + lat: 36.507847734146466, + height: 0 } ]); }); @@ -87,10 +87,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 99.66757224332352, - _lat: 36.507847734146466, - _height: 0 + id: 0, + lng: 99.66757224332352, + lat: 36.507847734146466, + height: 0 } ]); }); @@ -103,10 +103,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 99.72191962749187, - _lat: 36.54640697914567, - _height: 0 + id: 0, + lng: 99.72191962749187, + lat: 36.54640697914567, + height: 0 } ]); }); @@ -118,12 +118,12 @@ describe('lineAtOffset', () => { [ 99.228515625, 37.43997405227057 ], [ 100.72265625, 27.994401411046148 ] ], + height: 0, + id: 0, + lat: 36.54640697914567, + lng: 99.72191962749187, offset: 0.3, - _id: 0, - _lng: 99.72191962749187, - _lat: 36.54640697914567, - _height: 0 - } + }, ]); }); @@ -135,10 +135,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 99.39897617955312, - _lat: 36.46373143064039, - _height: undefined + id: 0, + lng: 99.39897617955312, + lat: 36.46373143064039, + height: undefined } ]); }); @@ -151,10 +151,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 1.7395272931153063, - _lat: 0.6371821457776072, - _height: 0 + id: 0, + lng: 1.7395272931153063, + lat: 0.6371821457776072, + height: 0 } ]); }); @@ -167,10 +167,10 @@ describe('lineAtOffset', () => { [ 100.72265625, 27.994401411046148 ] ], offset: 0.3, - _id: 0, - _lng: 99.3779296875, - _lat: 36.495416788148134, - _height: 0 + id: 0, + lng: 99.3779296875, + lat: 36.495416788148134, + height: 0 } ]); }); diff --git a/packages/utils/src/lineAtOffset/greatCircle.ts b/packages/utils/src/lineAtOffset/greatCircle.ts index d000d5ebc2..0eca00458f 100644 --- a/packages/utils/src/lineAtOffset/greatCircle.ts +++ b/packages/utils/src/lineAtOffset/greatCircle.ts @@ -43,13 +43,13 @@ function bezier3(arr: Point, t: number) { function getAngularDist(source: Point, target: Point) { const delta = [source[0] - target[0], source[1] - target[1]]; - const sin_half_delta = [Math.sin(delta[0] / 2.0), Math.sin(delta[1] / 2.0)]; // Math.sin(delta / 2.0); + const sinHalfDelta = [Math.sin(delta[0] / 2.0), Math.sin(delta[1] / 2.0)]; // Math.sin(delta / 2.0); const a = - sin_half_delta[1] * sin_half_delta[1] + + sinHalfDelta[1] * sinHalfDelta[1] + Math.cos(source[1]) * Math.cos(target[1]) * - sin_half_delta[0] * - sin_half_delta[0]; + sinHalfDelta[0] * + sinHalfDelta[0]; return 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a)); } @@ -77,15 +77,13 @@ export function interpolate( } const a = Math.sin((1.0 - offset) * angularDist) / Math.sin(angularDist); const b = Math.sin(offset * angularDist) / Math.sin(angularDist); - const sin_source = [Math.sin(source[0]), Math.sin(source[1])]; - const cos_source = [Math.cos(source[0]), Math.cos(source[1])]; - const sin_target = [Math.sin(target[0]), Math.sin(target[1])]; - const cos_target = [Math.cos(target[0]), Math.cos(target[1])]; - const x = - a * cos_source[1] * cos_source[0] + b * cos_target[1] * cos_target[0]; - const y = - a * cos_source[1] * sin_source[0] + b * cos_target[1] * sin_target[0]; - const z = a * sin_source[1] + b * sin_target[1]; + const sinSource = [Math.sin(source[0]), Math.sin(source[1])]; + const cosSource = [Math.cos(source[0]), Math.cos(source[1])]; + const sinTarget = [Math.sin(target[0]), Math.sin(target[1])]; + const cosTarget = [Math.cos(target[0]), Math.cos(target[1])]; + const x = a * cosSource[1] * cosSource[0] + b * cosTarget[1] * cosTarget[0]; + const y = a * cosSource[1] * sinSource[0] + b * cosTarget[1] * sinTarget[0]; + const z = a * sinSource[1] + b * sinTarget[1]; return [ radiansToDegrees(Math.atan2(y, x)), radiansToDegrees(Math.atan2(z, Math.sqrt(x * x + y * y))), diff --git a/packages/utils/src/lineAtOffset/index.ts b/packages/utils/src/lineAtOffset/index.ts index 1b520b0bee..60bb1d10ff 100644 --- a/packages/utils/src/lineAtOffset/index.ts +++ b/packages/utils/src/lineAtOffset/index.ts @@ -7,7 +7,7 @@ export function lineAtOffset(source: Source, option: ILineAtOffset) { const { featureId } = option; let features = source.data.dataArray; if (typeof featureId === 'number') { - features = features.filter(({ _id }: { _id: number }) => _id === featureId); + features = features.filter(({ id }: { id: number }) => id === featureId); } return features.map((feature: any) => { const position = getLineOffsetPosition(feature, option); @@ -46,12 +46,8 @@ function getLineOffsetPosition(feature: any, option: ILineAtOffset) { const source = coordinates[0]; const target = coordinates[1]; - let linetheatOffset; - if (typeof thetaOffset === 'string') { - linetheatOffset = feature[thetaOffset] || 0; - } else { - linetheatOffset = thetaOffset; - } + const linetheatOffset = + typeof thetaOffset === 'string' ? feature[thetaOffset] || 0 : thetaOffset; let calFunc; switch (shape) { @@ -75,8 +71,8 @@ function getLineOffsetPosition(feature: any, option: ILineAtOffset) { ); return { - _lng: lng, - _lat: lat, - _height: height, + lng, + lat, + height, }; } diff --git a/packages/utils/src/lineAtOffset/line.ts b/packages/utils/src/lineAtOffset/line.ts index 92bead5025..ed1d5b09c3 100644 --- a/packages/utils/src/lineAtOffset/line.ts +++ b/packages/utils/src/lineAtOffset/line.ts @@ -18,25 +18,25 @@ export function pathLineAtOffset(coords: Point[], offset: number) { }); } const offsetDistance = totalDistance * offset; - let _lng, _lat; - for (let i = 0; i < cachePoints.length; i++) { - const currentDistance = cachePoints[i].totalDistance; + let lng; + let lat; + for (const point of cachePoints) { + const currentDistance = point.totalDistance; if (currentDistance > offsetDistance) { - const p1 = cachePoints[i].p1; - const p2 = cachePoints[i].p2; + const p1 = point.p1; + const p2 = point.p2; const radius = - (offsetDistance - cachePoints[i].lastTotalDistance) / - cachePoints[i].distance; + (offsetDistance - point.lastTotalDistance) / point.distance; const offsetPoint = mixPoint(p2, p1, radius); - _lng = offsetPoint[0]; - _lat = offsetPoint[1]; + lng = offsetPoint[0]; + lat = offsetPoint[1]; break; } } return { - _lng, - _lat, - _height: 0, + lng, + lat, + height: 0, }; }