Skip to content

Commit d358e24

Browse files
simolus3srawlins
authored andcommitted
Use correct language version for formatter
1 parent 5cb8433 commit d358e24

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.4.6
2+
3+
* Pass actual language version of generating package to formatter.
4+
15
## 5.4.5
26

37
* Ignore "must_be_immutable" warning in generated files. Mocks cannot be made

lib/src/builder.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import 'package:collection/collection.dart';
4444
import 'package:dart_style/dart_style.dart';
4545
import 'package:mockito/annotations.dart';
4646
import 'package:mockito/src/version.dart';
47+
import 'package:package_config/package_config_types.dart';
4748
import 'package:path/path.dart' as p;
49+
import 'package:pub_semver/pub_semver.dart';
4850
import 'package:source_gen/source_gen.dart';
4951

5052
/// For a source Dart library, generate the mocks referenced therein.
@@ -141,12 +143,17 @@ class MockBuilder implements Builder {
141143
orderDirectives: true,
142144
useNullSafetySyntax: sourceLibIsNonNullable);
143145
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+
147155
final mockLibraryContent =
148-
DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
149-
.format('''
156+
DartFormatter(languageVersion: surroundingLanguageVersion).format('''
150157
// Mocks generated by Mockito $packageVersion from annotations
151158
// in ${entryLib.definingCompilationUnit.source.uri.path}.
152159
// Do not manually edit this file.

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: mockito
2-
version: 5.4.5
2+
version: 5.4.6
33
description: >-
44
A mock framework inspired by Mockito with APIs for Fakes, Mocks,
55
behavior verification, and stubbing.
@@ -20,7 +20,9 @@ dependencies:
2020
dart_style: '>=2.3.7 <4.0.0'
2121
matcher: ^0.12.16
2222
meta: ^1.15.0
23+
package_config: ^2.1.1
2324
path: ^1.9.0
25+
pub_semver: ^2.1.5
2426
source_gen: ">=1.4.0 <3.0.0"
2527
test_api: ">=0.6.1 <0.8.0"
2628

@@ -30,5 +32,4 @@ dev_dependencies:
3032
build_web_compilers: ^4.0.11
3133
http: ^1.0.0
3234
lints: ^5.1.0
33-
package_config: ^2.1.0
3435
test: ^1.24.4

0 commit comments

Comments
 (0)