diff --git a/packages/@winglang/wingc/src/jsify/tests.rs b/packages/@winglang/wingc/src/jsify/tests.rs index 5337529985e..775bfe69365 100644 --- a/packages/@winglang/wingc/src/jsify/tests.rs +++ b/packages/@winglang/wingc/src/jsify/tests.rs @@ -2055,6 +2055,15 @@ fn lift_self_reference() { ); } +#[test] +fn entrypoint_this() { + assert_compile_ok!( + r#" + this; + "# + ); +} + #[test] fn allow_type_def_before_super() { assert_compile_ok!( diff --git a/packages/@winglang/wingc/src/lib.rs b/packages/@winglang/wingc/src/lib.rs index d379b03f932..e76eef6e816 100644 --- a/packages/@winglang/wingc/src/lib.rs +++ b/packages/@winglang/wingc/src/lib.rs @@ -291,6 +291,11 @@ pub fn type_check_file( ); tc.add_builtins(scope); + // If the file is an entrypoint file, we add "this" to its symbol environment + if is_entrypoint_file(&file.path) { + tc.add_this(&mut env); + } + tc.type_check_file_or_dir(scope); } diff --git a/tests/valid/factory.test.w b/tests/valid/factory.test.w new file mode 100644 index 00000000000..b59987b72bd --- /dev/null +++ b/tests/valid/factory.test.w @@ -0,0 +1,25 @@ +bring cloud; +bring "constructs" as constructs; +bring expect; + +class BucketFactory { + pub static makeBucket(scope: constructs.IConstruct): cloud.Bucket { + let bucket = new cloud.Bucket() in scope; + // apply customizations to bucket... + return bucket; + } +} + +// Test that we can use the factory pattern by passing top-level "this" +let bucket = BucketFactory.makeBucket(this); +log(nodeof(bucket).path); + +test "can use bucket" { + bucket.put("hello", "world"); + expect.equal(bucket.list().length, 1); +} + +test "can use other bucket" { + bucket.put("yo", "sup"); + expect.equal(bucket.list().length, 1); +} diff --git a/tests/valid/inflight_closure_as_super_param.test.w b/tests/valid/inflight_closure_as_super_param.test.w index 836c919c3f0..b2ca8008966 100644 --- a/tests/valid/inflight_closure_as_super_param.test.w +++ b/tests/valid/inflight_closure_as_super_param.test.w @@ -1,5 +1,3 @@ -bring expect; - class Foo {} class Base { @@ -29,9 +27,9 @@ let c = new Derived() as "derived"; assert(nodeof(c.f).path.endsWith("derived/in_derived")); // Make sure the instance created in the super call is scoped to the parent (root) assert(!nodeof(c.f_base).path.endsWith("derived/in_root")); -let appPath = nodeof(@app).path; -expect.equal(nodeof(c.f_base).path, "{appPath}/Default/in_root"); +let appPath = nodeof(this).path; +assert(nodeof(c.f_base).path == "{appPath}/in_root"); -test "boom!" { - expect.equal(c.h(), "boom!"); +test "boom!" { + assert(c.h() == "boom!"); }