Skip to content

Commit eda53cb

Browse files
committed
chore: TS noUncheckedIndexedAccess - partial fixes
1 parent b2b8dd8 commit eda53cb

File tree

17 files changed

+100
-85
lines changed

17 files changed

+100
-85
lines changed

modules/core/src/adapter-utils/format-compiler-log.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) vis.gl contributors
44

55
import type {CompilerMessage} from '../adapter/types/compiler-message';
6+
import {assertDefined} from '../utils/assert';
67

78
/** @returns annotated errors or warnings */
89
export function formatCompilerLog(
@@ -21,20 +22,22 @@ export function formatCompilerLog(
2122
switch (options?.showSourceCode || 'no') {
2223
case 'all':
2324
// Parse the error - note: browser and driver dependent
24-
let currentMessage = 0;
25+
let currentMessageIndex = 0;
2526
for (let lineNum = 1; lineNum <= lines.length; lineNum++) {
26-
formattedLog += getNumberedLine(lines[lineNum - 1], lineNum, options);
27-
while (log.length > currentMessage && log[currentMessage].lineNum === lineNum) {
28-
const message = log[currentMessage++];
27+
const line = assertDefined(lines[lineNum - 1]);
28+
const currentMessage = assertDefined(log[currentMessageIndex]);
29+
formattedLog += getNumberedLine(line, lineNum, options);
30+
while (log.length > currentMessageIndex && currentMessage.lineNum === lineNum) {
31+
const message = assertDefined(log[currentMessageIndex++]);
2932
formattedLog += formatCompilerMessage(message, lines, message.lineNum, {
3033
...options,
3134
inlineSource: false
3235
});
3336
}
3437
}
3538
// Print any remaining messages
36-
while (log.length > currentMessage) {
37-
const message = log[currentMessage++];
39+
while (log.length > currentMessageIndex) {
40+
const message = assertDefined(log[currentMessageIndex++]);
3841
formattedLog += formatCompilerMessage(message, [], 0, {
3942
...options,
4043
inlineSource: false

modules/core/src/adapter/canvas-context.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {Framebuffer} from './resources/framebuffer';
88
import type {TextureFormatDepthStencil} from '../shadertypes/textures/texture-formats';
99
import {uid} from '../utils/uid';
1010
import {withResolvers} from '../utils/promise-utils';
11+
import {assertDefined} from '../utils/assert';
1112

1213
/** Properties for a CanvasContext */
1314
export type CanvasContextProps = {
@@ -346,27 +347,23 @@ export abstract class CanvasContext {
346347
*/
347348
protected _handleResize(entries: ResizeObserverEntry[]) {
348349
const entry = entries.find(entry_ => entry_.target === this.canvas);
349-
if (!entry) {
350-
return;
351-
}
352-
350+
const contentBoxSize = assertDefined(entry?.contentBoxSize?.[0]);
353351
// Update CSS size using content box size
354-
this.cssWidth = entry.contentBoxSize[0].inlineSize;
355-
this.cssHeight = entry.contentBoxSize[0].blockSize;
352+
this.cssWidth = contentBoxSize.inlineSize;
353+
this.cssHeight = contentBoxSize.blockSize;
356354

357355
// Update our drawing buffer size variables, saving the old values for logging
358356
const oldPixelSize = this.getDevicePixelSize();
359357

360358
// Use the most accurate drawing buffer size information the current browser can provide
361359
// Note: content box sizes are guaranteed to be integers
362360
// Note: Safari falls back to contentBoxSize
361+
const devicePixelContentBoxSize = assertDefined(entry?.devicePixelContentBoxSize?.[0]);
363362
const devicePixelWidth =
364-
entry.devicePixelContentBoxSize?.[0].inlineSize ||
365-
entry.contentBoxSize[0].inlineSize * devicePixelRatio;
363+
devicePixelContentBoxSize.inlineSize || contentBoxSize.inlineSize * devicePixelRatio;
366364

367365
const devicePixelHeight =
368-
entry.devicePixelContentBoxSize?.[0].blockSize ||
369-
entry.contentBoxSize[0].blockSize * devicePixelRatio;
366+
devicePixelContentBoxSize.blockSize || contentBoxSize.blockSize * devicePixelRatio;
370367

371368
// Make sure we don't overflow the maximum supported texture size
372369
const [maxDevicePixelWidth, maxDevicePixelHeight] = this.getMaxDrawingBufferSize();

modules/core/src/adapter/resources/command-encoder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export type CopyBufferToTextureOptions = {
2727
byteOffset?: number;
2828
destinationTexture: Texture;
2929
mipLevel?: number; // = 0;
30-
origin?: [number, number, number] | number[];
30+
origin?: [number, number, number];
3131
aspect?: 'all' | 'stencil-only' | 'depth-only';
3232
bytesPerRow: number;
3333
rowsPerImage: number;
34-
size: [number, number, number] | number[];
34+
size: [number, number, number];
3535
};
3636

3737
export type CopyTextureToBufferOptions = {
@@ -49,7 +49,7 @@ export type CopyTextureToBufferOptions = {
4949
width?: number;
5050
height?: number;
5151
depthOrArrayLayers?: number;
52-
origin?: number[];
52+
origin?: [number, number, number];
5353

5454
/** Destination buffer */
5555
destinationBuffer: Buffer;
@@ -74,7 +74,7 @@ export type CopyTextureToTextureOptions = {
7474
/** Mip-map level of the texture to copy to/from. (Default 0) */
7575
mipLevel?: number;
7676
/** Defines the origin of the copy - the minimum corner of the texture sub-region to copy from. */
77-
origin?: number[];
77+
origin?: [number, number, number];
7878
/** Defines which aspects of the {@link GPUImageCopyTexture#texture} to copy to/from. */
7979
aspect?: 'all' | 'stencil-only' | 'depth-only';
8080

@@ -83,7 +83,7 @@ export type CopyTextureToTextureOptions = {
8383
/** Mip-map level of the texture to copy to/from. (Default 0) */
8484
destinationMipLevel?: number;
8585
/** Defines the origin of the copy - the minimum corner of the texture sub-region to copy to. */
86-
destinationOrigin?: number[];
86+
destinationOrigin?: [number, number, number];
8787
/** Defines which aspects of the {@link GPUImageCopyTexture#texture} to copy to/from. */
8888
destinationAspect?: 'all' | 'stencil-only' | 'depth-only';
8989

modules/core/src/adapter/resources/framebuffer.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,15 @@ export abstract class Framebuffer extends Resource<FramebufferProps> {
146146
* and destroys existing textures if owned
147147
*/
148148
protected resizeAttachments(width: number, height: number): void {
149-
for (let i = 0; i < this.colorAttachments.length; ++i) {
150-
if (this.colorAttachments[i]) {
151-
const resizedTexture = this.colorAttachments[i].texture.clone({
152-
width,
153-
height
154-
});
155-
this.destroyAttachedResource(this.colorAttachments[i]);
156-
this.colorAttachments[i] = resizedTexture.view;
157-
this.attachResource(resizedTexture.view);
158-
}
159-
}
149+
this.colorAttachments.forEach((colorAttachment, i) => {
150+
const resizedTexture = colorAttachment.texture.clone({
151+
width,
152+
height
153+
});
154+
this.destroyAttachedResource(colorAttachment);
155+
this.colorAttachments[i] = resizedTexture.view;
156+
this.attachResource(resizedTexture.view);
157+
});
160158

161159
if (this.depthStencilAttachment) {
162160
const resizedTexture = this.depthStencilAttachment.texture.clone({

modules/core/src/adapter/resources/shader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ function getShaderIdFromProps(props: ShaderProps): string {
162162
function getShaderName(shader: string, defaultName: string = 'unnamed'): string {
163163
const SHADER_NAME_REGEXP = /#define[\s*]SHADER_NAME[\s*]([A-Za-z0-9_-]+)[\s*]/;
164164
const match = SHADER_NAME_REGEXP.exec(shader);
165-
return match ? match[1] : defaultName;
165+
return match?.[1] ?? defaultName;
166166
}

modules/core/src/adapter/resources/texture.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ export type TextureWriteOptions = {
9292
aspect?: 'all' | 'stencil-only' | 'depth-only';
9393
};
9494

95-
const BASE_DIMENSIONS: Record<string, '1d' | '2d' | '3d'> = {
95+
const BASE_DIMENSIONS = {
9696
'1d': '1d',
9797
'2d': '2d',
9898
'2d-array': '2d',
9999
cube: '2d',
100100
'cube-array': '2d',
101101
'3d': '3d'
102-
};
102+
} as const satisfies Record<string, '1d' | '2d' | '3d'>;
103103

104104
/** Texture properties */
105105
export type TextureProps = ResourceProps & {

modules/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export type {
216216

217217
// INTERNAL UTILS - for use in other luma.gl modules only
218218
export {log} from './utils/log';
219+
export {assert, assertDefined} from './utils/assert';
219220
export {getScratchArray} from './utils/array-utils-flat';
220221
export type {AttributeInfo} from './adapter-utils/get-attribute-from-layouts';
221222
export {getAttributeInfosFromLayouts} from './adapter-utils/get-attribute-from-layouts';

modules/core/src/shadertypes/textures/pixel-utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function readPixel(
7777

7878
// Extract each of the four channels.
7979
for (let i = 0; i < 4; i++) {
80-
const bits = bitsPerChannel[i];
80+
const bits = bitsPerChannel[i] ?? 0;
8181
// If a channel's bit width is zero or negative, consider it not present.
8282
if (bits <= 0) {
8383
channels.push(0);
@@ -88,7 +88,7 @@ export function readPixel(
8888
}
8989
}
9090

91-
return [channels[0], channels[1], channels[2], channels[3]];
91+
return [channels[0] ?? 0, channels[1] ?? 0, channels[2] ?? 0, channels[3] ?? 0];
9292
}
9393

9494
/**
@@ -130,10 +130,10 @@ export function writePixel(
130130
): void {
131131
let currentBitOffset = bitOffset;
132132
for (let channel = 0; channel < 4; channel++) {
133-
const bits = bitsPerChannel[channel];
133+
const bits = bitsPerChannel[channel] ?? 0;
134134
// Clamp the channel value to the maximum allowed by the bit width.
135135
const maxValue = (1 << bits) - 1;
136-
const channelValue = pixel[channel] & maxValue;
136+
const channelValue = (pixel[channel] ?? 0) & maxValue;
137137
writeBitsToDataView(dataView, currentBitOffset, bits, channelValue);
138138
currentBitOffset += bits;
139139
}

modules/core/src/shadertypes/textures/texture-format-decoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function getTextureFormatInfo(format: TextureFormat): TextureFormatInfo {
158158
const dataType = `${type}${length}` as NormalizedDataType;
159159
const decodedType = getDataTypeInfo(dataType);
160160
const bits = decodedType.byteLength * 8;
161-
const components = channels.length as 1 | 2 | 3 | 4;
161+
const components = (channels?.length ?? 1) as 1 | 2 | 3 | 4;
162162
const bitsPerChannel: [number, number, number, number] = [
163163
bits,
164164
components >= 2 ? bits : 0,
@@ -176,7 +176,7 @@ export function getTextureFormatInfo(format: TextureFormat): TextureFormatInfo {
176176
signed: decodedType.signed,
177177
normalized: decodedType.normalized,
178178
bitsPerChannel,
179-
bytesPerPixel: decodedType.byteLength * channels.length,
179+
bytesPerPixel: decodedType.byteLength * components,
180180
packed: formatInfo.packed,
181181
srgb: formatInfo.srgb
182182
};

modules/core/src/utils/assert.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// luma.gl
2+
// SPDX-License-Identifier: MIT
3+
// Copyright (c) vis.gl contributors
4+
5+
/** Throws if condition is true and narrows type */
6+
export function assert(condition: unknown, message?: string): asserts condition {
7+
if (!condition) {
8+
throw new Error(message ?? 'luma.gl assertion failed.');
9+
}
10+
}
11+
12+
/** Throws if value is not defined, narrows type */
13+
export function assertDefined<T>(value: T | undefined, message?: string): T {
14+
assert(value, message);
15+
return value;
16+
}

0 commit comments

Comments
 (0)