Skip to content

Commit

Permalink
Generate documentation for copyWith
Browse files Browse the repository at this point in the history
  • Loading branch information
rekire committed Apr 12, 2024
1 parent 4ce28bd commit 7be0f72
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/freezed/lib/src/templates/abstract_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ ${copyWith?.commonConcreteImpl ?? ''}
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _toJson {
if (!data.generateToJson) return '';
return 'Map<String, dynamic> toJson($_toJsonParams)'
return '/// Serializes this ${data.name} to a JSON map.\n'
'Map<String, dynamic> toJson($_toJsonParams)'
' => throw $privConstUsedErrorVarName;';
}

Expand Down
2 changes: 1 addition & 1 deletion packages/freezed/lib/src/templates/concrete_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ ${whenOrNullPrototype(data.constructors)} {
return constructor.impliedProperties.expand((p) {
return [
if (commonProperties.any((element) => element.name == p.name))
'@override ${p.abstractGetter}'
p.abstractGetter.toString(shouldOverride: true)
else
'${p.abstractGetter}',
if (!p.isFinal) p.abstractSetter,
Expand Down
12 changes: 7 additions & 5 deletions packages/freezed/lib/src/templates/copy_with.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ ${_abstractDeepCopyMethods().join()}
String get abstractCopyWithGetter {
if (cloneableProperties.isEmpty) return '';

return _maybeOverride(
'''
@JsonKey(ignore: true)
return '''
/// Create a copy of ${data.name}
/// with the given fields replaced by the non-null parameter values.
${_hasSuperClass ? '@override\n' : ''}@JsonKey(ignore: true)
$_abstractClassName${genericsParameter.append('$clonedClassName$genericsParameter')} get copyWith => throw $privConstUsedErrorVarName;
''',
);
''';
}

String get concreteCopyWithGetter {
if (cloneableProperties.isEmpty) return '';
return '''
/// Create a copy of ${data.name}
/// with the given fields replaced by the non-null parameter values.
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
Expand Down
5 changes: 3 additions & 2 deletions packages/freezed/lib/src/templates/properties.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ class Getter {
final String body;

@override
String toString() {
return '$doc${decorators.join()} $type get $name$body';
String toString({bool shouldOverride = false}) {
final finalDecorators = [if(shouldOverride)'@override', ...decorators];
return '$doc${finalDecorators.join()} $type get $name$body';
}
}

Expand Down

0 comments on commit 7be0f72

Please sign in to comment.