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

WIP: Custom eslint rule to catch unsafe Record<string, T> #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ module.exports = {
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: ['react', '@typescript-eslint', 'typescript-sort-keys', 'sort-destructure-keys', 'sort-keys-fix'],
plugins: [
'react',
'@typescript-eslint',
'typescript-sort-keys',
'sort-destructure-keys',
'sort-keys-fix',
'no-direct-record-string',
],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 0, // Verbose
'@typescript-eslint/no-empty-function': 0, // unnecessary
'@typescript-eslint/no-unused-vars': 1, // hint not error
'no-direct-record-string/no-direct-record-string': 'error', // type safety
'react/jsx-sort-props': [2, { callbacksLast: true, shorthandFirst: true }], // style
'react/no-unknown-property': [2, { ignore: ['jsx', 'global'] }], // inserted by next's styled-jsx
'react/react-in-jsx-scope': 0, // Handled by Next.js
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"eslint": "^8",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-no-direct-record-string": "file:src/_shared/custom-lint-plugins/no-direct-record-string",
"eslint-plugin-react": "^7.20.1",
"eslint-plugin-sort-destructure-keys": "1.5.0",
"eslint-plugin-sort-keys-fix": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'no-direct-record-string': require('./no-direct-record-string'),
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
create(context) {
return {
TSTypeReference(node) {
if (
node.typeName.name === 'Record' &&
node.typeParameters.params.length === 2 &&
node.typeParameters.params[0].type === 'TSStringKeyword'
) {
// Check if the parent node is a TSTypeReference or TSTypeOperator
// This handles cases like Partial<Record<string, T>>
let parent = node.parent
while (parent) {
if (parent.type === 'TSTypeReference' || parent.type === 'TSTypeOperator') {
// If the parent type is TSTypeReference or TSTypeOperator, it means
// Record<string, T> is nested inside another type, so skip reporting
return
}
parent = parent.parent
}

context.report({
message: 'Direct use of Record<string, T> is unsafe. Use Partial<Record<string, T>> or explicit indices.',
node,
})
}
},
}
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "eslint-plugin-no-direct-record-string",
"version": "1.0.0",
"description": "Custom eslint rule to warn against type Record<string, T>, which is unsafe. Use Partial<Record<string, T>",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin",
"typescript",
"typesafety"
],
"license": "ISC",
"author": "dsernst",
"main": "index.js"
}
17 changes: 11 additions & 6 deletions src/status/tally-votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { Item } from 'src/vote/storeElectionInfo'
import { mapValues } from '../utils'
import { tally_IRV_Items } from './tallying/rcv-irv'

export function tallyVotes(ballot_items_by_id: Record<string, Item>, votes: Record<string, string>[]) {
type SafeRecord<V> = Partial<Record<string, V>>

export function tallyVotes(
ballot_items_by_id: Partial<Record<string, Item>>,
votes: Partial<Record<string, string>>[],
) {
const multi_vote_regex = /_(\d+)$/

// Sum up votes
const tallies: Record<string, Record<string, number>> = {}
const IRV_columns_seen: Record<string, boolean> = {}
const tallies: SafeRecord<SafeRecord<number>> = {}
const IRV_columns_seen: SafeRecord<boolean> = {}
votes.forEach((vote) => {
Object.keys(vote).forEach((key) => {
// Skip 'tracking' key
Expand Down Expand Up @@ -41,7 +46,7 @@ export function tallyVotes(ballot_items_by_id: Record<string, Item>, votes: Reco
})

// Calc total votes cast per item, to speed up calc'ing %s
const totalsCastPerItems: Record<string, number> = {}
const totalsCastPerItems: SafeRecord<number> = {}
Object.keys(tallies).forEach((item) => {
totalsCastPerItems[item] = 0
Object.keys(tallies[item]).forEach((choice) => (totalsCastPerItems[item] += tallies[item][choice]))
Expand All @@ -50,11 +55,11 @@ export function tallyVotes(ballot_items_by_id: Record<string, Item>, votes: Reco
// Sort each item's totals from highest to lowest, with ties sorted alphabetically
const ordered = mapValues(tallies, (item_totals, item_id) =>
orderBy(
orderBy(Object.keys(item_totals as Record<string, number>)),
orderBy(Object.keys(item_totals as SafeRecord<number>)),
(selection) => tallies[item_id][selection],
'desc',
),
) as Record<string, string[]>
) as SafeRecord<string[]>

// Go back and IRV tally any of those that we skipped
const irv = tally_IRV_Items(IRV_columns_seen, ballot_items_by_id, votes)
Expand Down