@@ -44,7 +44,9 @@ import 'package:collection/collection.dart';
44
44
import 'package:dart_style/dart_style.dart' ;
45
45
import 'package:mockito/annotations.dart' ;
46
46
import 'package:mockito/src/version.dart' ;
47
+ import 'package:package_config/package_config_types.dart' ;
47
48
import 'package:path/path.dart' as p;
49
+ import 'package:pub_semver/pub_semver.dart' ;
48
50
import 'package:source_gen/source_gen.dart' ;
49
51
50
52
/// For a source Dart library, generate the mocks referenced therein.
@@ -141,12 +143,17 @@ class MockBuilder implements Builder {
141
143
orderDirectives: true ,
142
144
useNullSafetySyntax: sourceLibIsNonNullable);
143
145
final rawOutput = mockLibrary.accept (emitter).toString ();
144
- // The source lib may be pre-null-safety because of an explicit opt-out
145
- // (`// @dart=2.9`), as opposed to living in a pre-null-safety package. To
146
- // allow for this situation, we must also add an opt-out comment here.
146
+
147
+ final packageConfig = await buildStep.packageConfig;
148
+ var surroundingLanguageVersion = DartFormatter .latestLanguageVersion;
149
+ if (packageConfig[mockLibraryAsset.package]
150
+ case Package (: final languageVersion? )) {
151
+ surroundingLanguageVersion =
152
+ Version (languageVersion.major, languageVersion.minor, 0 );
153
+ }
154
+
147
155
final mockLibraryContent =
148
- DartFormatter (languageVersion: DartFormatter .latestLanguageVersion)
149
- .format ('''
156
+ DartFormatter (languageVersion: surroundingLanguageVersion).format ('''
150
157
// Mocks generated by Mockito $packageVersion from annotations
151
158
// in ${entryLib .definingCompilationUnit .source .uri .path }.
152
159
// Do not manually edit this file.
0 commit comments