Skip to content

Commit

Permalink
Merge branch 'main' into devdeps-lavamoat-allow-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gudahtt authored Jul 12, 2024
2 parents f34c22c + b8f869d commit 3e4ec19
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 64 deletions.
28 changes: 27 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [9.0.0]
### Changed
- **BREAKING:** The return types of functions `getChecksumAddress`, `numberToHex`, `bigIntToHex` are narrowed from `string` to `Hex` ([#193](https://github.com/MetaMask/utils/pull/193))

### Fixed
- Bump `@metamask/superstruct` from `^3.0.0` to `^3.1.0` ([#194](https://github.com/MetaMask/utils/pull/194))
- If `@metamask/utils` `<=8.5.0` is used with `@metamask/superstruct` `>=3.1.0` the following error may be encountered:
```ts
error TS2742: The inferred type of 'ExampleType' cannot be named without a reference to '@metamask/utils/node_modules/@metamask/superstruct'. This is likely not portable. A type annotation is necessary.
```
This can be resolved by updating `@metamask/utils` to `>=9.0.0`.
## [8.5.0]
### Changed
- Bump dependency `semver` from `^5.7.1` to `^7.6.0` ([#181](https://github.com/MetaMask/utils/pull/181)).
### Fixed
- Replace dependency `superstruct` `^1.0.3` with ESM-compatible `@metamask/superstruct` `^3.0.0` ([#185](https://github.com/MetaMask/utils/pull/185)).
- This fixes the issue of this package being unusable by any TypeScript project that uses `Node16` or `NodeNext` as its `moduleResolution` option.
- Produce and export ESM-compatible TypeScript type declaration files in addition to CommonJS-compatible declaration files ([#182](https://github.com/MetaMask/utils/pull/182))
- Previously, this package shipped with only one variant of type declaration files, and these files were only CommonJS-compatible, and the `exports` field in `package.json` linked to these files. This is an anti-pattern and was rightfully flagged by the ["Are the Types Wrong?"](https://arethetypeswrong.github.io/) tool as ["masquerading as CJS"](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md). All of the ATTW checks now pass.
- Remove chunk files ([#182](https://github.com/MetaMask/utils/pull/182)).
- Previously, the build tool we used to generate JavaScript files extracted common code to "chunk" files. While this was intended to make this package more tree-shakeable, it also made debugging more difficult for our development teams. These chunk files are no longer present.
## [8.4.0]
### Added
- Add `toCaipChainId` utility function ([#175](https://github.com/MetaMask/utils/pull/175))
Expand Down Expand Up @@ -223,7 +247,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release
[Unreleased]: https://github.com/MetaMask/utils/compare/v8.4.0...HEAD
[Unreleased]: https://github.com/MetaMask/utils/compare/v9.0.0...HEAD
[9.0.0]: https://github.com/MetaMask/utils/compare/v8.5.0...v9.0.0
[8.5.0]: https://github.com/MetaMask/utils/compare/v8.4.0...v8.5.0
[8.4.0]: https://github.com/MetaMask/utils/compare/v8.3.0...v8.4.0
[8.3.0]: https://github.com/MetaMask/utils/compare/v8.2.1...v8.3.0
[8.2.1]: https://github.com/MetaMask/utils/compare/v8.2.0...v8.2.1
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/utils",
"version": "8.4.0",
"version": "9.0.0",
"description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase",
"homepage": "https://github.com/MetaMask/utils#readme",
"bugs": {
Expand Down Expand Up @@ -62,13 +62,13 @@
},
"dependencies": {
"@ethereumjs/tx": "^4.2.0",
"@metamask/superstruct": "^3.1.0",
"@noble/hashes": "^1.3.1",
"@scure/base": "^1.1.3",
"@types/debug": "^4.1.7",
"debug": "^4.3.4",
"pony-cause": "^2.1.10",
"semver": "^7.5.4",
"superstruct": "^1.0.3",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down Expand Up @@ -106,7 +106,7 @@
"ts-node": "^10.7.0",
"tsd": "^0.29.0",
"typedoc": "^0.23.15",
"typescript": "~4.8.4"
"typescript": "~5.0.4"
},
"packageManager": "yarn@3.2.3",
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions src/assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, assert as superstructAssert } from 'superstruct';
import { string, assert as superstructAssert } from '@metamask/superstruct';

import {
assert,
Expand All @@ -7,8 +7,8 @@ import {
assertStruct,
} from './assert';

jest.mock('superstruct', () => ({
...jest.requireActual('superstruct'),
jest.mock('@metamask/superstruct', () => ({
...jest.requireActual('@metamask/superstruct'),
assert: jest.fn(),
}));

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('assertExhaustive', () => {

describe('assertStruct', () => {
beforeEach(() => {
const actual = jest.requireActual('superstruct');
const actual = jest.requireActual('@metamask/superstruct');
(
superstructAssert as jest.MockedFunction<typeof superstructAssert>
).mockImplementation(actual.assert);
Expand Down
4 changes: 2 additions & 2 deletions src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Struct } from 'superstruct';
import { assert as assertSuperstruct } from 'superstruct';
import type { Struct } from '@metamask/superstruct';
import { assert as assertSuperstruct } from '@metamask/superstruct';

import { getErrorMessage } from './errors';

Expand Down
2 changes: 1 addition & 1 deletion src/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is, size, string } from 'superstruct';
import { is, size, string } from '@metamask/superstruct';

import type { Base64Options } from './base64';
import { base64 } from './base64';
Expand Down
4 changes: 2 additions & 2 deletions src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Struct } from 'superstruct';
import { pattern } from 'superstruct';
import type { Struct } from '@metamask/superstruct';
import { pattern } from '@metamask/superstruct';

import { assert } from './assert';

Expand Down
4 changes: 2 additions & 2 deletions src/caip-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Infer } from 'superstruct';
import { is, pattern, string } from 'superstruct';
import type { Infer } from '@metamask/superstruct';
import { is, pattern, string } from '@metamask/superstruct';

export const CAIP_CHAIN_ID_REGEX =
/^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;
Expand Down
2 changes: 1 addition & 1 deletion src/checksum.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is } from 'superstruct';
import { is } from '@metamask/superstruct';

import { ChecksumStruct } from './checksum';

Expand Down
2 changes: 1 addition & 1 deletion src/checksum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { size, string } from 'superstruct';
import { size, string } from '@metamask/superstruct';

import { base64 } from './base64';

Expand Down
4 changes: 2 additions & 2 deletions src/coercers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Infer } from 'superstruct';
import type { Infer } from '@metamask/superstruct';
import {
bigint,
coerce,
Expand All @@ -8,7 +8,7 @@ import {
string,
StructError,
union,
} from 'superstruct';
} from '@metamask/superstruct';

import { assert } from './assert';
import { bytesToHex, hexToBytes } from './bytes';
Expand Down
6 changes: 3 additions & 3 deletions src/hex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Struct } from '@metamask/superstruct';
import { is, pattern, string } from '@metamask/superstruct';
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
import type { Struct } from 'superstruct';
import { is, pattern, string } from 'superstruct';

import { assert } from './assert';
import { bytesToHex } from './bytes';
Expand Down Expand Up @@ -87,7 +87,7 @@ export function isValidHexAddress(possibleAddress: Hex) {
* @returns The address encoded according to ERC-55.
* @see https://eips.ethereum.org/EIPS/eip-55
*/
export function getChecksumAddress(address: Hex) {
export function getChecksumAddress(address: Hex): Hex {
assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');
const unPrefixed = remove0x(address.toLowerCase());
const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
Expand Down
4 changes: 2 additions & 2 deletions src/json.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions */

import type { Infer } from 'superstruct';
import { boolean, number, optional, string } from 'superstruct';
import type { Infer } from '@metamask/superstruct';
import { boolean, number, optional, string } from '@metamask/superstruct';
import { expectAssignable, expectNotAssignable } from 'tsd';

import type { Json } from '.';
Expand Down
8 changes: 4 additions & 4 deletions src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
max,
number,
optional,
} from 'superstruct';
} from '@metamask/superstruct';

import {
assert,
Expand Down Expand Up @@ -46,8 +46,8 @@ import {
JSON_VALIDATION_FIXTURES,
} from './__fixtures__';

jest.mock('superstruct', () => ({
...jest.requireActual('superstruct'),
jest.mock('@metamask/superstruct', () => ({
...jest.requireActual('@metamask/superstruct'),
assert: jest.fn(),
}));

Expand Down Expand Up @@ -225,7 +225,7 @@ describe('exactOptional', () => {

describe('json', () => {
beforeEach(() => {
const actual = jest.requireActual('superstruct');
const actual = jest.requireActual('@metamask/superstruct');
(
superstructAssert as jest.MockedFunction<typeof superstructAssert>
).mockImplementation(actual.assert);
Expand Down
9 changes: 5 additions & 4 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Context, Infer } from 'superstruct';
import {
any,
array,
Expand All @@ -19,12 +18,14 @@ import {
union,
unknown,
Struct,
} from 'superstruct';
} from '@metamask/superstruct';
import type {
Context,
Infer,
ObjectSchema,
Optionalize,
Simplify,
} from 'superstruct/dist/utils';
Optionalize,
} from '@metamask/superstruct';

import type { AssertionErrorConstructor } from './assert';
import { assertStruct } from './assert';
Expand Down
22 changes: 21 additions & 1 deletion src/misc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { expectAssignable, expectNotAssignable, expectType } from 'tsd';

import type { RuntimeObject } from './misc';
import type { PublicInterface, RuntimeObject } from './misc';
import { isObject, hasProperty, getKnownPropertyNames } from './misc';

//=============================================================================
// PublicInterface
//=============================================================================

class ClassWithPrivateProperties {
#foo: string;

bar: string;

constructor({ foo, bar }: { foo: string; bar: string }) {
this.#foo = foo;
this.bar = bar;
}
}

// Private properties not required
expectAssignable<PublicInterface<ClassWithPrivateProperties>>({ bar: 'bar' });
// Public properties still required
expectNotAssignable<PublicInterface<ClassWithPrivateProperties>>({});

//=============================================================================
// isObject
//=============================================================================
Expand Down
9 changes: 9 additions & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export type Mutable<
[Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];
};

/**
* Get a type representing the public interface of the given type. The
* returned type will have all public properties, but will omit private
* properties.
*
* @template Interface - The interface to return a public representation of.
*/
export type PublicInterface<Interface> = Pick<Interface, keyof Interface>;

/**
* Useful for representing some value that _might_ be present and / or complete.
*
Expand Down
5 changes: 3 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from './assert';
import type { Hex } from './hex';
import { add0x, assertIsHexString } from './hex';

/**
Expand All @@ -18,7 +19,7 @@ import { add0x, assertIsHexString } from './hex';
* @returns The hexadecimal string, with the "0x"-prefix.
* @throws If the number is not a non-negative safe integer.
*/
export const numberToHex = (value: number): string => {
export const numberToHex = (value: number): Hex => {
assert(typeof value === 'number', 'Value must be a number.');
assert(value >= 0, 'Value must be a non-negative number.');
assert(
Expand All @@ -45,7 +46,7 @@ export const numberToHex = (value: number): string => {
* @returns The hexadecimal string, with the "0x"-prefix.
* @throws If the `bigint` is not a non-negative integer.
*/
export const bigIntToHex = (value: bigint): string => {
export const bigIntToHex = (value: bigint): Hex => {
assert(typeof value === 'bigint', 'Value must be a bigint.');
assert(value >= 0, 'Value must be a non-negative bigint.');

Expand Down
4 changes: 2 additions & 2 deletions src/versions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Struct } from '@metamask/superstruct';
import { is, refine, string } from '@metamask/superstruct';
import {
gt as gtSemver,
gtr as gtrSemver,
satisfies as satisfiesSemver,
valid as validSemVerVersion,
validRange as validSemVerRange,
} from 'semver';
import type { Struct } from 'superstruct';
import { is, refine, string } from 'superstruct';

import { assertStruct } from './assert';
import type { Opaque } from './opaque';
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2020", "dom"],
"module": "CommonJS",
"moduleResolution": "node",
"module": "nodenext",
"moduleResolution": "nodenext",
"noEmit": true,
"noErrorTruncation": true,
"noUncheckedIndexedAccess": true,
Expand Down
Loading

0 comments on commit 3e4ec19

Please sign in to comment.