@@ -174,7 +174,7 @@ export function uint8ArrayToBuffer(b: Uint8Array): Buffer {
174
174
*
175
175
* @abstract
176
176
*/
177
- export class Layout < T > {
177
+ export abstract class Layout < T > {
178
178
span : number ;
179
179
property ?: string ;
180
180
boundConstructor_ ?: any ;
@@ -237,9 +237,7 @@ export class Layout<T> {
237
237
*
238
238
* @abstract
239
239
*/
240
- decode ( b : Uint8Array , offset ?: number ) : T {
241
- throw new Error ( 'Layout is abstract' ) ;
242
- }
240
+ abstract decode ( b : Uint8Array , offset ?: number ) : T ;
243
241
244
242
/**
245
243
* Encode a JavaScript value into a Uint8Array.
@@ -263,9 +261,7 @@ export class Layout<T> {
263
261
*
264
262
* @abstract
265
263
*/
266
- encode ( src : T , b : Uint8Array , offset ?: number ) : number {
267
- throw new Error ( 'Layout is abstract' ) ;
268
- }
264
+ abstract encode ( src : T , b : Uint8Array , offset ?: number ) : number ;
269
265
270
266
/**
271
267
* Calculate the span of a specific instance of a layout.
@@ -377,6 +373,8 @@ export function nameWithProperty(name: string, lo: { property?: string }): strin
377
373
* @param {Layout } layout - the {@link Layout} instance used to encode
378
374
* instances of `Class`.
379
375
*/
376
+ // `Class` must be a constructor Function, but the assignment of a `layout_` property to it makes it difficult to type
377
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
380
378
export function bindConstructorLayout < T > ( Class : any , layout : Layout < T > ) : void {
381
379
if ( 'function' !== typeof Class ) {
382
380
throw new TypeError ( 'Class must be constructor' ) ;
@@ -428,7 +426,7 @@ export function bindConstructorLayout<T>(Class: any, layout: Layout<T>): void {
428
426
* @abstract
429
427
* @augments {Layout }
430
428
*/
431
- export class ExternalLayout extends Layout < number > {
429
+ export abstract class ExternalLayout extends Layout < number > {
432
430
/**
433
431
* Return `true` iff the external layout decodes to an unsigned
434
432
* integer layout.
@@ -1428,6 +1426,8 @@ export class UnionLayoutDiscriminator extends UnionDiscriminator<number> {
1428
1426
* @augments {Layout }
1429
1427
*/
1430
1428
export class Union extends Layout < LayoutObject > {
1429
+ // `property` is assigned in the Layout constructor
1430
+ // @ts -ignore
1431
1431
property : string ;
1432
1432
discriminator : UnionDiscriminator ;
1433
1433
usesPrefixDiscriminator : boolean ;
@@ -1648,6 +1648,8 @@ export class Union extends Layout<LayoutObject> {
1648
1648
}
1649
1649
dest = this . makeDestinationObject ( ) ;
1650
1650
dest [ dlo . property ] = discr ;
1651
+ // defaultLayout.property can be undefined, but this is allowed by buffer-layout
1652
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1651
1653
dest [ defaultLayout ! . property ! ] = defaultLayout ! . decode ( b , offset + contentOffset ) ;
1652
1654
} else {
1653
1655
dest = clo . decode ( b , offset ) ;
@@ -1666,13 +1668,15 @@ export class Union extends Layout<LayoutObject> {
1666
1668
if ( undefined === vlo ) {
1667
1669
const dlo = this . discriminator ;
1668
1670
// this.defaultLayout is not undefined when vlo is undefined
1671
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1669
1672
const clo = this . defaultLayout ! ;
1670
1673
let contentOffset = 0 ;
1671
1674
if ( this . usesPrefixDiscriminator ) {
1672
1675
contentOffset = ( dlo as UnionLayoutDiscriminator ) . layout . span ;
1673
1676
}
1674
1677
dlo . encode ( src [ dlo . property ] , b , offset ) ;
1675
1678
// clo.property is not undefined when vlo is undefined
1679
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1676
1680
return contentOffset + clo . encode ( src [ clo . property ! ] , b , offset + contentOffset ) ;
1677
1681
}
1678
1682
return vlo . encode ( src , b , offset ) ;
@@ -1752,6 +1756,8 @@ export class Union extends Layout<LayoutObject> {
1752
1756
* @augments {Layout }
1753
1757
*/
1754
1758
export class VariantLayout extends Layout < LayoutObject > {
1759
+ // `property` is assigned in the Layout constructor
1760
+ // @ts -ignore
1755
1761
property : string ;
1756
1762
union : Union ;
1757
1763
variant : number ;
@@ -2033,10 +2039,10 @@ export class BitStructure extends Layout<LayoutObject> {
2033
2039
* Layout#property|property}.
2034
2040
*
2035
2041
* @return {Boolean } */
2042
+ // `Boolean` conflicts with the native primitive type
2036
2043
// eslint-disable-next-line @typescript-eslint/ban-types
2037
2044
addBoolean ( property : string ) : Boolean {
2038
2045
// This is my Boolean, not the Javascript one.
2039
- // eslint-disable-next-line no-new-wrappers
2040
2046
const bf = new Boolean ( this , property ) ;
2041
2047
this . fields . push ( bf ) ;
2042
2048
return bf ;
@@ -2482,159 +2488,157 @@ export class Constant<T> extends Layout<T> {
2482
2488
}
2483
2489
2484
2490
/** Factory for {@link GreedyCount}. */
2485
- export const greedy = ( ( elementSpan : number , property ?: string ) => new GreedyCount ( elementSpan , property ) ) ;
2491
+ export const greedy = ( ( elementSpan : number , property ?: string ) : GreedyCount => new GreedyCount ( elementSpan , property ) ) ;
2486
2492
2487
2493
/** Factory for {@link OffsetLayout}. */
2488
- export const offset
2489
- = ( ( layout : Layout < number > , offset ?: number , property ?: string ) => new OffsetLayout ( layout , offset , property ) ) ;
2494
+ export const offset = ( ( layout : Layout < number > , offset ?: number , property ?: string ) : OffsetLayout =>
2495
+ new OffsetLayout ( layout , offset , property ) ) ;
2490
2496
2491
2497
/** Factory for {@link UInt|unsigned int layouts} spanning one
2492
2498
* byte. */
2493
- export const u8 = ( ( property ?: string ) => new UInt ( 1 , property ) ) ;
2499
+ export const u8 = ( ( property ?: string ) : UInt => new UInt ( 1 , property ) ) ;
2494
2500
2495
2501
/** Factory for {@link UInt|little-endian unsigned int layouts}
2496
2502
* spanning two bytes. */
2497
- export const u16 = ( ( property ?: string ) => new UInt ( 2 , property ) ) ;
2503
+ export const u16 = ( ( property ?: string ) : UInt => new UInt ( 2 , property ) ) ;
2498
2504
2499
2505
/** Factory for {@link UInt|little-endian unsigned int layouts}
2500
2506
* spanning three bytes. */
2501
- export const u24 = ( ( property ?: string ) => new UInt ( 3 , property ) ) ;
2507
+ export const u24 = ( ( property ?: string ) : UInt => new UInt ( 3 , property ) ) ;
2502
2508
2503
2509
/** Factory for {@link UInt|little-endian unsigned int layouts}
2504
2510
* spanning four bytes. */
2505
- export const u32 = ( ( property ?: string ) => new UInt ( 4 , property ) ) ;
2511
+ export const u32 = ( ( property ?: string ) : UInt => new UInt ( 4 , property ) ) ;
2506
2512
2507
2513
/** Factory for {@link UInt|little-endian unsigned int layouts}
2508
2514
* spanning five bytes. */
2509
- export const u40 = ( ( property ?: string ) => new UInt ( 5 , property ) ) ;
2515
+ export const u40 = ( ( property ?: string ) : UInt => new UInt ( 5 , property ) ) ;
2510
2516
2511
2517
/** Factory for {@link UInt|little-endian unsigned int layouts}
2512
2518
* spanning six bytes. */
2513
- export const u48 = ( ( property ?: string ) => new UInt ( 6 , property ) ) ;
2519
+ export const u48 = ( ( property ?: string ) : UInt => new UInt ( 6 , property ) ) ;
2514
2520
2515
2521
/** Factory for {@link NearUInt64|little-endian unsigned int
2516
2522
* layouts} interpreted as Numbers. */
2517
- export const nu64 = ( ( property ?: string ) => new NearUInt64 ( property ) ) ;
2523
+ export const nu64 = ( ( property ?: string ) : NearUInt64 => new NearUInt64 ( property ) ) ;
2518
2524
2519
2525
/** Factory for {@link UInt|big-endian unsigned int layouts}
2520
2526
* spanning two bytes. */
2521
- export const u16be = ( ( property ?: string ) => new UIntBE ( 2 , property ) ) ;
2527
+ export const u16be = ( ( property ?: string ) : UIntBE => new UIntBE ( 2 , property ) ) ;
2522
2528
2523
2529
/** Factory for {@link UInt|big-endian unsigned int layouts}
2524
2530
* spanning three bytes. */
2525
- export const u24be = ( ( property ?: string ) => new UIntBE ( 3 , property ) ) ;
2531
+ export const u24be = ( ( property ?: string ) : UIntBE => new UIntBE ( 3 , property ) ) ;
2526
2532
2527
2533
/** Factory for {@link UInt|big-endian unsigned int layouts}
2528
2534
* spanning four bytes. */
2529
- export const u32be = ( ( property ?: string ) => new UIntBE ( 4 , property ) ) ;
2535
+ export const u32be = ( ( property ?: string ) : UIntBE => new UIntBE ( 4 , property ) ) ;
2530
2536
2531
2537
/** Factory for {@link UInt|big-endian unsigned int layouts}
2532
2538
* spanning five bytes. */
2533
- export const u40be = ( ( property ?: string ) => new UIntBE ( 5 , property ) ) ;
2539
+ export const u40be = ( ( property ?: string ) : UIntBE => new UIntBE ( 5 , property ) ) ;
2534
2540
2535
2541
/** Factory for {@link UInt|big-endian unsigned int layouts}
2536
2542
* spanning six bytes. */
2537
- export const u48be = ( ( property ?: string ) => new UIntBE ( 6 , property ) ) ;
2543
+ export const u48be = ( ( property ?: string ) : UIntBE => new UIntBE ( 6 , property ) ) ;
2538
2544
2539
2545
/** Factory for {@link NearUInt64BE|big-endian unsigned int
2540
2546
* layouts} interpreted as Numbers. */
2541
- export const nu64be = ( ( property ?: string ) => new NearUInt64BE ( property ) ) ;
2547
+ export const nu64be = ( ( property ?: string ) : NearUInt64BE => new NearUInt64BE ( property ) ) ;
2542
2548
2543
2549
/** Factory for {@link Int|signed int layouts} spanning one
2544
2550
* byte. */
2545
- export const s8 = ( ( property ?: string ) => new Int ( 1 , property ) ) ;
2551
+ export const s8 = ( ( property ?: string ) : Int => new Int ( 1 , property ) ) ;
2546
2552
2547
2553
/** Factory for {@link Int|little-endian signed int layouts}
2548
2554
* spanning two bytes. */
2549
- export const s16 = ( ( property ?: string ) => new Int ( 2 , property ) ) ;
2555
+ export const s16 = ( ( property ?: string ) : Int => new Int ( 2 , property ) ) ;
2550
2556
2551
2557
/** Factory for {@link Int|little-endian signed int layouts}
2552
2558
* spanning three bytes. */
2553
- export const s24 = ( ( property ?: string ) => new Int ( 3 , property ) ) ;
2559
+ export const s24 = ( ( property ?: string ) : Int => new Int ( 3 , property ) ) ;
2554
2560
2555
2561
/** Factory for {@link Int|little-endian signed int layouts}
2556
2562
* spanning four bytes. */
2557
- export const s32 = ( ( property ?: string ) => new Int ( 4 , property ) ) ;
2563
+ export const s32 = ( ( property ?: string ) : Int => new Int ( 4 , property ) ) ;
2558
2564
2559
2565
/** Factory for {@link Int|little-endian signed int layouts}
2560
2566
* spanning five bytes. */
2561
- export const s40 = ( ( property ?: string ) => new Int ( 5 , property ) ) ;
2567
+ export const s40 = ( ( property ?: string ) : Int => new Int ( 5 , property ) ) ;
2562
2568
2563
2569
/** Factory for {@link Int|little-endian signed int layouts}
2564
2570
* spanning six bytes. */
2565
- export const s48 = ( ( property ?: string ) => new Int ( 6 , property ) ) ;
2571
+ export const s48 = ( ( property ?: string ) : Int => new Int ( 6 , property ) ) ;
2566
2572
2567
2573
/** Factory for {@link NearInt64|little-endian signed int layouts}
2568
2574
* interpreted as Numbers. */
2569
- export const ns64 = ( ( property ?: string ) => new NearInt64 ( property ) ) ;
2575
+ export const ns64 = ( ( property ?: string ) : NearInt64 => new NearInt64 ( property ) ) ;
2570
2576
2571
2577
/** Factory for {@link Int|big-endian signed int layouts}
2572
2578
* spanning two bytes. */
2573
- export const s16be = ( ( property ?: string ) => new IntBE ( 2 , property ) ) ;
2579
+ export const s16be = ( ( property ?: string ) : IntBE => new IntBE ( 2 , property ) ) ;
2574
2580
2575
2581
/** Factory for {@link Int|big-endian signed int layouts}
2576
2582
* spanning three bytes. */
2577
- export const s24be = ( ( property ?: string ) => new IntBE ( 3 , property ) ) ;
2583
+ export const s24be = ( ( property ?: string ) : IntBE => new IntBE ( 3 , property ) ) ;
2578
2584
2579
2585
/** Factory for {@link Int|big-endian signed int layouts}
2580
2586
* spanning four bytes. */
2581
- export const s32be = ( ( property ?: string ) => new IntBE ( 4 , property ) ) ;
2587
+ export const s32be = ( ( property ?: string ) : IntBE => new IntBE ( 4 , property ) ) ;
2582
2588
2583
2589
/** Factory for {@link Int|big-endian signed int layouts}
2584
2590
* spanning five bytes. */
2585
- export const s40be = ( ( property ?: string ) => new IntBE ( 5 , property ) ) ;
2591
+ export const s40be = ( ( property ?: string ) : IntBE => new IntBE ( 5 , property ) ) ;
2586
2592
2587
2593
/** Factory for {@link Int|big-endian signed int layouts}
2588
2594
* spanning six bytes. */
2589
- export const s48be = ( ( property ?: string ) => new IntBE ( 6 , property ) ) ;
2595
+ export const s48be = ( ( property ?: string ) : IntBE => new IntBE ( 6 , property ) ) ;
2590
2596
2591
2597
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
2592
2598
* interpreted as Numbers. */
2593
- export const ns64be = ( ( property ?: string ) => new NearInt64BE ( property ) ) ;
2599
+ export const ns64be = ( ( property ?: string ) : NearInt64BE => new NearInt64BE ( property ) ) ;
2594
2600
2595
2601
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
2596
- export const f32 = ( ( property ?: string ) => new Float ( property ) ) ;
2602
+ export const f32 = ( ( property ?: string ) : Float => new Float ( property ) ) ;
2597
2603
2598
2604
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
2599
- export const f32be = ( ( property ?: string ) => new FloatBE ( property ) ) ;
2605
+ export const f32be = ( ( property ?: string ) : FloatBE => new FloatBE ( property ) ) ;
2600
2606
2601
2607
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
2602
- export const f64 = ( ( property ?: string ) => new Double ( property ) ) ;
2608
+ export const f64 = ( ( property ?: string ) : Double => new Double ( property ) ) ;
2603
2609
2604
2610
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
2605
- export const f64be = ( ( property ?: string ) => new DoubleBE ( property ) ) ;
2611
+ export const f64be = ( ( property ?: string ) : DoubleBE => new DoubleBE ( property ) ) ;
2606
2612
2607
2613
/** Factory for {@link Structure} values. */
2608
- export const struct
2609
- = ( < T > ( fields : Layout < T [ keyof T ] > [ ] , property ?: string , decodePrefixes ?: boolean ) =>
2614
+ export const struct = ( < T > ( fields : Layout < T [ keyof T ] > [ ] , property ?: string , decodePrefixes ?: boolean ) : Structure < T > =>
2610
2615
new Structure < T > ( fields , property , decodePrefixes ) ) ;
2611
2616
2612
2617
/** Factory for {@link BitStructure} values. */
2613
- export const bits
2614
- = ( ( word : UInt | UIntBE , msb : boolean | string , property ?: string ) => new BitStructure ( word , msb , property ) ) ;
2618
+ export const bits = ( ( word : UInt | UIntBE , msb : boolean | string , property ?: string ) : BitStructure =>
2619
+ new BitStructure ( word , msb , property ) ) ;
2615
2620
2616
2621
/** Factory for {@link Sequence} values. */
2617
- export const seq
2618
- = ( < T > ( elementLayout : Layout < T > , count : number | ExternalLayout , property ?: string ) =>
2622
+ export const seq = ( < T > ( elementLayout : Layout < T > , count : number | ExternalLayout , property ?: string ) : Sequence < T > =>
2619
2623
new Sequence < T > ( elementLayout , count , property ) ) ;
2620
2624
2621
2625
/** Factory for {@link Union} values. */
2622
- export const union
2623
- = ( ( discr : Layout < LayoutObject > | UnionDiscriminator , defaultLayout : Layout < LayoutObject > | null , property : string ) =>
2626
+ export const union = ( ( discr : Layout < LayoutObject > | UnionDiscriminator ,
2627
+ defaultLayout : Layout < LayoutObject > | null , property : string ) : Union =>
2624
2628
new Union ( discr , defaultLayout , property ) ) ;
2625
2629
2626
2630
/** Factory for {@link UnionLayoutDiscriminator} values. */
2627
- export const unionLayoutDiscriminator
2628
- = ( ( layout : ExternalLayout , property : string ) => new UnionLayoutDiscriminator ( layout , property ) ) ;
2631
+ export const unionLayoutDiscriminator = ( ( layout : ExternalLayout , property : string ) : UnionLayoutDiscriminator =>
2632
+ new UnionLayoutDiscriminator ( layout , property ) ) ;
2629
2633
2630
2634
/** Factory for {@link Blob} values. */
2631
- export const blob = ( ( length : number | ExternalLayout , property ?: string ) => new Blob ( length , property ) ) ;
2635
+ export const blob = ( ( length : number | ExternalLayout , property ?: string ) : Blob => new Blob ( length , property ) ) ;
2632
2636
2633
2637
/** Factory for {@link CString} values. */
2634
- export const cstr = ( ( property ?: string ) => new CString ( property ) ) ;
2638
+ export const cstr = ( ( property ?: string ) : CString => new CString ( property ) ) ;
2635
2639
2636
2640
/** Factory for {@link UTF8} values. */
2637
- export const utf8 = ( ( maxSpan : number , property ?: string ) => new UTF8 ( maxSpan , property ) ) ;
2641
+ export const utf8 = ( ( maxSpan : number , property ?: string ) : UTF8 => new UTF8 ( maxSpan , property ) ) ;
2638
2642
2639
2643
/** Factory for {@link Constant} values. */
2640
- export const constant = ( < T > ( value : T , property ?: string ) => new Constant ( value , property ) ) ;
2644
+ export const constant = ( < T > ( value : T , property ?: string ) : Constant < T > => new Constant ( value , property ) ) ;
0 commit comments