Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump ESLint configs and related dependencies #60

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ module.exports = {

{
files: ['*.test.ts', '*.test.js'],
extends: ['@metamask/eslint-config-jest'],
extends: [
'@metamask/eslint-config-jest',
'@metamask/eslint-config-nodejs',
],
},
],

ignorePatterns: ['!.eslintrc.js', '!.prettierrc.js', 'dist/', 'docs/'],
ignorePatterns: [
'!.eslintrc.js',
'!.prettierrc.js',
'dist/',
'docs/',
'.yarn/',
],
};
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,30 @@
"devDependencies": {
"@lavamoat/allow-scripts": "^2.0.3",
"@metamask/auto-changelog": "^2.3.0",
"@metamask/eslint-config": "^9.0.0",
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@metamask/eslint-config": "^11.0.1",
"@metamask/eslint-config-jest": "^11.0.0",
"@metamask/eslint-config-nodejs": "^11.0.1",
"@metamask/eslint-config-typescript": "^11.0.0",
"@types/jest": "^28.1.7",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"eslint": "^7.23.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.3.4",
"eslint-plugin-jsdoc": "^36.1.0",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^39.6.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.2.2",
"json-bigint": "^1.0.0",
"prettier": "^2.2.1",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"stdio-mock": "^1.2.0",
"ts-jest": "^29.0.3",
"tsd": "^0.24.1",
"typedoc": "^0.23.10",
"typescript": "~4.7.4"
"typescript": "~4.8.4"
},
"packageManager": "yarn@3.2.3",
"engines": {
Expand Down
42 changes: 24 additions & 18 deletions src/__fixtures__/bytes.ts

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/__fixtures__/coercions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export const NEGATIVE_INTEGERS = [
-1, -10, -100, -1000, -123456789, -2147483647,
];
export const DECIMAL_NUMBERS = [
1.1, 1.123456789, 1.123456789123456789, -1.1, -1.123456789,
-1.123456789123456789,
1.1, 1.123456789, 1.123456789123456, -1.1, -1.123456789, -1.123456789123456,
];

export const HEX_STRINGS: Hex[] = [
Expand Down
8 changes: 4 additions & 4 deletions src/__fixtures__/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,18 +1063,18 @@ export const JSON_RPC_PENDING_RESPONSE_FIXTURES = {
export const COMPLEX_OBJECT = {
data: {
account: {
__typename: 'Account',
typename: 'Account',
registrations: [
{
__typename: 'Registration',
typename: 'Registration',
domain: {
__typename: 'Domain',
typename: 'Domain',
isMigrated: true,
labelName: 'mycrypto',
labelhash:
'0x9a781ca0d227debc3ee76d547c960b0803a6c9f58c6d3b4722f12ede7e6ef7c9',
name: 'mycrypto.eth',
parent: { __typename: 'Domain', name: 'eth' },
parent: { typename: 'Domain', name: 'eth' },
},
expiryDate: '1754111315',
},
Expand Down
20 changes: 10 additions & 10 deletions src/__fixtures__/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@ export const NUMBER_VALUES = [
{
number: 0,
bigint: BigInt(0),
hex: '0x0',
hexadecimal: '0x0',
},
{
number: 1,
bigint: BigInt(1),
hex: '0x1',
hexadecimal: '0x1',
},
{
number: 16,
bigint: BigInt(16),
hex: '0x10',
hexadecimal: '0x10',
},
{
number: 255,
bigint: BigInt(255),
hex: '0xff',
hexadecimal: '0xff',
},
{
number: 256,
bigint: BigInt(256),
hex: '0x100',
hexadecimal: '0x100',
},
{
number: 65535,
bigint: BigInt(65535),
hex: '0xffff',
hexadecimal: '0xffff',
},
{
number: 65536,
bigint: BigInt(65536),
hex: '0x10000',
hexadecimal: '0x10000',
},
{
number: 4294967295,
bigint: BigInt(4294967295),
hex: '0xffffffff',
hexadecimal: '0xffffffff',
},
{
number: 4294967296,
bigint: BigInt(4294967296),
hex: '0x100000000',
hexadecimal: '0x100000000',
},
{
number: 9007199254740991,
bigint: BigInt(9007199254740991),
hex: '0x1fffffffffffff',
hexadecimal: '0x1fffffffffffff',
},
];
4 changes: 3 additions & 1 deletion src/assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { string } from 'superstruct';
import * as superstructModule from 'superstruct';

import {
assert,
assertExhaustive,
Expand All @@ -18,7 +19,7 @@ describe('assert', () => {
assert(item !== undefined);

// This will fail to compile otherwise
expect(item.foo).toStrictEqual(1);
expect(item.foo).toBe(1);
});

it('throws', () => {
Expand Down Expand Up @@ -98,6 +99,7 @@ describe('assertStruct', () => {

it('includes the value thrown in the message if it is not an error', () => {
jest.spyOn(superstructModule, 'assert').mockImplementation(() => {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw 'foo.';
});

Expand Down
3 changes: 3 additions & 0 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function getErrorMessage(error: unknown): string {
* @param message - The error message.
* @returns The error object.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function getError(ErrorWrapper: AssertionErrorConstructor, message: string) {
if (isConstructable(ErrorWrapper)) {
return new ErrorWrapper({
Expand Down Expand Up @@ -92,6 +93,7 @@ export class AssertionError extends Error {
export function assert(
value: any,
message: string | Error = 'Assertion failed.',
// eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper: AssertionErrorConstructor = AssertionError,
): asserts value {
if (!value) {
Expand All @@ -118,6 +120,7 @@ export function assertStruct<T, S>(
value: unknown,
struct: Struct<T, S>,
errorPrefix = 'Assertion failed',
// eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper: AssertionErrorConstructor = AssertionError,
): asserts value is T {
try {
Expand Down
44 changes: 22 additions & 22 deletions src/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
BYTES_FIXTURES,
INVALID_BYTES_FIXTURES,
LARGE_BYTES_FIXTURES,
TWOS_COMPLEMENT_BYTES_FIXTURES,
UPPER_CASE_HEX_FIXTURES,
UTF_8_BYTES_FIXTURES,
} from './__fixtures__';
import {
assertIsBytes,
bigIntToBytes,
Expand All @@ -15,14 +23,6 @@ import {
stringToBytes,
valueToBytes,
} from './bytes';
import {
BYTES_FIXTURES,
INVALID_BYTES_FIXTURES,
LARGE_BYTES_FIXTURES,
TWOS_COMPLEMENT_BYTES_FIXTURES,
UPPER_CASE_HEX_FIXTURES,
UTF_8_BYTES_FIXTURES,
} from './__fixtures__/bytes';

describe('isBytes', () => {
it('returns true for a Node.js Buffer', () => {
Expand Down Expand Up @@ -55,15 +55,15 @@ describe('assertIsBytes', () => {
describe('bytesToHex', () => {
it.each(BYTES_FIXTURES)(
'returns a hex string from a byte array',
({ bytes, hex }) => {
expect(bytesToHex(bytes)).toBe(hex);
({ bytes, hexadecimal }) => {
expect(bytesToHex(bytes)).toBe(hexadecimal);
},
);

it.each(LARGE_BYTES_FIXTURES)(
'returns a hex string from a large byte array',
({ bytes, hex }) => {
expect(bytesToHex(bytes)).toBe(hex);
({ bytes, hexadecimal }) => {
expect(bytesToHex(bytes)).toBe(hexadecimal);
},
);

Expand Down Expand Up @@ -173,22 +173,22 @@ describe('bytesToString', () => {
describe('hexToBytes', () => {
it.each(BYTES_FIXTURES)(
'returns a byte array from a hex string',
({ bytes, hex }) => {
expect(hexToBytes(hex)).toStrictEqual(bytes);
({ bytes, hexadecimal }) => {
expect(hexToBytes(hexadecimal)).toStrictEqual(bytes);
},
);

it.each(LARGE_BYTES_FIXTURES)(
'returns a byte array from a large hex string',
({ bytes, hex }) => {
expect(hexToBytes(hex)).toStrictEqual(bytes);
({ bytes, hexadecimal }) => {
expect(hexToBytes(hexadecimal)).toStrictEqual(bytes);
},
);

it.each(UPPER_CASE_HEX_FIXTURES)(
'returns a byte array from an upper case hex string',
({ bytes, hex }) => {
expect(hexToBytes(hex)).toStrictEqual(bytes);
({ bytes, hexadecimal }) => {
expect(hexToBytes(hexadecimal)).toStrictEqual(bytes);
},
);

Expand Down Expand Up @@ -367,19 +367,19 @@ describe('stringToBytes', () => {
describe('valueToBytes', () => {
it.each(BYTES_FIXTURES)(
'returns a byte array from a value',
({ bigint, number, hex, bytes }) => {
({ bigint, number, hexadecimal, bytes }) => {
expect(valueToBytes(bigint)).toStrictEqual(bytes);
expect(valueToBytes(number)).toStrictEqual(bytes);
expect(valueToBytes(hex)).toStrictEqual(bytes);
expect(valueToBytes(hexadecimal)).toStrictEqual(bytes);
expect(valueToBytes(bytes)).toBe(bytes);
},
);

it.each(LARGE_BYTES_FIXTURES)(
'returns a byte array from a large value',
({ bigint, hex, bytes }) => {
({ bigint, hexadecimal, bytes }) => {
expect(valueToBytes(bigint)).toStrictEqual(bytes);
expect(valueToBytes(hex)).toStrictEqual(bytes);
expect(valueToBytes(hexadecimal)).toStrictEqual(bytes);
expect(valueToBytes(bytes)).toBe(bytes);
},
);
Expand Down
22 changes: 13 additions & 9 deletions src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export function bytesToHex(bytes: Uint8Array): Hex {
}

const lookupTable = getPrecomputedHexValues();
const hex = new Array(bytes.length);
const hexadecimal = new Array(bytes.length);

for (let i = 0; i < bytes.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
hex[i] = lookupTable[bytes[i]!];
hexadecimal[i] = lookupTable[bytes[i]!];
}

return add0x(hex.join(''));
return add0x(hexadecimal.join(''));
}

/**
Expand All @@ -105,8 +105,8 @@ export function bytesToHex(bytes: Uint8Array): Hex {
export function bytesToBigInt(bytes: Uint8Array): bigint {
assertIsBytes(bytes);

const hex = bytesToHex(bytes);
return BigInt(hex);
const hexadecimal = bytesToHex(bytes);
return BigInt(hexadecimal);
}

/**
Expand Down Expand Up @@ -226,8 +226,8 @@ export function bigIntToBytes(value: bigint): Uint8Array {
assert(typeof value === 'bigint', 'Value must be a bigint.');
assert(value >= BigInt(0), 'Value must be a non-negative bigint.');

const hex = value.toString(16);
return hexToBytes(hex);
const hexadecimal = value.toString(16);
return hexToBytes(hexadecimal);
}

/**
Expand Down Expand Up @@ -301,8 +301,8 @@ export function numberToBytes(value: number): Uint8Array {
'Value is not a safe integer. Use `bigIntToBytes` instead.',
);

const hex = value.toString(16);
return hexToBytes(hex);
const hexadecimal = value.toString(16);
return hexToBytes(hexadecimal);
}

/**
Expand Down Expand Up @@ -417,6 +417,10 @@ export function concatBytes(values: Bytes[]): Uint8Array {
* @returns The {@link DataView}.
*/
export function createDataView(bytes: Uint8Array): DataView {
// To maintain compatibility with Node.js, we need to check if the bytes are
// a Buffer. If so, we need to slice the buffer to get the underlying
// ArrayBuffer.
// eslint-disable-next-line no-restricted-globals
if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {
const buffer = bytes.buffer.slice(
bytes.byteOffset,
Expand Down
2 changes: 1 addition & 1 deletion src/coercers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
NEGATIVE_INTEGERS,
POSITIVE_INTEGERS,
} from './__fixtures__';
import { bytesToHex, hexToBytes } from './bytes';
import { createBigInt, createBytes, createHex, createNumber } from './coercers';
import { add0x } from './hex';
import { bytesToHex, hexToBytes } from './bytes';

describe('createNumber', () => {
it.each(POSITIVE_INTEGERS)(
Expand Down
Loading