Skip to content

Commit 2e9d805

Browse files
authored
Merge branch 'master' into migrate-deprecated-classes-methods
2 parents 99c2db9 + 2af6081 commit 2e9d805

File tree

9 files changed

+32
-71
lines changed

9 files changed

+32
-71
lines changed

example_usage/lib/library_source.info.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_gen/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 4.0.0
2+
3+
- **Breaking Change**: remove `TypeChecker.fromRuntime`, use
4+
`TypeChecker.typeNamed` instead. This removes all use of `dart:mirror`, so
5+
builders using `source_gen` can be AOT compiled for better performance.
6+
- Keep `// GENERATED FILE` comments on the first line.
7+
18
## 3.1.0
29

310
- Prepare to stop using `dart:mirrors`: deprecate `TypeChecker.fromRuntime`.

source_gen/lib/src/builder.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,14 @@ String _defaultFormatOutput(String code, Version version) =>
430430

431431
/// Prefixes a dart format width and formats [code].
432432
String _defaultFormatUnit(String code, Version version) {
433-
code = '$dartFormatWidth\n$code';
433+
if (code.startsWith('$defaultFileHeader\n')) {
434+
code =
435+
'$defaultFileHeader\n'
436+
'$dartFormatWidth\n'
437+
'${code.substring(defaultFileHeader.length)}';
438+
} else {
439+
code = '$dartFormatWidth\n$code';
440+
}
434441
return _defaultFormatOutput(code, version);
435442
}
436443

