From a7e3c8eed71a79a9fc4c5bae8058374a8600dd36 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Sat, 18 Jan 2025 17:12:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B2:=20Annotate=20pure=20functions=20w?= =?UTF-8?q?ith=20`@=5F=5FNO=5FSIDE=5FEFFECTS=5F=5F`=20to=20enable=20better?= =?UTF-8?q?=20tree-shaking.=20(#45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/typed-binary/package.json | 1 + packages/typed-binary/src/io/unwrapBuffer.ts | 1 + packages/typed-binary/src/structure/array.ts | 1 + packages/typed-binary/src/structure/chars.ts | 1 + packages/typed-binary/src/structure/concat.ts | 1 + packages/typed-binary/src/structure/dynamicArray.ts | 1 + packages/typed-binary/src/structure/keyed.ts | 1 + packages/typed-binary/src/structure/object.ts | 5 +++++ packages/typed-binary/src/structure/optional.ts | 1 + packages/typed-binary/src/structure/tuple.ts | 2 ++ packages/typed-binary/src/structure/typedArray.ts | 9 +++++++++ packages/typed-binary/src/util.ts | 2 ++ 12 files changed, 26 insertions(+) diff --git a/packages/typed-binary/package.json b/packages/typed-binary/package.json index 3ae782e..a2f06c7 100644 --- a/packages/typed-binary/package.json +++ b/packages/typed-binary/package.json @@ -1,6 +1,7 @@ { "name": "typed-binary", "version": "4.3.1", + "sideEffects": false, "description": "Describe binary structures with full TypeScript support. Encode and decode into pure JavaScript objects.", "packageManager": "pnpm@8.15.8+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e", "type": "module", diff --git a/packages/typed-binary/src/io/unwrapBuffer.ts b/packages/typed-binary/src/io/unwrapBuffer.ts index 1557d16..34c3963 100644 --- a/packages/typed-binary/src/io/unwrapBuffer.ts +++ b/packages/typed-binary/src/io/unwrapBuffer.ts @@ -7,6 +7,7 @@ interface UnwrapBufferResult { /** * Removes up to one layer of view over a buffer. */ +// @__NO_SIDE_EFFECTS__ export function unwrapBuffer( buffer: ArrayBufferLike | ArrayBufferView, ): UnwrapBufferResult { diff --git a/packages/typed-binary/src/structure/array.ts b/packages/typed-binary/src/structure/array.ts index 231d32c..522a78c 100644 --- a/packages/typed-binary/src/structure/array.ts +++ b/packages/typed-binary/src/structure/array.ts @@ -82,6 +82,7 @@ export class ArraySchema extends Schema< } } +// @__NO_SIDE_EFFECTS__ export function arrayOf( elementSchema: TSchema, length: number, diff --git a/packages/typed-binary/src/structure/chars.ts b/packages/typed-binary/src/structure/chars.ts index 02d2083..8b89e98 100644 --- a/packages/typed-binary/src/structure/chars.ts +++ b/packages/typed-binary/src/structure/chars.ts @@ -37,6 +37,7 @@ export class CharsSchema< } } +// @__NO_SIDE_EFFECTS__ export function chars(length: T): CharsSchema { return new CharsSchema(length); } diff --git a/packages/typed-binary/src/structure/concat.ts b/packages/typed-binary/src/structure/concat.ts index 8ffafb0..71a60ae 100644 --- a/packages/typed-binary/src/structure/concat.ts +++ b/packages/typed-binary/src/structure/concat.ts @@ -6,6 +6,7 @@ type Concat = ObjectSchema< MergeRecordUnion> >; +// @__NO_SIDE_EFFECTS__ export function concat( objs: Objs, ): Concat { diff --git a/packages/typed-binary/src/structure/dynamicArray.ts b/packages/typed-binary/src/structure/dynamicArray.ts index 83bbcd8..7fd16cb 100644 --- a/packages/typed-binary/src/structure/dynamicArray.ts +++ b/packages/typed-binary/src/structure/dynamicArray.ts @@ -119,6 +119,7 @@ export class DynamicArraySchema extends Schema< } } +// @__NO_SIDE_EFFECTS__ export function dynamicArrayOf( elementSchema: TSchema, ): DynamicArraySchema { diff --git a/packages/typed-binary/src/structure/keyed.ts b/packages/typed-binary/src/structure/keyed.ts index 5443770..3aeb791 100644 --- a/packages/typed-binary/src/structure/keyed.ts +++ b/packages/typed-binary/src/structure/keyed.ts @@ -145,6 +145,7 @@ export class KeyedSchema< } } +// @__NO_SIDE_EFFECTS__ export function keyed>( key: K, inner: (ref: ISchema>) => P, diff --git a/packages/typed-binary/src/structure/object.ts b/packages/typed-binary/src/structure/object.ts index e19de60..8eda59a 100644 --- a/packages/typed-binary/src/structure/object.ts +++ b/packages/typed-binary/src/structure/object.ts @@ -15,12 +15,14 @@ import { type UnwrapRecord, } from './types.ts'; +// @__NO_SIDE_EFFECTS__ export function exactEntries>( record: T, ): [keyof T, T[keyof T]][] { return Object.entries(record) as [keyof T, T[keyof T]][]; } +// @__NO_SIDE_EFFECTS__ export function resolveMap>( ctx: IRefResolver, refs: T, @@ -134,6 +136,7 @@ export class ObjectSchema> } } +// @__NO_SIDE_EFFECTS__ export function object

>( properties: P, ): ObjectSchema

{ @@ -306,6 +309,7 @@ export class GenericObjectSchema< } } +// @__NO_SIDE_EFFECTS__ export function generic< P extends Record, S extends { @@ -315,6 +319,7 @@ export function generic< return new GenericObjectSchema(SubTypeKey.STRING, properties, subTypeMap); } +// @__NO_SIDE_EFFECTS__ export function genericEnum< P extends Record, S extends { diff --git a/packages/typed-binary/src/structure/optional.ts b/packages/typed-binary/src/structure/optional.ts index 1db6aee..1a7d33a 100644 --- a/packages/typed-binary/src/structure/optional.ts +++ b/packages/typed-binary/src/structure/optional.ts @@ -72,6 +72,7 @@ export class OptionalSchema extends Schema< } } +// @__NO_SIDE_EFFECTS__ export function optional( innerType: TSchema, ): OptionalSchema { diff --git a/packages/typed-binary/src/structure/tuple.ts b/packages/typed-binary/src/structure/tuple.ts index 78f3139..4a3b6df 100644 --- a/packages/typed-binary/src/structure/tuple.ts +++ b/packages/typed-binary/src/structure/tuple.ts @@ -10,6 +10,7 @@ import { type UnwrapArray, } from './types.ts'; +// @__NO_SIDE_EFFECTS__ export function resolveArray( ctx: IRefResolver, refs: T, @@ -88,6 +89,7 @@ export class TupleSchema< } } +// @__NO_SIDE_EFFECTS__ export function tupleOf( schemas: TSchema, ): TupleSchema { diff --git a/packages/typed-binary/src/structure/typedArray.ts b/packages/typed-binary/src/structure/typedArray.ts index 1db70b6..38f7667 100644 --- a/packages/typed-binary/src/structure/typedArray.ts +++ b/packages/typed-binary/src/structure/typedArray.ts @@ -41,31 +41,40 @@ export class TypedArraySchema< } } +// @__NO_SIDE_EFFECTS__ export const u8Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint8Array); +// @__NO_SIDE_EFFECTS__ export const u8ClampedArray = ( length: number, ): TypedArraySchema => new TypedArraySchema(length, Uint8ClampedArray); +// @__NO_SIDE_EFFECTS__ export const u16Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint16Array); +// @__NO_SIDE_EFFECTS__ export const u32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint32Array); +// @__NO_SIDE_EFFECTS__ export const i8Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int8Array); +// @__NO_SIDE_EFFECTS__ export const i16Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int16Array); +// @__NO_SIDE_EFFECTS__ export const i32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int32Array); +// @__NO_SIDE_EFFECTS__ export const f32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Float32Array); +// @__NO_SIDE_EFFECTS__ export const f64Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Float64Array); diff --git a/packages/typed-binary/src/util.ts b/packages/typed-binary/src/util.ts index 8f2fe4f..fddf679 100644 --- a/packages/typed-binary/src/util.ts +++ b/packages/typed-binary/src/util.ts @@ -1,6 +1,7 @@ /** * @returns {Boolean} true if system is big endian */ +// @__NO_SIDE_EFFECTS__ function isSystemBigEndian(): boolean { const array = new Uint8Array(4); const view = new Uint32Array(array.buffer); @@ -10,6 +11,7 @@ function isSystemBigEndian(): boolean { return array[0] === 0; // if zero is the left-most byte, one was encoded as big endian } +// @__NO_SIDE_EFFECTS__ export function getSystemEndianness(): 'big' | 'little' { return isSystemBigEndian() ? 'big' : 'little'; }