Skip to content

Commit

Permalink
replace Prettier with ESLint (compound-finance#321)
Browse files Browse the repository at this point in the history
* add eslint, config, ignorefile

* update github action, pre-commit hook

* update plugins/ scripts/ src/ tasks/

* test/ fixes

* rename curry to compose; add types (compound-finance#322)

* additional updates

* post-rebase fixes

* post rebase fixes

* delete leftover chai, chai-as-promised
  • Loading branch information
scott-silver authored May 10, 2022
1 parent 2d17567 commit a6293f3
Show file tree
Hide file tree
Showing 68 changed files with 1,084 additions and 689 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins/scenario/worker/BootstrapWorker.js
plugins/deployment_manager/test/SolcList.json
scripts/build-spec.js
65 changes: 65 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module.exports = {
'env': {
'browser': true,
'es2021': true
},
'extends': [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
'parser': '@typescript-eslint/parser',
'parserOptions': {
'ecmaVersion': 'latest',
'sourceType': 'module'
},
'plugins': [
'@typescript-eslint'
],
'rules': {
'indent': [
'error',
2,
{
'SwitchCase': 1
}
],
'linebreak-style': [
'error',
'unix'
],
'no-prototype-builtins': 'off',
'no-unused-vars': 'off',
'prefer-const': 'off',
'prefer-spread': 'off',
'quotes': [
'error',
'single',
{
'avoidEscape': true,
'allowTemplateLiterals': true
}
],
'semi': [
'error',
'always'
],
'@typescript-eslint/member-delimiter-style': [
'error',
{
"multiline": {
"delimiter": "semi",
"requireLast": true
},
"singleline": {
"delimiter": "comma",
"requireLast": false
},
}
],
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-inferrable-types': [ 'warn', { 'ignoreParameters': true } ],
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unused-vars': ['error', { 'varsIgnorePattern': '^_', 'argsIgnorePattern': '^_' } ]
},
};
4 changes: 2 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g
# solhint
echo "$STAGED_FILES" | egrep '\.sol$' | xargs ./node_modules/.bin/solhint --fix

# prettier
echo "$STAGED_FILES" | xargs yarn prettier
# eslint
echo "$STAGED_FILES" | xargs yarn lint

# add back the modified/prettified files to staging
echo "$STAGED_FILES" | xargs git add
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Run Prettier
name: Run ESLint
on:
workflow_dispatch:
pull_request:
jobs:
run-coverage:
name: Run Prettier
name: Run ESLint
runs-on: ubuntu-latest
env:
ETHERSCAN_KEY: ${{ secrets.ETHERSCAN_KEY }}
Expand All @@ -22,5 +22,5 @@ jobs:
- name: Install packages
run: yarn install --non-interactive --frozen-lockfile && yarn build

- name: Run Prettier
run: yarn run prettier --check .
- name: Run ESLint
run: yarn lint
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"cover": "hardhat cover && npx istanbul report --include coverage.json html lcov",
"deploy": "npx hardhat run ./scripts/deploy-comet.ts",
"gas": "REPORT_GAS=true yarn test",
"lint": "eslint 'plugins/**/*' 'scripts/**/*' 'src/**/*' 'tasks/**/*' 'test/**/*' ",
"lint-contracts": "solhint 'contracts/**/*.sol'",
"lint-contracts:fix": "solhint --fix 'contracts/**/*.sol'",
"prettier": "prettier --ignore-unknown --write",
"scenario": "hardhat scenario",
"spider": "hardhat spider",
"test": "hardhat test",
Expand All @@ -50,8 +50,11 @@
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"chalk": "^5.0.0",
"dotenv": "^10.0.0",
"eslint": "^8.12.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.1",
"fast-glob": "^3.2.7",
Expand All @@ -62,7 +65,6 @@
"mocha-junit-reporter": "^2.0.2",
"mocha-multi-reporters": "hayesgm/mocha-multi-reporters#hayesgm/reporter-options-to-option",
"nock": "^13.2.2",
"prettier": "2.5.1",
"sc-istanbul": "^0.4.5",
"solhint": "^3.3.6",
"ts-node": "^10.4.0",
Expand Down
3 changes: 1 addition & 2 deletions plugins/deployment_manager/Aliases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Address, Alias } from './Types';
import { Cache } from './Cache';
import { objectToMap } from './Utils';

export type Aliases = Map<Alias, Address>;
export type InvertedAliases = Map<Address, Alias[]>;
Expand All @@ -10,7 +9,7 @@ let aliasesSpec = { rel: 'aliases.json' };

// Read aliases
export async function getAliases(cache: Cache): Promise<Aliases> {
return await cache.readMap<Alias, Address>(aliasesSpec);
return await cache.readMap<Address>(aliasesSpec);
}

// Stores aliases
Expand Down
12 changes: 5 additions & 7 deletions plugins/deployment_manager/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as fs from 'fs/promises';
import * as nodepath from 'path';
import { inspect } from 'util';

import { Address, BuildFile } from './Types';
import { fileExists, objectFromMap, objectToMap } from './Utils';

export type FileSpec = string | string[] | { rel: string | string[] };

function curry<A, B, C>(f: (A) => B, g: (B) => C): (A) => C {
function compose<A, B, C>(f: (a: A) => B, g: (b: B) => C): (a: A) => C {
return (x) => g(f(x));
}

Expand Down Expand Up @@ -96,7 +94,7 @@ export class Cache {
}
}
}
throw new Error("unreachable");
throw new Error('unreachable');
}

