[flutter_tools] fix RangeError in gen-l10n by checking for empty string (#107604)

This commit is contained in:
Christopher Fujino
2022-07-15 11:56:09 -07:00
committed by GitHub
parent d72853940f
commit 94da724868
3 changed files with 39 additions and 3 deletions

View File

@@ -1050,6 +1050,9 @@ class LocalizationsGenerator {
}
static bool _isValidGetterAndMethodName(String name) {
if (name.isEmpty) {
return false;
}
// Public Dart method name must not start with an underscore
if (name[0] == '_') {
return false;

View File

@@ -124,6 +124,9 @@ class L10nException implements Exception {
L10nException(this.message);
final String message;
@override
String toString() => message;
}
// One optional named parameter to be used by a NumberFormat.

View File

@@ -992,9 +992,7 @@ class AppLocalizationsEn extends AppLocalizations {
});
testWithoutContext(
'throws an error attempting to add preferred locales '
'when there is no corresponding arb file for that '
'locale',
'throws an error attempting to add preferred locales when there is no corresponding arb file for that locale',
() {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
@@ -1161,6 +1159,38 @@ class AppLocalizationsEn extends AppLocalizations {
)),
);
});
testWithoutContext('throws when an empty string is used as a key', () {
const String arbFileStringWithEmptyResourceId = '''
{
"market": "MARKET",
"": {
"description": "This key is invalid"
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile('app_en.arb')
.writeAsStringSync(arbFileStringWithEmptyResourceId);
expect(
() => LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: 'app_en.arb',
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
).loadResources(),
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('Invalid ARB resource name ""'),
)),
);
});
testWithoutContext('throws when the same locale is detected more than once', () {
const String secondMessageArbFileString = '''
{