Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed May 31, 2024
1 parent f1992e3 commit 2b717c8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 5 additions & 5 deletions build_cli/lib/src/arg_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ArgType _getArgType(FieldElement element, CliOption option) {

throwUnsupported(
element,
'`${targetType.getDisplayString()}`'
'`${targetType.toStringNonNullable()}`'
' is not a supported type.',
);
}
Expand Down Expand Up @@ -172,8 +172,8 @@ CliOption? _getOptions(FieldElement element) {
// field's type is `BuildTool?` it's accessors will be the
// non-nullable type, `BuildTool`.
// TODO: find a better way to compare the underlying type
p.returnType.getDisplayString() ==
element.type.getDisplayString(),
p.returnType.toStringNonNullable() ==
element.type.toStringNonNullable(),
)
.map((p) => p.name)
.toList();
Expand Down Expand Up @@ -308,12 +308,12 @@ CliOption? _getOptions(FieldElement element) {
}

if (!functionElement.returnType.isAssignableTo(element.type)) {
final typeString = functionElement.returnType.getDisplayString();
final typeString = functionElement.returnType.toStringNonNullable();
throwUnsupported(
element,
'The convert function `${functionElement.name}` return type '
'`$typeString` is not compatible with the field '
'type `${element.type.getDisplayString()}`.',
'type `${element.type.toStringNonNullable()}`.',
);
}
_convertName[option] = ConverterData(
Expand Down
4 changes: 2 additions & 2 deletions build_cli/lib/src/build_cli_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ ${element.name} $resultParserName(ArgResults result) =>''',
assert(info.optionData!.provideDefaultToOverride);
String typeInfo;
if (converterDataFromOptions(info.optionData!) == null) {
typeInfo = info.dartType.getDisplayString();
typeInfo = info.dartType.toStringNonNullable();
} else {
fyis.add(
'The value for [${_overrideParamName(fieldName)}] must be a '
'[String] that is convertible to '
'[${info.dartType.getDisplayString()}].',
'[${info.dartType.toStringNonNullable()}].',
);
typeInfo = 'String';
}
Expand Down
17 changes: 17 additions & 0 deletions build_cli/lib/src/to_share.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: implementation_imports

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
show InheritanceManager3;
import 'package:source_gen/source_gen.dart';
Expand Down Expand Up @@ -201,3 +202,19 @@ Set<String> writeConstructorInvocation(

return usedCtorParamsAndFields;
}

extension DartTypeExtension on DartType {
String toStringNonNullable() {
final val = getDisplayString();
if (val.endsWith('?')) return val.substring(0, val.length - 1);
return val;
}
}

extension ElementExtension on Element {
String toStringNonNullable() {
final val = getDisplayString();
if (val.endsWith('?')) return val.substring(0, val.length - 1);
return val;
}
}

0 comments on commit 2b717c8

Please sign in to comment.