From c2bd2c2d4c6f02b92e5084971452514c5452da38 Mon Sep 17 00:00:00 2001 From: Kaan <81116098+kaan650@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:33:40 +0300 Subject: [PATCH] Error when getting an unset context without a default (#101) Co-authored-by: centau <83140718+centau@users.noreply.github.com> --- docs/api/reactivity-utility.md | 2 ++ src/context.luau | 2 +- test/tests.luau | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/api/reactivity-utility.md b/docs/api/reactivity-utility.md index b8bff4d..c93b786 100644 --- a/docs/api/reactivity-utility.md +++ b/docs/api/reactivity-utility.md @@ -131,6 +131,8 @@ Creates a new context. Calling `context()` returns a new context function. Call this function with no arguments to get the context value. + If no context is set, the default value is returned, or an error is thrown + if no default was given. Call this function with a value and a function to create a new context with the given value. diff --git a/src/context.luau b/src/context.luau index 2ce3eff..ca9f8fb 100644 --- a/src/context.luau +++ b/src/context.luau @@ -40,7 +40,7 @@ local function context(...: T): Context return (if value ~= nil_symbol then value else nil) :: T end - if has_default ~= nil then + if has_default then return default_value else error("attempt to get context when no context is set and no default context is set", 0) diff --git a/test/tests.luau b/test/tests.luau index dd07517..9aa2da1 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -2828,6 +2828,16 @@ TEST("context()", function() end) end) end + + do CASE "get context without default" + local ctx = context() + + CHECK(not pcall(ctx)) + + root(function() + CHECK(not pcall(ctx)) + end) + end end) TEST("nested effects cases", function()