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

refactor(frontend): move utils to TypeScript #9820

Merged
merged 3 commits into from May 20, 2020
Merged

refactor(frontend): move utils to TypeScript #9820

merged 3 commits into from May 20, 2020

Conversation

ChristianMurphy
Copy link
Contributor

SUMMARY

Migrate several utility functions to typescript

TEST PLAN

Pass tsc and test scripts

ADDITIONAL INFORMATION

@codecov-io
Copy link

codecov-io commented May 16, 2020

Codecov Report

Merging #9820 into master will increase coverage by 4.72%.
The diff coverage is 52.94%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9820      +/-   ##
==========================================
+ Coverage   66.16%   70.89%   +4.72%     
==========================================
  Files         585      587       +2     
  Lines       30427    30455      +28     
  Branches     3152     3168      +16     
==========================================
+ Hits        20133    21590    +1457     
+ Misses      10113     8753    -1360     
+ Partials      181      112      -69     
Flag Coverage Δ
#cypress 53.57% <50.00%> (?)
#javascript 59.31% <52.94%> (+0.05%) ⬆️
#python 71.02% <ø> (-0.01%) ⬇️
Impacted Files Coverage Δ
...ntend/src/dashboard/util/backgroundStyleOptions.ts 100.00% <ø> (ø)
...rset-frontend/src/dashboard/util/componentTypes.ts 100.00% <ø> (ø)
superset-frontend/src/dashboard/util/constants.ts 100.00% <ø> (ø)
...set-frontend/src/dashboard/util/getLocationHash.ts 100.00% <ø> (ø)
...ntend/src/dashboard/util/getRevertedFilterScope.ts 11.76% <0.00%> (ø)
...-frontend/src/dashboard/util/headerStyleOptions.ts 100.00% <ø> (ø)
...set-frontend/src/dashboard/util/resizableConfig.ts 100.00% <ø> (ø)
...perset-frontend/src/dashboard/util/isValidChild.ts 86.66% <75.00%> (ø)
...rontend/src/dashboard/util/componentIsResizable.ts 100.00% <100.00%> (ø)
...ontend/src/dashboard/util/getDashboardFilterKey.ts 100.00% <100.00%> (ø)
... and 156 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea9b7f2...0925353. Read the comment docs.

Copy link
Member

@etr2460 etr2460 left a comment

Choose a reason for hiding this comment

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

Thanks for the typing! Only one major comment about not needing to export types in most (all?) of these files.

Also, @graceguo-supercat would you mind taking a look since you've worked on most of the dashboard util functions recently? Specifically if the types for isValidChild look good

@codecov-commenter
Copy link

codecov-commenter commented May 19, 2020

Codecov Report

Merging #9820 into master will decrease coverage by 1.55%.
The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9820      +/-   ##
==========================================
- Coverage   66.16%   64.60%   -1.56%     
==========================================
  Files         585      537      -48     
  Lines       30427    29412    -1015     
  Branches     3152     2883     -269     
==========================================
- Hits        20133    19003    -1130     
- Misses      10113    10230     +117     
+ Partials      181      179       -2     
Flag Coverage Δ
#cypress 54.02% <50.00%> (?)
#javascript ?
#python 71.27% <ø> (+0.24%) ⬆️
Impacted Files Coverage Δ
...rset-frontend/src/dashboard/util/componentTypes.ts 100.00% <ø> (ø)
superset-frontend/src/dashboard/util/constants.ts 100.00% <ø> (ø)
...set-frontend/src/dashboard/util/getLocationHash.ts 100.00% <ø> (ø)
...ntend/src/dashboard/util/getRevertedFilterScope.ts 0.00% <0.00%> (ø)
...set-frontend/src/dashboard/util/resizableConfig.ts 100.00% <ø> (ø)
...perset-frontend/src/dashboard/util/isValidChild.ts 84.61% <75.00%> (ø)
...rontend/src/dashboard/util/componentIsResizable.ts 100.00% <100.00%> (ø)
...ontend/src/dashboard/util/getDashboardFilterKey.ts 100.00% <100.00%> (ø)
...t-frontend/src/dashboard/util/setPeriodicRunner.ts 66.66% <100.00%> (ø)
...uperset-frontend/src/dashboard/util/dnd-reorder.js 0.00% <0.00%> (-100.00%) ⬇️
... and 329 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea9b7f2...4ddab59. Read the comment docs.

C extends keyof ParentMaxDepthLookup[P] = any,
D extends ParentMaxDepthLookup[P][C] = any
>(child: IsValidChildProps): child is ValidChild<P, C, D> {
const { parentType, childType, parentDepth } = child;
if (!parentType || !childType || typeof parentDepth !== 'number') {
Copy link

Choose a reason for hiding this comment

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

since we use TypeScript, is type check here still necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends, I aimed to keep the behavior the same, taking in an unknown and returning a known type.

This entire function could theoretically be completely removed, using a combo of keyof and generics can statically check most of this:

interface ValidChild<
  P extends keyof ParentMaxDepthLookup,
  C extends keyof ParentMaxDepthLookup[P],
  D extends ParentMaxDepthLookup[P][C]
> {
  parentType: P;
  childType: C;
  parentDepth: D;
}

as an intermediate step, I could set parentDepth to be number and remove this check.

thoughts? 💭

Choose a reason for hiding this comment

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

yes, i feel parentDepth should be number type. This intermediate step a little confused me. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@graceguo-supercat I removed the generics which were causing confusion.
The request to make parentDepth a number and remove the typeof check causes a cascade affect where two other test suites fail

Test Suites: 2 failed, 186 passed, 188 total
Tests:       17 failed, 4 skipped, 1040 passed, 1061 total
Snapshots:   12 passed, 12 total
Time:        67.338s, estimated 78s

specifically getDropPosition and isValidChild both expect the number check.

Copy link
Contributor Author

@ChristianMurphy ChristianMurphy left a comment

Choose a reason for hiding this comment

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

narrowing parentType and childType did turn up a bug in the test suite though.


if (i > 0 && shouldTestChild) {
// should test child
if (i > 0 && Array.isArray(childType)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently type narrowing using Array.isArray is limited to when the it is inside an if condition

Comment on lines +144 to +145
if (typeof parentType !== 'string')
throw TypeError('parent must be string');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type narrowing needed due to mixed string and string[] in the array, this should never throw

it(`(${exampleIdx})${getIndentation(
childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType} ❌`, () => {
expect(
isValidChild({
parentDepth,
parentType,
childType,
childType: childType[0],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this wasn't quite testing what it said it was testing, it was seeing if an array matches the string literal at a given level.
This fixes the tests to compare strings to strings.

@graceguo-supercat
Copy link

graceguo-supercat commented May 20, 2020

@ChristianMurphy thank you so much!! I feel this shows the actual significance of converting js to Typescript: find the potential logic flows in our code.

Do you think this PR is ready to merge?

Copy link

@graceguo-supercat graceguo-supercat left a comment

Choose a reason for hiding this comment

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

LGTM

@ChristianMurphy
Copy link
Contributor Author

Do you think this PR is ready to merge?

I think so ✔️

@graceguo-supercat graceguo-supercat merged commit a262ea7 into apache:master May 20, 2020
@ChristianMurphy ChristianMurphy deleted the refactor/move-utils-to-typescript branch May 20, 2020 21:50
auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
* refactor(frontend): move utils to typescript (apache#9101)

* refactor(frontend): don't export interfaces

* test(frontend): update types and test for isValidChild
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.37.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.37.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants