Skip to content

Commit

Permalink
Require Dart 3 and latest analyzer (#1319)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed May 16, 2023
1 parent d8d41af commit 8c39dd2
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 152 deletions.
278 changes: 187 additions & 91 deletions .github/workflows/dart.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _test_yaml/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: _test_yaml
publish_to: none

environment:
sdk: '>=2.19.0 <3.0.0'
sdk: ^3.0.0

dev_dependencies:
_json_serial_shared_test:
Expand Down
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linter:
- cancel_subscriptions
- cascade_invocations
- comment_references
- invalid_case_patterns
- join_return_with_assignment
- literal_only_boolean_expressions
- missing_whitespace_between_adjacent_strings
Expand All @@ -27,6 +28,8 @@ linter:
- prefer_relative_imports
- sort_child_properties_last
- test_types_in_equals
- type_literal_in_constant_pattern
- unnecessary_breaks
- unsafe_html
- use_full_hex_values_for_flutter_colors
- use_string_buffers
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: example
publish_to: none

environment:
sdk: '>=2.19.0 <3.0.0'
sdk: ^3.0.0

dependencies:
json_annotation: ^4.8.0
Expand Down
5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.7.0-dev

- Require Dart 3.0
- Require `analyzer: ^5.12.0`

## 6.6.2

- Better handling of `Object?` or `dynamic` as `fromJson` constructor param.
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/decode_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CreateFactoryResult {
CreateFactoryResult(this.output, this.usedFields);
}

abstract class DecodeHelper implements HelperCore {
mixin DecodeHelper implements HelperCore {
CreateFactoryResult createFactory(
Map<String, FieldElement> accessibleFields,
Map<String, String> unavailableReasons,
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/encoder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'type_helpers/generic_factory_helper.dart';
import 'type_helpers/json_converter_helper.dart';
import 'unsupported_type_error.dart';

abstract class EncodeHelper implements HelperCore {
mixin EncodeHelper implements HelperCore {
String _fieldAccess(FieldElement field) => '$_toJsonParamName.${field.name}';

String createPerFieldToJson(Set<FieldElement> accessibleFieldSet) {
Expand Down
4 changes: 2 additions & 2 deletions json_serializable/lib/src/type_helpers/json_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ TypeParameterType _decodeHelper(
final funcParamType = type.normalParameterTypes.single;

if ((funcParamType.isDartCoreObject && funcParamType.isNullableType) ||
funcParamType.isDynamic) {
funcParamType is DynamicType) {
return funcReturnType as TypeParameterType;
}
}
Expand All @@ -204,7 +204,7 @@ TypeParameterType _encodeHelper(
final type = param.type;

if (type is FunctionType &&
(type.returnType.isDartCoreObject || type.returnType.isDynamic) &&
(type.returnType.isDartCoreObject || type.returnType is DynamicType) &&
type.normalParameterTypes.length == 1) {
final funcParamType = type.normalParameterTypes.single;

Expand Down
6 changes: 3 additions & 3 deletions json_serializable/lib/src/type_helpers/map_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {

_checkSafeKeyType(expression, keyArg);

final valueArgIsAny = valueArg.isDynamic ||
final valueArgIsAny = valueArg is DynamicType ||
(valueArg.isDartCoreObject && valueArg.isNullableType);
final isKeyStringable = _isKeyStringable(keyArg);

Expand Down Expand Up @@ -114,7 +114,7 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {
if (keyArg.isEnum) {
keyUsage = context.deserialize(keyArg, _keyParam).toString();
} else if (context.config.anyMap &&
!(keyArg.isDartCoreObject || keyArg.isDynamic)) {
!(keyArg.isDartCoreObject || keyArg is DynamicType)) {
keyUsage = '$_keyParam as String';
} else if (context.config.anyMap &&
keyArg.isDartCoreObject &&
Expand Down Expand Up @@ -157,7 +157,7 @@ bool _isKeyStringable(DartType keyType) =>
void _checkSafeKeyType(String expression, DartType keyArg) {
// We're not going to handle converting key types at the moment
// So the only safe types for key are dynamic/Object/String/enum
if (keyArg.isDynamic ||
if (keyArg is DynamicType ||
(!keyArg.isNullableType &&
(keyArg.isDartCoreObject ||
coreStringTypeChecker.isExactlyType(keyArg) ||
Expand Down
4 changes: 2 additions & 2 deletions json_serializable/lib/src/type_helpers/value_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ValueHelper extends TypeHelper {
TypeHelperContext context,
) {
if (targetType.isDartCoreObject ||
targetType.isDynamic ||
targetType is DynamicType ||
simpleJsonTypeChecker.isAssignableFromType(targetType)) {
return expression;
}
Expand All @@ -39,7 +39,7 @@ class ValueHelper extends TypeHelper {
if (targetType.isDartCoreObject && !targetType.isNullableType) {
final question = defaultProvided ? '?' : '';
return '$expression as Object$question';
} else if (targetType.isDartCoreObject || targetType.isDynamic) {
} else if (targetType.isDartCoreObject || targetType is DynamicType) {
// just return it as-is. We'll hope it's safe.
return expression;
} else if (targetType.isDartCoreDouble) {
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ String typeToCode(
DartType type, {
bool forceNullable = false,
}) {
if (type.isDynamic) {
if (type is DynamicType) {
return 'dynamic';
} else if (type is InterfaceType) {
return [
Expand Down
8 changes: 4 additions & 4 deletions json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name: json_serializable
version: 6.6.2
version: 6.7.0-dev
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
repository: https://github.com/google/json_serializable.dart/tree/master/json_serializable
environment:
sdk: '>=2.19.0 <3.0.0'
sdk: ^3.0.0
topics:
- json
- build-runner
- json-serializable
- codegen

dependencies:
analyzer: ^5.2.0
analyzer: ^5.12.0
async: ^2.8.0
build: ^2.0.0
build_config: '>=0.4.4 <2.0.0'
Expand All @@ -26,7 +26,7 @@ dependencies:
path: ^1.8.0
pub_semver: ^2.0.0
pubspec_parse: ^1.0.0
source_gen: ^1.0.0
source_gen: ^1.3.2
source_helper: ^1.3.0

dev_dependencies:
Expand Down
30 changes: 12 additions & 18 deletions json_serializable/test/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,18 @@ void main() {
config[entry.key] = entry.value;

String lastLine;
switch (entry.key) {
case 'field_rename':
lastLine =
'`42` is not one of the supported values: none, kebab, snake, '
'pascal, screamingSnake';
break;
case 'constructor':
lastLine = "type 'int' is not a subtype of type 'String?' in type "
'cast';
break;
case 'create_to_json':
lastLine = "type 'int' is not a subtype of type 'bool?' in type "
'cast';
break;
default:
lastLine =
"type 'int' is not a subtype of type 'bool?' in type cast";
}
lastLine = switch (entry.key) {
'field_rename' =>
'`42` is not one of the supported values: none, kebab, snake, '
'pascal, screamingSnake',
'constructor' =>
"type 'int' is not a subtype of type 'String?' in type "
'cast',
'create_to_json' =>
"type 'int' is not a subtype of type 'bool?' in type "
'cast',
_ => "type 'int' is not a subtype of type 'bool?' in type cast"
};

final matcher = isA<StateError>().having(
(v) => v.message,
Expand Down
25 changes: 8 additions & 17 deletions json_serializable/test/kitchen_sink/kitchen_sink_yaml_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,14 @@ Matcher _getMatcher(bool checked, String? expectedKey, bool checkedAssignment) {
);

if (checkedAssignment) {
switch (expectedKey) {
case 'validatedPropertyNo42':
innerMatcher = isStateError;
break;
case 'no-42':
innerMatcher = isArgumentError;
break;
case 'strictKeysObject':
innerMatcher = _isAUnrecognizedKeysException('bob');
break;
case 'intIterable':
case 'datetime-iterable':
innerMatcher = isTypeError;
break;
default:
throw StateError('Not expected! - $expectedKey');
}
innerMatcher = switch (expectedKey) {
'validatedPropertyNo42' => isStateError,
'no-42' => isArgumentError,
'strictKeysObject' => _isAUnrecognizedKeysException('bob'),
'intIterable' => isTypeError,
'datetime-iterable' => isTypeError,
_ => throw StateError('Not expected! - $expectedKey')
};
}
}

Expand Down
15 changes: 5 additions & 10 deletions json_serializable/tool/readme_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,11 @@ class _ReadmeBuilder extends Builder {
final memberName = match.group(3);
final linkContent = '[`$className${memberName ?? ''}`]';
String linkValue;
switch (context) {
case 'core':
linkValue = _coreTypeUri(className);
break;
case 'ja':
linkValue = jsonAnnotationUri(className, memberName?.substring(1));
break;
default:
linkValue = 'https://unknown.com/$context/$className';
}
linkValue = switch (context) {
'core' => _coreTypeUri(className),
'ja' => jsonAnnotationUri(className, memberName?.substring(1)),
_ => 'https://unknown.com/$context/$className'
};
foundClasses[linkContent] = linkValue;
return linkContent;
}
Expand Down

0 comments on commit 8c39dd2

Please sign in to comment.