Skip to content

Commit

Permalink
[TypeScript] Lint TypeScipt code (#736)
Browse files Browse the repository at this point in the history
Currently the typescript code is not being linted in the CI. This PR adds some basic recommended typescript linting rules and fixes to code so that the linting succeeds.

In order to fix an import lint on importing `truffle-assertions`, I added some typings for the module.

### Test Plan

CI - linting should succeed.

### Commit History

* ignore fewer files

* lint TS files
  • Loading branch information
nlordell authored May 6, 2020
1 parent 1bdaea3 commit 0763337
Show file tree
Hide file tree
Showing 27 changed files with 1,312 additions and 1,436 deletions.
6 changes: 0 additions & 6 deletions .eslintignore

This file was deleted.

27 changes: 17 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
env: {
browser: true,
node: true,
commonjs: true,
es2020: true,
},
extends: "eslint:recommended",
extends: ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended"],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2015,
sourceType: "module",
},
plugins: ["react", "@typescript-eslint"],
plugins: ["@typescript-eslint"],
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
"no-console": "error",
quotes: ["error", "double"],
semi: ["error", "never"],
"prefer-const": ["error"],
"no-var": ["error"],

"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-use-before-define": "off",
},
globals: {
artifacts: false,
contract: false,
assert: false,
web3: false,
},
overrides: [
{
files: ["*.js"],
rules: {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-var-requires": "off",
},
},
],
ignorePatterns: ["build/", "coverage/", "node_modules/"],
}
2 changes: 0 additions & 2 deletions migration_conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const path = require("path")

// eslint-disable-next-line no-undef
const BUILD_DIR = path.join(__dirname, "build", "contracts")
// eslint-disable-next-line no-undef
const NETWORKS_FILE_PATH = path.join(__dirname, "networks.json")

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"networks-inject": "CONF_FILE=$(pwd)'/migration_conf.js' node node_modules/@gnosis.pm/util-contracts/src/inject_network_info.js",
"networks-reset": "mkdir -p build/contracts && truffle networks --clean && yarn run networks-inject",
"verify-stablex": "truffle run verify BatchExchange",
"lint": "eslint .",
"lint": "eslint . --ext .js,.ts",
"solhint": "solhint \"contracts/**/*.sol\"",
"prepack": "yarn run build",
"prettier:solidity": "prettier --write 'contracts/**/*.sol'",
Expand Down Expand Up @@ -91,7 +91,6 @@
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-plugin-no-only-tests": "^2.4.0",
"eslint-plugin-react": "^7.19.0",
"eth-gas-reporter": "^0.2.17",
"ganache-cli": "^6.9.1",
"husky": "^4.2.5",
Expand All @@ -117,6 +116,7 @@
"web3": "^1.2.7",
"web3-core": "^1.2.7",
"web3-eth-contract": "^1.2.7",
"web3-utils": "^1.2.7",
"yargs": "^15.3.1"
}
}
4 changes: 0 additions & 4 deletions scripts/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ module.exports = {
rules: {
"no-console": "off",
},
env: {
mocha: true,
node: true,
},
}
28 changes: 14 additions & 14 deletions src/balance_reader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BN from "bn.js";
import Web3 from "web3";
import {BatchExchangeInstance} from "../types/truffle-typings";
import BN from "bn.js"
import Web3 from "web3"
import {BatchExchangeInstance} from "../types/truffle-typings"

const WORD_DATA_LENGTH = 64;
const WORD_DATA_LENGTH = 64

/**
* Retrieves user's token balance as stored in the "balance" entry of the private exchange mapping balanceStates
Expand All @@ -22,7 +22,7 @@ export async function getBalanceState(
): Promise<BN> {
// TODO when Truffle depends on web3-utils version ^1.2.5:
// use soliditySha3Raw instead of soliditySha3 and remove type coercion
const BALANCESTATES_STORAGE_SLOT = "0x0";
const BALANCESTATES_STORAGE_SLOT = "0x0"

const userBalancestatesStorageSlot = web3Provider.utils.soliditySha3(
{
Expand All @@ -36,7 +36,7 @@ export async function getBalanceState(
WORD_DATA_LENGTH
),
}
) as string;
) as string

const targetStorageSlot = web3Provider.utils.soliditySha3(
{
Expand All @@ -50,13 +50,13 @@ export async function getBalanceState(
WORD_DATA_LENGTH
),
}
) as string;
) as string

const storageAtSlot = await web3Provider.eth.getStorageAt(
batchExchangeAddress,
targetStorageSlot
);
return web3Provider.utils.toBN(storageAtSlot);
)
return web3Provider.utils.toBN(storageAtSlot)
}

/**
Expand Down Expand Up @@ -92,14 +92,14 @@ export async function getWithdrawableAmount(
batchExchange.getPendingWithdraw(userAddress, tokenAddress),
batchExchange.lastCreditBatchId(userAddress, tokenAddress),
batchExchange.getCurrentBatchId(),
]);
let balance = balanceState;
])
let balance = balanceState
if (pendingDeposit[1].lt(batchId)) {
balance = balance.add(pendingDeposit[0]);
balance = balance.add(pendingDeposit[0])
}
if (pendingWithdrawal[1].gte(batchId) || lastCreditBatchId.gte(batchId)) {
return new BN(0);
return new BN(0)
} else {
return BN.min(balance, pendingWithdrawal[0]);
return BN.min(balance, pendingWithdrawal[0])
}
}
Loading

0 comments on commit 0763337

Please sign in to comment.