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
This commit is contained in:
Marshall Polaris 2022-05-25 17:12:36 -07:00 committed by GitHub
parent 0c3fa9f065
commit b8b1c0d056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 23 deletions

View File

@ -9,11 +9,18 @@ module.exports = {
{ {
files: ['**/*.ts'], files: ['**/*.ts'],
plugins: ['@typescript-eslint'], plugins: ['@typescript-eslint'],
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
}, },
], ],
rules: { rules: {
'no-unused-vars': 'off',
'no-constant-condition': ['error', { checkLoops: false }], 'no-constant-condition': ['error', { checkLoops: false }],
'lodash/import-scope': [2, 'member'], 'lodash/import-scope': [2, 'member'],
}, },

View File

@ -170,7 +170,7 @@ export function calculateNumericDpmShares(
([amount]) => amount ([amount]) => amount
).map(([, i]) => i) ).map(([, i]) => i)
for (let i of order) { for (const i of order) {
const [bucket, bet] = bets[i] const [bucket, bet] = bets[i]
shares[i] = calculateDpmShares(totalShares, bet, bucket) shares[i] = calculateDpmShares(totalShares, bet, bucket)
totalShares = addObjects(totalShares, { [bucket]: shares[i] }) totalShares = addObjects(totalShares, { [bucket]: shares[i] })

View File

@ -5,13 +5,13 @@ import { THEOREMONE_CONFIG } from './theoremone'
export const ENV = process.env.NEXT_PUBLIC_FIREBASE_ENV ?? 'PROD' export const ENV = process.env.NEXT_PUBLIC_FIREBASE_ENV ?? 'PROD'
const CONFIGS = { const CONFIGS: { [env: string]: EnvConfig } = {
PROD: PROD_CONFIG, PROD: PROD_CONFIG,
DEV: DEV_CONFIG, DEV: DEV_CONFIG,
THEOREMONE: THEOREMONE_CONFIG, THEOREMONE: THEOREMONE_CONFIG,
} }
// @ts-ignore
export const ENV_CONFIG: EnvConfig = CONFIGS[ENV] export const ENV_CONFIG = CONFIGS[ENV]
export function isWhitelisted(email?: string) { export function isWhitelisted(email?: string) {
if (!ENV_CONFIG.whitelistEmail) { if (!ENV_CONFIG.whitelistEmail) {

View File

@ -1,5 +1,4 @@
import { range } from 'lodash' import { range } from 'lodash'
import { PHANTOM_ANTE } from './antes'
import { import {
Binary, Binary,
Contract, Contract,
@ -12,7 +11,6 @@ import {
import { User } from './user' import { User } from './user'
import { parseTags } from './util/parse' import { parseTags } from './util/parse'
import { removeUndefinedProps } from './util/object' import { removeUndefinedProps } from './util/object'
import { calcDpmInitialPool } from './calculate-dpm'
export function getNewContract( export function getNewContract(
id: string, id: string,
@ -78,6 +76,9 @@ export function getNewContract(
return contract as Contract return contract as Contract
} }
/*
import { PHANTOM_ANTE } from './antes'
import { calcDpmInitialPool } from './calculate-dpm'
const getBinaryDpmProps = (initialProb: number, ante: number) => { const getBinaryDpmProps = (initialProb: number, ante: number) => {
const { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo } = const { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo } =
calcDpmInitialPool(initialProb, ante, PHANTOM_ANTE) calcDpmInitialPool(initialProb, ante, PHANTOM_ANTE)
@ -94,6 +95,7 @@ const getBinaryDpmProps = (initialProb: number, ante: number) => {
return system return system
} }
*/
const getBinaryCpmmProps = (initialProb: number, ante: number) => { const getBinaryCpmmProps = (initialProb: number, ante: number) => {
const pool = { YES: ante, NO: ante } const pool = { YES: ante, NO: ante }
@ -154,11 +156,3 @@ const getNumericProps = (
return system return system
} }
const getMultiProps = (
outcomes: string[],
initialProbs: number[],
ante: number
) => {
// Not implemented.
}

View File

@ -4,7 +4,7 @@ import { Bet } from './bet'
import { Binary, Contract, FullContract } from './contract' import { Binary, Contract, FullContract } from './contract'
import { getPayouts } from './payouts' import { getPayouts } from './payouts'
export function scoreCreators(contracts: Contract[], bets: Bet[][]) { export function scoreCreators(contracts: Contract[]) {
const creatorScore = mapValues( const creatorScore = mapValues(
groupBy(contracts, ({ creatorId }) => creatorId), groupBy(contracts, ({ creatorId }) => creatorId),
(contracts) => sumBy(contracts, ({ pool }) => pool.YES + pool.NO) (contracts) => sumBy(contracts, ({ pool }) => pool.YES + pool.NO)

12
common/tsconfig.json Normal file
View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"baseUrl": "../",
"moduleResolution": "node",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"include": ["**/*.ts"]
}

View File

@ -1,9 +1,9 @@
import { union } from 'lodash' import { union } from 'lodash'
export const removeUndefinedProps = <T>(obj: T): T => { export const removeUndefinedProps = <T>(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] if ((obj as any)[key] !== undefined) newObj[key] = (obj as any)[key]
} }
@ -17,7 +17,7 @@ export const addObjects = <T extends { [key: string]: number }>(
const keys = union(Object.keys(obj1), Object.keys(obj2)) const keys = union(Object.keys(obj1), Object.keys(obj2))
const newObj = {} as any const newObj = {} as any
for (let key of keys) { for (const key of keys) {
newObj[key] = (obj1[key] ?? 0) + (obj2[key] ?? 0) newObj[key] = (obj1[key] ?? 0) + (obj2[key] ?? 0)
} }

View File

@ -5,7 +5,8 @@ export const randomString = (length = 12) =>
export function genHash(str: string) { export function genHash(str: string) {
// xmur3 // 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 = Math.imul(h ^ str.charCodeAt(i), 3432918353)
h = (h << 13) | (h >>> 19) h = (h << 13) | (h >>> 19)
} }
@ -28,7 +29,7 @@ export function createRNG(seed: string) {
b >>>= 0 b >>>= 0
c >>>= 0 c >>>= 0
d >>>= 0 d >>>= 0
var t = (a + b) | 0 let t = (a + b) | 0
a = b ^ (b >>> 9) a = b ^ (b >>> 9)
b = (c + (c << 3)) | 0 b = (c + (c << 3)) | 0
c = (c << 21) | (c >>> 11) 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++) { for (let i = 0; i < array.length; i++) {
const swapIndex = Math.floor(rand() * (array.length - i)) const swapIndex = Math.floor(rand() * (array.length - i))
;[array[i], array[swapIndex]] = [array[swapIndex], array[i]] ;[array[i], array[swapIndex]] = [array[swapIndex], array[i]]

View File

@ -11,6 +11,7 @@ module.exports = {
plugins: ['@typescript-eslint'], plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
parserOptions: { parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'], project: ['./tsconfig.json'],
}, },
}, },

View File

@ -54,7 +54,7 @@ export async function getStaticPropz(props: { params: { slugs: string[] } }) {
) )
activeContracts = [...unresolved, ...resolved] activeContracts = [...unresolved, ...resolved]
const creatorScores = scoreCreators(contracts, bets) const creatorScores = scoreCreators(contracts)
const traderScores = scoreTraders(contracts, bets) const traderScores = scoreTraders(contracts, bets)
const [topCreators, topTraders] = await Promise.all([ const [topCreators, topTraders] = await Promise.all([
toTopUsers(creatorScores), toTopUsers(creatorScores),