Skip to content

Commit c2051dd

Browse files
authored
Enable and fix package:pedantic lints (flutter#135)
- always_declare_return_types - omit_local_variable_types - prefer_collection_literals - prefer_if_null_operators - unawaited_futures Bump min SDK to 2.2.0 to support Set literals.
1 parent 2075fe9 commit c2051dd

16 files changed

+104
-105
lines changed

analysis_options.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include: package:pedantic/analysis_options.1.9.0.yaml
12
linter:
23
rules:
34
- annotate_overrides

example/example.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ Analytics getAnalytics() {
3232
}
3333

3434
void _handleFoo() {
35-
Analytics analytics = getAnalytics();
35+
var analytics = getAnalytics();
3636
analytics.sendEvent('main', 'foo');
3737
}
3838

3939
void _handleBar() {
40-
Analytics analytics = getAnalytics();
40+
var analytics = getAnalytics();
4141
analytics.sendEvent('main', 'bar');
4242
}
4343

4444
void _changePage() {
45-
Analytics analytics = getAnalytics();
45+
var analytics = getAnalytics();
4646
window.history.pushState(null, 'new page', '${++_count}.html');
4747
analytics.sendScreenView(window.location.pathname);
4848
}

example/ga.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ library usage_ga;
77

88
import 'package:usage/usage_io.dart';
99

10-
main(List args) async {
11-
final String defaultUA = 'UA-55029513-1';
10+
void main(List args) async {
11+
final defaultUA = 'UA-55029513-1';
1212

1313
if (args.isEmpty) {
1414
print('usage: dart ga <GA tracking ID>');

lib/src/usage_impl.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '../uuid/uuid.dart';
1111
String postEncode(Map<String, dynamic> map) {
1212
// &foo=bar
1313
return map.keys.map((key) {
14-
String value = '${map[key]}';
14+
var value = '${map[key]}';
1515
return '${key}=${Uri.encodeComponent(value)}';
1616
}).join('&');
1717
}
@@ -44,10 +44,10 @@ class ThrottlingBucket {
4444
}
4545

4646
void _checkReplenish() {
47-
int now = DateTime.now().millisecondsSinceEpoch;
47+
var now = DateTime.now().millisecondsSinceEpoch;
4848

4949
if (_lastReplenish + 1000 >= now) {
50-
int inc = (now - _lastReplenish) ~/ 1000;
50+
var inc = (now - _lastReplenish) ~/ 1000;
5151
drops = math.min(drops + inc, startingCount);
5252
_lastReplenish += (1000 * inc);
5353
}
@@ -108,7 +108,7 @@ class AnalyticsImpl implements Analytics {
108108

109109
@override
110110
bool get enabled {
111-
bool optIn = analyticsOpt == AnalyticsOpt.optIn;
111+
var optIn = analyticsOpt == AnalyticsOpt.optIn;
112112
return optIn
113113
? properties['enabled'] == true
114114
: properties['enabled'] != false;
@@ -121,7 +121,7 @@ class AnalyticsImpl implements Analytics {
121121

122122
@override
123123
Future sendScreenView(String viewName, {Map<String, String> parameters}) {
124-
Map<String, dynamic> args = {'cd': viewName};
124+
var args = <String, dynamic>{'cd': viewName};
125125
if (parameters != null) {
126126
args.addAll(parameters);
127127
}
@@ -131,7 +131,7 @@ class AnalyticsImpl implements Analytics {
131131
@override
132132
Future sendEvent(String category, String action,
133133
{String label, int value, Map<String, String> parameters}) {
134-
Map<String, dynamic> args = {'ec': category, 'ea': action};
134+
var args = <String, dynamic>{'ec': category, 'ea': action};
135135
if (label != null) args['el'] = label;
136136
if (value != null) args['ev'] = value;
137137
if (parameters != null) {
@@ -142,14 +142,14 @@ class AnalyticsImpl implements Analytics {
142142

143143
@override
144144
Future sendSocial(String network, String action, String target) {
145-
Map<String, dynamic> args = {'sn': network, 'sa': action, 'st': target};
145+
var args = <String, dynamic>{'sn': network, 'sa': action, 'st': target};
146146
return _sendPayload('social', args);
147147
}
148148

149149
@override
150150
Future sendTiming(String variableName, int time,
151151
{String category, String label}) {
152-
Map<String, dynamic> args = {'utv': variableName, 'utt': time};
152+
var args = <String, dynamic>{'utv': variableName, 'utt': time};
153153
if (label != null) args['utl'] = label;
154154
if (category != null) args['utc'] = category;
155155
return _sendPayload('timing', args);
@@ -165,7 +165,7 @@ class AnalyticsImpl implements Analytics {
165165
Future sendException(String description, {bool fatal}) {
166166
// We trim exceptions to a max length; google analytics will apply it's own
167167
// truncation, likely around 150 chars or so.
168-
const int maxExceptionLength = 1000;
168+
const maxExceptionLength = 1000;
169169

170170
// In order to ensure that the client of this API is not sending any PII
171171
// data, we strip out any stack trace that may reference a path on the
@@ -180,7 +180,7 @@ class AnalyticsImpl implements Analytics {
180180
description = description.substring(0, maxExceptionLength);
181181
}
182182

183-
Map<String, dynamic> args = {'exd': description};
183+
var args = <String, dynamic>{'exd': description};
184184
if (fatal != null && fatal) args['exf'] = '1';
185185
return _sendPayload('exception', args);
186186
}

lib/src/usage_impl_html.dart

+7-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class AnalyticsHtml extends AnalyticsImpl {
2121
applicationName: applicationName,
2222
applicationVersion: applicationVersion,
2323
analyticsUrl: analyticsUrl) {
24-
int screenWidth = window.screen.width;
25-
int screenHeight = window.screen.height;
24+
var screenWidth = window.screen.width;
25+
var screenHeight = window.screen.height;
2626

2727
setSessionValue('sr', '${screenWidth}x$screenHeight');
2828
setSessionValue('sd', '${window.screen.pixelDepth}-bits');
@@ -40,14 +40,13 @@ class HtmlPostHandler extends PostHandler {
4040

4141
@override
4242
Future sendPost(String url, Map<String, dynamic> parameters) {
43-
int viewportWidth = document.documentElement.clientWidth;
44-
int viewportHeight = document.documentElement.clientHeight;
43+
var viewportWidth = document.documentElement.clientWidth;
44+
var viewportHeight = document.documentElement.clientHeight;
4545

4646
parameters['vp'] = '${viewportWidth}x$viewportHeight';
4747

48-
String data = postEncode(parameters);
49-
HttpRequestor requestor =
50-
mockRequestor == null ? HttpRequest.request : mockRequestor;
48+
var data = postEncode(parameters);
49+
var requestor = mockRequestor ?? HttpRequest.request;
5150
return requestor(url, method: 'POST', sendData: data).catchError((e) {
5251
// Catch errors that can happen during a request, but that we can't do
5352
// anything about, e.g. a missing internet connection.
@@ -62,7 +61,7 @@ class HtmlPersistentProperties extends PersistentProperties {
6261
Map _map;
6362

6463
HtmlPersistentProperties(String name) : super(name) {
65-
String str = window.localStorage[name];
64+
var str = window.localStorage[name];
6665
if (str == null || str.isEmpty) str = '{}';
6766
_map = jsonDecode(str);
6867
}

lib/src/usage_impl_io.dart

+17-17
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class AnalyticsIO extends AnalyticsImpl {
3333
applicationName: applicationName,
3434
applicationVersion: applicationVersion,
3535
analyticsUrl: analyticsUrl) {
36-
final String locale = getPlatformLocale();
36+
final locale = getPlatformLocale();
3737
if (locale != null) {
3838
setSessionValue('ul', locale);
3939
}
4040
}
4141
}
4242

4343
String _createUserAgent() {
44-
final String locale = getPlatformLocale() ?? '';
44+
final locale = getPlatformLocale() ?? '';
4545

4646
if (Platform.isAndroid) {
4747
return 'Mozilla/5.0 (Android; Mobile; ${locale})';
@@ -55,20 +55,20 @@ String _createUserAgent() {
5555
return 'Mozilla/5.0 (Linux; Linux; Linux; ${locale})';
5656
} else {
5757
// Dart/1.8.0 (macos; macos; macos; en_US)
58-
String os = Platform.operatingSystem;
58+
var os = Platform.operatingSystem;
5959
return 'Dart/${getDartVersion()} (${os}; ${os}; ${os}; ${locale})';
6060
}
6161
}
6262

6363
String userHomeDir() {
64-
String envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
65-
String value = Platform.environment[envKey];
66-
return value == null ? '.' : value;
64+
var envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
65+
var value = Platform.environment[envKey];
66+
return value ?? '.';
6767
}
6868

6969
String getDartVersion() {
70-
String ver = Platform.version;
71-
int index = ver.indexOf(' ');
70+
var ver = Platform.version;
71+
var index = ver.indexOf(' ');
7272
if (index != -1) ver = ver.substring(0, index);
7373
return ver;
7474
}
@@ -83,18 +83,18 @@ class IOPostHandler extends PostHandler {
8383

8484
@override
8585
Future sendPost(String url, Map<String, dynamic> parameters) async {
86-
String data = postEncode(parameters);
86+
var data = postEncode(parameters);
8787

8888
if (_client == null) {
89-
_client = mockClient != null ? mockClient : HttpClient();
89+
_client = mockClient ?? HttpClient();
9090
_client.userAgent = _userAgent;
9191
}
9292

9393
try {
94-
HttpClientRequest req = await _client.postUrl(Uri.parse(url));
94+
var req = await _client.postUrl(Uri.parse(url));
9595
req.write(data);
96-
HttpClientResponse response = await req.close();
97-
response.drain();
96+
var response = await req.close();
97+
await response.drain();
9898
} catch (exception) {
9999
// Catch errors that can happen during a request, but that we can't do
100100
// anything about, e.g. a missing internet connection.
@@ -112,7 +112,7 @@ class IOPersistentProperties extends PersistentProperties {
112112
Map _map;
113113

114114
IOPersistentProperties(String name, {String documentDirPath}) : super(name) {
115-
String fileName = '.${name.replaceAll(' ', '_')}';
115+
var fileName = '.${name.replaceAll(' ', '_')}';
116116
documentDirPath ??= userHomeDir();
117117
_file = File(path.join(documentDirPath, fileName));
118118
if (!_file.existsSync()) {
@@ -151,7 +151,7 @@ class IOPersistentProperties extends PersistentProperties {
151151
@override
152152
void syncSettings() {
153153
try {
154-
String contents = _file.readAsStringSync();
154+
var contents = _file.readAsStringSync();
155155
if (contents.isEmpty) contents = '{}';
156156
_map = jsonDecode(contents);
157157
} catch (_) {
@@ -163,12 +163,12 @@ class IOPersistentProperties extends PersistentProperties {
163163
/// Return the string for the platform's locale; return's `null` if the locale
164164
/// can't be determined.
165165
String getPlatformLocale() {
166-
String locale = Platform.localeName;
166+
var locale = Platform.localeName;
167167
if (locale == null) return null;
168168

169169
if (locale != null) {
170170
// Convert `en_US.UTF-8` to `en_US`.
171-
int index = locale.indexOf('.');
171+
var index = locale.indexOf('.');
172172
if (index != -1) locale = locale.substring(0, index);
173173

174174
// Convert `en_US` to `en-us`.

lib/usage.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ class AnalyticsMock implements Analytics {
294294
/// an exception. This will try and make those first 100 chars contain
295295
/// information useful to debugging the issue.
296296
String sanitizeStacktrace(dynamic st, {bool shorten = true}) {
297-
String str = '${st}';
297+
var str = '${st}';
298298

299299
Iterable<Match> iter = _pathRegex.allMatches(str);
300300
iter = iter.toList().reversed;
301301

302-
for (Match match in iter) {
303-
String replacement = match.group(1);
302+
for (var match in iter) {
303+
var replacement = match.group(1);
304304
str =
305305
str.substring(0, match.start) + replacement + str.substring(match.end);
306306
}

lib/uuid/uuid.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Uuid {
2323
/// random numbers as the source of the generated uuid.
2424
String generateV4() {
2525
// Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12.
26-
int special = 8 + _random.nextInt(4);
26+
var special = 8 + _random.nextInt(4);
2727

2828
return '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-'
2929
'${_bitsDigits(16, 4)}-'

pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ homepage: https://github.com/dart-lang/usage
99
author: Dart Team <[email protected]>
1010

1111
environment:
12-
sdk: '>=2.0.0-dev.30 <3.0.0'
12+
sdk: '>=2.2.0 <3.0.0'
1313

1414
dependencies:
1515
path: ^1.4.0
1616

1717
dev_dependencies:
1818
grinder: ^0.8.0
19+
pedantic: ^1.9.0
1920
test: ^1.0.0

0 commit comments

Comments
 (0)