This is an automated email from the ASF dual-hosted git repository.
graceguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
The following commit(s) were added to refs/heads/master by this push:
new a262ea7 refactor(frontend): move utils to TypeScript (#9820)
a262ea7 is described below
commit a262ea7487d4f5090b333d7d6e6517360cb1f5b5
Author: Christian Murphy <[email protected]>
AuthorDate: Wed May 20 14:47:40 2020 -0700
refactor(frontend): move utils to TypeScript (#9820)
* refactor(frontend): move utils to typescript (#9101)
* refactor(frontend): don't export interfaces
* test(frontend): update types and test for isValidChild
---
...izable_spec.js => componentIsResizable_spec.ts} | 0
.../{isValidChild_spec.js => isValidChild_spec.ts} | 12 +++++----
...ndStyleOptions.js => backgroundStyleOptions.ts} | 0
...onentIsResizable.js => componentIsResizable.ts} | 2 +-
.../util/{componentTypes.js => componentTypes.ts} | 0
.../dashboard/util/{constants.js => constants.ts} | 0
...hboardFilterKey.js => getDashboardFilterKey.ts} | 13 +++++++--
.../{getLocationHash.js => getLocationHash.ts} | 0
...tedFilterScope.js => getRevertedFilterScope.ts} | 31 +++++++++++++++-------
...headerStyleOptions.js => headerStyleOptions.ts} | 0
.../util/{isValidChild.js => isValidChild.ts} | 13 +++++++--
.../{resizableConfig.js => resizableConfig.ts} | 0
.../{setPeriodicRunner.js => setPeriodicRunner.ts} | 10 +++++--
13 files changed, 60 insertions(+), 21 deletions(-)
diff --git
a/superset-frontend/spec/javascripts/dashboard/util/componentIsResizable_spec.js
b/superset-frontend/spec/javascripts/dashboard/util/componentIsResizable_spec.ts
similarity index 100%
rename from
superset-frontend/spec/javascripts/dashboard/util/componentIsResizable_spec.js
rename to
superset-frontend/spec/javascripts/dashboard/util/componentIsResizable_spec.ts
diff --git
a/superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.js
b/superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.ts
similarity index 95%
rename from
superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.js
rename to superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.ts
index f8abb5a..d689f1e 100644
--- a/superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.js
+++ b/superset-frontend/spec/javascripts/dashboard/util/isValidChild_spec.ts
@@ -31,7 +31,7 @@ import {
TAB_TYPE as TAB,
} from 'src/dashboard/util/componentTypes';
-const getIndentation = depth =>
+const getIndentation = (depth: number) =>
Array(depth * 3)
.fill('')
.join('-');
@@ -136,12 +136,14 @@ describe('isValidChild', () => {
invalidExamples.forEach((example, exampleIdx) => {
let childDepth = 0;
example.forEach((childType, i) => {
- const shouldTestChild = Array.isArray(childType);
-
- if (i > 0 && shouldTestChild) {
+ // should test child
+ if (i > 0 && Array.isArray(childType)) {
const parentDepth = childDepth - 1;
const parentType = example[i - 1];
+ if (typeof parentType !== 'string')
+ throw TypeError('parent must be string');
+
it(`(${exampleIdx})${getIndentation(
childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType} ❌`, () => {
@@ -149,7 +151,7 @@ describe('isValidChild', () => {
isValidChild({
parentDepth,
parentType,
- childType,
+ childType: childType[0],
}),
).toBe(false);
});
diff --git a/superset-frontend/src/dashboard/util/backgroundStyleOptions.js
b/superset-frontend/src/dashboard/util/backgroundStyleOptions.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/backgroundStyleOptions.js
rename to superset-frontend/src/dashboard/util/backgroundStyleOptions.ts
diff --git a/superset-frontend/src/dashboard/util/componentIsResizable.js
b/superset-frontend/src/dashboard/util/componentIsResizable.ts
similarity index 92%
rename from superset-frontend/src/dashboard/util/componentIsResizable.js
rename to superset-frontend/src/dashboard/util/componentIsResizable.ts
index afff2f9..21d0ef1 100644
--- a/superset-frontend/src/dashboard/util/componentIsResizable.js
+++ b/superset-frontend/src/dashboard/util/componentIsResizable.ts
@@ -18,6 +18,6 @@
*/
import { COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE } from './componentTypes';
-export default function componentIsResizable(entity) {
+export default function componentIsResizable(entity: { type: string }) {
return [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE].indexOf(entity.type) > -1;
}
diff --git a/superset-frontend/src/dashboard/util/componentTypes.js
b/superset-frontend/src/dashboard/util/componentTypes.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/componentTypes.js
rename to superset-frontend/src/dashboard/util/componentTypes.ts
diff --git a/superset-frontend/src/dashboard/util/constants.js
b/superset-frontend/src/dashboard/util/constants.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/constants.js
rename to superset-frontend/src/dashboard/util/constants.ts
diff --git a/superset-frontend/src/dashboard/util/getDashboardFilterKey.js
b/superset-frontend/src/dashboard/util/getDashboardFilterKey.ts
similarity index 80%
rename from superset-frontend/src/dashboard/util/getDashboardFilterKey.js
rename to superset-frontend/src/dashboard/util/getDashboardFilterKey.ts
index e6307c3..26f00b7 100644
--- a/superset-frontend/src/dashboard/util/getDashboardFilterKey.js
+++ b/superset-frontend/src/dashboard/util/getDashboardFilterKey.ts
@@ -16,11 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
-export function getDashboardFilterKey({ chartId, column }) {
+
+interface GetDashboardFilterKeyProps {
+ chartId: string;
+ column: string;
+}
+
+export function getDashboardFilterKey({
+ chartId,
+ column,
+}: GetDashboardFilterKeyProps) {
return `${chartId}_${column}`;
}
-export function getChartIdAndColumnFromFilterKey(key) {
+export function getChartIdAndColumnFromFilterKey(key: string) {
const [chartId, ...parts] = key.split('_');
const column = parts.slice().join('_');
return { chartId: parseInt(chartId, 10), column };
diff --git a/superset-frontend/src/dashboard/util/getLocationHash.js
b/superset-frontend/src/dashboard/util/getLocationHash.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/getLocationHash.js
rename to superset-frontend/src/dashboard/util/getLocationHash.ts
diff --git a/superset-frontend/src/dashboard/util/getRevertedFilterScope.js
b/superset-frontend/src/dashboard/util/getRevertedFilterScope.ts
similarity index 71%
rename from superset-frontend/src/dashboard/util/getRevertedFilterScope.js
rename to superset-frontend/src/dashboard/util/getRevertedFilterScope.ts
index b31f275..9dac058 100644
--- a/superset-frontend/src/dashboard/util/getRevertedFilterScope.js
+++ b/superset-frontend/src/dashboard/util/getRevertedFilterScope.ts
@@ -18,20 +18,33 @@
*/
import { getChartIdAndColumnFromFilterKey } from './getDashboardFilterKey';
+interface FilterScopeMap {
+ [key: string]: number[];
+}
+
+interface GetRevertFilterScopeProps {
+ checked: string[];
+ filterFields: string[];
+ filterScopeMap: FilterScopeMap;
+}
+
export default function getRevertedFilterScope({
checked = [],
filterFields = [],
filterScopeMap = {},
-}) {
- const checkedChartIdsByFilterField = checked.reduce((map, value) => {
- const [chartId, filterField] = value.split(':');
- return {
- ...map,
- [filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)),
- };
- }, {});
+}: GetRevertFilterScopeProps) {
+ const checkedChartIdsByFilterField = checked.reduce<FilterScopeMap>(
+ (map, value) => {
+ const [chartId, filterField] = value.split(':');
+ return {
+ ...map,
+ [filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)),
+ };
+ },
+ {},
+ );
- return filterFields.reduce((map, filterField) => {
+ return filterFields.reduce<FilterScopeMap>((map, filterField) => {
const { chartId } = getChartIdAndColumnFromFilterKey(filterField);
// force display filter_box chart as unchecked, but show checkbox as
disabled
const updatedCheckedIds = (
diff --git a/superset-frontend/src/dashboard/util/headerStyleOptions.js
b/superset-frontend/src/dashboard/util/headerStyleOptions.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/headerStyleOptions.js
rename to superset-frontend/src/dashboard/util/headerStyleOptions.ts
diff --git a/superset-frontend/src/dashboard/util/isValidChild.js
b/superset-frontend/src/dashboard/util/isValidChild.ts
similarity index 91%
rename from superset-frontend/src/dashboard/util/isValidChild.js
rename to superset-frontend/src/dashboard/util/isValidChild.ts
index d90dc4e..70ec4d0 100644
--- a/superset-frontend/src/dashboard/util/isValidChild.js
+++ b/superset-frontend/src/dashboard/util/isValidChild.ts
@@ -105,12 +105,21 @@ const parentMaxDepthLookup = {
[MARKDOWN_TYPE]: {},
};
-export default function isValidChild({ parentType, childType, parentDepth }) {
+interface IsValidChildProps {
+ parentType?: string;
+ childType?: string;
+ parentDepth?: unknown;
+}
+
+export default function isValidChild(child: IsValidChildProps): boolean {
+ const { parentType, childType, parentDepth } = child;
if (!parentType || !childType || typeof parentDepth !== 'number') {
return false;
}
- const maxParentDepth = (parentMaxDepthLookup[parentType] || {})[childType];
+ const maxParentDepth: number | undefined = (parentMaxDepthLookup[
+ parentType
+ ] || {})[childType];
return typeof maxParentDepth === 'number' && parentDepth <= maxParentDepth;
}
diff --git a/superset-frontend/src/dashboard/util/resizableConfig.js
b/superset-frontend/src/dashboard/util/resizableConfig.ts
similarity index 100%
rename from superset-frontend/src/dashboard/util/resizableConfig.js
rename to superset-frontend/src/dashboard/util/resizableConfig.ts
diff --git a/superset-frontend/src/dashboard/util/setPeriodicRunner.js
b/superset-frontend/src/dashboard/util/setPeriodicRunner.ts
similarity index 84%
rename from superset-frontend/src/dashboard/util/setPeriodicRunner.js
rename to superset-frontend/src/dashboard/util/setPeriodicRunner.ts
index 95b84e6..e0207e8 100644
--- a/superset-frontend/src/dashboard/util/setPeriodicRunner.js
+++ b/superset-frontend/src/dashboard/util/setPeriodicRunner.ts
@@ -16,17 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
-const stopPeriodicRender = refreshTimer => {
+const stopPeriodicRender = (refreshTimer?: number) => {
if (refreshTimer) {
clearInterval(refreshTimer);
}
};
+interface SetPeriodicRunnerProps {
+ interval?: number;
+ periodicRender: TimerHandler;
+ refreshTimer?: number;
+}
+
export default function setPeriodicRunner({
interval = 0,
periodicRender,
refreshTimer,
-}) {
+}: SetPeriodicRunnerProps) {
stopPeriodicRender(refreshTimer);
if (interval > 0) {