source_gen/lib/src/generator_for_annotation.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ abstract class GeneratorForAnnotation<T> extends Generator {
5757
/// annotations. You can override this by setting [throwOnUnresolved] to
5858
/// `false`.
5959
///
60-
/// With `source_gen` 4.0.0 this class will stop using mirrors for matching
61-
/// annotations and will fall back to comparing the name of `T`. Pass
62-
/// [inPackage] and [inSdk] to tighten the check; see [TypeChecker.typeNamed].
60+
/// [TypeChecker.typeNamed] on `T` is used to match the annotation. By default
61+
/// it matches any annotation with the same name. Pass [inPackage] and [inSdk]
62+
/// to tighten the check; see [TypeChecker.typeNamed] for details.
63+
///
6364
/// To use a custom annotation check, override [typeChecker].
6465
const GeneratorForAnnotation({
6566
this.throwOnUnresolved = true,
6667
this.inPackage,
6768
this.inSdk,
6869
});
6970

70-
TypeChecker get typeChecker => TypeChecker.typeNamed(T);
71+
TypeChecker get typeChecker =>
72+
TypeChecker.typeNamed(T, inPackage: inPackage, inSdk: inSdk);
7173

7274
@override
7375
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {

source_gen/lib/src/type_checker.dart

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:mirrors' hide SourceLocation;
5+
// ignore_for_file: deprecated_member_use until analyzer 7 support is dropped.
66

77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
@@ -32,20 +32,6 @@ abstract class TypeChecker {
3232
/// ```
3333
const factory TypeChecker.any(Iterable<TypeChecker> checkers) = _AnyChecker;
3434

35-
/// Create a new [TypeChecker] backed by a runtime [type].
36-
///
37-
/// This implementation uses `dart:mirrors` (runtime reflection).
38-
@Deprecated('''
39-
Will be removed in 4.0.0 to drop `dart:mirrors` dependency.
40-
41-
Recommended: replace `fromRuntime(Foo)` with
42-
`typeNamed(Foo, inPackage: 'foo_package')`. This is a slighly weaker check than
43-
`fromRuntime(Foo)` as it matches any annotation named `Foo` in
44-
`package:foo_package`.
45-
46-
If you need an exact match, use `fromUrl`.''')
47-
const factory TypeChecker.fromRuntime(Type type) = _MirrorTypeChecker;
48-
4935
/// Create a new [TypeChecker] for types matching the name of [type].
5036
///
5137
/// Optionally, also pass [inPackage] to restrict to a specific package by
@@ -252,29 +238,6 @@ class _LibraryTypeChecker extends TypeChecker {
252238
String toString() => urlOfElement(_type.element!);
253239
}
254240

255-
// Checks a runtime type against a static type.
256-
class _MirrorTypeChecker extends TypeChecker {
257-
static Uri _uriOf(ClassMirror mirror) => normalizeUrl(
258-
(mirror.owner as LibraryMirror).uri,
259-
).replace(fragment: MirrorSystem.getName(mirror.simpleName));
260-
261-
// Precomputed type checker for types that already have been used.
262-
static final _cache = Expando<TypeChecker>();
263-
264-
final Type _type;
265-
266-
const _MirrorTypeChecker(this._type) : super._();
267-
268-
TypeChecker get _computed =>
269-
_cache[this] ??= TypeChecker.fromUrl(_uriOf(reflectClass(_type)));
270-
271-
@override
272-
bool isExactly(Element element) => _computed.isExactly(element);
273-
274-
@override
275-
String toString() => _computed.toString();
276-
}
277-
278241
// Checks a runtime type name and optional package against a static type.
279242
class _NameTypeChecker extends TypeChecker {
280243
final Type _type;

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 3.1.0
2+
version: 4.0.0
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/builder_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ final int foo = 42
10581058
''';
10591059

10601060
const _testGenPartContent = '''
1061-
$dartFormatWidth
10621061
// GENERATED CODE - DO NOT MODIFY BY HAND
1062+
$dartFormatWidth
10631063
10641064
part of 'test_lib.dart';
10651065
@@ -1072,8 +1072,8 @@ part of 'test_lib.dart';
10721072
''';
10731073

10741074
const _testGenPartContentForLibrary = '''
1075-
$dartFormatWidth
10761075
// GENERATED CODE - DO NOT MODIFY BY HAND
1076+
$dartFormatWidth
10771077
10781078
part of 'test_lib.dart';
10791079
@@ -1085,8 +1085,8 @@ part of 'test_lib.dart';
10851085
''';
10861086

10871087
const _testGenStandaloneContent = '''
1088-
$dartFormatWidth
10891088
// GENERATED CODE - DO NOT MODIFY BY HAND
1089+
$dartFormatWidth
10901090
10911091
// **************************************************************************
10921092
// CommentGenerator
@@ -1097,8 +1097,8 @@ $dartFormatWidth
10971097
''';
10981098

10991099
const _testGenPartContentForClassesAndLibrary = '''
1100-
$dartFormatWidth
11011100
// GENERATED CODE - DO NOT MODIFY BY HAND
1101+
$dartFormatWidth
11021102
11031103
part of 'test_lib.dart';
11041104
@@ -1112,8 +1112,8 @@ part of 'test_lib.dart';
11121112
''';
11131113

11141114
const _testGenNoLibrary = '''
1115-
$dartFormatWidth
11161115
// GENERATED CODE - DO NOT MODIFY BY HAND
1116+
$dartFormatWidth
11171117
11181118
part of 'test_lib.dart';
11191119

source_gen/test/generator_for_annotation_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void main() {
5050
_inputMap,
5151
outputs: {
5252
'a|lib/file.g.dart': '''
53-
$dartFormatWidth
5453
// GENERATED CODE - DO NOT MODIFY BY HAND
54+
$dartFormatWidth
5555
5656
// **************************************************************************
5757
// Generator: Repeating
@@ -148,8 +148,8 @@ main() {}''',
148148
},
149149
outputs: {
150150
'a|lib/file.g.dart': '''
151-
$dartFormatWidth
152151
// GENERATED CODE - DO NOT MODIFY BY HAND
152+
$dartFormatWidth
153153
154154
// **************************************************************************
155155
// Generator: Deprecated
@@ -192,8 +192,8 @@ $dartFormatWidth
192192
},
193193
outputs: {
194194
'a|lib/file.g.dart': '''
195-
$dartFormatWidth
196195
// GENERATED CODE - DO NOT MODIFY BY HAND
196+
$dartFormatWidth
197197
198198
// **************************************************************************
199199
// Generator: Deprecated

source_gen/test/type_checker_test.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,6 @@ void main() {
332332
});
333333
}
334334

335-
group('TypeChecker.forRuntime', () {
336-
commonTests(
337-
checkIterable: () => const TypeChecker.typeNamed(Iterable),
338-
checkEnum: () => const TypeChecker.typeNamed(Enum),
339-
checkEnumMixin: () => const TypeChecker.typeNamed(MyEnumMixin),
340-
checkMap: () => const TypeChecker.typeNamed(Map),
341-
checkMapMixin: () => const TypeChecker.typeNamed(MyMapMixin),
342-
checkHashMap: () => const TypeChecker.typeNamed(HashMap),
343-
checkGenerator: () => const TypeChecker.typeNamed(Generator),
344-
345-
checkGeneratorForAnnotation:
346-
() => const TypeChecker.typeNamed(
347-
GeneratorForAnnotation,
348-
inPackage: 'source_gen',
349-
),
350-
);
351-
});
352-
353335
group('TypeChecker.typeNamed without package', () {
354336
commonTests(
355337
checkIterable: () => const TypeChecker.typeNamed(Iterable),

0 commit comments

Comments
 (0)