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(js): Fix a variety of type definitions that will break in future Flow versions #4749

Merged
merged 3 commits into from
Jan 15, 2020

Conversation

mcous
Copy link
Contributor

@mcous mcous commented Jan 14, 2020

overview

Flow v0.111 (latest: 0.115, ours: 0.110.1) introduced drastic changes to how object spreads are typed. If we upgrade, roughly 300 individual flow errors are introduced by ~150 lines in about ~50 files.

In my examination, we can break these cases down into a few categories:

  1. Problems solved by strict object typing
  2. Actually incorrect code
  3. Problems in code that is deprecated anyway
  4. New errors caused by the fact the Flow no longer allows union types as the indexer in object types

changelog

This PR attempts to...

  • Make object types exact where it helps (1)
  • Fix the most trivial instances of (2)
    • Trivial meaning 1 or 2 line fixes that were ideally already covered by tests
  • Remove any unnecessary existing $FlowFixMes

...without actually updating the version of Flow we use.

review requests

Point (4) above is a gnarly one. Basically, we do a lot of stuff like this:

type Mount = 'left' | 'right'

type StateByMount = {| [Mount]: boolean |} 

Moving forward with Flow, this won't work anymore. (Aside: this also doesn't work with TypeScript, but TS has something else called Mapped Types which we would use instead)

This makes a whole host of things we do a lot stop working. Flow's recommendation moving forward to is explicitly define the types at the get-go...

type StateByMount = {|
  left: boolean,
  right: boolean,
|}

...which works for some stuff, but is onerous for others.

Flow v0.115, however, is quite fast. So I think organizationally, we have some decisions to make regarding:

remaining flow errors

After this PR is applied, updating flow-bin and suppressing all errors results in:

  • 232 new Flow errors suppressed by...
  • 97 new $FlowFixMes in...
  • App - 6 files
    • app/src/components/DeckMap/index.js
    • app/src/components/JogControls/index.js
    • app/src/components/RobotSettings/SelectNetwork/ConnectForm.js
    • app/src/components/calibrate-pipettes/Pipettes.js
    • app/src/pipettes/selectors.js
    • app/src/robot/reducer/calibration.js
  • Components - 2 files
    • components/src/deck/RobotCoordsForeignDiv.js
    • components/src/tooltips/Tooltip.js
  • Protocol Designer - 22 files
    • protocol-designer/src/components/SettingsPage/FeatureFlagCard/FeatureFlagCard.js
    • protocol-designer/src/components/StepEditForm/fields/TipPosition/TipPositionModal.js
    • protocol-designer/src/components/modals/FilePipettesModal/index.js
    • protocol-designer/src/file-data/selectors/fileCreator.js
    • protocol-designer/src/labware-ingred/reducers/index.js
    • protocol-designer/src/labware-ingred/selectors.js
    • protocol-designer/src/load-file/migration/1_1_0.js
    • protocol-designer/src/persist.js
    • protocol-designer/src/step-forms/reducers/index.js
    • protocol-designer/src/step-forms/selectors/index.js
    • protocol-designer/src/step-generation/getNextRobotStateAndWarnings/dispenseUpdateLiquidState.js
    • protocol-designer/src/step-generation/test-with-flow/fixtures/robotStateFixtures.js
    • protocol-designer/src/step-generation/test-with-flow/mix.test.js
    • protocol-designer/src/step-generation/test-with-flow/transfer.test.js
    • protocol-designer/src/step-generation/utils/misc.js
    • protocol-designer/src/steplist/formLevel/generateNewForm.js
    • protocol-designer/src/steplist/formLevel/handleFormChange/dependentFieldsUpdateMix.js
    • protocol-designer/src/steplist/formLevel/handleFormChange/dependentFieldsUpdateMoveLiquid.js
    • protocol-designer/src/steplist/generateSubsteps.js
    • protocol-designer/src/steplist/substepTimeline.js
    • protocol-designer/src/tutorial/reducers.js
    • protocol-designer/src/ui/steps/actions/actions.js

@mcous mcous requested a review from a team January 14, 2020 20:03
@codecov
Copy link

codecov bot commented Jan 14, 2020

Codecov Report

Merging #4749 into edge will increase coverage by 0.21%.
The diff coverage is 25.64%.

Impacted file tree graph

