Skip to content

Commit

Permalink
Merge pull request #83 from netglade/update-analysis
Browse files Browse the repository at this point in the history
Update analysis
  • Loading branch information
tenhobi authored Jun 27, 2023
2 parents ea852ef + 22971ce commit aadd65d
Show file tree
Hide file tree
Showing 102 changed files with 2,908 additions and 4,291 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ jobs:
- name: Analyze
run: melos run lint:all

- name: Install DCM
run: |
sudo apt-get update
wget -qO- https://dcm.dev/pgp-key.public | sudo gpg --dearmor -o /usr/share/keyrings/dcm.gpg
echo 'deb [signed-by=/usr/share/keyrings/dcm.gpg arch=amd64] https://dcm.dev/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list
sudo apt-get update
sudo apt-get install dcm
- name: DCM Analyze
run: dcm analyze --reporter=github --ci-key ${{ secrets.DCM_KEY }} --email ${{ secrets.DCM_EMAIL }} --fatal-style --fatal-performance --fatal-warnings .

- name: Test
run: melos run test
2 changes: 1 addition & 1 deletion examples/drift/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
drift_dev: ^2.7.0
netglade_analysis: ^1.2.3
netglade_analysis: ^2.0.0
test: ^1.16.0
2 changes: 1 addition & 1 deletion examples/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ dependencies:
dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
netglade_analysis: ^1.2.3
netglade_analysis: ^2.0.0
test: ^1.16.0
2 changes: 1 addition & 1 deletion examples/freezed/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
freezed: ^2.3.2
netglade_analysis: ^1.2.3
netglade_analysis: ^2.0.0
test: ^1.16.0
2 changes: 1 addition & 1 deletion examples/injectable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
injectable_generator: ^2.1.5
netglade_analysis: 1.2.0
netglade_analysis: ^2.0.0
test: ^1.16.0
2 changes: 1 addition & 1 deletion examples/json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
json_serializable: ^6.6.1
netglade_analysis: ^1.2.3
netglade_analysis: ^2.0.0
test: ^1.16.0
1 change: 1 addition & 0 deletions packages/auto_mappr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
- Add support for library aliases, so mapping now supports types from different libraries with the same name.
It also work in cases when library alias's exports. [#62](https://github.com/netglade/auto_mappr/pull/62)
- Adhere to netglade_analysis 2.0.0.

