diff options
| -rw-r--r-- | lua/mason-core/installer/registry/expr.lua | 28 | ||||
| -rw-r--r-- | tests/mason-core/installer/registry/expr_spec.lua | 22 |
2 files changed, 26 insertions, 24 deletions
diff --git a/lua/mason-core/installer/registry/expr.lua b/lua/mason-core/installer/registry/expr.lua index 539b557c..c6a6aafe 100644 --- a/lua/mason-core/installer/registry/expr.lua +++ b/lua/mason-core/installer/registry/expr.lua @@ -39,12 +39,13 @@ end ---@param str string ---@param ctx table<string, any> -function M.eval(str, ctx) +function M.interpolate(str, ctx) ctx = shallow_clone(ctx) return Result.pcall(function() setmetatable(ctx, { __index = FILTERS }) return _.gsub("{{([^}]+)}}", function(expr) local components = parse_expr(expr) + local value = assert( setfenv( assert( @@ -55,18 +56,17 @@ function M.eval(str, ctx) )(), ("Value is nil: %q"):format(components.value_expr) ) - return _.reduce( - _.apply_to, - value, - _.map(function(filter_expr) - local filter = setfenv( - assert(loadstring("return " .. filter_expr), ("Failed to parse filter: %q"):format(filter_expr)), - ctx - )() - assert(type(filter) == "function", ("Invalid filter expression: %q"):format(filter_expr)) - return filter - end, components.filters) - ) + + local filters = _.map(function(filter_expr) + local filter = setfenv( + assert(loadstring("return " .. filter_expr), ("Failed to parse filter: %q"):format(filter_expr)), + ctx + )() + assert(type(filter) == "function", ("Invalid filter expression: %q"):format(filter_expr)) + return filter + end, components.filters) + + return _.reduce(_.apply_to, value, filters) end, str) end) end @@ -80,7 +80,7 @@ function M.tbl_interpolate(tbl, ctx) local interpolated = {} for k, v in pairs(tbl) do if type(v) == "string" then - interpolated[k] = try(M.eval(v, ctx)) + interpolated[k] = try(M.interpolate(v, ctx)) elseif type(v) == "table" then interpolated[k] = try(M.tbl_interpolate(v, ctx)) else diff --git a/tests/mason-core/installer/registry/expr_spec.lua b/tests/mason-core/installer/registry/expr_spec.lua index abd1874e..a52ca48b 100644 --- a/tests/mason-core/installer/registry/expr_spec.lua +++ b/tests/mason-core/installer/registry/expr_spec.lua @@ -4,11 +4,11 @@ local Result = require "mason-core.result" describe("registry expressions", function() it("should eval simple expressions", function() - assert.same(Result.success "Hello, world!", expr.eval("Hello, world!", {})) + assert.same(Result.success "Hello, world!", expr.interpolate("Hello, world!", {})) assert.same( Result.success "Hello, John Doe!", - expr.eval("Hello, {{firstname}} {{ lastname }}!", { + expr.interpolate("Hello, {{firstname}} {{ lastname }}!", { firstname = "John", lastname = "Doe", }) @@ -18,14 +18,14 @@ describe("registry expressions", function() it("should eval nested access", function() assert.same( Result.success "Hello, world!", - expr.eval("Hello, {{greeting.name}}!", { greeting = { name = "world" } }) + expr.interpolate("Hello, {{greeting.name}}!", { greeting = { name = "world" } }) ) end) it("should eval benign expressions", function() assert.same( Result.success "Hello, JOHNDOE JR.!", - expr.eval("Hello, {{greeting.firstname .. greeting.lastname .. tostring(tbl) | to_upper}}!", { + expr.interpolate("Hello, {{greeting.firstname .. greeting.lastname .. tostring(tbl) | to_upper}}!", { greeting = { firstname = "John", lastname = "Doe" }, tbl = setmetatable({}, { __tostring = function() @@ -37,7 +37,7 @@ describe("registry expressions", function() assert.same( Result.success "Gloves", - expr.eval("G{{ 'Cloves' | strip_prefix(trim) }}", { + expr.interpolate("G{{ 'Cloves' | strip_prefix(trim) }}", { trim = "C", }) ) @@ -46,7 +46,7 @@ describe("registry expressions", function() it("should eval expressions with filters", function() assert.same( Result.success "Hello, MR. John!", - expr.eval("Hello, {{prefix|to_upper}} {{ name | trim }}!", { + expr.interpolate("Hello, {{prefix|to_upper}} {{ name | trim }}!", { prefix = "Mr.", name = " John ", }) @@ -54,7 +54,7 @@ describe("registry expressions", function() assert.same( Result.success "Hello, Sir MR. John!", - expr.eval("Hello, {{prefix|to_upper | format 'Sir %s'}} {{ name | trim }}!", { + expr.interpolate("Hello, {{prefix|to_upper | format 'Sir %s'}} {{ name | trim }}!", { prefix = "Mr.", name = " John ", }) @@ -63,20 +63,22 @@ describe("registry expressions", function() it("should reject invalid values", function() assert.is_true( - match.matches [[^.*Value is nil: "non_existent"]](expr.eval("Hello, {{non_existent}}", {}):err_or_nil()) + match.matches [[^.*Value is nil: "non_existent"]]( + expr.interpolate("Hello, {{non_existent}}", {}):err_or_nil() + ) ) end) it("should reject invalid filters", function() assert.is_true( match.matches [[^.*Invalid filter expression: "whut"]]( - expr.eval("Hello, {{ value | whut }}", { value = "value" }):err_or_nil() + expr.interpolate("Hello, {{ value | whut }}", { value = "value" }):err_or_nil() ) ) assert.is_true( match.matches [[^.*Failed to parse filter: "wh%-!uut"]]( - expr.eval("Hello, {{ value | wh-!uut }}", { value = "value" }):err_or_nil() + expr.interpolate("Hello, {{ value | wh-!uut }}", { value = "value" }):err_or_nil() ) ) end) |
