Skip to content

Commit

Permalink
[unified_analytics] Add lints to consistently use final (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Jun 24, 2024
1 parent e660a68 commit 7a231e5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions pkgs/unified_analytics/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
linter:
rules:
- avoid_catches_without_on_clauses
- prefer_final_in_for_each
- prefer_final_locals
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final Analytics analytics = Analytics.development(

// Timing a process and sending the event
void main() async {
var start = DateTime.now();
final start = DateTime.now();

// Each client using this package will have it's own
// method to show the message but the below is a trivial
Expand Down
6 changes: 3 additions & 3 deletions pkgs/unified_analytics/lib/src/asserts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void checkBody(Map<String, Object?> body) {
}

// Checks for each event object
for (var eventMap in events.cast<Map<String, Object?>>()) {
for (final eventMap in events.cast<Map<String, Object?>>()) {
final eventName = eventMap['name'] as String;

// GA4 Limitation:
Expand Down Expand Up @@ -73,7 +73,7 @@ void checkBody(Map<String, Object?> body) {
}

// Loop through each of the event parameters
for (var entry in eventParams.entries) {
for (final entry in eventParams.entries) {
final key = entry.key;
final value = entry.value;

Expand Down Expand Up @@ -138,7 +138,7 @@ void checkBody(Map<String, Object?> body) {
}

// Checks for each user property item
for (var entry in userProperties.entries) {
for (final entry in userProperties.entries) {
final key = entry.key;
final value = entry.value as Map<String, Object?>;

Expand Down
6 changes: 3 additions & 3 deletions pkgs/unified_analytics/lib/src/log_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class LogFileStats {

@override
String toString() {
final encoder = const JsonEncoder.withIndent(' ');
const encoder = JsonEncoder.withIndent(' ');
return encoder.convert({
'startDateTime': startDateTime.toString(),
'minsFromStartDateTime': minsFromStartDateTime,
Expand Down Expand Up @@ -219,7 +219,7 @@ class LogHandler {
final eventCount = <String, int>{};
final flutterChannelCount = <String, int>{};
final toolCount = <String, int>{};
for (var record in records) {
for (final record in records) {
counter['sessions']!.add(record.sessionId);
counter['tool']!.add(record.tool);
if (record.flutterChannel != null) {
Expand Down Expand Up @@ -432,7 +432,7 @@ class LogItem {
hostOsVersion,
locale,
];
for (var value in values) {
for (final value in values) {
if (value == null) return null;
}

Expand Down
4 changes: 2 additions & 2 deletions pkgs/unified_analytics/lib/src/survey_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Survey {

@override
String toString() {
final encoder = const JsonEncoder.withIndent(' ');
const encoder = JsonEncoder.withIndent(' ');
return encoder.convert({
'uniqueId': uniqueId,
'startDate': startDate.toString(),
Expand Down Expand Up @@ -229,7 +229,7 @@ class SurveyHandler {

// Initialize the list of persisted surveys and add to them
// as they are being parsed
var persistedSurveys = <String, PersistedSurvey>{};
final persistedSurveys = <String, PersistedSurvey>{};
contents.forEach((key, value) {
value as Map<String, dynamic>;

Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/user_property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class UserProperty {
/// https://developers.google.com/analytics/devguides/collection/protocol/ga4/user-properties?client_type=gtag
Map<String, Map<String, Object?>> preparePayload() {
return <String, Map<String, Object?>>{
for (MapEntry<String, Object?> entry in _toMap().entries)
for (final entry in _toMap().entries)
entry.key: <String, Object?>{'value': entry.value}
};
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Map<String, Object?> generateRequestBody({
/// contain all of the analytics files.
Directory? getHomeDirectory(FileSystem fs) {
String? home;
var envVars = io.Platform.environment;
final envVars = io.Platform.environment;

if (io.Platform.isMacOS) {
home = envVars['HOME'];
Expand Down Expand Up @@ -320,7 +320,7 @@ class Uuid {
/// random numbers as the source of the generated uuid.
String generateV4() {
// Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12.
var special = 8 + _random.nextInt(4);
final special = 8 + _random.nextInt(4);

return '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-'
'${_bitsDigits(16, 4)}-'
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/test/event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ void main() {

test('Confirm all constructors were checked', () {
var constructorCount = 0;
for (var declaration in reflectClass(Event).declarations.keys) {
for (final declaration in reflectClass(Event).declarations.keys) {
// Count public constructors but omit private constructors
if (declaration.toString().contains('Event.') &&
!declaration.toString().contains('Event._')) constructorCount++;
Expand Down
14 changes: 7 additions & 7 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
];
expect(analytics.userPropertyMap.keys.length, userPropertyKeys.length,
reason: 'There should only be ${userPropertyKeys.length} keys');
for (var key in userPropertyKeys) {
for (final key in userPropertyKeys) {
expect(analytics.userPropertyMap.keys.contains(key), true,
reason: 'The $key variable is required');
}
Expand Down Expand Up @@ -944,7 +944,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
secondAnalytics!.send(testEvent);

// Query the log file stats to verify that there are two tools
var query = analytics.logFileStats()!;
final query = analytics.logFileStats()!;

expect(query.toolCount, {'flutter-tool': 1, 'dart-tool': 1},
reason: 'There should have been two tools in the persisted logs');
Expand Down Expand Up @@ -1048,7 +1048,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
secondAnalytics!.send(testEvent);

// Query the log file stats to verify that there are two tools
var query = analytics.logFileStats()!;
final query = analytics.logFileStats()!;

expect(query.toolCount, {'dart-tool': 1},
reason: 'There should have only been on tool that sent events');
Expand All @@ -1059,7 +1059,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
// Sending a query with the first analytics instance which has flutter information
// available should reflect in the query that there is 1 flutter channel present
analytics.send(testEvent);
LogFileStats? query2 = analytics.logFileStats()!;
final query2 = analytics.logFileStats()!;

expect(query2.toolCount, {'dart-tool': 1, 'flutter-tool': 1},
reason: 'Two different analytics instances have '
Expand Down Expand Up @@ -1098,7 +1098,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion

var userPropLengthValid = true;
final invalidUserProps = <String>[];
for (var key in userPropPayload.keys) {
for (final key in userPropPayload.keys) {
if (key.length > maxUserPropLength) {
userPropLengthValid = false;
invalidUserProps.add(key);
Expand All @@ -1117,7 +1117,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
final toolLabelPattern = RegExp(r'^[a-zA-Z][a-zA-Z\_-]{0,35}$');
var toolLengthValid = true;
final invalidTools = <DashTool>[];
for (var tool in DashTool.values) {
for (final tool in DashTool.values) {
if (!toolLabelPattern.hasMatch(tool.label)) {
toolLengthValid = false;
invalidTools.add(tool);
Expand All @@ -1137,7 +1137,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
final eventLabelPattern = RegExp(r'^[a-zA-Z]{1}[a-zA-Z0-9\_]{0,39}$');
var eventValid = true;
final invalidEvents = <DashEvent>[];
for (var event in DashEvent.values) {
for (final event in DashEvent.values) {
if (!eventLabelPattern.hasMatch(event.label)) {
eventValid = false;
invalidEvents.add(event);
Expand Down

0 comments on commit 7a231e5

Please sign in to comment.