Skip to content

Commit bdedab5

Browse files
authored
Disable format (#1160)
1 parent 89e2658 commit bdedab5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1828
-1867
lines changed

packages/freezed/CHANGELOG.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
## Unreleased 3.0.0
22

3-
### Breaking changes:
4-
5-
- **Breaking**: Removed `map/when` and variants. These have been discouraged since Dart got pattern matching.
6-
- **Breaking**: Freezed classes should now either be `abstract`, `sealed`, or manually implements `_$MyClass`.
7-
83
### New: Mixed mode
94

105
Freezed 3.0 is about supporting a "mixed mode".
@@ -161,18 +156,21 @@ This feature offers fine-grained control over every parts of your models.
161156
Unfortunately, it is kind of required to "extend" the parent class (so here `extends Result<T>`). This is because Dart doesn't support `sealed mixin class`, so you can't do
162157
`with Result<T>` instead.
163158

164-
### New: private unions
165-
166-
It is now possible to have a private constructor for unions:
159+
### Other changes:
167160

168-
```dart
169-
@freezed
170-
sealed class Result<T> with _$Result {
171-
// It wasn't possible to write _data before, but now is.
172-
factory Result._data(T data) = ResultData;
173-
factory Result.error(Object error) = ResultError;
174-
}
175-
```
161+
- **Breaking**: Removed `map/when` and variants. These have been discouraged since Dart got pattern matching.
162+
- **Breaking**: Freezed classes should now either be `abstract`, `sealed`, or manually implements `_$MyClass`.
163+
- When formatting is disabled (default), Freezed now generates `// dart format off`. This
164+
prevents having to exclude generated file from formatting check in the CI.
165+
- It is now possible to have a private constructor for unions:
166+
```dart
167+
@freezed
168+
sealed class Result<T> with _$Result {
169+
// It wasn't possible to write _data before, but now is.
170+
factory Result._data(T data) = ResultData;
171+
factory Result.error(Object error) = ResultError;
172+
}
173+
```
176174

177175
## 2.5.8 - 2025-01-06
178176

packages/freezed/build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ targets:
1010
include:
1111
- test/*
1212
- test/**/*
13+
options:
14+
format: true
1315
source_gen|combining_builder:
1416
options:
1517
ignore_for_file:

packages/freezed/lib/builder.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:build/build.dart';
2-
import 'package:dart_style/dart_style.dart';
32
import 'package:freezed_annotation/freezed_annotation.dart';
43
import 'package:source_gen/source_gen.dart';
54

@@ -8,12 +7,12 @@ import 'src/freezed_generator.dart';
87
/// Builds generators for `build_runner` to run
98
Builder freezed(BuilderOptions options) {
109
return PartBuilder(
11-
[FreezedGenerator(Freezed.fromJson(options.config))],
12-
formatOutput: (str, version) {
13-
if (options.config['format'] == false) return str;
14-
15-
return DartFormatter(languageVersion: version).format(str);
16-
},
10+
[
11+
FreezedGenerator(
12+
Freezed.fromJson(options.config),
13+
format: options.config['format'] as bool? ?? false,
14+
),
15+
],
1716
'.freezed.dart',
1817
header: '''
1918
// coverage:ignore-file

packages/freezed/lib/src/ast.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ extension AstX on AstNode {
55
String? get documentation {
66
final builder = StringBuffer();
77

8-
for (Token? token = beginToken.precedingComments;
9-
token != null;
10-
token = token.next) {
8+
for (
9+
Token? token = beginToken.precedingComments;
10+
token != null;
11+
token = token.next
12+
) {
1113
builder.writeln(token);
1214
}
1315

@@ -25,7 +27,7 @@ extension ClassX on ClassDeclaration {
2527
element,
2628
...element.allSupertypes
2729
.where((e) => !e.isDartCoreObject)
28-
.map((e) => e.element)
30+
.map((e) => e.element),
2931
]) {
3032
for (final method in type.methods) {
3133
if (method.name == 'toString') {
@@ -44,7 +46,7 @@ extension ClassX on ClassDeclaration {
4446
element,
4547
...element.allSupertypes
4648
.where((e) => !e.isDartCoreObject)
47-
.map((e) => e.element)
49+
.map((e) => e.element),
4850
]) {
4951
for (final method in type.methods.where((e) => e.isOperator)) {
5052
if (method.name == '==') {
@@ -61,8 +63,9 @@ extension ConstructorX on ConstructorDeclaration {
6163
// ignore: deprecated_member_use, latest analyzer with enclosingElement3 not available in stable channel
6264
final classElement = declaredElement!.enclosingElement3;
6365

64-
var generics =
65-
classElement.typeParameters.map((e) => '\$${e.name}').join(', ');
66+
var generics = classElement.typeParameters
67+
.map((e) => '\$${e.name}')
68+
.join(', ');
6669
if (generics.isNotEmpty) {
6770
generics = '<$generics>';
6871
}
@@ -77,8 +80,9 @@ extension ConstructorX on ConstructorDeclaration {
7780
// ignore: deprecated_member_use, latest analyzer with enclosingElement3 not available in stable channel
7881
final classElement = declaredElement!.enclosingElement3;
7982

80-
var generics =
81-
classElement.typeParameters.map((e) => '\$${e.name}').join(', ');
83+
var generics = classElement.typeParameters
84+
.map((e) => '\$${e.name}')
85+
.join(', ');
8286
if (generics.isNotEmpty) {
8387
generics = '<$generics>';
8488
}

packages/freezed/lib/src/freezed_generator.dart

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@ extension StringX on String {
1919

2020
@immutable
2121
class FreezedGenerator extends ParserGenerator<Freezed> {
22-
FreezedGenerator(this._buildYamlConfigs);
22+
FreezedGenerator(this._buildYamlConfigs, {required this.format});
2323

2424
final Freezed _buildYamlConfigs;
25+
final bool format;
2526

2627
Iterable<DeepCloneableProperty> _getCommonDeepCloneableProperties(
2728
List<ConstructorDetails> constructors,
2829
PropertyList commonProperties,
2930
) sync* {
3031
for (final commonProperty in commonProperties.cloneableProperties) {
31-
final commonGetter = commonProperties.readableProperties
32-
.firstWhereOrNull((e) => e.name == commonProperty.name);
32+
final commonGetter = commonProperties.readableProperties.firstWhereOrNull(
33+
(e) => e.name == commonProperty.name,
34+
);
3335
final deepCopyProperty = constructors.firstOrNull?.deepCloneableProperties
3436
.firstWhereOrNull((e) => e.name == commonProperty.name);
3537

3638
if (deepCopyProperty == null || commonGetter == null) continue;
3739

3840
yield deepCopyProperty.copyWith(
39-
nullable: deepCopyProperty.nullable ||
41+
nullable:
42+
deepCopyProperty.nullable ||
4043
commonProperty.isNullable ||
4144
commonGetter.isNullable,
4245
);
@@ -54,6 +57,8 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
5457
) sync* {
5558
if (annotations.isEmpty) return;
5659

60+
if (!format) yield '// dart format off';
61+
5762
final library = Library.from(units);
5863
for (final value in _generateForAll(library)) {
5964
yield value;
@@ -71,30 +76,31 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
7176
yield value;
7277
}
7378
}
79+
80+
if (!format) yield '// dart format on';
7481
}
7582

76-
Iterable<Object> _generateForData(
77-
Library globalData,
78-
Class data,
79-
) sync* {
83+
Iterable<Object> _generateForData(Library globalData, Class data) sync* {
8084
if (data.options.fromJson) yield FromJson(data);
8185

82-
final commonCopyWith = data.options.annotation.copyWith ??
83-
data.properties.cloneableProperties.isNotEmpty
84-
? CopyWith(
85-
parents: data.parents,
86-
clonedClassName: data.name,
87-
readableProperties: data.properties.readableProperties,
88-
cloneableProperties: data.properties.cloneableProperties,
89-
deepCloneableProperties: _getCommonDeepCloneableProperties(
90-
data.constructors,
91-
data.properties,
92-
).toList(),
93-
genericsDefinition: data.genericsDefinitionTemplate,
94-
genericsParameter: data.genericsParameterTemplate,
95-
data: data,
96-
)
97-
: null;
86+
final commonCopyWith =
87+
data.options.annotation.copyWith ??
88+
data.properties.cloneableProperties.isNotEmpty
89+
? CopyWith(
90+
parents: data.parents,
91+
clonedClassName: data.name,
92+
readableProperties: data.properties.readableProperties,
93+
cloneableProperties: data.properties.cloneableProperties,
94+
deepCloneableProperties:
95+
_getCommonDeepCloneableProperties(
96+
data.constructors,
97+
data.properties,
98+
).toList(),
99+
genericsDefinition: data.genericsDefinitionTemplate,
100+
genericsParameter: data.genericsParameterTemplate,
101+
data: data,
102+
)
103+
: null;
98104

99105
yield Abstract(
100106
data: data,
@@ -109,21 +115,24 @@ class FreezedGenerator extends ParserGenerator<Freezed> {
109115
constructor: constructor,
110116
commonProperties: data.properties.readableProperties,
111117
globalData: globalData,
112-
copyWith: data.options.annotation.copyWith ??
113-
constructor.parameters.allParameters.isNotEmpty
114-
? CopyWith(
115-
parents: [],
116-
clonedClassName: constructor.redirectedName,
117-
cloneableProperties: constructor.properties.toList(),
118-
readableProperties:
119-
constructor.properties.where((e) => e.isSynthetic).toList(),
120-
deepCloneableProperties: constructor.deepCloneableProperties,
121-
genericsDefinition: data.genericsDefinitionTemplate,
122-
genericsParameter: data.genericsParameterTemplate,
123-
data: data,
124-
parent: commonCopyWith,
125-
)
126-
: null,
118+
copyWith:
119+
data.options.annotation.copyWith ??
120+
constructor.parameters.allParameters.isNotEmpty
121+
? CopyWith(
122+
parents: [],
123+
clonedClassName: constructor.redirectedName,
124+
cloneableProperties: constructor.properties.toList(),
125+
readableProperties:
126+
constructor.properties
127+
.where((e) => e.isSynthetic)
128+
.toList(),
129+
deepCloneableProperties: constructor.deepCloneableProperties,
130+
genericsDefinition: data.genericsDefinitionTemplate,
131+
genericsParameter: data.genericsParameterTemplate,
132+
data: data,
133+
parent: commonCopyWith,
134+
)
135+
: null,
127136
);
128137
}
129138
}

0 commit comments

Comments
 (0)