Skip to content

Commit

Permalink
Enable TS strict mode by default (denoland/deno#3899)
Browse files Browse the repository at this point in the history
Fixes denoland/deno#3324 

Co-authored-by: Kitson Kelly <me@kitsonkelly.com>
  • Loading branch information
2 people authored and caspervonb committed Jan 31, 2021
1 parent a5b5888 commit 1dafc71
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 94 deletions.
2 changes: 1 addition & 1 deletion encoding/base32_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { encode, decode } from "./base32.ts";

// Lifted from https://stackoverflow.com/questions/38987784
const fromHexString = (hexString: string): Uint8Array =>
new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
new Uint8Array(hexString.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16)));
const toHexString = (bytes: Uint8Array): string =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");

Expand Down
10 changes: 6 additions & 4 deletions encoding/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function chkOptions(opt: ReadOptions): void {
}
if (
INVALID_RUNE.includes(opt.comma) ||
INVALID_RUNE.includes(opt.comment) ||
(typeof opt.comment === "string" && INVALID_RUNE.includes(opt.comment)) ||
opt.comma === opt.comment
) {
throw new Error("Invalid Delimiter");
Expand Down Expand Up @@ -122,7 +122,7 @@ export async function readMatrix(
}
): Promise<string[][]> {
const result: string[][] = [];
let _nbFields: number;
let _nbFields: number | undefined;
let lineResult: string[];
let first = true;
let lineIndex = 0;
Expand Down Expand Up @@ -253,8 +253,10 @@ export async function parse(
});
}
if (opt.parse) {
assert(opt.parse != null, "opt.parse must be set");
return r.map((e: string[]): unknown => opt.parse(e));
return r.map((e: string[]): unknown => {
assert(opt.parse, "opt.parse must be set");
return opt.parse(e);
});
}
return r;
}
34 changes: 20 additions & 14 deletions encoding/csv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,26 +476,32 @@ for (const t of testCases) {
if (t.Error) {
let err;
try {
actual = await readMatrix(new BufReader(new StringReader(t.Input)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
fieldsPerRecord: fieldsPerRec,
lazyQuotes: lazyquote
});
actual = await readMatrix(
new BufReader(new StringReader(t.Input ?? "")),
{
comma: comma,
comment: comment,
trimLeadingSpace: trim,
fieldsPerRecord: fieldsPerRec,
lazyQuotes: lazyquote
}
);
} catch (e) {
err = e;
}
assert(err);
assertEquals(err.message, t.Error);
} else {
actual = await readMatrix(new BufReader(new StringReader(t.Input)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
fieldsPerRecord: fieldsPerRec,
lazyQuotes: lazyquote
});
actual = await readMatrix(
new BufReader(new StringReader(t.Input ?? "")),
{
comma: comma,
comment: comment,
trimLeadingSpace: trim,
fieldsPerRecord: fieldsPerRec,
lazyQuotes: lazyquote
}
);
const expected = t.Output;
assertEquals(actual, expected);
}
Expand Down
6 changes: 3 additions & 3 deletions encoding/yaml/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ interface DirectiveHandlers {
[directive: string]: (
state: LoaderState,
name: string,
...args: unknown[]
...args: string[]
) => void;
}

Expand Down Expand Up @@ -362,7 +362,7 @@ function storeMappingPair(
mergeMappings(state, result, valueNode[index], overridableKeys);
}
} else {
mergeMappings(state, result, valueNode, overridableKeys);
mergeMappings(state, result, valueNode as ArrayObject, overridableKeys);
}
} else {
if (
Expand Down Expand Up @@ -1610,7 +1610,7 @@ function readDocument(state: LoaderState): void {
const documentStart = state.position;
let position: number,
directiveName: string,
directiveArgs: unknown[],
directiveArgs: string[],
hasDirectives = false,
ch: number;

Expand Down
2 changes: 1 addition & 1 deletion examples/gist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (parsedArgs._.length === 0) {
Deno.exit(1);
}

const files = {};
const files: Record<string, { content: string }> = {};
for (const filename of parsedArgs._) {
const base = pathBase(filename);
const content = await Deno.readFile(filename);
Expand Down
2 changes: 2 additions & 0 deletions fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ async function copyFile(
await Deno.copyFile(src, dest);
if (options.preserveTimestamps) {
const statInfo = await Deno.stat(src);
assert(statInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(statInfo.modified != null, `statInfo.modified is unavailable`);
await Deno.utime(dest, statInfo.accessed, statInfo.modified);
}
}
Expand Down
3 changes: 3 additions & 0 deletions fs/empty_dir_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertStrContains,
assertThrows,
Expand Down Expand Up @@ -225,6 +226,8 @@ Deno.test(async function emptyDirPermission(): Promise<void> {
args: args
});

assert(stdout);

const output = await Deno.readAll(stdout);

assertStrContains(new TextDecoder().decode(output), s.output);
Expand Down
2 changes: 1 addition & 1 deletion fs/exists_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Deno.test(async function existsPermission(): Promise<void> {
args: args
});

const output = await Deno.readAll(stdout);
const output = await Deno.readAll(stdout!);

assertStrContains(new TextDecoder().decode(output), s.output);
}
Expand Down
16 changes: 8 additions & 8 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export async function* walk(
includeFiles = true,
includeDirs = true,
followSymlinks = false,
exts = null,
match = null,
skip = null
exts = undefined,
match = undefined,
skip = undefined
}: WalkOptions = {}
): AsyncIterableIterator<WalkInfo> {
if (maxDepth < 0) {
Expand All @@ -76,7 +76,7 @@ export async function* walk(
if (includeDirs && include(root, exts, match, skip)) {
yield { filename: root, info: await stat(root) };
}
if (maxDepth < 1 || !include(root, null, null, skip)) {
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
const ls: FileInfo[] = await readDir(root);
Expand Down Expand Up @@ -119,9 +119,9 @@ export function* walkSync(
includeFiles = true,
includeDirs = true,
followSymlinks = false,
exts = null,
match = null,
skip = null
exts = undefined,
match = undefined,
skip = undefined
}: WalkOptions = {}
): IterableIterator<WalkInfo> {
if (maxDepth < 0) {
Expand All @@ -130,7 +130,7 @@ export function* walkSync(
if (includeDirs && include(root, exts, match, skip)) {
yield { filename: root, info: statSync(root) };
}
if (maxDepth < 1 || !include(root, null, null, skip)) {
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
const ls: FileInfo[] = readDirSync(root);
Expand Down
2 changes: 1 addition & 1 deletion http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function toString(cookie: Cookie): string {
if (cookie.httpOnly) {
out.push("HttpOnly");
}
if (Number.isInteger(cookie.maxAge)) {
if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
assert(cookie.maxAge > 0, "Max-Age must be an integer superior to 0");
out.push(`Max-Age=${cookie.maxAge}`);
}
Expand Down
20 changes: 11 additions & 9 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Response
} from "./server.ts";
import { parse } from "../flags/mod.ts";
import { assert } from "../testing/asserts.ts";

interface EntryInfo {
mode: string;
Expand All @@ -40,10 +41,10 @@ const encoder = new TextEncoder();
const serverArgs = parse(args) as FileServerArgs;

const CORSEnabled = serverArgs.cors ? true : false;
const target = posix.resolve(serverArgs._[1] || "");
const addr = `0.0.0.0:${serverArgs.port || serverArgs.p || 4500}`;
const target = posix.resolve(serverArgs._[1] ?? "");
const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4500}`;

if (serverArgs.h || serverArgs.help) {
if (serverArgs.h ?? serverArgs.help) {
console.log(`Deno File Server
Serves a local directory in HTTP.
Expand Down Expand Up @@ -125,8 +126,8 @@ async function serveDir(
const listEntry: EntryInfo[] = [];
const fileInfos = await readDir(dirPath);
for (const fileInfo of fileInfos) {
const filePath = posix.join(dirPath, fileInfo.name);
const fileUrl = posix.join(dirUrl, fileInfo.name);
const filePath = posix.join(dirPath, fileInfo.name ?? "");
const fileUrl = posix.join(dirUrl, fileInfo.name ?? "");
if (fileInfo.name === "index.html" && fileInfo.isFile()) {
// in case index.html as dir...
return await serveFile(req, filePath);
Expand All @@ -139,7 +140,7 @@ async function serveDir(
listEntry.push({
mode: modeToString(fileInfo.isDirectory(), mode),
size: fileInfo.isFile() ? fileLenToString(fileInfo.len) : "",
name: fileInfo.name,
name: fileInfo.name ?? "",
url: fileUrl
});
}
Expand Down Expand Up @@ -311,7 +312,7 @@ listenAndServe(
}
const fsPath = posix.join(target, normalizedUrl);

let response: Response;
let response: Response | undefined;
try {
const info = await stat(fsPath);
if (info.isDirectory()) {
Expand All @@ -324,10 +325,11 @@ listenAndServe(
response = await serveFallback(req, e);
} finally {
if (CORSEnabled) {
assert(response);
setCORS(response);
}
serverLog(req, response);
req.respond(response);
serverLog(req, response!);
req.respond(response!);
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test(async function serveFile(): Promise<void> {
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assert(res.headers.has("content-type"));
assert(res.headers.get("content-type").includes("charset=utf-8"));
assert(res.headers.get("content-type")!.includes("charset=utf-8"));
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
await Deno.readFile("README.md")
Expand Down
2 changes: 1 addition & 1 deletion http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class Server implements AsyncIterable<ServerRequest> {
): AsyncIterableIterator<ServerRequest> {
const bufr = new BufReader(conn);
const w = new BufWriter(conn);
let req: ServerRequest | Deno.EOF;
let req: ServerRequest | Deno.EOF | undefined;
let err: Error | undefined;

while (!this.closing) {
Expand Down
5 changes: 3 additions & 2 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ test(async function testReadRequestError(): Promise<void> {
for (const test of testCases) {
const reader = new BufReader(new StringReader(test.in));
let err;
let req: ServerRequest | Deno.EOF;
let req: ServerRequest | Deno.EOF | undefined;
try {
req = await readRequest(mockConn as Deno.Conn, reader);
} catch (e) {
Expand All @@ -559,7 +559,7 @@ test(async function testReadRequestError(): Promise<void> {
assert(err instanceof (test.err as typeof UnexpectedEOFError));
} else {
assert(req instanceof ServerRequest);
assert(test.headers != null);
assert(test.headers);
assertEquals(err, undefined);
assertNotEquals(req, Deno.EOF);
for (const h of test.headers) {
Expand Down Expand Up @@ -719,6 +719,7 @@ if (Deno.build.os !== "win") {
const serverRoutine = async (): Promise<void> => {
let reqCount = 0;
const server = serve(":8124");
// @ts-ignore
const serverRid = server.listener["rid"];
let connRid = -1;
for await (const req of server) {
Expand Down
2 changes: 1 addition & 1 deletion io/bufio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export class BufReader implements Reader {
*/
async readSlice(delim: number): Promise<Uint8Array | Deno.EOF> {
let s = 0; // search start index
let slice: Uint8Array;
let slice: Uint8Array | undefined;

while (true) {
// Search buffer.
Expand Down
2 changes: 1 addition & 1 deletion manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ Build with Cargo:
cargo build -vv

# Run:
./target/debug/deno tests/002_hello.ts
./target/debug/deno cli/tests/002_hello.ts
```

#### Testing and Tools
Expand Down
Loading

0 comments on commit 1dafc71

Please sign in to comment.