typescript: Enable strictNullChecks.

Let’s do this right from the beginning instead of making a mess to
clean up later.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-07-26 13:19:12 -07:00 committed by Tim Abbott
parent eb2809effe
commit 0e0baca41e
5 changed files with 25 additions and 20 deletions

View File

@ -85,10 +85,11 @@ run_test("clear", () => {
});
run_test("undefined_keys", () => {
blueslip.expect("error", "Tried to call a FoldDict method with an undefined key.", 2);
const d = new FoldDict();
assert.equal(d.has(undefined), false);
assert.strictEqual(d.get(undefined), undefined);
assert.throws(
() => d.has(undefined),
TypeError,
"Tried to call a FoldDict method with an undefined key.",
);
});

View File

@ -17,16 +17,19 @@ type NumberedLine = {
};
type CleanStackFrame = {
full_path: string;
show_path: string;
function_name: FunctionName;
line_number: number;
context: NumberedLine[] | undefined;
full_path?: string;
show_path?: string;
function_name?: FunctionName;
line_number?: number;
context?: NumberedLine[];
};
export function clean_path(full_path: string): string {
export function clean_path(full_path?: string): string | undefined {
// If the file is local, just show the filename.
// Otherwise, show the full path starting from node_modules.
if (full_path === undefined) {
return undefined;
}
const idx = full_path.indexOf("/node_modules/");
if (idx !== -1) {
return full_path.slice(idx + "/node_modules/".length);
@ -55,18 +58,21 @@ const sourceCache: {[source: string]: string | Promise<string>} = {};
const stack_trace_gps = new StackTraceGPS({sourceCache});
async function get_context(location: StackFrame): Promise<NumberedLine[] | undefined> {
const sourceContent = await sourceCache[location.getFileName()];
const {fileName, lineNumber} = location;
if (fileName === undefined || lineNumber === undefined) {
return undefined;
}
const sourceContent = await sourceCache[fileName];
if (sourceContent === undefined) {
return undefined;
}
const lines = sourceContent.split("\n");
const line_number = location.getLineNumber();
const lo_line_num = Math.max(line_number - 5, 0);
const hi_line_num = Math.min(line_number + 4, lines.length);
const lo_line_num = Math.max(lineNumber - 5, 0);
const hi_line_num = Math.min(lineNumber + 4, lines.length);
return lines.slice(lo_line_num, hi_line_num).map((line: string, i: number) => ({
line_number: lo_line_num + i + 1,
line,
focus: lo_line_num + i + 1 === line_number,
focus: lo_line_num + i + 1 === lineNumber,
}));
}

View File

@ -65,10 +65,9 @@ export class FoldDict<V> {
}
// Handle case-folding of keys and the empty string.
private _munge(key: string): string | undefined {
private _munge(key: string): string {
if (key === undefined) {
blueslip.error("Tried to call a FoldDict method with an undefined key.");
return undefined;
throw new TypeError("Tried to call a FoldDict method with an undefined key.");
}
return key.toString().toLowerCase();

View File

@ -248,7 +248,7 @@ export default (env?: string): webpack.Configuration[] => {
{path: "jquery/dist/jquery.js", name: ["$", "jQuery"]},
{path: "handlebars/dist/cjs/handlebars.runtime.js", name: "Handlebars"},
];
config.module.rules.unshift(...getExposeLoaders(exposeOptions));
config.module!.rules.unshift(...getExposeLoaders(exposeOptions));
if (!production) {
// Out JS debugging tools

View File

@ -23,7 +23,6 @@
/* Strict type-checking */
"strict": true,
"strictNullChecks": false,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,