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

Use the library effective language version when formatting #797

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 5.4.6

* Pass actual language version of generating package to formatter.
* When formatting a generated mocks library, use the language version of the
library with the mockito annotation.

## 5.4.5

Expand All @@ -25,9 +26,9 @@

## 5.4.3

* Require analyzer 5.12.0, allow analyzer version 6.x;
* Require analyzer 5.12.0, allow analyzer version 6.x.
* Add example of writing a class to mock function objects.
* Add support for the `build_extensions` build.yaml option
* Add support for the `build_extensions` build.yaml option.
* Require Dart >=3.1.0.
* **Potentially breaking** Changed default `String` value returned by nice
mocks' unstubbed method to include some useful info. This could break the
Expand Down
15 changes: 3 additions & 12 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/src/version.dart';
import 'package:package_config/package_config_types.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:source_gen/source_gen.dart';

/// For a source Dart library, generate the mocks referenced therein.
Expand Down Expand Up @@ -144,16 +142,9 @@ class MockBuilder implements Builder {
useNullSafetySyntax: sourceLibIsNonNullable);
final rawOutput = mockLibrary.accept(emitter).toString();

final packageConfig = await buildStep.packageConfig;
var surroundingLanguageVersion = DartFormatter.latestLanguageVersion;
if (packageConfig[mockLibraryAsset.package]
case Package(:final languageVersion?)) {
surroundingLanguageVersion =
Version(languageVersion.major, languageVersion.minor, 0);
}

final mockLibraryContent =
DartFormatter(languageVersion: surroundingLanguageVersion).format('''
DartFormatter(languageVersion: entryLib.languageVersion.effective)
.format('''
// Mocks generated by Mockito $packageVersion from annotations
// in ${entryLib.definingCompilationUnit.source.uri.path}.
// Do not manually edit this file.
Expand Down Expand Up @@ -202,7 +193,7 @@ $rawOutput
interface.typeArguments.forEach(addTypesFrom);
interface.allSupertypes.forEach(addTypesFrom);
case final analyzer.RecordType record:
record.positionalTypes.forEach(addTypesFrom);
record.positionalFields.map((e) => e.type).forEach(addTypesFrom);
record.namedFields.map((e) => e.type).forEach(addTypesFrom);
}
}
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ dependencies:
dart_style: '>=2.3.7 <4.0.0'
matcher: ^0.12.16
meta: ^1.15.0
package_config: ^2.1.1
path: ^1.9.0
pub_semver: ^2.1.5
source_gen: ">=1.4.0 <3.0.0"
test_api: ">=0.6.1 <0.8.0"

Expand All @@ -32,4 +30,5 @@ dev_dependencies:
build_web_compilers: ^4.0.11
http: ^1.0.0
lints: ^5.1.0
package_config: ^2.1.1
test: ^1.24.4
Loading