Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.

Commit 69b7382

Browse files
Dart Teamalan-knight
Dart Team
authored andcommitted
Make Intl generated code pass most current lint options
Mostly using ignore_for_file, but fixing a few of the most obvious issues Started out as integrating #31 but then expanded. PiperOrigin-RevId: 264686901
1 parent 0f6933c commit 69b7382

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

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

lib/generate_localized.dart

+31-35
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
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
162-
final messages = new MessageLookup();
163+
final messages = 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,8 +182,7 @@ 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
189-
throw new UnsupportedError(
185+
throw UnsupportedError(
190186
"No translation found for message '\$name',\\n"
191187
" original text '\$message_str'");
192188
}
@@ -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': () => 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
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,17 +261,13 @@ Future<bool> initializeMessages(String localeName) async {
260261
(locale) => _deferredLibraries[locale] != null,
261262
onFailure: (_) => null);
262263
if (availableLocale == null) {
263-
// ignore: unnecessary_new
264-
return new Future.value(false);
264+
return Future.value(false);
265265
}
266266
var lib = _deferredLibraries[availableLocale];
267-
// ignore: unnecessary_new
268-
await (lib == null ? new Future.value(false) : lib());
269-
// ignore: unnecessary_new
270-
initializeInternalMessageLookup(() => new CompositeMessageLookup());
267+
await (lib == null ? Future.value(false) : lib());
268+
initializeInternalMessageLookup(() => CompositeMessageLookup());
271269
messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
272-
// ignore: unnecessary_new
273-
return new Future.value(true);
270+
return Future.value(true);
274271
}
275272
276273
bool _messagesExistFor(String locale) {
@@ -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;
@@ -328,9 +325,9 @@ import '${generatedFilePrefix}messages_all.dart' show evaluateJsonTemplate;
328325
void writeTranslations(
329326
Iterable<TranslatedMessage> usableTranslations, String locale) {
330327
output.write(r"""
331-
var _messages;
332-
// ignore: unnecessary_new
333-
get messages => _messages ??= new JsonDecoder().convert(messageText);
328+
Map<String, dynamic> _messages;
329+
Map<String,dynamic> 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,8 +395,7 @@ 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
402-
var output = new StringBuffer();
398+
var output = StringBuffer();
403399
for (var entry in template) {
404400
if (entry is int) {
405401
output.write("\${args[entry]}");

0 commit comments

Comments
 (0)