Skip to content

Commit

Permalink
refactor(shared-data): port shared-data JS source to TypeScript (#7731)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored May 3, 2021
1 parent e9e7051 commit 11c8328
Show file tree
Hide file tree
Showing 123 changed files with 2,826 additions and 830 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ shared-data/python/**

# flow
flow-typed/npm/**
discovery-client/flow-types/**
components/flow-types/**
shared-data/flow-types/**

# typescript
app-shell/lib/**
Expand Down
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = {
'@typescript-eslint/prefer-nullish-coalescing': 'warn',
'@typescript-eslint/prefer-optional-chain': 'warn',
'@typescript-eslint/restrict-plus-operands': 'warn',
'@typescript-eslint/restrict-template-expressions': 'warn',
},
},
{
Expand All @@ -87,6 +88,7 @@ module.exports = {
'jest/no-disabled-tests': 'error',
'jest/consistent-test-it': 'error',
'@typescript-eslint/consistent-type-assertions': 'off',
'@typescript-eslint/no-var-requires': 'off',

// TODO(mc, 2021-01-29): fix these and remove warning overrides
'jest/no-deprecated-functions': 'warn',
Expand Down
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
; read type signatures from node_modules but ignore flow errors within
.*/node_modules/.*
<PROJECT_ROOT>/components/flow-types/.*
<PROJECT_ROOT>/shared-data/flow-types/.*

[include]

Expand Down
23 changes: 14 additions & 9 deletions app-shell/src/labware/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { validateLabwareFiles, validateNewLabwareFile } from '../validation'

import validLabwareA from '@opentrons/shared-data/labware/fixtures/2/fixture_96_plate.json'
import validLabwareB from '@opentrons/shared-data/labware/fixtures/2/fixture_12_trough.json'
import uncheckedLabwareA from '@opentrons/shared-data/labware/fixtures/2/fixture_96_plate.json'
import uncheckedLabwareB from '@opentrons/shared-data/labware/fixtures/2/fixture_12_trough.json'

import type { CheckedLabwareFile } from '@opentrons/app/src/redux/custom-labware/types'

import type { LabwareDefinition2 } from '@opentrons/shared-data'

const validLabwareA = uncheckedLabwareA as LabwareDefinition2
const validLabwareB = uncheckedLabwareB as LabwareDefinition2

describe('validateLabwareFiles', () => {
it('handles unparseable and invalid labware files', () => {
const files = [
Expand All @@ -28,8 +33,8 @@ describe('validateLabwareFiles', () => {

it('handles valid labware files', () => {
const files = [
{ filename: 'a.json', data: validLabwareA, modified: Date.now() },
{ filename: 'b.json', data: validLabwareB, modified: Date.now() },
{ filename: 'a.json', data: uncheckedLabwareA, modified: Date.now() },
{ filename: 'b.json', data: uncheckedLabwareB, modified: Date.now() },
]

expect(validateLabwareFiles(files)).toEqual([
Expand All @@ -50,9 +55,9 @@ describe('validateLabwareFiles', () => {

it('handles non-unique labware files', () => {
const files = [
{ filename: 'a.json', data: validLabwareA, modified: 3 },
{ filename: 'b.json', data: validLabwareB, modified: 2 },
{ filename: 'c.json', data: validLabwareA, modified: 1 },
{ filename: 'a.json', data: uncheckedLabwareA, modified: 3 },
{ filename: 'b.json', data: uncheckedLabwareB, modified: 2 },
{ filename: 'c.json', data: uncheckedLabwareA, modified: 1 },
]

expect(validateLabwareFiles(files)).toEqual([
Expand Down Expand Up @@ -100,7 +105,7 @@ describe('validateNewLabwareFile', () => {
const existing: CheckedLabwareFile[] = []
const newFile = {
filename: 'a.json',
data: validLabwareA,
data: uncheckedLabwareA,
modified: 42,
}

Expand All @@ -123,7 +128,7 @@ describe('validateNewLabwareFile', () => {
]
const newFile = {
filename: 'a.json',
data: validLabwareA,
data: uncheckedLabwareA,
modified: 21,
}

Expand Down
3 changes: 3 additions & 0 deletions app-shell/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"references": [
{
"path": "../discovery-client"
},
{
"path": "../shared-data"
}
],
"compilerOptions": {
Expand Down
6 changes: 0 additions & 6 deletions app-shell/typings/opentrons__shared-data.d.ts

This file was deleted.

5 changes: 1 addition & 4 deletions app/src/redux/protocol/types.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @flow
// protocol type defs
import type {
JsonProtocolFile,
ProtocolFileV1,
} from '@opentrons/shared-data/protocol'
import type { JsonProtocolFile, ProtocolFileV1 } from '@opentrons/shared-data'

import typeof { TYPE_JSON, TYPE_PYTHON, TYPE_ZIP } from './constants'

Expand Down
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
alias: {
'^@opentrons/discovery-client$': `@opentrons/discovery-client/src/index.ts`,
'^@opentrons/components$': `@opentrons/components/src/index.ts`,
'^@opentrons/shared-data$': `@opentrons/shared-data/js/index.ts`,
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions components/flow-types/deck/Module.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type ModuleProps = {|
slot: DeckSlot,

/**
* USB port details of this module
*/
* USB port detail of the connected module
*/
usbInfoString?: string,
|};
declare export function Module(props: ModuleProps): React$Node;
7 changes: 2 additions & 5 deletions components/flow-types/instrument/InstrumentDiagram.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
* @flow
*/

import type {
PipetteNameSpecs,
PipetteModelSpecs,
} from "@opentrons/shared-data";
import type { PipetteNameSpecs } from "@opentrons/shared-data";
import type { Mount } from "../robot-types";
export type InstrumentDiagramProps = {|
pipetteSpecs?: PipetteNameSpecs | PipetteModelSpecs | null,
pipetteSpecs?: Pick<PipetteNameSpecs, "displayCategory" | "channels"> | null,
className?: string,
mount: Mount,
|};
Expand Down
10 changes: 5 additions & 5 deletions components/flow-types/instrument/InstrumentInfo.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
*/

import * as React from "react";
import type {
PipetteNameSpecs,
PipetteModelSpecs,
} from "@opentrons/shared-data";
import type { Mount } from "../robot-types";
import type { InstrumentDiagramProps } from "./InstrumentDiagram";
export type InstrumentInfoProps = {|
/**
* 'left' or 'right'
Expand Down Expand Up @@ -40,7 +37,10 @@ export type InstrumentInfoProps = {|
/**
* specs of mounted pipette
*/
pipetteSpecs?: PipetteModelSpecs | PipetteNameSpecs | null | void,
pipetteSpecs?: $PropertyType<
InstrumentDiagramProps,
"pipetteSpecs"
> | null | void,

/**
* classes to apply
Expand Down
4 changes: 2 additions & 2 deletions components/flow-types/lists/CollapsibleItem.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import * as React from "react";
export type CollapsibleItemProps = {|
/**
* header
*/
* header
*/
header?: string,

/**
Expand Down
6 changes: 3 additions & 3 deletions components/flow-types/lists/ListItem.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ declare type ListItemProps = {|
declare export var ListItem: React.ForwardRefExoticComponent<
Pick<
ListItemProps,
| "exact"
| "children"
| "url"
| "activeClassName"
| "exact"
| "className"
| "aria-describedby"
| "onClick"
| "onMouseEnter"
| "onMouseLeave"
| "onPointerEnter"
| "onPointerLeave"
| "isDisabled"
| "iconName"
| "isDisabled"
| "activeClassName"
> &
React.RefAttributes<HTMLLIElement>
>;
Expand Down
6 changes: 4 additions & 2 deletions components/flow-types/robot-types.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
* @flow
*/

export type Channels = 1 | 8;
export type Mount = "left" | "right";
declare export {
PipetteChannels as Channels,
PipetteMount as Mount,
} from "@opentrons/shared-data";
2 changes: 2 additions & 0 deletions components/src/deck/labwareInternals/StaticLabware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const TipDecoration = React.memo(function TipDecoration(props: {
well: LabwareWell
}) {
const { well } = props
// @ts-expect-error(mc, 2021-04-27): refine well type before accessing `diameter`
if (well.diameter) {
// @ts-expect-error(mc, 2021-04-27): refine well type before accessing `diameter`
const radius = well.diameter / 2
return (
<circle className={styles.tip} cx={well.x} cy={well.y} r={radius - 1} />
Expand Down
10 changes: 4 additions & 6 deletions components/src/instrument/InstrumentDiagram.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import * as React from 'react'
import cx from 'classnames'
import type {
PipetteNameSpecs,
PipetteModelSpecs,
} from '@opentrons/shared-data'

import type { Mount } from '../robot-types'
import singleSrc from './pipetteSingle.png'
import multiSrc from './pipetteMulti.png'
import singleGEN2Src from './pipetteGEN2Single.png'
import multiGEN2Src from './pipetteGEN2Multi.png'
import styles from './instrument.css'

import type { PipetteNameSpecs } from '@opentrons/shared-data'
import type { Mount } from '../robot-types'

export interface InstrumentDiagramProps {
pipetteSpecs?: PipetteNameSpecs | PipetteModelSpecs | null
pipetteSpecs?: Pick<PipetteNameSpecs, 'displayCategory' | 'channels'> | null
className?: string
mount: Mount
}
Expand Down
13 changes: 5 additions & 8 deletions components/src/instrument/InstrumentInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import * as React from 'react'
import cx from 'classnames'
import type {
PipetteNameSpecs,
PipetteModelSpecs,
} from '@opentrons/shared-data'

import type { Mount } from '../robot-types'
import { InfoItem } from './InfoItem'
import { InstrumentDiagram } from './InstrumentDiagram'

import styles from './instrument.css'

import type { Mount } from '../robot-types'
import type { InstrumentDiagramProps } from './InstrumentDiagram'

export interface InstrumentInfoProps {
/** 'left' or 'right' */
mount: Mount
/** if true, show labels 'LEFT PIPETTE' / 'RIGHT PIPETTE' */
showMountLabel?: boolean | null | undefined
showMountLabel?: boolean | null
/** human-readable description, eg 'p300 Single-channel' */
description: string
/** paired tiprack model */
tiprackModel?: string
/** if disabled, pipette & its info are grayed out */
isDisabled: boolean
/** specs of mounted pipette */
pipetteSpecs?: PipetteModelSpecs | PipetteNameSpecs | null | undefined
pipetteSpecs?: InstrumentDiagramProps['pipetteSpecs'] | null
/** classes to apply */
className?: string
/** classes to apply to the info group child */
Expand Down
4 changes: 3 additions & 1 deletion components/src/instrument/PipetteSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export interface PipetteSelectProps {
const NONE = 'None'
const OPTION_NONE = { value: '', label: NONE }

const PIPETTE_SORT = ['maxVolume', 'channels']
const PIPETTE_SORT = ['maxVolume', 'channels'] as const

// @ts-expect-error(mc, 2021-04-27): use TS type guard for filter
const allPipetteNameSpecs: PipetteNameSpecs[] = getAllPipetteNames(
...PIPETTE_SORT
)
Expand Down
4 changes: 2 additions & 2 deletions components/src/instrument/__tests__/PipetteSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('PipetteSelect', () => {
'channels'
)
.map(getPipetteNameSpecs)
.filter(Boolean)
.filter((specs): specs is PipetteNameSpecs => specs !== null)

const gen2Specs = pipetteSpecs.filter(s => s.displayCategory === GEN2)
const gen1Specs = pipetteSpecs.filter(s => s.displayCategory === GEN1)
Expand All @@ -67,7 +67,7 @@ describe('PipetteSelect', () => {
'channels'
)
.map(getPipetteNameSpecs)
.filter(Boolean)
.filter((specs): specs is PipetteNameSpecs => specs !== null)

const gen2Specs = pipetteSpecs.filter(s => s.displayCategory === GEN2)
const nameBlocklist = pipetteSpecs
Expand Down
7 changes: 4 additions & 3 deletions components/src/robot-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type Channels = 1 | 8

export type Mount = 'left' | 'right'
export type {
PipetteChannels as Channels,
PipetteMount as Mount,
} from '@opentrons/shared-data'
6 changes: 5 additions & 1 deletion components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"extends": "../tsconfig-base.json",
"references": [],
"references": [
{
"path": "../shared-data"
}
],
"compilerOptions": {
"composite": true,
"rootDir": "src",
Expand Down
Loading

0 comments on commit 11c8328

Please sign in to comment.