@@            Coverage Diff             @@
##             edge    #4749      +/-   ##
==========================================
+ Coverage   57.96%   58.18%   +0.21%     
==========================================
  Files         956      956              
  Lines       26189    26467     +278     
==========================================
+ Hits        15181    15400     +219     
- Misses      11008    11067      +59
Impacted Files Coverage Δ
...pp/src/components/CalibrateDeck/ConfirmPosition.js 0% <ø> (ø) ⬆️
protocol-designer/src/analytics/fullstory.js 0% <ø> (ø) ⬆️
labware-library/src/components/ui/ClickableIcon.js 0% <ø> (ø) ⬆️
components/src/forms/InputField.js 100% <ø> (ø) ⬆️
components/src/forms/SelectField.js 13.63% <ø> (ø) ⬆️
...c/components/RobotSettings/SelectNetwork/fields.js 0% <ø> (ø) ⬆️
components/src/structure/PageTabs.js 100% <ø> (ø) ⬆️
...pp/src/components/CalibrateLabware/ProceedToRun.js 0% <ø> (ø) ⬆️
protocol-designer/src/components/FilePage.js 0% <ø> (ø) ⬆️
app/src/analytics/make-event.js 72.13% <ø> (ø) ⬆️
... and 35 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 1f06422...bc77e31. Read the comment docs.

@mcous mcous changed the title Chore type fixes refactor(js): Fix a variety of type definitions that will break in future Flow versions Jan 14, 2020
@@ -49,7 +50,7 @@ function ConfirmPositionContents(props: Props) {
{...{ labware, calibrator, calibrateToBottom, useCenteredTroughs }}
buttonText={confirmButtonText}
/>
<JogControls {...props} />
<JogControls jog={jog} />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

<JogControls> only takes the one prop, so this code change is safe

<Icon
name={'alert-circle'}
className={styles.robot_item_icon}
disabled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

<Icon> has no prop named disabled

const labwareType = robotSelectors.labwareType(props)
const { onClick, ...labware } = props
const { name, definition, slot } = labware
const labwareType = robotSelectors.labwareType(labware)
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 change was driven by switching Labware to an exact object type. This minor code change keeps that all behaving

'debug',
'silly',
]
type Logger = {|
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 change demonstrates what I was talking about in my PR description. The app logger is simple enough that I figured I'd include this change as an example. plus, this will make the inevitable TS port a little easier

@@ -54,7 +54,6 @@ function LabwareList(props: Props) {
modulesBySlot[lw.slot].name
}
isDisabled={disabled}
confirmed={lw.confirmed}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

lw was already being spread in, so this line was unnecessary

@@ -68,8 +69,7 @@ export type DeckCalStartState = ApiCall<DeckStartRequest, DeckStartResponse>
export type DeckCalCommandState = ApiCall<DeckCalRequest, DeckCalResponse>

type RobotCalState = {
'calibration/deck/start'?: DeckCalStartState,
'calibration/deck'?: DeckCalCommandState,
[string]: any,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The typing on this state was an illusion and this file is super deprecated, so this just makes the danger more explicit. Unit tests are already in place for this state


export type RobotApiState = $Shape<{|
...NetworkingState,
[path: string]: any,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same story here: types were an illusion

@@ -48,7 +48,6 @@ function mergeProps(

return {
...passThruProps,
...dispatchProps,
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 just spread bare dispatch into the props of the component, which was not used

@mcous mcous marked this pull request as ready for review January 14, 2020 21:40
@@ -168,7 +168,7 @@ const LabwareSelectionModal = (props: Props) => {
>(
defs,
(acc, def: $Values<typeof defs>) => {
const category = def.metadata.displayCategory
const category: string = def.metadata.displayCategory
Copy link
Contributor

@IanLondon IanLondon Jan 15, 2020

Choose a reason for hiding this comment

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

I wonder why it's not inferring this one?

EDIT: Oh it's string | LabwareDisplayCategory and enum type isn't a valid key for object indexing in new flow 😠 ...what the heck, Flow?

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's inferring string | LabwareCategory where LabwareCategory is a union type that makes Flow mad at its usage in a computed properly below this line

Copy link
Contributor

@IanLondon IanLondon 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 doing this! Looks good, only change I'd like is to protocol-designer/src/step-forms/selectors/index.js which is just a slightly different workaround

@mcous mcous merged commit 11c0f76 into edge Jan 15, 2020
@mcous mcous deleted the chore_type-fixes branch January 15, 2020 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants