mirror of
https://github.com/denoland/deno.git
synced 2025-09-29 21:54:48 +00:00
feat(cli): enable useUnknownInCatchVariables
by default (#12547)
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.
This commit is contained in:
parent
be68b82eb4
commit
a065604155
17 changed files with 56 additions and 17 deletions
|
@ -160,8 +160,6 @@ pub(crate) fn get_ts_config(
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"tsBuildInfoFile": "deno:///.tsbuildinfo",
|
"tsBuildInfoFile": "deno:///.tsbuildinfo",
|
||||||
"useDefineForClassFields": true,
|
"useDefineForClassFields": true,
|
||||||
// TODO(@kitsonk) remove for Deno 2.0
|
|
||||||
"useUnknownInCatchVariables": false,
|
|
||||||
}));
|
}));
|
||||||
if tsc_emit {
|
if tsc_emit {
|
||||||
ts_config.merge(&json!({
|
ts_config.merge(&json!({
|
||||||
|
@ -213,8 +211,6 @@ pub(crate) fn get_ts_config(
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"tsBuildInfoFile": "deno:///.tsbuildinfo",
|
"tsBuildInfoFile": "deno:///.tsbuildinfo",
|
||||||
"useDefineForClassFields": true,
|
"useDefineForClassFields": true,
|
||||||
// TODO(@kitsonk) remove for Deno 2.0
|
|
||||||
"useUnknownInCatchVariables": false,
|
|
||||||
}));
|
}));
|
||||||
if tsc_emit {
|
if tsc_emit {
|
||||||
ts_config.merge(&json!({
|
ts_config.merge(&json!({
|
||||||
|
|
|
@ -629,8 +629,6 @@ impl Inner {
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"useDefineForClassFields": true,
|
"useDefineForClassFields": true,
|
||||||
// TODO(@kitsonk) remove for Deno 1.15
|
|
||||||
"useUnknownInCatchVariables": false,
|
|
||||||
}));
|
}));
|
||||||
let config = &self.config;
|
let config = &self.config;
|
||||||
let workspace_settings = config.get_workspace_settings();
|
let workspace_settings = config.get_workspace_settings();
|
||||||
|
|
|
@ -1367,6 +1367,13 @@ itest!(error_import_map_unable_to_load {
|
||||||
exit_code: 1,
|
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
|
// Test that setting `self` in the main thread to some other value doesn't break
|
||||||
// the world.
|
// the world.
|
||||||
itest!(replace_self {
|
itest!(replace_self {
|
||||||
|
|
2
cli/tests/testdata/070_location.ts
vendored
2
cli/tests/testdata/070_location.ts
vendored
|
@ -4,5 +4,7 @@ console.log(location);
|
||||||
try {
|
try {
|
||||||
location.hostname = "bar";
|
location.hostname = "bar";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.toString());
|
console.log(error.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
try {
|
try {
|
||||||
await import("./delayed_error.ts");
|
await import("./delayed_error.ts");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(`Caught: ${error.stack}`);
|
console.log(`Caught: ${error.stack}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
try {
|
try {
|
||||||
await import("./error_001.ts");
|
await import("./error_001.ts");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(`Caught: ${error.stack}`);
|
console.log(`Caught: ${error.stack}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await import("./error_001.ts");
|
await import("./error_001.ts");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(`Caught: ${error.stack}`);
|
console.log(`Caught: ${error.stack}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ function foo(): never {
|
||||||
try {
|
try {
|
||||||
foo();
|
foo();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.stack);
|
console.log(error.stack);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ class A {
|
||||||
try {
|
try {
|
||||||
new A();
|
new A();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.stack);
|
console.log(error.stack);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
2
cli/tests/testdata/error_021_stack_method.ts
vendored
2
cli/tests/testdata/error_021_stack_method.ts
vendored
|
@ -7,6 +7,8 @@ class A {
|
||||||
try {
|
try {
|
||||||
new A().m();
|
new A().m();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.stack);
|
console.log(error.stack);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
2
cli/tests/testdata/error_023_stack_async.ts
vendored
2
cli/tests/testdata/error_023_stack_async.ts
vendored
|
@ -7,6 +7,8 @@ const p = (async () => {
|
||||||
try {
|
try {
|
||||||
await p;
|
await p;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.stack);
|
console.log(error.stack);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ const p = Promise.all([
|
||||||
try {
|
try {
|
||||||
await p;
|
await p;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
console.log(error.stack);
|
console.log(error.stack);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
6
cli/tests/testdata/resolve_dns.ts
vendored
6
cli/tests/testdata/resolve_dns.ts
vendored
|
@ -38,5 +38,9 @@ console.log(JSON.stringify(txt));
|
||||||
try {
|
try {
|
||||||
await Deno.resolveDns("not-found-example.com", "A", nameServer);
|
await Deno.resolveDns("not-found-example.com", "A", nameServer);
|
||||||
} catch (e) {
|
} 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
cli/tests/testdata/useUnknownInCatchVariables.ts
vendored
Normal file
5
cli/tests/testdata/useUnknownInCatchVariables.ts
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
try {
|
||||||
|
throw new Error();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e.message);
|
||||||
|
}
|
5
cli/tests/testdata/useUnknownInCatchVariables.ts.out
vendored
Normal file
5
cli/tests/testdata/useUnknownInCatchVariables.ts.out
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[WILDCARD]
|
||||||
|
error: TS2571 [ERROR]: Object is of type 'unknown'.
|
||||||
|
console.log(e.message);
|
||||||
|
^
|
||||||
|
at file://[WILDCARD]/useUnknownInCatchVariables.ts:4:15
|
|
@ -12,7 +12,9 @@ unitTest(async function sendAsyncStackTrace() {
|
||||||
await Deno.read(rid, buf);
|
await Deno.read(rid, buf);
|
||||||
unreachable();
|
unreachable();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const s = error.stack.toString();
|
assert(error instanceof Error);
|
||||||
|
const s = error.stack?.toString();
|
||||||
|
assert(s);
|
||||||
console.log(s);
|
console.log(s);
|
||||||
assertStringIncludes(s, "opcall_test.ts");
|
assertStringIncludes(s, "opcall_test.ts");
|
||||||
assertStringIncludes(s, "read");
|
assertStringIncludes(s, "read");
|
||||||
|
|
|
@ -98,6 +98,7 @@ unitTest(function textDecoderErrorEncoding() {
|
||||||
new TextDecoder("Foo");
|
new TextDecoder("Foo");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
didThrow = true;
|
didThrow = true;
|
||||||
|
assert(e instanceof Error);
|
||||||
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
|
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
|
||||||
}
|
}
|
||||||
assert(didThrow);
|
assert(didThrow);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
import {
|
import {
|
||||||
|
assert,
|
||||||
assertEquals,
|
assertEquals,
|
||||||
assertRejects,
|
assertRejects,
|
||||||
assertThrows,
|
assertThrows,
|
||||||
|
@ -251,6 +252,7 @@ unitTest(
|
||||||
try {
|
try {
|
||||||
await Deno.writeFile(filename, data, { signal: ac.signal });
|
await Deno.writeFile(filename, data, { signal: ac.signal });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
assert(e instanceof Error);
|
||||||
assertEquals(e.name, "AbortError");
|
assertEquals(e.name, "AbortError");
|
||||||
}
|
}
|
||||||
const stat = Deno.statSync(filename);
|
const stat = Deno.statSync(filename);
|
||||||
|
@ -269,6 +271,7 @@ unitTest(
|
||||||
try {
|
try {
|
||||||
await Deno.writeFile(filename, data, { signal: ac.signal });
|
await Deno.writeFile(filename, data, { signal: ac.signal });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
assert(e instanceof Error);
|
||||||
assertEquals(e.name, "AbortError");
|
assertEquals(e.name, "AbortError");
|
||||||
}
|
}
|
||||||
const stat = Deno.statSync(filename);
|
const stat = Deno.statSync(filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue