From 75f948af0c34c58846be0ac58ec980b2fd454a45 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 10 Apr 2021 21:41:15 +0100 Subject: [PATCH] Reland #9774 --- cli/ast.rs | 1 + cli/lsp/language_server.rs | 1 + cli/module_graph.rs | 2 ++ cli/tests/091_use_define_for_class_fields.ts | 4 ++++ cli/tests/091_use_define_for_class_fields.ts.out | 4 ++++ cli/tests/integration_tests.rs | 6 ++++++ cli/tsc_config.rs | 1 + 7 files changed, 19 insertions(+) create mode 100644 cli/tests/091_use_define_for_class_fields.ts create mode 100644 cli/tests/091_use_define_for_class_fields.ts.out diff --git a/cli/ast.rs b/cli/ast.rs index 83d0e16e9bc111..5d2047280386eb 100644 --- a/cli/ast.rs +++ b/cli/ast.rs @@ -267,6 +267,7 @@ fn strip_config_from_emit_options( typescript::strip::ImportsNotUsedAsValues::Remove } }, + use_define_for_class_fields: true, ..Default::default() } } diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index d1f6c67ecb0810..70682c41ba02b1 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -372,6 +372,7 @@ impl Inner { "noEmit": true, "strict": true, "target": "esnext", + "useDefineForClassFields": true, })); let (maybe_config, maybe_root_uri) = { let config = &self.config; diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 18cb3209cdf4e5..51e2573396061c 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -803,6 +803,7 @@ impl Graph { "strict": true, "target": "esnext", "tsBuildInfoFile": "deno:///.tsbuildinfo", + "useDefineForClassFields": true, })); if options.emit { config.merge(&json!({ @@ -948,6 +949,7 @@ impl Graph { "module": "esnext", "strict": true, "target": "esnext", + "useDefineForClassFields": true, })); let opts = match options.bundle_type { BundleType::Esm | BundleType::Iife => json!({ diff --git a/cli/tests/091_use_define_for_class_fields.ts b/cli/tests/091_use_define_for_class_fields.ts new file mode 100644 index 00000000000000..9be984abf4011d --- /dev/null +++ b/cli/tests/091_use_define_for_class_fields.ts @@ -0,0 +1,4 @@ +class A { + b = this.a; + constructor(public a: unknown) {} +} diff --git a/cli/tests/091_use_define_for_class_fields.ts.out b/cli/tests/091_use_define_for_class_fields.ts.out new file mode 100644 index 00000000000000..08f94a9671f239 --- /dev/null +++ b/cli/tests/091_use_define_for_class_fields.ts.out @@ -0,0 +1,4 @@ +[WILDCARD]error: TS2729 [ERROR]: Property 'a' is used before its initialization. + b = this.a; + ^ +[WILDCARD] diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 827fc871b1d7ef..5067b81637e8dc 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2867,6 +2867,12 @@ console.log("finish"); util::test_pty(args, output, input); } + itest!(_091_use_define_for_class_fields { + args: "run 091_use_define_for_class_fields.ts", + output: "091_use_define_for_class_fields.ts.out", + exit_code: 1, + }); + itest!(js_import_detect { args: "run --quiet --reload js_import_detect.ts", output: "js_import_detect.ts.out", diff --git a/cli/tsc_config.rs b/cli/tsc_config.rs index 9211f92a5f1d08..0d85adb2ed3ef1 100644 --- a/cli/tsc_config.rs +++ b/cli/tsc_config.rs @@ -128,6 +128,7 @@ pub const IGNORED_RUNTIME_COMPILER_OPTIONS: &[&str] = &[ "traceResolution", "tsBuildInfoFile", "typeRoots", + "useDefineForClassFields", "version", "watch", ];