From 5000fe4a9873ddf5b1dbdae7ddabe077c427b1f5 Mon Sep 17 00:00:00 2001 From: Alan Knight Date: Wed, 2 Nov 2022 15:15:03 -0400 Subject: [PATCH 1/2] Compare message strings omitting curly braces --- lib/src/intl_suggestors/intl_messages.dart | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/src/intl_suggestors/intl_messages.dart b/lib/src/intl_suggestors/intl_messages.dart index c658d10e..547c70a4 100644 --- a/lib/src/intl_suggestors/intl_messages.dart +++ b/lib/src/intl_suggestors/intl_messages.dart @@ -89,9 +89,19 @@ Attempting to add a different message with the same name: /// Is there already something with this name, but a different messageText? bool isNameTaken(String name, String messageText) { var method = methods[name]; - return method != null && methods[name]!.messageText != messageText; + if (method == null) return false; + if (method.messageText == messageText) return false; + return _withoutCurlies(method.messageText) != _withoutCurlies(messageText); } + RegExp _curlyBraces = RegExp('[{}]'); + + /// Earlier generated messages, or those hand-written, might not use {} around + /// interpolated variables. Newer code always does, but then this might lead + /// to a false difference in the strings. Since {} are rare in user text, + /// check if strings with the same method name are equal when we remove them. + String _withoutCurlies(String text) => text.replaceAll(_curlyBraces, ''); + /// Delete our generated file. Used for tests. void delete() => outputFile.deleteSync(); From 8133dab269b66e0340e4528ccf099516b06fdcc4 Mon Sep 17 00:00:00 2001 From: Alan Knight Date: Wed, 2 Nov 2022 15:30:29 -0400 Subject: [PATCH 2/2] Better approach, generate the expression the right way --- lib/src/intl_suggestors/intl_message_syntax.dart | 6 +++++- lib/src/intl_suggestors/intl_messages.dart | 12 +----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/src/intl_suggestors/intl_message_syntax.dart b/lib/src/intl_suggestors/intl_message_syntax.dart index fb557b97..dc11d150 100644 --- a/lib/src/intl_suggestors/intl_message_syntax.dart +++ b/lib/src/intl_suggestors/intl_message_syntax.dart @@ -141,7 +141,11 @@ class MessageSyntax { String nameForNode(StringLiteral body, {String? initialName, bool startAtZero = false}) { - var messageText = body.toSource(); + // If the body is an interpolation, make sure we generate it the same way we will see it in + // the final file, otherwise comparisons may fail. Notably, be sure we always use ${} form. + var messageText = body is StringInterpolation + ? intlParameterizedMessage(body) + : body.toSource(); var functionName = toVariableName(messageText); functionName = owner.nameForString(initialName ?? functionName, messageText, startAtZero: startAtZero); diff --git a/lib/src/intl_suggestors/intl_messages.dart b/lib/src/intl_suggestors/intl_messages.dart index 547c70a4..c658d10e 100644 --- a/lib/src/intl_suggestors/intl_messages.dart +++ b/lib/src/intl_suggestors/intl_messages.dart @@ -89,19 +89,9 @@ Attempting to add a different message with the same name: /// Is there already something with this name, but a different messageText? bool isNameTaken(String name, String messageText) { var method = methods[name]; - if (method == null) return false; - if (method.messageText == messageText) return false; - return _withoutCurlies(method.messageText) != _withoutCurlies(messageText); + return method != null && methods[name]!.messageText != messageText; } - RegExp _curlyBraces = RegExp('[{}]'); - - /// Earlier generated messages, or those hand-written, might not use {} around - /// interpolated variables. Newer code always does, but then this might lead - /// to a false difference in the strings. Since {} are rare in user text, - /// check if strings with the same method name are equal when we remove them. - String _withoutCurlies(String text) => text.replaceAll(_curlyBraces, ''); - /// Delete our generated file. Used for tests. void delete() => outputFile.deleteSync();