private async putDisk<T>(spec: FileSpec, data: T, transformer: (T) => string) {
Expand Down Expand Up @@ -138,12 +136,12 @@ export class Cache {
}
}

async readMap<K, V>(spec: FileSpec): Promise<Map<string, V>> {
return await this.readCache(spec, curry<K, string, Map<string, V>>(parseJson, objectToMap));
async readMap<V>(spec: FileSpec): Promise<Map<string, V>> {
return await this.readCache(spec, compose<string, object, Map<string, V>>(parseJson, objectToMap));
}

async storeMap<K, V>(spec: FileSpec, map: Map<K, V>) {
await this.storeCache(spec, map, curry(objectFromMap, stringifyJson));
await this.storeCache(spec, map, compose(objectFromMap, stringifyJson));
}

clearMemory() {
Expand Down
7 changes: 3 additions & 4 deletions plugins/deployment_manager/ContractMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ async function getContractByAddressProxy(
accAlias: string,
accAddress: Address
): Promise<Contract> {
let name;
let { contract, abi } = await getContractByAddress(cache, accAlias, accAddress, hre, signer);
let { abi } = await getContractByAddress(cache, accAlias, accAddress, hre, signer);
let nextABI = mergeABI(abi, accABI); // duplicate entries (like constructor) defer to accABI
if (proxies.has(accAlias)) {
return await getContractByAddressProxy(
Expand All @@ -75,12 +74,12 @@ async function getContractByAddress(
hre: HardhatRuntimeEnvironment,
signer: Signer,
implBuildFile?: BuildFile
): Promise<{ name: string; contract: Contract; abi: ABI }> {
): Promise<{ name: string, contract: Contract, abi: ABI }> {
let buildFile = await getRequiredBuildFile(cache, address, alias);
let [contractName, metadata] = getPrimaryContract(buildFile);
let abi;
if (implBuildFile) {
let [implContractName, implMetadata] = getPrimaryContract(implBuildFile);
let [_implContractName, implMetadata] = getPrimaryContract(implBuildFile);
abi = implMetadata.abi;
} else {
abi = metadata.abi;
Expand Down
4 changes: 2 additions & 2 deletions plugins/deployment_manager/Deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as fs from 'fs/promises';

import { Contract, ContractFactory, Signer } from 'ethers';
import { Contract, Signer } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

import { putAlias } from './Aliases';
Expand Down Expand Up @@ -32,7 +32,7 @@ async function deployFromBuildFile(
deployArgs: any[],
hre: HardhatRuntimeEnvironment
): Promise<Contract> {
let [contractName, metadata] = getPrimaryContract(buildFile);
let [_contractName, metadata] = getPrimaryContract(buildFile);
const [signer] = await hre.ethers.getSigners(); // TODO: Hmm?
const contractFactory = new hre.ethers.ContractFactory(metadata.abi, metadata.bin, signer);
const contract = await contractFactory.deploy(...deployArgs);
Expand Down
3 changes: 0 additions & 3 deletions plugins/deployment_manager/DeploymentManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { Contract, Signer } from 'ethers';

import { Alias, Address, BuildFile } from './Types';

import { Aliases, getAliases, putAlias, storeAliases } from './Aliases';
import { Cache } from './Cache';
import { ContractMap, getContracts } from './ContractMap';
Expand All @@ -12,7 +10,6 @@ import { Proxies, getProxies, putProxy, storeProxies } from './Proxies';
import { getRelationConfig } from './RelationConfig';
import { Roots, getRoots, putRoots } from './Roots';
import { spider } from './Spider';
import { verifyContract } from './Verify';
import { Migration, getArtifactSpec } from './Migration';
import { generateMigration } from './MigrationTemplate';

Expand Down
4 changes: 2 additions & 2 deletions plugins/deployment_manager/ManualVerify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs/promises';
import { Contract, ContractFactory, Signer } from 'ethers';
import { ContractMetadata, BuildFile } from './Types';
import { Contract } from 'ethers';
import { BuildFile } from './Types';
import { getPrimaryContract } from './Utils';
import { NomicLabsHardhatPluginError } from 'hardhat/plugins';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
Expand Down
7 changes: 3 additions & 4 deletions plugins/deployment_manager/Migration.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import fg from 'fast-glob';
import * as path from 'path';

import { DeploymentManager } from './DeploymentManager';
import { FileSpec } from './Cache';

interface Action<T> {
prepare: (DeploymentManager) => Promise<T>;
enact: (DeploymentManager, T) => Promise<void>;
enacted: (DeploymentManager) => Promise<boolean>;
prepare: (dm: DeploymentManager) => Promise<T>;
enact: (dm: DeploymentManager, t: T) => Promise<void>;
enacted: (dm: DeploymentManager) => Promise<boolean>;
}

export interface Migration<T> {
Expand Down
2 changes: 1 addition & 1 deletion plugins/deployment_manager/Proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let proxiesSpec = { rel: 'proxies.json' };

// Reads root information for given deployment
export async function getProxies(cache: Cache): Promise<Proxies> {
return await cache.readMap<Alias, Address>(proxiesSpec);
return await cache.readMap<Address>(proxiesSpec);
}

// Stores new proxies for a given deployment in cache
Expand Down
7 changes: 2 additions & 5 deletions plugins/deployment_manager/RelationConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Contract, utils } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

import { DeploymentManagerConfig } from './type-extensions';
import { Address, Alias } from './Types';
import { Cache } from './Cache';

export type AliasFunction = (contract: Contract) => Promise<string>;
export type AliasTemplateString = string;
Expand Down Expand Up @@ -42,7 +39,7 @@ export function getRelationConfig(
return baseRelationConfigMap;
}
throw new Error(
`Must set \`relationConfigMap\` key of \`deploymentManager\` in Hardhat config to use spider.`
`Must set \`relationConfigMap\` key of \`deploymentManager\` in Hardhat config to use spider.`
);
}

Expand Down Expand Up @@ -92,7 +89,7 @@ export async function readField(contract: Contract, fieldKey: FieldKey): Promise
let val = await readKey(contract, fieldKey.key);
return asAddressArray(val, fieldKey.key);
} else if (fieldKey.getter) {
return asAddressArray(await fieldKey.getter(contract), "custom function");
return asAddressArray(await fieldKey.getter(contract), 'custom function');
} else {
throw new Error(`Unknown or invalid field key ${JSON.stringify(fieldKey)}`);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/deployment_manager/Roots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let rootsSpec = { rel: 'roots.json' };

// Reads root information for given deployment
export async function getRoots(cache: Cache): Promise<Roots> {
return await cache.readMap<Alias, Address>(rootsSpec);
return await cache.readMap<Address>(rootsSpec);
}

// Stores new roots for a given deployment in cache
Expand Down
14 changes: 7 additions & 7 deletions plugins/deployment_manager/Spider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
readAlias,
readField,
} from './RelationConfig';
import { Address, Alias, BuildFile, ContractMetadata } from './Types';
import { Address, Alias, BuildFile } from './Types';
import { Aliases } from './Aliases';
import { Proxies } from './Proxies';
import { Roots } from './Roots';
import { asArray, cross, debug, getPrimaryContract, objectFromMap, mergeABI } from './Utils';
import { asArray, cross, debug, getPrimaryContract, mergeABI } from './Utils';
import { fetchAndCacheContract } from './Import';

function isValidAddress(address: Address): boolean {
Expand Down Expand Up @@ -87,8 +87,8 @@ async function runSpider(

let relationConfig = relationConfigMap[contractName] ??
(typeof aliasTemplate === 'string' ? relationConfigMap[aliasTemplate] : undefined) ?? {
relations: {},
};
relations: {},
};

// Relation Config tells us how to keep spidering from here, plus possibly
// alternative aliases. If we don't have config, we can skip all of this.
Expand All @@ -108,7 +108,7 @@ async function runSpider(
);

if (implDiscovered.length > 0) {
[implAddress] = implDiscovered.map(([address, alias]) => address);
[implAddress] = implDiscovered.map(([address]) => address);

// Recurse a single step to get implementation
visited = await runSpider(
Expand All @@ -128,7 +128,7 @@ async function runSpider(
`Failed to spider implementation for ${defaultAlias} at ${implAddress}`
);
}
const [proxyContractName, proxyContractMetadata] = getPrimaryContract(proxyBuildFile);
const [_proxyContractName, proxyContractMetadata] = getPrimaryContract(proxyBuildFile);

// duplicate entries (like constructor) defer to contractMetadata.abi
const mergedABI = mergeABI(proxyContractMetadata.abi, contractMetadata.abi);
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function spider(
roots: Roots,
importRetries?: number,
importRetryDelay?: number
): Promise<{ cache: Cache; aliases: Aliases; proxies: Proxies }> {
): Promise<{ cache: Cache, aliases: Aliases, proxies: Proxies }> {
let discovered: [Address, AliasTemplate][] = [...roots.entries()].map(([alias, address]) => {
return [address, aliasTemplateFromAlias(alias)];
});
Expand Down
6 changes: 3 additions & 3 deletions plugins/deployment_manager/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export function mergeABI(abi0: ABI, abi1: ABI): ABIEntry[] {

const mergedABI = [...parsedABI0, ...parsedABI1];

const entries = {}
const entries = {};

for (const abiEntry of mergedABI) {
// only allow one constructor or one unique entry
const key = abiEntry.type === "constructor" ? "constructor" : JSON.stringify(abiEntry);
const key = abiEntry.type === 'constructor' ? 'constructor' : JSON.stringify(abiEntry);

entries[key] = abiEntry;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ export function asArray<A>(v: A | A[]): A[] {
return v;
} else {
if (v === undefined) {
return []
return [];
} else {
return [v];
}
Expand Down
Loading

0 comments on commit a6293f3

Please sign in to comment.