## 1.4.0
- Add modules. [#67](https://github.com/netglade/auto_mappr/pull/67)
Expand Down
4 changes: 4 additions & 0 deletions packages/auto_mappr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ Each AutoMappr class can include a list of modules
that can be used to nest modules
and use all of its underlying mappings.

Note that modules work as disjunctive units
and their mappings cannot be internally used by by mappr that imported it.
They only work for grouping at the moment.

Applications are often split into independent parts (we will call them **features**).
Each feature should probably have its own independent mappr,
that is used as a module.
Expand Down
13 changes: 7 additions & 6 deletions packages/auto_mappr/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ include: package:netglade_analysis/lints.yaml
# TODO include: package:netglade_analysis/lints_package.yaml

analyzer:
plugins:
- dart_code_metrics

exclude:
- test/integration/error_fixture/**

dart_code_metrics:
extends:
- package:netglade_analysis/dcm.yaml
pubspec-rules:
prefer-publish-to-none: false
rules:
- format-comment: false
- avoid-non-null-assertion: false
- avoid-top-level-members-in-tests: false
avoid-non-null-assertion: false
avoid-top-level-members-in-tests:
exclude:
- test/integration/fixture/**
avoid-long-parameter-list: false
2 changes: 1 addition & 1 deletion packages/auto_mappr/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ targets:
generate_for:
exclude:
- test/integration/error_fixture/**

builders:
auto_mappr:
import: "package:auto_mappr/auto_mappr.dart"
Expand Down
2 changes: 1 addition & 1 deletion packages/auto_mappr/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ dependencies:
dev_dependencies:
auto_mappr: ^1.3.0
build_runner: ^2.0.0
netglade_analysis: ^1.2.3
netglade_analysis: ^2.0.0
test: ^1.16.0
6 changes: 3 additions & 3 deletions packages/auto_mappr/lib/auto_mappr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'package:auto_mappr/src/generator/auto_mappr_generator.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

/// Main Builder for the `AutoMappr` Annotation
/// Main Builder for the `AutoMappr` Annotation.
Builder autoMapprBuilder(BuilderOptions _) => SharedPartBuilder(
[AutoMapprGenerator()],
[const AutoMapprGenerator()],
'auto_mappr',
);

Builder autoMapprBuilderNotShared(BuilderOptions _) => PartBuilder(
[AutoMapprGenerator()],
[const AutoMapprGenerator()],
'.auto_mappr.dart',
);
14 changes: 6 additions & 8 deletions packages/auto_mappr/lib/src/builder/auto_mappr_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class AutoMapprBuilder {
'unnecessary_raw_strings',
'unnecessary_lambdas',

// Can we fix this somehow? (const defaults, const customs)
// Can we fix this somehow? (const defaults, const customs).
'prefer_const_constructors',
'prefer_const_literals_to_create_immutables'
];

AutoMapprBuilder({
const AutoMapprBuilder({
required this.config,
required this.mapperClassElement,
});
Expand Down Expand Up @@ -54,9 +54,7 @@ class AutoMapprBuilder {
List<Constructor> _buildConstructors() {
return [
// Constant constructor to allow usage of modules.
Constructor(
(builder) => builder..constant = true,
),
Constructor((builder) => builder..constant = true),
];
}

Expand Down Expand Up @@ -113,11 +111,11 @@ class AutoMapprBuilder {
iterableTransformer: 'toSet',
).buildMethod(),

// Internal convert method
// Internal convert method.
PrivateConvertMethodBuilder(config).buildMethod(),

// Generate non-nullable mapping method.
// TODO(later): switch to MappingMethodBuilder
// TODO(later): switch to MappingMethodBuilder.
for (final mapping in config.mappers)
Method(
(b) => b
Expand All @@ -140,7 +138,7 @@ class AutoMapprBuilder {
),

// Generates nullable mapping method only when nullable method is used.
// TODO(later): switch to MappingMethodBuilder
// TODO(later): switch to MappingMethodBuilder.
for (final mapping in config.mappers.where(nullableMappings.contains))
Method(
(b) => b
Expand Down
16 changes: 6 additions & 10 deletions packages/auto_mappr/lib/src/builder/enum_assignment_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class EnumAssignmentBuilder {
final AutoMapprConfig mapperConfig;
final TypeMapping mapping;

EnumAssignmentBuilder({
const EnumAssignmentBuilder({
required this.mapperConfig,
required this.mapping,
});
Expand All @@ -30,14 +30,8 @@ class EnumAssignmentBuilder {
final sourceEnum = mapping.source.element as EnumElement;
final targetEnum = mapping.target.element as EnumElement;

final sourceValues = sourceEnum.fields
.where((e) => e.isEnumConstant && e.isPublic)
.map((e) => e.name)
.toSet();
final targetValues = targetEnum.fields
.where((e) => e.isEnumConstant && e.isPublic)
.map((e) => e.name)
.toSet();
final sourceValues = sourceEnum.fields.where((e) => e.isEnumConstant && e.isPublic).map((e) => e.name).toSet();
final targetValues = targetEnum.fields.where((e) => e.isEnumConstant && e.isPublic).map((e) => e.name).toSet();

final sourceIsSubset = targetValues.containsAll(sourceValues);

Expand All @@ -51,7 +45,9 @@ class EnumAssignmentBuilder {
);
}

final targetReference = refer(mapping.target.getDisplayStringWithLibraryAlias(config: mapperConfig));
final targetReference = refer(
mapping.target.getDisplayStringWithLibraryAlias(config: mapperConfig),
);

return targetReference
.property('values')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MapModelBodyMethodBuilder {
final bool nullable;
final void Function(TypeMapping? mapping)? usedNullableMethodCallback;

MapModelBodyMethodBuilder({
const MapModelBodyMethodBuilder({
required this.mapperConfig,
required this.mapping,
this.usedNullableMethodCallback,
Expand Down Expand Up @@ -123,11 +123,20 @@ class MapModelBodyMethodBuilder {

final targetConstructor = _findBestConstructor(mapping.target, forcedConstructor: mapping.constructor);

if (targetConstructor == null) {
throw InvalidGenerationSourceError(
'There is no target constructor in ${mapping.target}',
);
}

final targetClassGetters = (mapping.target).getAllGetters();

// Map constructor parameters
for (var i = 0; i < targetConstructor.parameters.length; i++) {
final param = targetConstructor.parameters[i];
final param = targetConstructor.parameters.elementAtOrNull(i);

if (param == null) continue;

final paramPosition = param.isPositional ? i : null;
final constructorAssignment = ConstructorAssignment(param: param, position: paramPosition);

Expand All @@ -140,7 +149,9 @@ class MapModelBodyMethodBuilder {
// Custom mapping has precedence.
if (fieldMapping?.hasCustomMapping() ?? false) {
final targetField =
targetClassGetters.firstWhere((targetField) => targetField.displayName == fieldMapping?.field);
targetClassGetters.firstWhereOrNull((targetField) => targetField.displayName == fieldMapping?.field);

if (targetField == null) continue;

if (mapping.fieldShouldBeIgnored(targetField.displayName)) {
_assertParamFieldCanBeIgnored(param, targetField);
Expand All @@ -164,9 +175,11 @@ class MapModelBodyMethodBuilder {

final targetField = from != null
// support custom field rename mapping
? targetClassGetters.firstWhere((field) => field.displayName == fieldMapping?.field)
? targetClassGetters.firstWhereOrNull((field) => field.displayName == fieldMapping?.field)
// find target field based on matching source field
: targetClassGetters.firstWhere((field) => field.displayName == sourceField.displayName);
: targetClassGetters.firstWhereOrNull((field) => field.displayName == sourceField.displayName);

if (targetField == null) continue;

if (mapping.fieldShouldBeIgnored(targetField.displayName)) {
_assertParamFieldCanBeIgnored(param, sourceField);
Expand Down Expand Up @@ -289,7 +302,9 @@ class MapModelBodyMethodBuilder {

var cascadedAssignments = constructorExpression;
for (final sourceField in fields) {
final targetField = targetClassGetters.firstWhere((field) => field.displayName == sourceField.displayName);
final targetField = targetClassGetters.firstWhereOrNull((field) => field.displayName == sourceField.displayName);

if (targetField == null) continue;

// Assign result.X = model.X
cascadedAssignments = cascadedAssignments.cascade(sourceField.displayName).assign(
Expand All @@ -316,16 +331,14 @@ class MapModelBodyMethodBuilder {
}) {
final fieldsWithGetter = classType.getAllGetters();

return {
for (final field in fieldsWithGetter) field.name: field,
};
return {for (final field in fieldsWithGetter) field.name: field};
}

/// Tries to find best constructor for mapping.
///
/// Returns a constructor with the most parameter count.
/// Prefer non factory constructors over factory ones.
ConstructorElement _findBestConstructor(InterfaceType classType, {String? forcedConstructor}) {
ConstructorElement? _findBestConstructor(InterfaceType classType, {String? forcedConstructor}) {
if (forcedConstructor != null) {
final selectedConstructor = classType.constructors.firstWhereOrNull((c) => c.name == forcedConstructor);
if (selectedConstructor != null) return selectedConstructor;
Expand All @@ -346,7 +359,7 @@ class MapModelBodyMethodBuilder {
allConstructors.where((c) => c.isFactory && c.name != 'fromJson').sorted(sortConstructors);

// Prefers non factory constructors over factory ones.
return [...nonFactoryConstructors, ...factoryConstructors].first;
return [...nonFactoryConstructors, ...factoryConstructors].firstOrNull;
}

Code _whenModelIsNullHandling() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:code_builder/code_builder.dart';

// ignore: one_member_abstracts, it is implemented in builders
abstract class CallableMethod {
Expression methodCall({
Map<String, Expression> namedArguments = const {},
});
const CallableMethod();

Expression methodCall({Map<String, Expression> namedArguments = const {}});


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:code_builder/code_builder.dart';

// ignore: one_member_abstracts, it is implemented in builders
abstract class CallableProperty {
const CallableProperty();

Expression propertyCall({
required Reference on,
Map<String, Expression> namedArguments = const {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ class CanConvertMethodBuilder extends MethodBuilderBase implements CallableMetho
}

@override
Expression methodCall({
Map<String, Expression> namedArguments = const {},
}) {
Expression methodCall({Map<String, Expression> namedArguments = const {}}) {
return refer('canConvert').call(
const [],
namedArguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class TryConvertMethodBuilder extends MethodBuilderBase {
).code,
);

block.addExpression(
literalNull.returned,
);
block.addExpression(literalNull.returned);

return block.build();
}
Expand Down
Loading

0 comments on commit aadd65d

Please sign in to comment.