diff --git a/CHANGELOG.md b/CHANGELOG.md index 06fb5c82..d92bae73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.4.6 + +* Pass actual language version of generating package to formatter. + ## 5.4.5 * Ignore "must_be_immutable" warning in generated files. Mocks cannot be made diff --git a/lib/src/builder.dart b/lib/src/builder.dart index 5573cecd..d42507ae 100644 --- a/lib/src/builder.dart +++ b/lib/src/builder.dart @@ -44,7 +44,9 @@ 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. @@ -141,12 +143,17 @@ class MockBuilder implements Builder { orderDirectives: true, useNullSafetySyntax: sourceLibIsNonNullable); final rawOutput = mockLibrary.accept(emitter).toString(); - // The source lib may be pre-null-safety because of an explicit opt-out - // (`// @dart=2.9`), as opposed to living in a pre-null-safety package. To - // allow for this situation, we must also add an opt-out comment here. + + 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: DartFormatter.latestLanguageVersion) - .format(''' + DartFormatter(languageVersion: surroundingLanguageVersion).format(''' // Mocks generated by Mockito $packageVersion from annotations // in ${entryLib.definingCompilationUnit.source.uri.path}. // Do not manually edit this file. diff --git a/pubspec.yaml b/pubspec.yaml index c3931c96..11a69413 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: mockito -version: 5.4.5 +version: 5.4.6 description: >- A mock framework inspired by Mockito with APIs for Fakes, Mocks, behavior verification, and stubbing. @@ -20,7 +20,9 @@ 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" @@ -30,5 +32,4 @@ dev_dependencies: build_web_compilers: ^4.0.11 http: ^1.0.0 lints: ^5.1.0 - package_config: ^2.1.0 test: ^1.24.4