Skip to content

Commit 1865a35

Browse files
authored
Add APIs for using analyzer element model 2. (#3775)
* Add APIs for using analyzer element model 2. * Update CHANGELOG.md
1 parent fbdfb67 commit 1865a35

File tree

8 files changed

+250
-34
lines changed

8 files changed

+250
-34
lines changed

build/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- Bump the min sdk to 3.6.0-228.0.dev.
44
- Remove some unnecessary casts and non-null assertions now that we have private
55
field promotion.
6-
- Require analyzer ^6.9.0.
6+
- Require analyzer ^7.0.0.
77
- Fix analyzer deprecations.
88

99
## 2.4.1

build/lib/src/analyzer/resolver.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:analyzer/dart/analysis/results.dart';
55
import 'package:analyzer/dart/analysis/session.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
89
import 'package:analyzer/error/error.dart';
910

1011
import '../asset/id.dart';
@@ -29,6 +30,17 @@ abstract class Resolver {
2930
/// instance because due to imports or exports.
3031
Stream<LibraryElement> get libraries;
3132

33+
/// All libraries resolved by this resolver.
34+
///
35+
/// This includes the following libraries:
36+
/// - The primary input of this resolver (in other words, the
37+
/// [BuildStep.inputId] of a build step).
38+
/// - Libraries resolved with a direct [libraryFor] call.
39+
/// - Every public `dart:` library part of the SDK.
40+
/// - All libraries recursively accessible from the mentioned sources, for
41+
/// instance because due to imports or exports.
42+
Stream<LibraryElement2> get libraries2;
43+
3244
/// Returns the parsed [AstNode] for [Element].
3345
///
3446
/// This should always be preferred over using the [AnalysisSession]
@@ -42,6 +54,19 @@ abstract class Resolver {
4254
/// reason.
4355
Future<AstNode?> astNodeFor(Element element, {bool resolve = false});
4456

57+
/// Returns the parsed [AstNode] for [Element].
58+
///
59+
/// This should always be preferred over using the [AnalysisSession]
60+
/// directly, because it avoids [InconsistentAnalysisException] issues.
61+
///
62+
/// If [resolve] is `true` then you will get a resolved ast node, otherwise
63+
/// it will only be a parsed ast node.
64+
///
65+
/// Returns `null` if the ast node can not be found. This can happen if an
66+
/// element is coming from a summary, or is unavailable for some other
67+
/// reason.
68+
Future<AstNode?> astNodeFor2(Fragment element, {bool resolve = false});
69+
4570
/// Returns a parsed AST structor representing the file defined in [assetId].
4671
///
4772
/// * If the [assetId] has syntax errors, and [allowSyntaxErrors] is set to
@@ -60,6 +85,14 @@ abstract class Resolver {
6085
Future<LibraryElement> libraryFor(AssetId assetId,
6186
{bool allowSyntaxErrors = false});
6287

88+
/// Returns a resolved library representing the file defined in [assetId].
89+
///
90+
/// * Throws [NonLibraryAssetException] if [assetId] is not a Dart library.
91+
/// * If the [assetId] has syntax errors, and [allowSyntaxErrors] is set to
92+
/// `false` (the default), throws a [SyntaxErrorInAssetException].
93+
Future<LibraryElement2> libraryFor2(AssetId assetId,
94+
{bool allowSyntaxErrors = false});
95+
6396
/// Returns the first resolved library identified by [libraryName].
6497
///
6598
/// A library is resolved if it's recursively accessible from the entry point
@@ -72,6 +105,19 @@ abstract class Resolver {
72105
/// being unique.
73106
Future<LibraryElement?> findLibraryByName(String libraryName);
74107

108+
/// Returns the first resolved library identified by [libraryName].
109+
///
110+
/// A library is resolved if it's recursively accessible from the entry point
111+
/// or subsequent calls to [libraryFor]. In other words, this searches for
112+
/// libraries in [libraries].
113+
/// If no library can be found, returns `null`.
114+
///
115+
/// **NOTE**: In general, its recommended to use [libraryFor] with an absolute
116+
/// asset id instead of a named identifier that has the possibility of not
117+
/// being unique.
118+
Future<LibraryElement2?> findLibraryByName2(
119+
String libraryName);
120+
75121
/// Returns the [AssetId] of the Dart library or part declaring [element].
76122
///
77123
/// If [element] is defined in the SDK or in a summary throws
@@ -81,6 +127,16 @@ abstract class Resolver {
81127
/// The returned asset is not necessarily the asset that should be imported to
82128
/// use the element, it may be a part file instead of the library.
83129
Future<AssetId> assetIdForElement(Element element);
130+
131+
/// Returns the [AssetId] of the Dart library or part declaring [element].
132+
///
133+
/// If [element] is defined in the SDK or in a summary throws
134+
/// `UnresolvableAssetException`, although a non-throwing return here does not
135+
/// guarantee that the asset is readable.
136+
///
137+
/// The returned asset is not necessarily the asset that should be imported to
138+
/// use the element, it may be a part file instead of the library.
139+
Future<AssetId> assetIdForElement2(Element2 element);
84140
}
85141

86142
/// A resolver that should be manually released at the end of a build step.

build/lib/src/builder/build_step.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:async';
55
import 'dart:convert';
66

77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
89
import 'package:meta/meta.dart';
910
import 'package:package_config/package_config_types.dart';
1011

@@ -39,6 +40,21 @@ abstract class BuildStep implements AssetReader, AssetWriter {
3940
/// ```
4041
Future<LibraryElement> get inputLibrary;
4142

43+
/// Resolved library defined by [inputId].
44+
///
45+
/// Throws [NonLibraryAssetException] if [inputId] is not a Dart library file.
46+
/// Throws [SyntaxErrorInAssetException] if [inputId] contains syntax errors.
47+
/// If you want to support libraries with syntax errors, resolve the library
48+
/// manually instead of using [inputLibrary]:
49+
/// ```dart
50+
/// Future<void> build(BuildStep step) async {
51+
/// // Resolve the input library, allowing syntax errors
52+
/// final inputLibrary =
53+
/// await step.resolver.libraryFor(step.inputId, allowSyntaxErrors: true);
54+
/// }
55+
/// ```
56+
Future<LibraryElement2> get inputLibrary2;
57+
4258
/// Gets an instance provided by [resource] which is guaranteed to be unique
4359
/// within a single build, and may be reused across build steps within a
4460
/// build if the implementation allows.

build/lib/src/builder/build_step_impl.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77

88
import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1011
import 'package:async/async.dart';
1112
import 'package:crypto/crypto.dart';
1213
import 'package:glob/glob.dart';
@@ -39,6 +40,12 @@ class BuildStepImpl implements BuildStep {
3940
return resolver.libraryFor(inputId);
4041
}
4142

43+
@override
44+
Future<LibraryElement2> get inputLibrary2 async {
45+
if (_isComplete) throw BuildStepCompletedException();
46+
return resolver.libraryFor2(inputId);
47+
}
48+
4249
/// The list of all outputs which are expected/allowed to be output from this
4350
/// step.
4451
@override
@@ -220,10 +227,21 @@ class _DelayedResolver implements Resolver {
220227
return completer.stream;
221228
}
222229

230+
@override
231+
Stream<LibraryElement2> get libraries2 {
232+
var completer = StreamCompleter<LibraryElement2>();
233+
_delegate.then((r) => completer.setSourceStream(r.libraries2));
234+
return completer.stream;
235+
}
236+
223237
@override
224238
Future<AstNode?> astNodeFor(Element element, {bool resolve = false}) async =>
225239
(await _delegate).astNodeFor(element, resolve: resolve);
226240

241+
@override
242+
Future<AstNode?> astNodeFor2(Fragment fragment, {bool resolve = false}) async =>
243+
(await _delegate).astNodeFor2(fragment, resolve: resolve);
244+
227245
@override
228246
Future<CompilationUnit> compilationUnitFor(AssetId assetId,
229247
{bool allowSyntaxErrors = false}) async =>
@@ -236,11 +254,25 @@ class _DelayedResolver implements Resolver {
236254
(await _delegate)
237255
.libraryFor(assetId, allowSyntaxErrors: allowSyntaxErrors);
238256

257+
@override
258+
Future<LibraryElement2> libraryFor2(AssetId assetId,
259+
{bool allowSyntaxErrors = false}) async =>
260+
(await _delegate)
261+
.libraryFor2(assetId, allowSyntaxErrors: allowSyntaxErrors);
262+
239263
@override
240264
Future<LibraryElement?> findLibraryByName(String libraryName) async =>
241265
(await _delegate).findLibraryByName(libraryName);
242266

267+
@override
268+
Future<LibraryElement2?> findLibraryByName2(String libraryName) async =>
269+
(await _delegate).findLibraryByName2(libraryName);
270+
243271
@override
244272
Future<AssetId> assetIdForElement(Element element) async =>
245273
(await _delegate).assetIdForElement(element);
274+
275+
@override
276+
Future<AssetId> assetIdForElement2(Element2 element) async =>
277+
(await _delegate).assetIdForElement2(element);
246278
}

build/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ environment:
1111
sdk: ^3.6.0-228.0.dev
1212

1313
dependencies:
14-
analyzer: ^6.9.0
14+
analyzer: ^7.0.0
1515
async: ^2.5.0
1616
convert: ^3.0.0
1717
crypto: ^3.0.0

build_resolvers/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix SDK summary reads when multiple isolates are using build resolvers (not
77
recommended).
88
- Fix analyzer deprecations.
9+
- Require analyzer ^7.0.0.
910

1011
## 2.4.2
1112

0 commit comments

Comments
 (0)