From 40aec939d613873eb6f5d1e04040819bf035b662 Mon Sep 17 00:00:00 2001 From: Brian Donovan <1938+eventualbuddha@users.noreply.github.com> Date: Mon, 18 Oct 2021 16:45:14 -0700 Subject: [PATCH 1/2] style(lint): use optional properties/fields/params https://google.github.io/styleguide/tsguide.html#optionals-vs-undefined-type Closes #1075 --- apps/bmd/src/AppRoot.tsx | 2 +- apps/bmd/src/pages/PollWorkerScreen.tsx | 9 +- .../utils/ScreenReader/AriaScreenReader.ts | 4 +- apps/bsd/src/AppRoot.tsx | 4 +- .../bsd/src/screens/AdvancedOptionsScreen.tsx | 12 +- apps/bsd/test/renderInAppContext.tsx | 2 +- .../src/contexts/AppContext.ts | 3 +- .../src/screens/DefinitionContestsScreen.tsx | 2 +- .../test/renderInAppContext.tsx | 5 +- apps/module-scan/src/importer.ts | 7 +- apps/module-scan/src/store.ts | 2 +- apps/precinct-scanner/src/api/config.ts | 2 +- .../components/ExportResultsModal.test.tsx | 1 - .../docs/rules/gts-use-optionals.md | 38 +++++ .../src/configs/recommended.ts | 1 + .../src/rules/gts-use-optionals.ts | 135 ++++++++++++++++++ libs/eslint-plugin-vx/src/rules/index.ts | 2 + .../tests/rules/gts-use-optionals.test.ts | 120 ++++++++++++++++ libs/hmpb-interpreter/src/utils/defined.ts | 4 +- libs/lsd/src/cli/index.ts | 2 +- libs/lsd/src/util/geometry.ts | 4 +- libs/plustek-sdk/src/mocks.ts | 4 +- libs/utils/src/format.ts | 6 +- 23 files changed, 323 insertions(+), 48 deletions(-) create mode 100644 libs/eslint-plugin-vx/docs/rules/gts-use-optionals.md create mode 100644 libs/eslint-plugin-vx/src/rules/gts-use-optionals.ts create mode 100644 libs/eslint-plugin-vx/tests/rules/gts-use-optionals.test.ts diff --git a/apps/bmd/src/AppRoot.tsx b/apps/bmd/src/AppRoot.tsx index 279f5c83b..a935b5fa9 100644 --- a/apps/bmd/src/AppRoot.tsx +++ b/apps/bmd/src/AppRoot.tsx @@ -92,7 +92,7 @@ interface CardState { pauseProcessingUntilNoCardPresent: boolean showPostVotingInstructions?: PostVotingInstructions voterCardCreatedAt: number - tallyOnCard?: Optional + tallyOnCard?: PrecinctScannerCardTally } interface UserState { diff --git a/apps/bmd/src/pages/PollWorkerScreen.tsx b/apps/bmd/src/pages/PollWorkerScreen.tsx index 771039c10..2ff1444a7 100644 --- a/apps/bmd/src/pages/PollWorkerScreen.tsx +++ b/apps/bmd/src/pages/PollWorkerScreen.tsx @@ -1,12 +1,7 @@ import { DateTime } from 'luxon' import React, { useState, useEffect, useCallback } from 'react' -import { - ElectionDefinition, - Optional, - Tally, - VotingMethod, -} from '@votingworks/types' +import { ElectionDefinition, Tally, VotingMethod } from '@votingworks/types' import { Button, ButtonList, @@ -60,7 +55,7 @@ interface Props { machineConfig: MachineConfig printer: Printer togglePollsOpen: () => void - tallyOnCard: Optional + tallyOnCard?: PrecinctScannerCardTally clearTalliesOnCard: () => Promise } diff --git a/apps/bmd/src/utils/ScreenReader/AriaScreenReader.ts b/apps/bmd/src/utils/ScreenReader/AriaScreenReader.ts index 84b9b208c..3b46b9b84 100644 --- a/apps/bmd/src/utils/ScreenReader/AriaScreenReader.ts +++ b/apps/bmd/src/utils/ScreenReader/AriaScreenReader.ts @@ -146,9 +146,7 @@ export default class AriaScreenReader implements ScreenReader { : this.describeElement(node) } - private cleanDescription( - description: string | undefined - ): string | undefined { + private cleanDescription(description?: string): string | undefined { if (!description) { return } diff --git a/apps/bsd/src/AppRoot.tsx b/apps/bsd/src/AppRoot.tsx index c00121080..d53ea7311 100644 --- a/apps/bsd/src/AppRoot.tsx +++ b/apps/bsd/src/AppRoot.tsx @@ -123,7 +123,7 @@ const App = ({ card, hardware }: AppRootProps): JSX.Element => { setMarkThresholds(await config.getMarkThresholdOverrides()) }, []) - const updateElectionDefinition = async (e: Optional) => { + const updateElectionDefinition = async (e?: ElectionDefinition) => { setElectionDefinition(e) setElectionJustLoaded(true) } @@ -264,7 +264,7 @@ const App = ({ card, hardware }: AppRootProps): JSX.Element => { }, [history, isTestMode, refreshConfig]) const setMarkThresholdOverrides = useCallback( - async (markThresholdOverrides: Optional) => { + async (markThresholdOverrides?: MarkThresholds) => { await config.setMarkThresholdOverrides(markThresholdOverrides) await refreshConfig() history.replace('/') diff --git a/apps/bsd/src/screens/AdvancedOptionsScreen.tsx b/apps/bsd/src/screens/AdvancedOptionsScreen.tsx index d2c591341..8ae94f947 100644 --- a/apps/bsd/src/screens/AdvancedOptionsScreen.tsx +++ b/apps/bsd/src/screens/AdvancedOptionsScreen.tsx @@ -1,8 +1,4 @@ -import { - ElectionDefinition, - MarkThresholds, - Optional, -} from '@votingworks/types' +import { ElectionDefinition, MarkThresholds } from '@votingworks/types' import React, { useCallback, useEffect, useState, useContext } from 'react' import Button from '../components/Button' import LinkButton from '../components/LinkButton' @@ -23,10 +19,8 @@ interface Props { isTestMode: boolean isTogglingTestMode: boolean toggleTestMode: () => Promise - setMarkThresholdOverrides: ( - markThresholds: Optional - ) => Promise - markThresholds: Optional + setMarkThresholdOverrides: (markThresholds?: MarkThresholds) => Promise + markThresholds?: MarkThresholds electionDefinition: ElectionDefinition } diff --git a/apps/bsd/test/renderInAppContext.tsx b/apps/bsd/test/renderInAppContext.tsx index b73743279..98a524af5 100644 --- a/apps/bsd/test/renderInAppContext.tsx +++ b/apps/bsd/test/renderInAppContext.tsx @@ -8,7 +8,7 @@ import { Router } from 'react-router-dom' import AppContext from '../src/contexts/AppContext' interface RenderInAppContextParams { - route?: string | undefined + route?: string history?: MemoryHistory // eslint-disable-line @typescript-eslint/no-explicit-any electionDefinition?: ElectionDefinition machineId?: string diff --git a/apps/election-manager/src/contexts/AppContext.ts b/apps/election-manager/src/contexts/AppContext.ts index 476e733e8..61050da93 100644 --- a/apps/election-manager/src/contexts/AppContext.ts +++ b/apps/election-manager/src/contexts/AppContext.ts @@ -3,7 +3,6 @@ import { ElectionDefinition, FullElectionTally, FullElectionExternalTally, - Optional, UserSession, } from '@votingworks/types' import { usbstick, NullPrinter, Printer } from '@votingworks/utils' @@ -48,7 +47,7 @@ export interface AppContextInterface { ) => Promise setIsTabulationRunning: React.Dispatch> generateExportableTallies: () => ExportableTallies - currentUserSession: Optional + currentUserSession?: UserSession attemptToAuthenticateAdminUser: (passcode: string) => boolean lockMachine: () => void machineConfig: MachineConfig diff --git a/apps/election-manager/src/screens/DefinitionContestsScreen.tsx b/apps/election-manager/src/screens/DefinitionContestsScreen.tsx index da67323cd..af4a8331c 100644 --- a/apps/election-manager/src/screens/DefinitionContestsScreen.tsx +++ b/apps/election-manager/src/screens/DefinitionContestsScreen.tsx @@ -89,7 +89,7 @@ const TextField = ({ pattern?: string step?: number type?: 'text' | 'textarea' | 'number' - value: string | number | undefined + value?: string | number }) => (