Skip to content

Commit 959926c

Browse files
rigzadmaennchen
andauthored
Deduplicate source references when merging extracted messages (#441)
* Deduplicate source references when merging extracted messages Two extraction calls that resolve to the same msgid at the same file:line each contribute a reference group, and merge_messages_after_checks/2 concatenates them without uniquing. Macro-generated code produces N calls attributed to a single line, so the same comment is written N times. on the very next line is already uniqued; this makes consistent with it. * Update test/gettext/extractor_test.exs Co-authored-by: Jonatan Männchen <jonatan@maennchen.ch> --------- Co-authored-by: Jonatan Männchen <jonatan@maennchen.ch>
1 parent d3d3046 commit 959926c

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/gettext/extractor_agent.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ defmodule Gettext.ExtractorAgent do
102102

103103
defp merge_messages_after_checks(message_1, message_2) do
104104
message_1
105-
|> Map.put(:references, message_1.references ++ message_2.references)
105+
|> Map.put(:references, Enum.uniq(message_1.references ++ message_2.references))
106106
|> Map.put(
107107
:extracted_comments,
108108
Enum.uniq(message_1.extracted_comments ++ message_2.extracted_comments)

test/gettext/extractor_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,41 @@ defmodule Gettext.ExtractorTest do
476476
Extractor.disable()
477477
end
478478

479+
test "duplicate references for one message are recorded once" do
480+
Extractor.enable()
481+
482+
code = """
483+
defmodule Gettext.ExtractorTest.SameLineGettext do
484+
use Gettext.Backend, otp_app: :test_application
485+
end
486+
487+
defmodule SameLine do
488+
require Gettext.Macros
489+
490+
def bar do
491+
[Gettext.Macros.gettext_with_backend(Gettext.ExtractorTest.SameLineGettext, "foo"), Gettext.Macros.gettext_with_backend(Gettext.ExtractorTest.SameLineGettext, "foo")]
492+
end
493+
end
494+
"""
495+
496+
Code.compile_string(code, Path.join(File.cwd!(), "same_line.ex"))
497+
498+
[{_path, {:changed, contents}}] =
499+
:test_application
500+
|> Extractor.pot_files([])
501+
|> Enum.reject(&match?({_path, :unchanged}, &1))
502+
503+
references =
504+
contents
505+
|> IO.iodata_to_binary()
506+
|> String.split("\n")
507+
|> Enum.filter(&String.starts_with?(&1, "#: same_line.ex"))
508+
509+
assert references == ["#: same_line.ex:9"]
510+
after
511+
Extractor.disable()
512+
end
513+
479514
defp write_file(path, contents) do
480515
path |> Path.dirname() |> File.mkdir_p!()
481516
File.write!(path, contents)

0 commit comments

Comments
 (0)