From 5fc540f8fd6ea828c7cc510bf437d20331ece752 Mon Sep 17 00:00:00 2001 From: "yuqi.pyq" Date: Fri, 25 Oct 2019 14:15:54 +0800 Subject: [PATCH] build: upgrade TS, babel plugin and to support optional chaining --- .vscode/settings.json | 3 +++ babel.config.js | 1 + package.json | 5 ++-- .../core/src/services/layer/StyleAttribute.ts | 8 +++--- .../renderer/passes/PixelPickingPass.ts | 2 +- packages/layers/src/core/BaseLayer.ts | 2 +- .../layers/src/plugins/DataMappingPlugin.ts | 2 +- .../layers/src/plugins/FeatureScalePlugin.ts | 12 ++++----- packages/maps/src/mapbox/index.ts | 3 --- yarn.lock | 25 +++++++++++++++---- 10 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..55712c19f1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/babel.config.js b/babel.config.js index 1375b8ad63..01953ccc2e 100644 --- a/babel.config.js +++ b/babel.config.js @@ -22,6 +22,7 @@ module.exports = (api) => { '@babel/preset-typescript', ], plugins: [ + '@babel/plugin-proposal-optional-chaining', [ '@babel/plugin-proposal-decorators', { diff --git a/package.json b/package.json index 9fbc63461e..13ad39938c 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "devDependencies": { "@babel/cli": "^7.5.5", "@babel/core": "^7.5.5", + "@babel/plugin-proposal-optional-chaining": "^7.6.0", "@babel/preset-env": "^7.5.5", "@babel/preset-react": "^7.0.0", "@babel/preset-typescript": "^7.3.3", @@ -49,7 +50,7 @@ "npm-run-all": "^4.1.5", "postcss": "^7.0.18", "postcss-plugin": "^1.0.0", - "prettier": "^1.14.2", + "prettier": "^1.18.2", "raw-loader": "^1.0.0", "react": "^16.8.6", "react-docgen-typescript-loader": "^3.1.0", @@ -68,7 +69,7 @@ "tslint-config-prettier": "^1.15.0", "tslint-plugin-prettier": "^1.3.0", "tslint-react": "^3.6.0", - "typescript": "^3.6.2", + "typescript": "^3.7.0-beta", "webpack": "^4.17.1", "webpack-cli": "^3.1.0", "webpack-dev-server": "^3.1.7", diff --git a/packages/core/src/services/layer/StyleAttribute.ts b/packages/core/src/services/layer/StyleAttribute.ts index 9ffba518ac..993b6cfc76 100644 --- a/packages/core/src/services/layer/StyleAttribute.ts +++ b/packages/core/src/services/layer/StyleAttribute.ts @@ -53,9 +53,9 @@ export default class StyleAttribute implements IStyleAttribute { /** * 当用户设置的 callback 返回 null 时, 应该返回默认 callback 中的值 */ - if (this!.scale!.callback) { + if (this.scale?.callback) { // 使用用户返回的值处理 - const ret = this!.scale!.callback(params); + const ret = this.scale?.callback(params); if (!isNil(ret)) { return [ret]; } @@ -68,10 +68,10 @@ export default class StyleAttribute implements IStyleAttribute { private defaultCallback = (params: unknown[]): unknown[] => { // 没有 params 的情况,是指没有指定 fields,直接返回配置的 values 常量 if (params.length === 0) { - return this!.scale!.values; + return this.scale?.values || []; } return params.map((param, idx) => { - const scaleFunc = this.scale!.scalers![idx].func; + const scaleFunc = this.scale?.scalers![idx].func; // @ts-ignore const value = scaleFunc(param); return value; diff --git a/packages/core/src/services/renderer/passes/PixelPickingPass.ts b/packages/core/src/services/renderer/passes/PixelPickingPass.ts index b95f2da9d1..9ce74de6f2 100644 --- a/packages/core/src/services/renderer/passes/PixelPickingPass.ts +++ b/packages/core/src/services/renderer/passes/PixelPickingPass.ts @@ -148,7 +148,7 @@ export default class PixelPickingPass implements IPass { ) { this.logger.info('picked'); const pickedFeatureIdx = decodePickingColor(pickedColors); - const rawFeature = this.layer.getSource()!.data!.dataArray[ + const rawFeature = this.layer.getSource()?.data?.dataArray[ pickedFeatureIdx ]; diff --git a/packages/layers/src/core/BaseLayer.ts b/packages/layers/src/core/BaseLayer.ts index 2aecff28ab..938c228c8c 100644 --- a/packages/layers/src/core/BaseLayer.ts +++ b/packages/layers/src/core/BaseLayer.ts @@ -213,7 +213,7 @@ export default class BaseLayer implements ILayer { }; return this; } - public style(options: unknown): ILayer { + public style(options: object): ILayer { // @ts-ignore this.styleOptions = { ...this.styleOptions, diff --git a/packages/layers/src/plugins/DataMappingPlugin.ts b/packages/layers/src/plugins/DataMappingPlugin.ts index 4a2ce7f1b4..886751e6bd 100644 --- a/packages/layers/src/plugins/DataMappingPlugin.ts +++ b/packages/layers/src/plugins/DataMappingPlugin.ts @@ -84,7 +84,7 @@ export default class DataMappingPlugin implements ILayerPlugin { return []; } - const scalers = attribute!.scale!.scalers || []; + const scalers = attribute?.scale?.scalers || []; const params: unknown[] = []; scalers.forEach(({ field }) => { diff --git a/packages/layers/src/plugins/FeatureScalePlugin.ts b/packages/layers/src/plugins/FeatureScalePlugin.ts index 8bd47366c1..65924b533d 100644 --- a/packages/layers/src/plugins/FeatureScalePlugin.ts +++ b/packages/layers/src/plugins/FeatureScalePlugin.ts @@ -71,8 +71,8 @@ export default class FeatureScalePlugin implements ILayerPlugin { this.scaleCache = {}; attributes.forEach((attribute) => { if (attribute.scale) { - attribute!.scale!.scalers = this.parseFields( - attribute!.scale!.field || '', + attribute.scale.scalers = this.parseFields( + attribute.scale.field || '', ).map((field: string) => ({ field, func: this.getOrCreateScale(field, attribute, dataArray), @@ -93,7 +93,7 @@ export default class FeatureScalePlugin implements ILayerPlugin { this.scaleCache[field] = this.createScale(field, dataArray); (this.scaleCache[field] as { range: (c: unknown[]) => void; - }).range(attribute!.scale!.values); + }).range(attribute?.scale?.values || []); return this.scaleCache[field]; } @@ -114,7 +114,7 @@ export default class FeatureScalePlugin implements ILayerPlugin { private createScale(field: string, data?: IParseDataItem[]): unknown { // 首先查找全局默认配置例如 color - const scaleOption: IScale = this.configService.getConfig()!.scales![field]; + const scaleOption: IScale | undefined = this.configService.getConfig()?.scales?.[field]; if (!data || !data.length) { // 数据为空 @@ -122,7 +122,7 @@ export default class FeatureScalePlugin implements ILayerPlugin { ? this.createDefaultScale(scaleOption) : d3.scaleOrdinal([field]); } - const firstValue = data!.find((d) => !isNil(d[field]))![field]; + const firstValue = data.find((d) => !isNil(d[field]))?.[field]; // 常量 Scale if (isNumber(field) || (isNil(firstValue) && !scaleOption)) { return d3.scaleOrdinal([field]); @@ -152,7 +152,7 @@ export default class FeatureScalePlugin implements ILayerPlugin { const cfg: IScale = { type, }; - const values = data!.map((item) => item[field]); + const values = data?.map((item) => item[field]) || []; // 默认类型为 Quantile Scales https://github.com/d3/d3-scale#quantile-scales if (type !== ScaleTypes.CAT && type !== ScaleTypes.QUANTILE) { cfg.domain = extent(values); diff --git a/packages/maps/src/mapbox/index.ts b/packages/maps/src/mapbox/index.ts index 6e0b4f0a10..2ce6f0d59c 100644 --- a/packages/maps/src/mapbox/index.ts +++ b/packages/maps/src/mapbox/index.ts @@ -29,8 +29,6 @@ mapboxgl.accessToken = 'pk.eyJ1IjoieGlhb2l2ZXIiLCJhIjoiY2pxcmc5OGNkMDY3cjQzbG42cXk5NTl3YiJ9.hUC5Chlqzzh0FFd_aEc-uQ'; const LNGLAT_OFFSET_ZOOM_THRESHOLD = 12; -let counter = 1; - /** * AMapService */ @@ -166,7 +164,6 @@ export default class MapboxService implements IMapService { public async init(mapConfig: IMapConfig): Promise { const { id, attributionControl = false, ...rest } = mapConfig; this.$mapContainer = document.getElementById(id); - this.$mapContainer!.classList.add(`${counter++}`); this.viewport = new Viewport(); diff --git a/yarn.lock b/yarn.lock index f394d1ab61..aba3055899 100644 --- a/yarn.lock +++ b/yarn.lock @@ -313,6 +313,14 @@ "@babel/helper-plugin-utils" "^7.0.0" "@babel/plugin-syntax-optional-catch-binding" "^7.2.0" +"@babel/plugin-proposal-optional-chaining@^7.6.0": + version "7.6.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.6.0.tgz#e9bf1f9b9ba10c77c033082da75f068389041af8" + integrity sha512-kj4gkZ6qUggkprRq3Uh5KP8XnE1MdIO0J7MhdDX8+rAbB6dJ2UrensGIS+0NPZAaaJ1Vr0PN6oLUgXMU1uMcSg== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-optional-chaining" "^7.2.0" + "@babel/plugin-proposal-unicode-property-regex@^7.4.4": version "7.4.4" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.4.4.tgz#501ffd9826c0b91da22690720722ac7cb1ca9c78" @@ -378,6 +386,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-optional-chaining@^7.2.0": + version "7.2.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.2.0.tgz#a59d6ae8c167e7608eaa443fda9fa8fa6bf21dff" + integrity sha512-HtGCtvp5Uq/jH/WNUPkK6b7rufnCPLLlDAFN7cmACoIjaOOiXxUt3SswU5loHqrhtqTsa/WoLQ1OQ1AGuZqaWA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-typescript@^7.2.0", "@babel/plugin-syntax-typescript@^7.3.3": version "7.3.3" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.3.3.tgz#a7cc3f66119a9f7ebe2de5383cce193473d65991" @@ -11815,7 +11830,7 @@ prepend-http@^1.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc" integrity sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw= -prettier@^1.14.2, prettier@^1.16.4: +prettier@^1.16.4, prettier@^1.18.2: version "1.18.2" resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== @@ -14755,10 +14770,10 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= -typescript@^3.6.2: - version "3.6.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.2.tgz#105b0f1934119dde543ac8eb71af3a91009efe54" - integrity sha512-lmQ4L+J6mnu3xweP8+rOrUwzmN+MRAj7TgtJtDaXE5PMyX2kCrklhg3rvOsOIfNeAWMQWO2F1GPc1kMD2vLAfw== +typescript@^3.7.0-beta: + version "3.7.0-beta" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.0-beta.tgz#4ad556e0eee14b90ecc39261001690e16e5eeba9" + integrity sha512-4jyCX+IQamrPJxgkABPq9xf+hUN+GWHVxoj+oey1TadCPa4snQl1RKwUba+1dyzYCamwlCxKvZQ3TjyWLhMGBA== ua-parser-js@^0.7.18: version "0.7.20"