From b8b1c0d056b89a46d43e8f844bd0038e9251338f Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Wed, 25 May 2022 17:12:36 -0700 Subject: [PATCH] Add tsconfig, more linting to `common` package (#324) * Add tsconfig.json for common * Prefer `const` over `let` over `var` * Kill dead code * Fix some trivial Typescript issues * Turn on Typescript linting in common except for no-explicit-any * Correctly specify tsconfig dir name in functions eslintrc --- common/.eslintrc.js | 9 ++++++++- common/calculate-dpm.ts | 2 +- common/envs/constants.ts | 6 +++--- common/new-contract.ts | 14 ++++---------- common/scoring.ts | 2 +- common/tsconfig.json | 12 ++++++++++++ common/util/object.ts | 6 +++--- common/util/random.ts | 7 ++++--- functions/.eslintrc.js | 1 + web/pages/fold/[...slugs]/index.tsx | 2 +- 10 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 common/tsconfig.json diff --git a/common/.eslintrc.js b/common/.eslintrc.js index 54b878e3..6e7b62cd 100644 --- a/common/.eslintrc.js +++ b/common/.eslintrc.js @@ -9,11 +9,18 @@ module.exports = { { files: ['**/*.ts'], plugins: ['@typescript-eslint'], + extends: ['plugin:@typescript-eslint/recommended'], parser: '@typescript-eslint/parser', + parserOptions: { + tsconfigRootDir: __dirname, + project: ['./tsconfig.json'], + }, + rules: { + '@typescript-eslint/no-explicit-any': 'off', + }, }, ], rules: { - 'no-unused-vars': 'off', 'no-constant-condition': ['error', { checkLoops: false }], 'lodash/import-scope': [2, 'member'], }, diff --git a/common/calculate-dpm.ts b/common/calculate-dpm.ts index 39b348ab..104b5ef7 100644 --- a/common/calculate-dpm.ts +++ b/common/calculate-dpm.ts @@ -170,7 +170,7 @@ export function calculateNumericDpmShares( ([amount]) => amount ).map(([, i]) => i) - for (let i of order) { + for (const i of order) { const [bucket, bet] = bets[i] shares[i] = calculateDpmShares(totalShares, bet, bucket) totalShares = addObjects(totalShares, { [bucket]: shares[i] }) diff --git a/common/envs/constants.ts b/common/envs/constants.ts index db82f014..c03c44bc 100644 --- a/common/envs/constants.ts +++ b/common/envs/constants.ts @@ -5,13 +5,13 @@ import { THEOREMONE_CONFIG } from './theoremone' export const ENV = process.env.NEXT_PUBLIC_FIREBASE_ENV ?? 'PROD' -const CONFIGS = { +const CONFIGS: { [env: string]: EnvConfig } = { PROD: PROD_CONFIG, DEV: DEV_CONFIG, THEOREMONE: THEOREMONE_CONFIG, } -// @ts-ignore -export const ENV_CONFIG: EnvConfig = CONFIGS[ENV] + +export const ENV_CONFIG = CONFIGS[ENV] export function isWhitelisted(email?: string) { if (!ENV_CONFIG.whitelistEmail) { diff --git a/common/new-contract.ts b/common/new-contract.ts index d658a943..b70dee37 100644 --- a/common/new-contract.ts +++ b/common/new-contract.ts @@ -1,5 +1,4 @@ import { range } from 'lodash' -import { PHANTOM_ANTE } from './antes' import { Binary, Contract, @@ -12,7 +11,6 @@ import { import { User } from './user' import { parseTags } from './util/parse' import { removeUndefinedProps } from './util/object' -import { calcDpmInitialPool } from './calculate-dpm' export function getNewContract( id: string, @@ -78,6 +76,9 @@ export function getNewContract( return contract as Contract } +/* +import { PHANTOM_ANTE } from './antes' +import { calcDpmInitialPool } from './calculate-dpm' const getBinaryDpmProps = (initialProb: number, ante: number) => { const { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo } = calcDpmInitialPool(initialProb, ante, PHANTOM_ANTE) @@ -94,6 +95,7 @@ const getBinaryDpmProps = (initialProb: number, ante: number) => { return system } +*/ const getBinaryCpmmProps = (initialProb: number, ante: number) => { const pool = { YES: ante, NO: ante } @@ -154,11 +156,3 @@ const getNumericProps = ( return system } - -const getMultiProps = ( - outcomes: string[], - initialProbs: number[], - ante: number -) => { - // Not implemented. -} diff --git a/common/scoring.ts b/common/scoring.ts index d4855851..3f0c44b6 100644 --- a/common/scoring.ts +++ b/common/scoring.ts @@ -4,7 +4,7 @@ import { Bet } from './bet' import { Binary, Contract, FullContract } from './contract' import { getPayouts } from './payouts' -export function scoreCreators(contracts: Contract[], bets: Bet[][]) { +export function scoreCreators(contracts: Contract[]) { const creatorScore = mapValues( groupBy(contracts, ({ creatorId }) => creatorId), (contracts) => sumBy(contracts, ({ pool }) => pool.YES + pool.NO) diff --git a/common/tsconfig.json b/common/tsconfig.json new file mode 100644 index 00000000..158a5218 --- /dev/null +++ b/common/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "moduleResolution": "node", + "noImplicitReturns": true, + "outDir": "lib", + "sourceMap": true, + "strict": true, + "target": "es2017" + }, + "include": ["**/*.ts"] +} diff --git a/common/util/object.ts b/common/util/object.ts index 031e674c..c970cb24 100644 --- a/common/util/object.ts +++ b/common/util/object.ts @@ -1,9 +1,9 @@ import { union } from 'lodash' export const removeUndefinedProps = (obj: T): T => { - let newObj: any = {} + const newObj: any = {} - for (let key of Object.keys(obj)) { + for (const key of Object.keys(obj)) { if ((obj as any)[key] !== undefined) newObj[key] = (obj as any)[key] } @@ -17,7 +17,7 @@ export const addObjects = ( const keys = union(Object.keys(obj1), Object.keys(obj2)) const newObj = {} as any - for (let key of keys) { + for (const key of keys) { newObj[key] = (obj1[key] ?? 0) + (obj2[key] ?? 0) } diff --git a/common/util/random.ts b/common/util/random.ts index f52294f1..c26b361b 100644 --- a/common/util/random.ts +++ b/common/util/random.ts @@ -5,7 +5,8 @@ export const randomString = (length = 12) => export function genHash(str: string) { // xmur3 - for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) { + let h: number + for (let i = 0, h = 1779033703 ^ str.length; i < str.length; i++) { h = Math.imul(h ^ str.charCodeAt(i), 3432918353) h = (h << 13) | (h >>> 19) } @@ -28,7 +29,7 @@ export function createRNG(seed: string) { b >>>= 0 c >>>= 0 d >>>= 0 - var t = (a + b) | 0 + let t = (a + b) | 0 a = b ^ (b >>> 9) b = (c + (c << 3)) | 0 c = (c << 21) | (c >>> 11) @@ -39,7 +40,7 @@ export function createRNG(seed: string) { } } -export const shuffle = (array: any[], rand: () => number) => { +export const shuffle = (array: unknown[], rand: () => number) => { for (let i = 0; i < array.length; i++) { const swapIndex = Math.floor(rand() * (array.length - i)) ;[array[i], array[swapIndex]] = [array[swapIndex], array[i]] diff --git a/functions/.eslintrc.js b/functions/.eslintrc.js index 749ab4f5..c5b8e16f 100644 --- a/functions/.eslintrc.js +++ b/functions/.eslintrc.js @@ -11,6 +11,7 @@ module.exports = { plugins: ['@typescript-eslint'], parser: '@typescript-eslint/parser', parserOptions: { + tsconfigRootDir: __dirname, project: ['./tsconfig.json'], }, }, diff --git a/web/pages/fold/[...slugs]/index.tsx b/web/pages/fold/[...slugs]/index.tsx index ad286d60..095cf274 100644 --- a/web/pages/fold/[...slugs]/index.tsx +++ b/web/pages/fold/[...slugs]/index.tsx @@ -54,7 +54,7 @@ export async function getStaticPropz(props: { params: { slugs: string[] } }) { ) activeContracts = [...unresolved, ...resolved] - const creatorScores = scoreCreators(contracts, bets) + const creatorScores = scoreCreators(contracts) const traderScores = scoreTraders(contracts, bets) const [topCreators, topTraders] = await Promise.all([ toTopUsers(creatorScores),