Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use better implementation of hashCode when available #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions sum_data_types_generator/lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:source_gen/source_gen.dart';
import 'package:sum_data_types/sum_data_types.dart';

Expand Down Expand Up @@ -167,11 +168,13 @@ class CodgenConfig {
final bool genToString;
final bool genEqHashCode;
final bool nnbd;
final bool hashCodeImpl;

const CodgenConfig({
bool? toString,
bool? eqHashCode,
required this.nnbd,
required this.hashCodeImpl,
}) : genToString = toString ?? true,
genEqHashCode = eqHashCode ?? true;
}
Expand Down Expand Up @@ -217,6 +220,7 @@ class CommonClassModel<FieldModel> {
toString: genToString,
eqHashCode: genEqHashCode,
nnbd: lib.featureSet.isEnabled(Feature.non_nullable),
hashCodeImpl: lib.languageVersion.effective >= Version(2, 14,0),
);

final mixinName = clazz.name;
Expand Down Expand Up @@ -281,19 +285,26 @@ String eqImpl(String className, List<String> fieldNames) {
}''';
}

String hashCodeImpl(List<String> fieldNames) {
if (fieldNames.isEmpty) {
String hashCodeImpl(CodgenConfig config, List<String> fieldNames) {
if (config.hashCodeImpl) {
final updates = fieldNames.map((name) => 'this.$name,').join();
return '''@override
int get hashCode => Object.hashAll([$updates]);
''';
} else {
if (fieldNames.isEmpty) {
return '''@override
int get hashCode => 0;
''';
}
const result = r'__result$';
final updates =
fieldNames.map((name) => '$result = 37 * $result + this.$name.hashCode;').join('\n');
return '''@override
int get hashCode => 0;
''';
int get hashCode {
var $result = 17;
$updates
return $result;
}''';
}
const result = r'__result$';
final updates =
fieldNames.map((name) => '$result = 37 * $result + this.$name.hashCode;').join('\n');
return '''@override
int get hashCode {
var $result = 17;
$updates
return $result;
}''';
}
2 changes: 1 addition & 1 deletion sum_data_types_generator/lib/data_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class DataClassGenerator extends GeneratorForAnnotation<DataClass> {

${clazz.config.genEqHashCode ? eqImpl(clazz.className, clazz.fieldNames) : ''}

${clazz.config.genEqHashCode ? hashCodeImpl(clazz.fieldNames) : ''}
${clazz.config.genEqHashCode ? hashCodeImpl(clazz.config, clazz.fieldNames) : ''}

${clazz.config.genToString ? toStringMethod : ''}
}''';
Expand Down
2 changes: 1 addition & 1 deletion sum_data_types_generator/lib/sum_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class SumTypeGenerator extends GeneratorForAnnotation<SumType> {

${clazz.config.genEqHashCode ? eqImpl(clazz.className, clazz.internalFieldNames) : ''}

${clazz.config.genEqHashCode ? hashCodeImpl(clazz.internalFieldNames) : ''}
${clazz.config.genEqHashCode ? hashCodeImpl(clazz.config, clazz.internalFieldNames) : ''}

${clazz.config.genToString ? toStringMethod : ''}
}
Expand Down
9 changes: 5 additions & 4 deletions sum_data_types_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
analyzer: ^1.3.0
pedantic: ^1.8.0
analyzer: ^2.2.0
build: ^2.0.0
meta: ^1.1.7
sum_data_types: ^0.1.1
pedantic: ^1.8.0
pub_semver: ^2.0.0
source_gen: ^1.0.0
sum_data_types: ^0.1.1

dev_dependencies:
build_runner: ^1.12.2
build_runner: ^2.1.2