Skip to content

Commit

Permalink
Merge pull request cosmos#72 from confio/migrate-to-eslint
Browse files Browse the repository at this point in the history
Migrate from tslint to eslint
  • Loading branch information
ethanfrey authored Feb 1, 2022
2 parents 606d510 + 7c66393 commit 00a9d52
Show file tree
Hide file tree
Showing 14 changed files with 1,350 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ workflows:
jobs:
typescript:
docker:
- image: circleci/node:dubnium
- image: circleci/node:12
working_directory: ~/proofs/js
steps:
# we checkout here, all other code runs in js subdir
Expand Down
5 changes: 5 additions & 0 deletions js/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated protobuf code
generated/

build/
node_modules/
91 changes: 91 additions & 0 deletions js/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module.exports = {
env: {
es6: true,
jasmine: true,
node: true,
worker: true,
},
parser: "@typescript-eslint/parser",
parserOptions: {
// ecmaVersion: 2018,
project: "./tsconfig.json",
tsconfigRootDir: __dirname,
},
plugins: ["@typescript-eslint", "prettier", "simple-import-sort", "import"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended",
"plugin:import/typescript",
],
rules: {
curly: ["warn", "multi-line", "consistent"],
"no-bitwise": "warn",
"no-console": ["warn", { allow: ["error", "info", "table", "warn"] }],
"no-param-reassign": "warn",
"no-shadow": "off", // disabled in favour of @typescript-eslint/no-shadow, see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-shadow.md
"no-unused-vars": "off", // disabled in favour of @typescript-eslint/no-unused-vars, see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md
"prefer-const": "warn",
radix: ["warn", "always"],
"spaced-comment": ["warn", "always", { line: { markers: ["/ <reference"] } }],
"import/no-cycle": "warn",
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn",
"@typescript-eslint/array-type": ["warn", { default: "array-simple" }],
"@typescript-eslint/await-thenable": "warn",
"@typescript-eslint/ban-types": "warn",
"@typescript-eslint/explicit-function-return-type": ["warn", { allowExpressions: true }],
"@typescript-eslint/explicit-member-accessibility": "warn",
"@typescript-eslint/naming-convention": [
"warn",
{
selector: "default",
format: ["strictCamelCase"],
},
{
selector: "typeLike",
format: ["StrictPascalCase"],
},
{
selector: "enumMember",
format: ["StrictPascalCase"],
},
{
selector: "variable",
format: ["strictCamelCase"],
leadingUnderscore: "allow",
},
{
selector: "parameter",
format: ["strictCamelCase"],
leadingUnderscore: "allow",
},
],
"@typescript-eslint/no-dynamic-delete": "warn",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "warn",
"@typescript-eslint/no-parameter-properties": "warn",
"@typescript-eslint/no-shadow": "warn",
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }],
"@typescript-eslint/no-unnecessary-type-assertion": "warn",
// This one is used in CosmJS but would require a lot ot code change here
// "@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/prefer-readonly": "warn",
// Re-activate once the codebase is migrates to ts-proto
"@typescript-eslint/no-non-null-assertion": "off",
},
overrides: [
{
files: "**/*.js",
rules: {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
},
},
],
};
13 changes: 9 additions & 4 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"yarn.lock"
],
"scripts": {
"lint": "tslint -t verbose --project .",
"lint": "eslint --max-warnings 0 './src/**/*.ts'",
"lint:fix": "yarn lint --fix",
"format": "prettier --write --loglevel warn \"./src/**/*.ts\"",
"prebuild": "yarn lint && yarn format",
"prepublish": "yarn build",
Expand All @@ -32,14 +33,18 @@
},
"devDependencies": {
"@types/jasmine": "^3.5.0",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"jasmine": "^3.5.0",
"jasmine-console-reporter": "^3.1.0",
"prettier": "^2.5.1",
"shx": "^0.3.4",
"source-map-support": "^0.5.16",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.18.0",
"tslint-immutable": "^6.0.1",
"typescript": "^3.8.3"
},
"dependencies": {
Expand Down
35 changes: 17 additions & 18 deletions js/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function compress(
if (!proof.batch) {
return proof;
}
return { compressed: compress_batch(proof.batch) };
return { compressed: compressBatch(proof.batch) };
}

export function decompress(
Expand All @@ -15,26 +15,25 @@ export function decompress(
if (!proof.compressed) {
return proof;
}
return { batch: decompress_batch(proof.compressed) };
return { batch: decompressBatch(proof.compressed) };
}

// tslint:disable:readonly-array
function compress_batch(proof: ics23.IBatchProof): ics23.ICompressedBatchProof {
function compressBatch(proof: ics23.IBatchProof): ics23.ICompressedBatchProof {
const centries: ics23.ICompressedBatchEntry[] = [];
const lookup: ics23.IInnerOp[] = [];
const registry = new Map<Uint8Array, number>();

for (const entry of proof.entries!) {
if (!!entry.exist) {
const centry = { exist: compress_exist(entry.exist, lookup, registry) };
if (entry.exist) {
const centry = { exist: compressExist(entry.exist, lookup, registry) };
centries.push(centry);
} else if (!!entry.nonexist) {
} else if (entry.nonexist) {
const non = entry.nonexist;
const centry = {
nonexist: {
key: non.key,
left: compress_exist(non.left, lookup, registry),
right: compress_exist(non.right, lookup, registry),
left: compressExist(non.left, lookup, registry),
right: compressExist(non.right, lookup, registry),
},
};
centries.push(centry);
Expand All @@ -49,7 +48,7 @@ function compress_batch(proof: ics23.IBatchProof): ics23.ICompressedBatchProof {
};
}

function compress_exist(
function compressExist(
exist: ics23.IExistenceProof | null | undefined,
lookup: ics23.IInnerOp[],
registry: Map<Uint8Array, number>
Expand Down Expand Up @@ -77,20 +76,20 @@ function compress_exist(
};
}

function decompress_batch(
function decompressBatch(
proof: ics23.ICompressedBatchProof
): ics23.IBatchProof {
const lookup = proof.lookupInners!;
const entries = proof.entries!.map((comp) => {
if (!!comp.exist) {
return { exist: decompress_exist(comp.exist, lookup) };
} else if (!!comp.nonexist) {
if (comp.exist) {
return { exist: decompressExist(comp.exist, lookup) };
} else if (comp.nonexist) {
const non = comp.nonexist;
return {
nonexist: {
key: non.key,
left: decompress_exist(non.left, lookup),
right: decompress_exist(non.right, lookup),
left: decompressExist(non.left, lookup),
right: decompressExist(non.right, lookup),
},
};
} else {
Expand All @@ -102,9 +101,9 @@ function decompress_batch(
};
}

function decompress_exist(
function decompressExist(
exist: ics23.ICompressedExistenceProof | null | undefined,
lookup: ReadonlyArray<ics23.IInnerOp>
lookup: readonly ics23.IInnerOp[]
): ics23.IExistenceProof | undefined {
if (!exist) {
return undefined;
Expand Down
10 changes: 5 additions & 5 deletions js/src/ics23.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function batchVerifyNonMembership(
proof: ics23.ICommitmentProof,
spec: ics23.IProofSpec,
root: CommitmentRoot,
keys: ReadonlyArray<Uint8Array>
keys: readonly Uint8Array[]
): boolean {
const norm = decompress(proof);
for (const key of keys) {
Expand All @@ -111,11 +111,11 @@ function getExistForKey(
proof: ics23.ICommitmentProof,
key: Uint8Array
): ics23.IExistenceProof | undefined | null {
const match = (p: ics23.IExistenceProof | null | undefined) =>
const match = (p: ics23.IExistenceProof | null | undefined): boolean =>
!!p && bytesEqual(key, p.key!);
if (match(proof.exist)) {
return proof.exist!;
} else if (!!proof.batch) {
} else if (proof.batch) {
return proof.batch.entries!.map((x) => x.exist || null).find(match);
}
return undefined;
Expand All @@ -125,7 +125,7 @@ function getNonExistForKey(
proof: ics23.ICommitmentProof,
key: Uint8Array
): ics23.INonExistenceProof | undefined | null {
const match = (p: ics23.INonExistenceProof | null | undefined) => {
const match = (p: ics23.INonExistenceProof | null | undefined): boolean => {
return (
!!p &&
(!p.left || bytesBefore(p.left.key!, key)) &&
Expand All @@ -134,7 +134,7 @@ function getNonExistForKey(
};
if (match(proof.nonexist)) {
return proof.nonexist!;
} else if (!!proof.batch) {
} else if (proof.batch) {
return proof.batch.entries!.map((x) => x.nonexist || null).find(match);
}
return undefined;
Expand Down
1 change: 0 additions & 1 deletion js/src/ops.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ics23 } from "./generated/codecimpl";

import { applyInner, applyLeaf, doHash } from "./ops";
import { fromHex, toAscii } from "./testhelpers.spec";

Expand Down
17 changes: 6 additions & 11 deletions js/src/ops.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// tslint:disable-next-line:no-submodule-imports
import { ripemd160 } from "@noble/hashes/ripemd160";
// tslint:disable-next-line:no-submodule-imports
import { sha256 } from "@noble/hashes/sha256";
// tslint:disable-next-line:no-submodule-imports
import { sha512, sha512_256 } from "@noble/hashes/sha512";

import { ics23 } from "./generated/codecimpl";
Expand Down Expand Up @@ -55,11 +52,11 @@ function ensure<T>(maybe: T | undefined | null, value: T): T {
return maybe === undefined || maybe === null ? value : maybe;
}

const ensureHash = (h: ics23.HashOp | null | undefined) =>
const ensureHash = (h: ics23.HashOp | null | undefined): ics23.HashOp =>
ensure(h, ics23.HashOp.NO_HASH);
const ensureLength = (l: ics23.LengthOp | null | undefined) =>
const ensureLength = (l: ics23.LengthOp | null | undefined): ics23.LengthOp =>
ensure(l, ics23.LengthOp.NO_PREFIX);
const ensureBytes = (b: Uint8Array | null | undefined) =>
const ensureBytes = (b: Uint8Array | null | undefined): Uint8Array =>
ensure(b, new Uint8Array([]));

function prepareLeafData(
Expand Down Expand Up @@ -117,7 +114,7 @@ function doLengthOp(lengthOp: ics23.LengthOp, data: Uint8Array): Uint8Array {
}
return data;
case ics23.LengthOp.FIXED32_LITTLE:
return new Uint8Array([...encodeFixed32LE(data.length), ...data]);
return new Uint8Array([...encodeFixed32Le(data.length), ...data]);
// TODO
// case LengthOp_VAR_RLP:
// case LengthOp_FIXED32_BIG:
Expand All @@ -128,7 +125,7 @@ function doLengthOp(lengthOp: ics23.LengthOp, data: Uint8Array): Uint8Array {
}

function encodeVarintProto(n: number): Uint8Array {
let enc: ReadonlyArray<number> = [];
let enc: readonly number[] = [];
let l = n;
while (l >= 128) {
const b = (l % 128) + 128;
Expand All @@ -139,13 +136,11 @@ function encodeVarintProto(n: number): Uint8Array {
return new Uint8Array(enc);
}

function encodeFixed32LE(n: number): Uint8Array {
function encodeFixed32Le(n: number): Uint8Array {
const enc = new Uint8Array(4);
let l = n;
for (let i = enc.length; i > 0; i--) {
/* tslint:disable */
enc[Math.abs(i - enc.length)] = l % 256;
/* tslint:enable */
l = Math.floor(l / 256);
}
return enc;
Expand Down
1 change: 0 additions & 1 deletion js/src/proofs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ics23 } from "./generated/codecimpl";

import { calculateExistenceRoot, ensureSpec, iavlSpec } from "./proofs";
import { fromHex, toAscii } from "./testhelpers.spec";

Expand Down
15 changes: 7 additions & 8 deletions js/src/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export function verifyNonExistence(
throw new Error("neither left nor right proof defined");
}

if (!!leftKey) {
if (leftKey) {
ensureBytesBefore(leftKey, key);
}
if (!!rightKey) {
if (rightKey) {
ensureBytesBefore(key, rightKey);
}

Expand Down Expand Up @@ -181,7 +181,7 @@ export function ensureSpec(

function ensureLeftMost(
spec: ics23.IInnerSpec,
path: ReadonlyArray<ics23.IInnerOp>
path: readonly ics23.IInnerOp[]
): void {
const { minPrefix, maxPrefix, suffix } = getPadding(spec, 0);

Expand All @@ -195,7 +195,7 @@ function ensureLeftMost(

function ensureRightMost(
spec: ics23.IInnerSpec,
path: ReadonlyArray<ics23.IInnerOp>
path: readonly ics23.IInnerOp[]
): void {
const len = spec.childOrder!.length - 1;
const { minPrefix, maxPrefix, suffix } = getPadding(spec, len);
Expand All @@ -210,10 +210,9 @@ function ensureRightMost(

export function ensureLeftNeighbor(
spec: ics23.IInnerSpec,
left: ReadonlyArray<ics23.IInnerOp>,
right: ReadonlyArray<ics23.IInnerOp>
left: readonly ics23.IInnerOp[],
right: readonly ics23.IInnerOp[]
): void {
// tslint:disable:readonly-array
const mutleft: ics23.IInnerOp[] = [...left];
const mutright: ics23.IInnerOp[] = [...right];

Expand Down Expand Up @@ -296,7 +295,7 @@ function getPadding(spec: ics23.IInnerSpec, branch: number): PaddingResult {
return { minPrefix, maxPrefix, suffix };
}

function getPosition(order: ReadonlyArray<number>, branch: number): number {
function getPosition(order: readonly number[], branch: number): number {
if (branch < 0 || branch >= order.length) {
throw new Error(`Invalid branch: ${branch}`);
}
Expand Down
Loading

0 comments on commit 00a9d52

Please sign in to comment.