Skip to content

Commit aea126a

Browse files
Dart Teamalan-knight
Dart Team
authored andcommitted
Make Intl generated code pass most current lint options
Trying this again, but reverted the "new" removal and one additional type in generated code, as the generated code is used in Dart 1 projects that are still active. Mostly using ignore_for_file, but fixing a few of the most obvious issues Started out as integrating dart-archive/intl_translation#31 but then expanded. Cloned from CL 264686901 by 'g4 patch'. Original change by alanknight@alanknight:intl_maintenance:290:citc on 2019/08/21 14:02:13. PiperOrigin-RevId: 264925581
1 parent 30cd01d commit aea126a

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

pkgs/intl_translation/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 0.17.6
22
* Strip indentation from generated JSON output to improve codesize.
3+
* Make generated code not trigger most lints, either by fixing issues
4+
or by using lots of ignore_for_file directives.
35

46
## 0.17.5
57
* Allow multiple ARB files with the same locale and combine

pkgs/intl_translation/lib/generate_localized.dart

+23-27
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MessageGeneration {
5757
/// so it's left undefined.
5858
String package;
5959

60-
get releaseMode => codegenMode == 'release';
60+
bool get releaseMode => codegenMode == 'release';
6161

6262
bool get jsonMode => false;
6363

@@ -105,10 +105,8 @@ class MessageGeneration {
105105
for (var translation in usableTranslations) {
106106
// Some messages we generate as methods in this class. Simpler ones
107107
// we inline in the map from names to messages.
108-
var messagesThatNeedMethods = translation.originalMessages
109-
.where((each) => _hasArguments(each))
110-
.toSet()
111-
.toList();
108+
var messagesThatNeedMethods =
109+
translation.originalMessages.where(_hasArguments).toSet().toList();
112110
for (var original in messagesThatNeedMethods) {
113111
output
114112
..write(" ")
@@ -153,22 +151,21 @@ class MessageGeneration {
153151
// messages from the main program should be duplicated here with the same
154152
// function name.
155153
156-
// ignore_for_file: unnecessary_brace_in_string_interps
154+
// Ignore issues from commonly used lints in this file.
155+
// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
156+
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
157+
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
158+
// ignore_for_file:unused_import, file_names
157159
158160
import 'package:$intlImportPath/intl.dart';
159161
import 'package:$intlImportPath/message_lookup_by_library.dart';
160162
$extraImports
161-
// ignore: unnecessary_new
162163
final messages = new MessageLookup();
163164
164-
// ignore: unused_element
165-
final _keepAnalysisHappy = Intl.defaultLocale;
166-
167-
// ignore: non_constant_identifier_names
168-
typedef MessageIfAbsent(String message_str, List<dynamic> args);
165+
typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
169166
170167
class MessageLookup extends MessageLookupByLibrary {
171-
get localeName => '$locale';
168+
String get localeName => '$locale';
172169
173170
""" +
174171
(releaseMode ? overrideLookup : "");
@@ -185,7 +182,6 @@ class MessageLookup extends MessageLookupByLibrary {
185182
// If there's no message_str, then we are an internal lookup, e.g. an
186183
// embedded plural, and shouldn't fail.
187184
if (message_str == null) return null;
188-
// ignore: unnecessary_new
189185
throw new UnsupportedError(
190186
"No translation found for message '\$name',\\n"
191187
" original text '\$message_str'");
@@ -215,12 +211,11 @@ class MessageLookup extends MessageLookupByLibrary {
215211
var locale = Intl.canonicalizedLocale(rawLocale);
216212
var loadOperation = (useDeferredLoading)
217213
? " '$locale': ${libraryName(locale)}.loadLibrary,\n"
218-
: "// ignore: unnecessary_new\n"
219-
" '$locale': () => new Future.value(null),\n";
214+
: " '$locale': () => new Future.value(null),\n";
220215
output.write(loadOperation);
221216
}
222217
output.write("};\n");
223-
output.write("\nMessageLookupByLibrary _findExact(localeName) {\n"
218+
output.write("\nMessageLookupByLibrary _findExact(String localeName) {\n"
224219
" switch (localeName) {\n");
225220
for (var rawLocale in allLocales) {
226221
var locale = Intl.canonicalizedLocale(rawLocale);
@@ -238,11 +233,17 @@ class MessageLookup extends MessageLookupByLibrary {
238233
// This is a library that looks up messages for specific locales by
239234
// delegating to the appropriate library.
240235
236+
// Ignore issues from commonly used lints in this file.
237+
// ignore_for_file:implementation_imports, file_names, unnecessary_new
238+
// ignore_for_file:unnecessary_brace_in_string_interps, directives_ordering
239+
// ignore_for_file:argument_type_not_assignable, invalid_assignment
240+
// ignore_for_file:prefer_single_quotes, prefer_generic_function_type_aliases
241+
// ignore_for_file:comment_references
242+
241243
import 'dart:async';
242244
243245
import 'package:$intlImportPath/intl.dart';
244246
import 'package:$intlImportPath/message_lookup_by_library.dart';
245-
// ignore: implementation_imports
246247
import 'package:$intlImportPath/src/intl_helpers.dart';
247248
248249
""";
@@ -260,16 +261,12 @@ Future<bool> initializeMessages(String localeName) async {
260261
(locale) => _deferredLibraries[locale] != null,
261262
onFailure: (_) => null);
262263
if (availableLocale == null) {
263-
// ignore: unnecessary_new
264264
return new Future.value(false);
265265
}
266266
var lib = _deferredLibraries[availableLocale];
267-
// ignore: unnecessary_new
268267
await (lib == null ? new Future.value(false) : lib());
269-
// ignore: unnecessary_new
270268
initializeInternalMessageLookup(() => new CompositeMessageLookup());
271269
messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
272-
// ignore: unnecessary_new
273270
return new Future.value(true);
274271
}
275272
@@ -281,7 +278,7 @@ bool _messagesExistFor(String locale) {
281278
}
282279
}
283280
284-
MessageLookupByLibrary _findGeneratedMessagesFor(locale) {
281+
MessageLookupByLibrary _findGeneratedMessagesFor(String locale) {
285282
var actualLocale = Intl.verifiedLocale(locale, _messagesExistFor,
286283
onFailure: (_) => null);
287284
if (actualLocale == null) return null;
@@ -329,8 +326,8 @@ import '${generatedFilePrefix}messages_all.dart' show evaluateJsonTemplate;
329326
Iterable<TranslatedMessage> usableTranslations, String locale) {
330327
output.write(r"""
331328
var _messages;
332-
// ignore: unnecessary_new
333-
get messages => _messages ??= new JsonDecoder().convert(messageText);
329+
get messages => _messages ??=
330+
const JsonDecoder().convert(messageText) as Map<String, dynamic>;
334331
""");
335332

336333
output.write(" static final messageText = ");
@@ -358,7 +355,7 @@ import '${generatedFilePrefix}messages_all.dart' show evaluateJsonTemplate;
358355
/// * \['Intl.gender', String gender, (templates for female, male, other)\]
359356
/// * \['Intl.select', String choice, { 'case' : template, ...} \]
360357
/// * \['text alternating with ', 0 , ' indexes in the argument list'\]
361-
String evaluateJsonTemplate(Object input, List<dynamic> args) {
358+
String evaluateJsonTemplate(dynamic input, List<dynamic> args) {
362359
if (input == null) return null;
363360
if (input is String) return input;
364361
if (input is int) {
@@ -398,7 +395,6 @@ String evaluateJsonTemplate(Object input, List<dynamic> args) {
398395
399396
// If we get this far, then we are a basic interpolation, just strings and
400397
// ints.
401-
// ignore: unnecessary_new
402398
var output = new StringBuffer();
403399
for (var entry in template) {
404400
if (entry is int) {

0 commit comments

Comments
 (0)