Skip to content

Commit

Permalink
Enable @typescript-eslint/no-non-null-assertion rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Sep 5, 2023
1 parent 874889a commit 6ec2eaa
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
Expand Down
8 changes: 6 additions & 2 deletions packages/miniflare/src/http/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class WebSocket extends TypedEventTarget<WebSocketEventMap> {
}

async #queuingDispatchToPair(event: ValueOf<WebSocketEventMap>) {
const pair = this[kPair]!;
const pair = this[kPair];
assert(pair !== undefined);
if (pair[kAccepted]) {
pair.dispatchEvent(event);
} else {
Expand Down Expand Up @@ -183,8 +184,11 @@ export class WebSocket extends TypedEventTarget<WebSocketEventMap> {
// ------- | ------- -------------
// |

const pair = this[kPair];
assert(pair !== undefined);

this[kClosedOutgoing] = true;
this[kPair]![kClosedIncoming] = true;
pair[kClosedIncoming] = true;

const event = new CloseEvent("close", { code, reason });
void this.#queuingDispatchToPair(event);
Expand Down
1 change: 1 addition & 0 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ export class Miniflare {
);
} else if (url.pathname === "/core/log") {
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const level = parseInt(request.headers.get(SharedHeaders.LOG_LEVEL)!);
assert(
LogLevel.NONE <= level && level <= LogLevel.VERBOSE,
Expand Down
1 change: 1 addition & 0 deletions packages/miniflare/src/workers/cache/cache.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ export class CacheObject extends MiniflareDurableObject {
// If we know the size, avoid passing the body through a transform stream to
// count it (trusting `workerd` to send correct value here).
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(res.headers.get("Content-Length")!);
let sizePromise: Promise<number>;
if (Number.isNaN(contentLength)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/miniflare/src/workers/core/entry.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ function maybeInjectLiveReload(
}

const headers = new Headers(response.headers);
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(headers.get("content-length")!);
if (!isNaN(contentLength)) {
headers.set(
Expand Down
3 changes: 2 additions & 1 deletion packages/miniflare/src/workers/kv/namespace.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ export class KVNamespaceObject extends MiniflareDurableObject {
// Validate value size: if we know the value length, avoid passing the body
// through a transform stream to count it (trusting `workerd` to send
// correct value here).
// Safety of `!`: `parseInt(null)` is `NaN`
let value = req.body;
assert(value !== null);
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(req.headers.get("Content-Length")!);
const valueLengthHint = Number.isNaN(contentLength)
? undefined
Expand Down
3 changes: 3 additions & 0 deletions packages/miniflare/src/workers/r2/bucket.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function rangeOverlaps(a: InclusiveRange, b: InclusiveRange): boolean {

async function decodeMetadata(req: Request<unknown, unknown>) {
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const metadataSize = parseInt(req.headers.get(R2Headers.METADATA_SIZE)!);
if (Number.isNaN(metadataSize)) throw new InvalidMetadata();

Expand Down Expand Up @@ -1045,6 +1046,7 @@ export class R2BucketObject extends MiniflareDurableObject {
return new Response();
} else if (metadata.method === "put") {
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(req.headers.get("Content-Length")!);
// `workerd` requires a known value size for R2 put requests:
// - https://github.com/cloudflare/workerd/blob/e3479895a2ace28e4fd5f1399cea4c92291966ab/src/workerd/api/r2-rpc.c%2B%2B#L154-L156
Expand All @@ -1066,6 +1068,7 @@ export class R2BucketObject extends MiniflareDurableObject {
return encodeJSONResult(result);
} else if (metadata.method === "uploadPart") {
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(req.headers.get("Content-Length")!);
// `workerd` requires a known value size for R2 put requests as above
assert(!isNaN(contentLength));
Expand Down
2 changes: 2 additions & 0 deletions packages/miniflare/src/workers/shared/blob.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function fetchSingleRange(
// If we specified a range, but received full content, make sure the range
// covered the full content
// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(res.headers.get("Content-Length")!);
assert(!Number.isNaN(contentLength));
assertFullRangeRequest(range, contentLength);
Expand Down Expand Up @@ -120,6 +121,7 @@ async function fetchMultipleRanges(
assert(res.ok);

// Safety of `!`: `parseInt(null)` is `NaN`
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contentLength = parseInt(res.headers.get("Content-Length")!);
assert(!Number.isNaN(contentLength));

Expand Down
3 changes: 3 additions & 0 deletions packages/miniflare/src/workers/shared/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class DeferredPromise<T> extends Promise<T> {
return executor(resolve, reject);
});
// Cannot access `this` until after `super`
// Safety of `!`: callback passed to `super()` is executed immediately
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.resolve = promiseResolve!;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.reject = promiseReject!;
}
}
Expand Down

0 comments on commit 6ec2eaa

Please sign in to comment.