Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make generated classes sealed/final #1121

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/freezed/lib/src/freezed_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class FreezedGenerator extends ParserGenerator<GlobalData, Data, Freezed> {
genericsParameterTemplate: GenericsParameterTemplate.fromGenericElement(
declaration.declaredElement!.typeParameters,
),
shouldMarkSealedOrFinal: configs.finalize ?? false,
shouldMarkSealedOrFinal: configs.finalize!,
);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/freezed/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ dependency_overrides:
quiver: ^3.2.0
# watcher 1.0.0 extends, implements, or mixens sealed class 'FileSystemEvent'
watcher: ^1.1.0
# There seems to be a regression with Flutter ^3.24.0
# that causes an error when generating sources using the build_runner
# after running `flutter pub downgrade` in GitHub actions,
# because it then installs frontend_server_client: ^3.0.0:
# Could not find a command named
# "/path/to/flutter/stable-3.24.0-x64/bin/cache/dart-sdk/bin/snapshots/frontend_server.dart.snapshot".
# see https://github.com/dart-lang/build/issues/3733#issuecomment-2272082820
Yegair marked this conversation as resolved.
Show resolved Hide resolved
frontend_server_client: ^4.0.0
61 changes: 61 additions & 0 deletions packages/freezed/test/finalized_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,65 @@ void main() {
expect(error.errorCode.name, 'PATTERN_NEVER_MATCHES_VALUE_TYPE');
});
});

group('disabled (finalize: false)', () {
test('finalize is disabled by default', () async {
final main = await resolveSources(
{
'freezed|test/integration/main.dart': r'''
library main;
import 'finalized.dart';

void main() {
switch (FinalizedDisabledFoo()) {
case FinalizedDisabledBar():
break;

case FinalizedDisabledFoo():
break;
}
}
''',
},
(r) => r.findLibraryByName('main'),
);

final errorResult = await main!.session
.getErrors('/freezed/test/integration/main.dart') as ErrorsResult;

// the absence of a warning means that the generated subclasses of FinalizedDisabled
// or not sealed/final, and therefore it is disabled by default
expect(errorResult.errors, isEmpty);
});
});
group('default config', () {
test('finalize is disabled by default', () async {
final main = await resolveSources(
{
'freezed|test/integration/main.dart': r'''
library main;
import 'finalized.dart';

void main() {
switch (FinalizedDefaultFoo()) {
case FinalizedDefaultBar():
break;

case FinalizedDefaultFoo():
break;
}
}
''',
},
(r) => r.findLibraryByName('main'),
);

final errorResult = await main!.session
.getErrors('/freezed/test/integration/main.dart') as ErrorsResult;

// the absence of a warning means that the generated subclasses of FinalizedDefault
// or not sealed/final, and therefore it is disabled by default
expect(errorResult.errors, isEmpty);
});
});
}
12 changes: 12 additions & 0 deletions packages/freezed/test/integration/finalized.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ sealed class FinalizedMultiple with _$FinalizedMultiple {
factory FinalizedMultiple.b() = FinalizedMultipleB;
factory FinalizedMultiple.c() = FinalizedMultipleC;
}

@Freezed()
sealed class FinalizedDefault with _$FinalizedDefault {
factory FinalizedDefault.foo() = FinalizedDefaultFoo;
factory FinalizedDefault.bar() = FinalizedDefaultBar;
}

@Freezed(finalize: false)
sealed class FinalizedDisabled with _$FinalizedDisabled {
factory FinalizedDisabled.foo() = FinalizedDisabledFoo;
factory FinalizedDisabled.bar() = FinalizedDisabledBar;
}
1 change: 1 addition & 0 deletions packages/freezed_annotation/lib/freezed_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ class Freezed {
/// break;
/// }
/// ```
@JsonKey(defaultValue: false)
final bool? finalize;

/// Options for customizing the generation of `map` functions
Expand Down
2 changes: 1 addition & 1 deletion packages/freezed_annotation/lib/freezed_annotation.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/freezed_annotation/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ dependency_overrides:
pub_semver: ^2.1.3
# watcher 1.0.0 extends, implements, or mixens sealed class 'FileSystemEvent'
watcher: ^1.1.0
# There seems to be a regression with Flutter ^3.24.0
# that causes an error when generating sources using the build_runner
# after running `flutter pub downgrade` in GitHub actions,
# because it then installs frontend_server_client: ^3.0.0:
# Could not find a command named
# "/path/to/flutter/stable-3.24.0-x64/bin/cache/dart-sdk/bin/snapshots/frontend_server.dart.snapshot".
# see https://github.com/dart-lang/build/issues/3733#issuecomment-2272082820
frontend_server_client: ^4.0.0
1 change: 1 addition & 0 deletions packages/freezed_annotation/test/freezed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void main() {
expect(defaultValue.unionValueCase, isNull);
expect(defaultValue.when, isNull);
expect(defaultValue.makeCollectionsUnmodifiable, isTrue);
expect(defaultValue.finalize, isFalse);
});

test('.fromJson({map: x})', () {
Expand Down