Skip to content

Commit

Permalink
add back this
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Sep 4, 2024
1 parent 7cb88d8 commit e494308
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/@winglang/wingc/src/jsify/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
5 changes: 5 additions & 0 deletions packages/@winglang/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/valid/factory.test.w
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 4 additions & 6 deletions tests/valid/inflight_closure_as_super_param.test.w
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
bring expect;

class Foo {}

class Base {
Expand Down Expand Up @@ -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!");
}

0 comments on commit e494308

Please sign in to comment.