Skip to content

Commit ad4760d

Browse files
committed
Cleanup parent resource tracking
1 parent bf7dc6a commit ad4760d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

fluent/_nodes.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ local FluentNode = class()
1616
FluentNode._name = "FluentNode"
1717

1818
function FluentNode:_init (ast, resource)
19-
if self._name == "MessageReference" or self._name == "TermReference" then
20-
rawset(getmetatable(self), "_resource", resource)
21-
end
19+
self:set_parent(resource)
2220
for key, value in pairs(ast) do
2321
if type(key) == "string" then
2422
if key == "id" then
@@ -36,6 +34,16 @@ function FluentNode:_init (ast, resource)
3634
end)
3735
end
3836

37+
function FluentNode:set_parent (resource)
38+
if self._name == "MessageReference" or self._name == "TermReference" then
39+
rawset(getmetatable(self), "_resource", resource)
40+
end
41+
end
42+
43+
function FluentNode:get_parent ()
44+
return rawget(getmetatable(self), "_resource")
45+
end
46+
3947
function FluentNode:inject (node)
4048
if type(node) ~= "table" then return nil end
4149
local elements = rawget(self, "elements")
@@ -295,7 +303,7 @@ FTL.MessageReference = class(FluentNode)
295303
FTL.MessageReference._name = "MessageReference"
296304

297305
function FTL.MessageReference:format (parameters)
298-
return rawget(getmetatable(self), "_resource"):get_message(self.id.name):format(parameters)
306+
return self:get_parent():get_message(self.id.name):format(parameters)
299307
end
300308

301309
FTL.TermReference = class(FluentNode)
@@ -314,7 +322,7 @@ function FTL.TermReference:__mul (node)
314322
end
315323

316324
function FTL.TermReference:format (parameters)
317-
return rawget(getmetatable(self), "_resource"):get_term(self.id.name):format(parameters)
325+
return self:get_parent():get_term(self.id.name):format(parameters)
318326
end
319327

320328
FTL._TermReference = FTL.TermReference

fluent/resource.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888
function FluentResource:__add (other)
8989
if not self:is_a(other:is_a()) then error("Cannot merge unlike types") end
9090
for _, node in ipairs(other.body) do
91-
if rawget(node, "_resource") then rawset(node, "_resource", self) end
91+
node:set_parent(self)
9292
self:load_node(node)
9393
end
9494
return self

0 commit comments

Comments
 (0)