From 8aa0d5f96ed418e21efb99967f1f6b7fea0dc87f Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 10 Apr 2021 22:56:40 +0100 Subject: [PATCH] feat: set useDefineForClassFields to true (#10119) Fixes: #9773 --- cli/ast.rs | 2 +- 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/bundle/fixture13.out | 4 +--- cli/tests/integration_tests.rs | 6 ++++++ cli/tsc_config.rs | 1 + 8 files changed, 20 insertions(+), 4 deletions(-) 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..f76bf5e9a07dba 100644 --- a/cli/ast.rs +++ b/cli/ast.rs @@ -267,7 +267,7 @@ fn strip_config_from_emit_options( typescript::strip::ImportsNotUsedAsValues::Remove } }, - ..Default::default() + use_define_for_class_fields: true, } } 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/bundle/fixture13.out b/cli/tests/bundle/fixture13.out index 2c511528011b35..1c7a8c99197e4d 100644 --- a/cli/tests/bundle/fixture13.out +++ b/cli/tests/bundle/fixture13.out @@ -9,11 +9,9 @@ function d() { return Object.assign(promise, methods); } class A { + s = d(); a() { this.s.resolve(); } - constructor(){ - this.s = d(); - } } new A(); 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", ];