diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c7c39f..805f529 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). -------------------------------------------------------------------------------- +## Unreleased + +### Added + +- Context functions now also return results. + +-------------------------------------------------------------------------------- + ## [0.3.0] - 2024-10-06 ### Added diff --git a/src/context.luau b/src/context.luau index fc0e8c1..6c571d0 100644 --- a/src/context.luau +++ b/src/context.luau @@ -9,7 +9,7 @@ local push_scope = graph.push_scope local pop_scope = graph.pop_scope local set_context = graph.set_context -export type Context = (() -> T) & ((T, () -> ()) -> ()) +export type Context = (() -> T) & ((T, () -> U) -> U) local nil_symbol = newproxy() local count = 0 @@ -21,7 +21,7 @@ local function context(...: T): Context local has_default = select("#", ...) > 0 local default_value = ... - return function(...) + return function(...): any -- todo: fix type error local scope: Node? | false = get_scope() if select("#", ...) == 0 then -- get @@ -66,6 +66,8 @@ local function context(...: T): Context if not ok then throw(`error while running context:\n\n{result}`) end + + return result end return nil :: any diff --git a/test/tests.luau b/test/tests.luau index 3fbdbf4..45aa41f 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -2192,10 +2192,12 @@ TEST("context()", function() CHECK(ctx() == 1) root(function() - ctx(2, function() + local v = ctx(2, function() CHECK(ctx() == 2) + return ctx() end) + CHECK(v == 2) CHECK(ctx() == 1) end) end