Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable "exactOptionalPropertyTypes" option #5892

Merged
10 changes: 5 additions & 5 deletions archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const USTAR_MAGIC_HEADER = "ustar\u000000" as const;
* Simple file reader
*/
class FileReader implements Reader {
#file?: Deno.FsFile;
#file: Deno.FsFile | undefined;
#filePath: string;

constructor(filePath: string) {
Expand Down Expand Up @@ -112,7 +112,7 @@ function formatHeader(data: TarData): Uint8Array {
const buffer = new Uint8Array(HEADER_LENGTH);
let offset = 0;
for (const { field, length } of USTAR_STRUCTURE) {
const entry = encoder.encode(data[field as keyof TarData] || "");
const entry = encoder.encode(data[field as keyof TarData] ?? "");
buffer.set(entry, offset);
offset += length;
}
Expand Down Expand Up @@ -334,7 +334,7 @@ export class Tar {
}
}

source = source || {};
source = source ?? {};

// set meta data
let info: Deno.FileInfo | undefined;
Expand All @@ -351,8 +351,8 @@ export class Tar {
const mtime = Math.floor(
source.mtime ?? (info?.mtime ?? new Date()).valueOf() / 1000,
);
const uid = source.uid || 0;
const gid = source.gid || 0;
const uid = source.uid ?? 0;
const gid = source.gid ?? 0;

if (typeof source.owner === "string" && source.owner.length >= 32) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions archive/untar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class TarEntry implements Reader {
this.#reader = reader;

// File Size
this.#size = this.fileSize || 0;
this.#size = this.fileSize ?? 0;
// Entry Size
const blocks = Math.ceil(this.#size / HEADER_LENGTH);
this.#entrySize = blocks * HEADER_LENGTH;
Expand Down Expand Up @@ -259,7 +259,7 @@ export class TarEntry implements Reader {
const n = await readBlock(this.#reader, block);
const bytesLeft = this.#size - this.#read;

this.#read += n || 0;
this.#read += n ?? 0;
if (n === null || bytesLeft <= 0) {
if (n === null) this.#consumed = true;
return null;
Expand Down
2 changes: 1 addition & 1 deletion cli/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class Spinner {
message: string;

#interval: number;
#color?: Color;
#color: Color | undefined;
#intervalId: number | undefined;
#active = false;

Expand Down
6 changes: 3 additions & 3 deletions csv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Parser {
#options: {
separator: string;
trimLeadingSpace: boolean;
comment?: string;
lazyQuotes?: boolean;
fieldsPerRecord?: number;
comment: string | undefined;
lazyQuotes: boolean | undefined;
fieldsPerRecord: number | undefined;
};
constructor({
separator = ",",
Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true,
"useUnknownInCatchVariables": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true
Expand Down
2 changes: 1 addition & 1 deletion dotenv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function expandCharacters(str: string): string {

return str.replace(
/\\([nrt])/g,
($1: keyof CharactersMap): string => charactersMap[$1] || "",
($1: keyof CharactersMap): string => charactersMap[$1] ?? "",
);
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function encodeBase58(data: ArrayBuffer | Uint8Array | string): string {
(carry > 0 || i < length) && reverseIterator !== -1;
reverseIterator--, i++
) {
carry += (b58Encoding[reverseIterator] || 0) * 256;
carry += (b58Encoding[reverseIterator] ?? 0) * 256;
b58Encoding[reverseIterator] = Math.round(carry % 58);
carry = Math.floor(carry / 58);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ export function decodeBase58(b58: string): Uint8Array {
(carry > 0 || i < length) && reverseIterator !== -1;
reverseIterator--, i++
) {
carry += 58 * (output[reverseIterator] || 0);
carry += 58 * (output[reverseIterator] ?? 0);
output[reverseIterator] = Math.round(carry % 256);
carry = Math.floor(carry / 256);
}
Expand Down
2 changes: 1 addition & 1 deletion expect/_assert_equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export function assertEquals<T>(
return;
}

const message = buildEqualErrorMessage(actual, expected, options || {});
const message = buildEqualErrorMessage(actual, expected, options ?? {});
throw new AssertionError(message);
}
4 changes: 2 additions & 2 deletions expect/_build_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function buildEqualErrorMessage<T>(
expected: T,
options: EqualErrorMessageOptions,
): string {
const { formatter = format, msg } = options || {};
const { formatter = format, msg } = options ?? {};
const msgSuffix = msg ? `: ${msg}` : ".";
const actualString = formatter(actual);
const expectedString = formatter(expected);
Expand All @@ -39,7 +39,7 @@ export function buildNotEqualErrorMessage<T>(
expected: T,
options: EqualErrorMessageOptions,
): string {
const { msg } = options || {};
const { msg } = options ?? {};
const actualString = String(actual);
const expectedString = String(expected);

Expand Down
6 changes: 3 additions & 3 deletions expect/_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function asymmetricEqual(a: unknown, b: unknown) {
* @param options for the equality check
*/
export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
const { customTesters = [], strictCheck } = options || {};
const { customTesters = [], strictCheck } = options ?? {};
const seen = new Map();

return (function compare(a: unknown, b: unknown): boolean {
Expand Down Expand Up @@ -113,8 +113,8 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
return true;
}

const aKeys = Object.keys(a || {});
const bKeys = Object.keys(b || {});
const aKeys = Object.keys(a ?? {});
const bKeys = Object.keys(b ?? {});
let aLen = aKeys.length;
let bLen = bKeys.length;

Expand Down
16 changes: 8 additions & 8 deletions fmt/printf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ const F = {
} as const;

class Flags {
plus?: boolean;
dash?: boolean;
sharp?: boolean;
space?: boolean;
zero?: boolean;
lessthan?: boolean;
plus: boolean | undefined;
dash: boolean | undefined;
sharp: boolean | undefined;
space: boolean | undefined;
zero: boolean | undefined;
lessthan: boolean | undefined;
width = -1;
precision = -1;
}
Expand All @@ -213,7 +213,7 @@ class Printf {
haveSeen: boolean[];

// barf, store precision and width errors for later processing ...
tmpError?: string;
tmpError: string | undefined;

constructor(format: string, ...args: unknown[]) {
this.format = format;
Expand Down Expand Up @@ -719,7 +719,7 @@ class Printf {
? this.flags.precision
: DEFAULT_PRECISION;
const [fractional, rounding] = this.roundFractionToPrecision(
m[F.fractional] || "",
m[F.fractional] ?? "",
precision,
);

Expand Down
2 changes: 1 addition & 1 deletion front_matter/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function extractAndParse<T>(
if (!match || match.index !== 0) {
throw new TypeError("Unexpected end of input");
}
const frontMatter = match.at(-1)?.replace(/^\s+|\s+$/g, "") || "";
const frontMatter = match.at(-1)?.replace(/^\s+|\s+$/g, "") ?? "";
const attrs = parse(frontMatter) as T;
const body = input.replace(match[0], "");
return { frontMatter, body, attrs };
Expand Down
4 changes: 2 additions & 2 deletions http/_negotiation/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

export interface Specificity {
i: number;
o?: number;
o: number | undefined;
q: number;
s?: number;
s: number | undefined;
}

export function compareSpecs(a: Specificity, b: Specificity): number {
Expand Down
6 changes: 4 additions & 2 deletions http/_negotiation/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function parseEncoding(
return undefined;
}

const encoding = match[1];
const encoding = match[1]!;
let q = 1;
if (match[2]) {
const params = match[2].split(";");
Expand All @@ -59,7 +59,7 @@ function parseEncoding(
}
}

return { encoding, q, i };
return { encoding, o: undefined, q, i, s: undefined };
}

function specify(
Expand Down Expand Up @@ -104,8 +104,10 @@ function parseAcceptEncoding(accept: string): EncodingSpecificity[] {
if (!hasIdentity) {
parsedAccepts.push({
encoding: "identity",
o: undefined,
q: minQuality,
i: accepts.length - 1,
s: undefined,
});
}

Expand Down
6 changes: 3 additions & 3 deletions http/_negotiation/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { compareSpecs, isQuality, type Specificity } from "./common.ts";

interface LanguageSpecificity extends Specificity {
prefix: string;
suffix?: string;
suffix: string | undefined;
full: string;
}

Expand All @@ -53,7 +53,7 @@ function parseLanguage(
return undefined;
}

const full = suffix ? `${prefix}-${suffix}` : prefix;
const full = suffix !== undefined ? `${prefix}-${suffix}` : prefix;

let q = 1;
if (match[3]) {
Expand All @@ -67,7 +67,7 @@ function parseLanguage(
}
}

return { prefix, suffix, full, q, i };
return { prefix, suffix, full, i, o: undefined, q, s: undefined };
}

function parseAcceptLanguage(accept: string): LanguageSpecificity[] {
Expand Down
14 changes: 7 additions & 7 deletions http/_negotiation/media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function parseMediaType(
}
}

return { type, subtype, params, q, i };
return { type, subtype, params, i, o: undefined, q, s: undefined };
}

function parseAccept(accept: string): MediaTypeSpecificity[] {
Expand Down Expand Up @@ -131,8 +131,8 @@ function specify(
if (keys.length) {
if (
keys.every((key) =>
(spec.params[key] || "").toLowerCase() ===
(p.params[key] || "").toLowerCase()
(spec.params[key] ?? "").toLowerCase() ===
(p.params[key] ?? "").toLowerCase()
)
) {
s |= 1;
Expand Down Expand Up @@ -161,9 +161,9 @@ function getMediaTypePriority(

if (
spec &&
((priority.s || 0) - (spec.s || 0) ||
(priority.q || 0) - (spec.q || 0) ||
(priority.o || 0) - (spec.o || 0)) < 0
((priority.s ?? 0) - (spec.s ?? 0) ||
(priority.q ?? 0) - (spec.q ?? 0) ||
(priority.o ?? 0) - (spec.o ?? 0)) < 0
) {
priority = spec;
}
Expand All @@ -176,7 +176,7 @@ export function preferredMediaTypes(
accept?: string | null,
provided?: string[],
): string[] {
const accepts = parseAccept(accept === undefined ? "*/*" : accept || "");
const accepts = parseAccept(accept === undefined ? "*/*" : accept ?? "");

if (!provided) {
return accepts
Expand Down
2 changes: 1 addition & 1 deletion http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function parseSetCookie(value: string): Cookie | null {
cookie.httpOnly = true;
break;
case "samesite":
cookie.sameSite = value as Cookie["sameSite"];
cookie.sameSite = value as NonNullable<Cookie["sameSite"]>;
break;
default:
if (!Array.isArray(cookie.unparsed)) {
Expand Down
4 changes: 2 additions & 2 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ async function createServeDirResponse(
req: Request,
opts: ServeDirOptions,
) {
const target = opts.fsRoot || ".";
const target = opts.fsRoot ?? ".";
const urlRoot = opts.urlRoot;
const showIndex = opts.showIndex ?? true;
const showDotfiles = opts.showDotfiles || false;
Expand Down Expand Up @@ -800,7 +800,7 @@ function main() {
},
});
const port = serverArgs.port ? Number(serverArgs.port) : undefined;
const headers = serverArgs.header || [];
const headers = serverArgs.header ?? [];
const host = serverArgs.host;
const certFile = serverArgs.cert;
const keyFile = serverArgs.key;
Expand Down
10 changes: 5 additions & 5 deletions http/user_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,11 @@ const matchers: Matchers = {
* ```
*/
export class UserAgent {
#browser?: Browser;
#cpu?: Cpu;
#device?: Device;
#engine?: Engine;
#os?: Os;
#browser: Browser | undefined;
#cpu: Cpu | undefined;
#device: Device | undefined;
#engine: Engine | undefined;
#os: Os | undefined;
#ua: string;

/**
Expand Down
2 changes: 1 addition & 1 deletion internal/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function createFp(
}
const isAdding = (down?.y === -1) ||
k === M ||
(slide?.y || 0) > (down?.y || 0) + 1;
(slide?.y ?? 0) > (down?.y ?? 0) + 1;
if (slide && isAdding) {
const prev = slide.id;
ptr++;
Expand Down
5 changes: 4 additions & 1 deletion log/file_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export type LogMode = "a" | "w" | "x";

export interface FileHandlerOptions extends BaseHandlerOptions {
filename: string;
/**
* @default {"a"}
*/
mode?: LogMode;
/**
* Buffer size for writing log messages to file, in bytes.
Expand Down Expand Up @@ -60,7 +63,7 @@ export class FileHandler extends BaseHandler {
super(levelName, options);
this[filenameSymbol] = options.filename;
// default to append mode, write only
this[modeSymbol] = options.mode ? options.mode : "a";
this[modeSymbol] = options.mode ?? "a";
this[openOptionsSymbol] = {
createNew: this[modeSymbol] === "x",
create: this[modeSymbol] !== "x",
Expand Down
2 changes: 1 addition & 1 deletion log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Logger {
) {
this.#loggerName = loggerName;
this.#level = getLevelByName(levelName);
this.handlers = options.handlers || [];
this.handlers = options.handlers ?? [];
}

/** Use this to retrieve the current numeric log level. */
Expand Down
Loading