Add flag to disable setting of default properties

This commit is contained in:
centauri 2025-06-23 21:36:51 +01:00
parent 3c2fafa1cd
commit 5f752fe903
3 changed files with 18 additions and 12 deletions

View file

@ -3,23 +3,26 @@ local Instance = game and Instance or require "../test/mock".Instance :: never
local defaults = require "./defaults"
local apply = require "./apply"
local flags = require "./flags"
local ctor_cache = {} :: { [string]: () -> Instance }
setmetatable(ctor_cache :: any, {
__index = function(self, class)
local ok, instance: Instance = pcall(Instance.new, class :: any)
if not ok then error(`invalid class name, could not create instance of class { class }`, 0) end
local default: { [string]: unknown }? = defaults[class]
if default then
for i, v in next, default do
(instance :: any)[i] = v
end
end
local function ctor(properties: Props): Instance
return apply(instance:Clone(), properties)
local ok, instance: Instance = pcall(Instance.new, class :: any)
if not ok then error(`invalid class name { class }`, 0) end
if flags.defaults then
local default: { [string]: unknown }? = defaults[class]
if default then
for i, v in default do
(instance :: any)[i] = v
end
end
end
return apply(instance, properties)
end
self[class] = ctor

View file

@ -7,5 +7,7 @@ local is_O2 = inline_test() ~= "inline_test"
return {
strict = not is_O2,
batch = false,
defer_nested_properties = true
defaults = true,
use_default_properties = true,
defer_nested_properties = true,
}