From 2e5487609e212ae62912c67b603eeb118c90d9fb Mon Sep 17 00:00:00 2001 From: Alan Knight Date: Thu, 19 Oct 2023 15:49:12 -0400 Subject: [PATCH] Start at fixing the constant migrator's date format detection --- lib/src/intl_suggestors/intl_migrator.dart | 23 +++++++------- .../constant_migrator_test.dart | 31 +++++++++++++++++-- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/src/intl_suggestors/intl_migrator.dart b/lib/src/intl_suggestors/intl_migrator.dart index b306ccd8..33f5f4a4 100644 --- a/lib/src/intl_suggestors/intl_migrator.dart +++ b/lib/src/intl_suggestors/intl_migrator.dart @@ -119,18 +119,19 @@ class ConstantStringMigrator extends GeneralizingAstVisitor if (string.toUpperCase() == string) return; // Is the first character uppercase, excluding strings that start with two of the same // uppercase character, which has a good chance of being a date format (e.g. 'MM/dd/YYYY'). - if (firstLetter != secondLetter && - firstLetter.toLowerCase() != firstLetter) { - // Constant strings might be private. - var name = publicNameFor(node); - names.add(name); - final functionCall = - _messages.syntax.getterCall(literal, _className, name: name); - final functionDef = - _messages.syntax.getterDefinition(literal, _className, name: name); - yieldPatch('final String ${node.name} = $functionCall', start, end); - addMethodToClass(_messages, functionDef); + if (firstLetter == secondLetter && + firstLetter.toUpperCase() == firstLetter) { + return; } + // Constant strings might be private. + var name = publicNameFor(node); + names.add(name); + final functionCall = + _messages.syntax.getterCall(literal, _className, name: name); + final functionDef = + _messages.syntax.getterDefinition(literal, _className, name: name); + yieldPatch('final String ${node.name} = $functionCall', start, end); + addMethodToClass(_messages, functionDef); } } diff --git a/test/intl_suggestors/constant_migrator_test.dart b/test/intl_suggestors/constant_migrator_test.dart index d6c5a151..22cdeb1e 100644 --- a/test/intl_suggestors/constant_migrator_test.dart +++ b/test/intl_suggestors/constant_migrator_test.dart @@ -61,10 +61,10 @@ void main() { test('non-matching standalone constant', () async { await testSuggestor( input: ''' - const foo = 'probably-not'; + const foo = 'probably_not'; ''', expectedOutput: ''' - const foo = 'probably-not'; + const foo = 'probably_not'; ''', ); final expectedFileContent = ''; @@ -145,6 +145,33 @@ void main() { expect(messages.messageContents(), expectedFileContent); }); + test('date formats are ignored', () async { + await testSuggestor( + input: ''' + const foo = 'MM/dd/yy'; + ''', + expectedOutput: ''' + const foo = 'MM/dd/yy'; + ''', + ); + final expectedFileContent = ''; + expect(messages.messageContents(), expectedFileContent); + }); + + test('date format logic does not have false positives', () async { + await testSuggestor( + input: ''' + const migrateMe = 'migrate me'; + ''', + expectedOutput: ''' + final String migrateMe = TestClassIntl.migrateMe; + ''', + ); + final expectedFileContent = + '\n static String get migrateMe => Intl.message(\'migrate me\', name: \'TestClassIntl_migrateMe\');\n'; + expect(messages.messageContents(), expectedFileContent); + }); + test('static constant', () async { await testSuggestor( input: '''