Skip to content

Commit

Permalink
feat(cli): enable useUnknownInCatchVariables by default (#12547)
Browse files Browse the repository at this point in the history
Closes #11826

**BREAKING CHANGE** this behaviour was disable when introduced in Deno 1.14/TypeScript 4.4. It will highlight code that unsafely handles variables that are caught, and will cause type errors in unsafe code.
  • Loading branch information
kitsonk committed Oct 27, 2021
1 parent be68b82 commit a065604
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 17 deletions.
4 changes: 0 additions & 4 deletions cli/emit.rs
Expand Up @@ -160,8 +160,6 @@ pub(crate) fn get_ts_config(
"target": "esnext",
"tsBuildInfoFile": "deno:///.tsbuildinfo",
"useDefineForClassFields": true,
// TODO(@kitsonk) remove for Deno 2.0
"useUnknownInCatchVariables": false,
}));
if tsc_emit {
ts_config.merge(&json!({
Expand Down Expand Up @@ -213,8 +211,6 @@ pub(crate) fn get_ts_config(
"target": "esnext",
"tsBuildInfoFile": "deno:///.tsbuildinfo",
"useDefineForClassFields": true,
// TODO(@kitsonk) remove for Deno 2.0
"useUnknownInCatchVariables": false,
}));
if tsc_emit {
ts_config.merge(&json!({
Expand Down
2 changes: 0 additions & 2 deletions cli/lsp/language_server.rs
Expand Up @@ -629,8 +629,6 @@ impl Inner {
"strict": true,
"target": "esnext",
"useDefineForClassFields": true,
// TODO(@kitsonk) remove for Deno 1.15
"useUnknownInCatchVariables": false,
}));
let config = &self.config;
let workspace_settings = config.get_workspace_settings();
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/integration/run_tests.rs
Expand Up @@ -1367,6 +1367,13 @@ itest!(error_import_map_unable_to_load {
exit_code: 1,
});

// This test ensure that useUnknownInCatchVariables is enabled by default.
itest!(use_unknown_in_catch_variables {
args: "run useUnknownInCatchVariables.ts",
output: "useUnknownInCatchVariables.ts.out",
exit_code: 1,
});

// Test that setting `self` in the main thread to some other value doesn't break
// the world.
itest!(replace_self {
Expand Down
4 changes: 3 additions & 1 deletion cli/tests/testdata/070_location.ts
Expand Up @@ -4,5 +4,7 @@ console.log(location);
try {
location.hostname = "bar";
} catch (error) {
console.log(error.toString());
if (error instanceof Error) {
console.log(error.toString());
}
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/085_dynamic_import_async_error.ts
@@ -1,5 +1,7 @@
try {
await import("./delayed_error.ts");
} catch (error) {
console.log(`Caught: ${error.stack}`);
if (error instanceof Error) {
console.log(`Caught: ${error.stack}`);
}
}
8 changes: 6 additions & 2 deletions cli/tests/testdata/086_dynamic_import_already_rejected.ts
@@ -1,11 +1,15 @@
try {
await import("./error_001.ts");
} catch (error) {
console.log(`Caught: ${error.stack}`);
if (error instanceof Error) {
console.log(`Caught: ${error.stack}`);
}
}

try {
await import("./error_001.ts");
} catch (error) {
console.log(`Caught: ${error.stack}`);
if (error instanceof Error) {
console.log(`Caught: ${error.stack}`);
}
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/error_019_stack_function.ts
Expand Up @@ -5,6 +5,8 @@ function foo(): never {
try {
foo();
} catch (error) {
console.log(error.stack);
if (error instanceof Error) {
console.log(error.stack);
}
throw error;
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/error_020_stack_constructor.ts
Expand Up @@ -7,6 +7,8 @@ class A {
try {
new A();
} catch (error) {
console.log(error.stack);
if (error instanceof Error) {
console.log(error.stack);
}
throw error;
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/error_021_stack_method.ts
Expand Up @@ -7,6 +7,8 @@ class A {
try {
new A().m();
} catch (error) {
console.log(error.stack);
if (error instanceof Error) {
console.log(error.stack);
}
throw error;
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/error_023_stack_async.ts
Expand Up @@ -7,6 +7,8 @@ const p = (async () => {
try {
await p;
} catch (error) {
console.log(error.stack);
if (error instanceof Error) {
console.log(error.stack);
}
throw error;
}
4 changes: 3 additions & 1 deletion cli/tests/testdata/error_024_stack_promise_all.ts
Expand Up @@ -9,6 +9,8 @@ const p = Promise.all([
try {
await p;
} catch (error) {
console.log(error.stack);
if (error instanceof Error) {
console.log(error.stack);
}
throw error;
}
6 changes: 5 additions & 1 deletion cli/tests/testdata/resolve_dns.ts
Expand Up @@ -38,5 +38,9 @@ console.log(JSON.stringify(txt));
try {
await Deno.resolveDns("not-found-example.com", "A", nameServer);
} catch (e) {
console.log(`Error ${e.name} thrown for not-found-example.com`);
console.log(
`Error ${
e instanceof Error ? e.name : "[non-error]"
} thrown for not-found-example.com`,
);
}
5 changes: 5 additions & 0 deletions cli/tests/testdata/useUnknownInCatchVariables.ts
@@ -0,0 +1,5 @@
try {
throw new Error();
} catch (e) {
console.log(e.message);
}
5 changes: 5 additions & 0 deletions cli/tests/testdata/useUnknownInCatchVariables.ts.out
@@ -0,0 +1,5 @@
[WILDCARD]
error: TS2571 [ERROR]: Object is of type 'unknown'.
console.log(e.message);
^
at file://[WILDCARD]/useUnknownInCatchVariables.ts:4:15
4 changes: 3 additions & 1 deletion cli/tests/unit/opcall_test.ts
Expand Up @@ -12,7 +12,9 @@ unitTest(async function sendAsyncStackTrace() {
await Deno.read(rid, buf);
unreachable();
} catch (error) {
const s = error.stack.toString();
assert(error instanceof Error);
const s = error.stack?.toString();
assert(s);
console.log(s);
assertStringIncludes(s, "opcall_test.ts");
assertStringIncludes(s, "read");
Expand Down
1 change: 1 addition & 0 deletions cli/tests/unit/text_encoding_test.ts
Expand Up @@ -98,6 +98,7 @@ unitTest(function textDecoderErrorEncoding() {
new TextDecoder("Foo");
} catch (e) {
didThrow = true;
assert(e instanceof Error);
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
}
assert(didThrow);
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/unit/write_file_test.ts
@@ -1,5 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertRejects,
assertThrows,
Expand Down Expand Up @@ -251,6 +252,7 @@ unitTest(
try {
await Deno.writeFile(filename, data, { signal: ac.signal });
} catch (e) {
assert(e instanceof Error);
assertEquals(e.name, "AbortError");
}
const stat = Deno.statSync(filename);
Expand All @@ -269,6 +271,7 @@ unitTest(
try {
await Deno.writeFile(filename, data, { signal: ac.signal });
} catch (e) {
assert(e instanceof Error);
assertEquals(e.name, "AbortError");
}
const stat = Deno.statSync(filename);
Expand Down

0 comments on commit a065604

Please sign in to comment.