Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
tenhobi committed Sep 8, 2023
1 parent fac61f4 commit 8d9d8b5
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 177 deletions.
5 changes: 2 additions & 3 deletions packages/auto_mappr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[//]: # (## Unreleased)

## Unreleased
- **Breaking**: Allow "absorbing" modules using `includes` on `@AutoMappr`. Previous `modules` is now `delegates`.
- **Breaking**: Remove shared AutoMappr builder that used PartBuilder, now `.auto_mappr.dart` is generated using LibraryBuilder.
- Add a `reverse` option on `MapType`, which includes the reverse mapping. [#115](https://github.com/netglade/auto_mappr/pull/115)
- Add a support for Dart 3 and Records feature. [#116](https://github.com/netglade/auto_mappr/pull/116)

- Allow use modules using `includes` on `@AutoMappr`. Previous `modules` is now `delegates`.
- Remove shared AutoMappr builder that used PartBuilder, now `.auto_mappr.dart` is generated using LibraryBuilder.

## 1.7.0
- Adhere to netglade_analysis. [#94](https://github.com/netglade/auto_mappr/pull/94)
- Update analyzer and mocktail packages. [#111](https://github.com/netglade/auto_mappr/pull/111)
Expand Down
45 changes: 6 additions & 39 deletions packages/auto_mappr/lib/src/builder/auto_mappr_builder.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// ignore_for_file: public_member_api_docs

import 'package:analyzer/dart/element/element.dart';
import 'package:auto_mappr/src/builder/map_model_body_method_builder.dart';
import 'package:auto_mappr/src/builder/methods/methods.dart';
import 'package:auto_mappr/src/extensions/expression_extension.dart';
import 'package:auto_mappr/src/helpers/emitter_helper.dart';
import 'package:auto_mappr/src/helpers/urls.dart';
import 'package:auto_mappr/src/models/auto_mappr_config.dart';
import 'package:auto_mappr/src/models/type_mapping.dart';
Expand Down Expand Up @@ -109,47 +106,17 @@ class AutoMapprBuilder {
PrivateConvertMethodBuilder(config).buildMethod(),

// Generate non-nullable mapping method.
// TODO(later): switch to MappingMethodBuilder.
for (final mapping in config.mappers)
Method(
(b) => b
..name = mapping.mappingMethodName(config: config)
..requiredParameters.addAll([
Parameter(
(p) => p
..name = 'input'
..type = EmitterHelper.current.typeRefer(type: mapping.source).nullabled(),
),
])
..returns = EmitterHelper.current.typeRefer(type: mapping.target)
..body = MapModelBodyMethodBuilder(
mapping: mapping,
mapperConfig: config,
usedNullableMethodCallback: usedNullableMappingMethod,
).build(),
),
MappingMethodBuilder(
config,
mapping: mapping,
usedNullableMethodCallback: usedNullableMappingMethod,
).buildMethod(),

// Generates nullable mapping method only when nullable method is used.
// TODO(later): switch to MappingMethodBuilder.
// ignore: avoid-shadowing
for (final mapping in config.mappers.where(nullableMappings.contains))
Method(
(b) => b
..name = mapping.nullableMappingMethodName(config: config)
..requiredParameters.addAll([
Parameter(
(p) => p
..name = 'input'
..type = EmitterHelper.current.typeRefer(type: mapping.source).nullabled(),
),
])
..returns = EmitterHelper.current.typeRefer(type: mapping.target).nullabled()
..body = MapModelBodyMethodBuilder(
mapping: mapping,
mapperConfig: config,
nullable: true,
).build(),
),
MappingMethodBuilder(config, mapping: mapping, nullable: true).buildMethod(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
const ClassBodyBuilder({
required super.mapperConfig,
required super.mapping,
required super.nullable,
required super.usedNullableMethodCallback,
});

Expand Down Expand Up @@ -116,8 +115,6 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
targetField: targetField,
targetConstructorParam: constructorAssignment,
fieldMapping: mapping.tryGetFieldMapping(targetField.displayName),
typeMapping: mapping,
config: mapperConfig,
);

mappedTargetConstructorParams.add(sourceAssignment);
Expand All @@ -144,8 +141,6 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
targetField: targetField,
targetConstructorParam: constructorAssignment,
fieldMapping: mapping.tryGetFieldMapping(targetField.displayName),
typeMapping: mapping,
config: mapperConfig,
);

mappedTargetConstructorParams.add(sourceAssignment);
Expand All @@ -169,8 +164,6 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
targetField: targetField,
fieldMapping: fieldMapping,
targetConstructorParam: constructorAssignment,
typeMapping: mapping,
config: mapperConfig,
),
);
}
Expand Down Expand Up @@ -252,8 +245,6 @@ class ClassBodyBuilder extends MapBodyBuilderBase {
assignment: SourceAssignment(
sourceField: sourceField,
targetField: targetField,
typeMapping: mapping,
config: mapperConfig,
),
usedNullableMethodCallback: usedNullableMethodCallback,
).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class EnumBodyBuilder extends MapBodyBuilderBase {
const EnumBodyBuilder({
required super.mapperConfig,
required super.mapping,
required super.nullable,
required super.usedNullableMethodCallback,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ abstract class MapBodyBuilderBase {
final AutoMapprConfig mapperConfig;
final TypeMapping mapping;

final bool nullable;
final void Function(TypeMapping? mapping)? usedNullableMethodCallback;

const MapBodyBuilderBase({
required this.mapperConfig,
required this.mapping,
required this.nullable,
required this.usedNullableMethodCallback,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class MapModelBodyMethodBuilder {
final enumMapBodyBuilder = EnumBodyBuilder(
mapperConfig: mapperConfig,
mapping: mapping,
nullable: nullable,
usedNullableMethodCallback: usedNullableMethodCallback,
);
if (enumMapBodyBuilder.canProcess()) {
Expand All @@ -41,7 +40,6 @@ class MapModelBodyMethodBuilder {
final classMapBodyBuilder = ClassBodyBuilder(
mapperConfig: mapperConfig,
mapping: mapping,
nullable: nullable,
usedNullableMethodCallback: usedNullableMethodCallback,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:built_collection/built_collection.dart';
import 'package:code_builder/code_builder.dart';

class CanConvertMethodBuilder extends MethodBuilderBase implements CallableMethod, CallableProperty {
CanConvertMethodBuilder(super.config);
const CanConvertMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ConvertIterableMethodBuilder extends MethodBuilderBase {
final String wrapper;
final String? iterableTransformer;

ConvertIterableMethodBuilder(
const ConvertIterableMethodBuilder(
super.config, {
required this.wrapper,
this.iterableTransformer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:built_collection/built_collection.dart';
import 'package:code_builder/code_builder.dart';

class ConvertMethodBuilder extends MethodBuilderBase {
ConvertMethodBuilder(super.config);
const ConvertMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:auto_mappr/src/builder/map_model_body_method_builder.dart';
import 'package:auto_mappr/src/builder/methods/method_builder_base.dart';
import 'package:auto_mappr/src/extensions/expression_extension.dart';
import 'package:auto_mappr/src/helpers/emitter_helper.dart';
import 'package:auto_mappr/src/models/type_mapping.dart';
import 'package:code_builder/code_builder.dart';
Expand All @@ -9,7 +10,7 @@ class MappingMethodBuilder extends MethodBuilderBase {
final bool nullable;
final void Function(TypeMapping? mapping)? usedNullableMethodCallback;

MappingMethodBuilder(
const MappingMethodBuilder(
super.config, {
required this.mapping,
this.nullable = false,
Expand All @@ -18,49 +19,35 @@ class MappingMethodBuilder extends MethodBuilderBase {

@override
Method buildMethod() {
// TODO(later): rework mapping to this builder
var returnType = EmitterHelper.current.typeRefer(type: mapping.target);

if (nullable) {
returnType = returnType.nullabled();
}

return Method(
(b) => b
..name = mapping.mappingMethodName(config: config)
..name =
nullable ? mapping.nullableMappingMethodName(config: config) : mapping.mappingMethodName(config: config)
..requiredParameters.addAll([
Parameter(
(p) => p
..name = 'input'
..type = EmitterHelper.current.typeRefer(type: mapping.source),
..type = EmitterHelper.current.typeRefer(type: mapping.source).nullabled(),
),
])
..returns = EmitterHelper.current.typeRefer(type: mapping.target)
..body = MapModelBodyMethodBuilder(
mapping: mapping,
mapperConfig: config,
usedNullableMethodCallback: usedNullableMethodCallback,
).build(),
..returns = returnType
..body = buildBody(),
);

// nullable
// return Method(
// (b) => b
// ..name = mapping.nullableMappingMethodName
// ..requiredParameters.addAll([
// Parameter(
// (p) => p
// ..name = 'input'
// ..type = refer('${mapping.source.getDisplayString(withNullability: false)}?'),
// )
// ])
// ..returns = refer('${mapping.target.getDisplayString(withNullability: true)}?')
// ..body = MapModelBodyMethodBuilder(
// mapping: mapping,
// mapperConfig: config,
// nullable: true,
// ).build(),
// );
}

@override
Code buildBody() {
// TODO(later): rework
return const Code('T');
return MapModelBodyMethodBuilder(
mapping: mapping,
mapperConfig: config,
usedNullableMethodCallback: usedNullableMethodCallback,
nullable: nullable,
).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ abstract class MethodBuilderBase {
static final ListBuilder<Reference> overrideAnnotation = ListBuilder([const Reference('override')]);

final AutoMapprConfig config;
final Set<TypeMapping> nullableMappings;

MethodBuilderBase(this.config) : nullableMappings = {};
const MethodBuilderBase(this.config);

static String constructConvertMethodName({
required DartType source,
Expand All @@ -47,16 +46,6 @@ abstract class MethodBuilderBase {
config: config,
)}_Nullable';

bool shouldGenerateNullableMappingMethod(TypeMapping mapping) {
return nullableMappings.contains(mapping);
}

void usedNullableMappingMethod(TypeMapping? mapping) {
if (mapping == null) return;

final _ = nullableMappings.add(mapping);
}

Method buildMethod();

@protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:auto_mappr/src/helpers/emitter_helper.dart';
import 'package:code_builder/code_builder.dart';

class PrivateConvertMethodBuilder extends MethodBuilderBase {
PrivateConvertMethodBuilder(super.config);
const PrivateConvertMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:auto_mappr/src/helpers/urls.dart';
import 'package:code_builder/code_builder.dart';

class PrivateModulesMethodBuilder extends MethodBuilderBase {
PrivateModulesMethodBuilder(super.config);
const PrivateModulesMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TryConvertIterableMethodBuilder extends MethodBuilderBase {
final String wrapper;
final String? iterableTransformer;

TryConvertIterableMethodBuilder(
const TryConvertIterableMethodBuilder(
super.config, {
required this.wrapper,
this.iterableTransformer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:code_builder/code_builder.dart';
// modules OK
// modules tests OK
class TryConvertMethodBuilder extends MethodBuilderBase {
TryConvertMethodBuilder(super.config);
const TryConvertMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:auto_mappr/src/builder/methods/method_builder_base.dart';
import 'package:code_builder/code_builder.dart';

class TypeOfMethodBuilder extends MethodBuilderBase {
TypeOfMethodBuilder(super.config);
const TypeOfMethodBuilder(super.config);

@override
Method buildMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import 'package:analyzer/dart/constant/value.dart';
import 'package:auto_mappr/src/extensions/executable_element_extension.dart';
import 'package:auto_mappr/src/helpers/emitter_helper.dart';
import 'package:auto_mappr/src/models/auto_mappr_config.dart';
import 'package:code_builder/code_builder.dart';
import 'package:collection/collection.dart';
import 'package:source_gen/source_gen.dart';
Expand All @@ -12,10 +11,7 @@ extension DartObjectExtension on DartObject {
/// If the top most object is a function, then return its code expression.
///
/// Otherwise return code expression of literals or objects.
Expression? toCodeExpression({
required AutoMapprConfig config,
bool passModelArgument = false,
}) {
Expression? toCodeExpression({bool passModelArgument = false}) {
if (isNull) {
return null;
}
Expand All @@ -28,16 +24,14 @@ extension DartObjectExtension on DartObject {
]);
}

final output = _ToCodeExpressionConverter(config: config).convert(this).accept(EmitterHelper.current.emitter);
final output = const _ToCodeExpressionConverter().convert(this).accept(EmitterHelper.current.emitter);

return CodeExpression(Code('$output'));
}
}

class _ToCodeExpressionConverter {
final AutoMapprConfig config;

const _ToCodeExpressionConverter({required this.config});
const _ToCodeExpressionConverter();

Spec convert(DartObject dartObject) {
return _toSpec(dartObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ extension ExpressionExtension on Expression {
);
}

Expression maybeAsA(Expression expression, {required bool condition}) {
if (!condition) return this;

return asA(expression);
}

static Reference ifStatement({
required Spec condition,
required Spec ifBody,
Expand Down
Loading

0 comments on commit 8d9d8b5

Please sign in to comment.