Skip to content

Commit 74af5f3

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Add a representation of the information captured from an nnbd migration
Change-Id: Ia201c59ebb31709278ec4efaf7432af757716cf6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/117764 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent bd28bad commit 74af5f3

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/ast/ast.dart';
6+
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/src/generated/source.dart';
8+
import 'package:nnbd_migration/instrumentation.dart';
9+
import 'package:nnbd_migration/nnbd_migration.dart';
10+
11+
/// The instrumentation information gathered from the migration engine.
12+
class InstrumentationInformation {
13+
/// The node used for type sources that are always `null`.
14+
NullabilityNodeInfo always;
15+
16+
/// A map from elements outside of the code being migrated, to the nullability
17+
/// nodes associated with the type of the element.
18+
final Map<Element, DecoratedTypeInfo> externalDecoratedType = {};
19+
20+
/// A map from the graph edges between nullability nodes, to information about
21+
/// the edge that was created and why it was created.
22+
final Map<EdgeInfo, EdgeOriginInfo> edgeOrigin = {};
23+
24+
/// The node used for type sources that are never `null`.
25+
NullabilityNodeInfo never;
26+
27+
/// A list of the steps in the propagation of nullability information through
28+
/// the nullability graph, to report details of the step that was performed
29+
/// and why it was performed.
30+
final List<PropagationInfo> propagationSteps = [];
31+
32+
/// The instrumentation information that is specific to a single source.
33+
final Map<Source, SourceInformation> sourceInformation = {};
34+
35+
/// Initialize a newly created holder of instrumentation information.
36+
InstrumentationInformation();
37+
}
38+
39+
/// The instrumentation information gathered from the migration engine that is
40+
/// specific to a single source.
41+
class SourceInformation {
42+
/// A map from the type annotations found in the source code, to the
43+
/// nullability nodes that are associated with that type.
44+
final Map<TypeAnnotation, NullabilityNodeInfo> explicitTypeNullability = {};
45+
46+
/// A map from the fixes that were decided on to the reasons for the fix.
47+
final Map<SingleNullabilityFix, List<FixReasonInfo>> fixes = {};
48+
49+
/// A map from AST nodes that have an implicit return type to the nullability
50+
/// node associated with the implicit return type of the AST node. The node
51+
/// can be an
52+
/// - executable declaration,
53+
/// - function-typed formal parameter declaration,
54+
/// - function type alias declaration,
55+
/// - generic function type, or
56+
/// - function expression.
57+
final Map<AstNode, DecoratedTypeInfo> implicitReturnType = {};
58+
59+
/// A map from AST nodes that have an implicit type to the nullability node
60+
/// associated with the implicit type of the AST node. The node can be a
61+
/// - formal parameter,
62+
/// - declared identifier, or
63+
/// - variable in a variable declaration list.
64+
final Map<AstNode, DecoratedTypeInfo> implicitType = {};
65+
66+
/// Called whenever the migration engine encounters an AST node with implicit
67+
/// type arguments, to report the nullability nodes associated with the
68+
/// implicit type arguments of the AST node.
69+
///
70+
/// A map from AST nodes that have implicit type arguments to the nullability
71+
/// nodes associated with the implicit type arguments of the AST node. The
72+
/// node can be a
73+
/// - constructor redirection,
74+
/// - function expression invocation,
75+
/// - method invocation,
76+
/// - instance creation expression,
77+
/// - list/map/set literal, or
78+
/// - type annotation.
79+
final Map<AstNode, List<DecoratedTypeInfo>> implicitTypeArguments = {};
80+
81+
/// Initialize a newly created holder of instrumentation information that is
82+
/// specific to a single source.
83+
SourceInformation();
84+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/edit/nnbd_migration/instrumentation_information.dart';
6+
import 'package:analyzer/dart/ast/ast.dart';
7+
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/src/generated/source.dart';
9+
import 'package:nnbd_migration/instrumentation.dart';
10+
import 'package:nnbd_migration/nnbd_migration.dart';
11+
12+
/// A listener used to gather instrumentation information from the migration
13+
/// engine.
14+
class InstrumentationListener implements NullabilityMigrationInstrumentation {
15+
/// The instrumentation information being gathered.
16+
InstrumentationInformation data = InstrumentationInformation();
17+
18+
/// Initialize a newly created listener.
19+
InstrumentationListener();
20+
21+
@override
22+
void explicitTypeNullability(
23+
Source source, TypeAnnotation typeAnnotation, NullabilityNodeInfo node) {
24+
_sourceInfo(source).explicitTypeNullability[typeAnnotation] = node;
25+
}
26+
27+
@override
28+
void externalDecoratedType(Element element, DecoratedTypeInfo decoratedType) {
29+
data.externalDecoratedType[element] = decoratedType;
30+
}
31+
32+
@override
33+
void fix(SingleNullabilityFix fix, Iterable<FixReasonInfo> reasons) {
34+
_sourceInfo(fix.source).fixes[fix] = reasons.toList();
35+
}
36+
37+
@override
38+
void graphEdge(EdgeInfo edge, EdgeOriginInfo originInfo) {
39+
data.edgeOrigin[edge] = originInfo;
40+
}
41+
42+
@override
43+
void immutableNodes(NullabilityNodeInfo never, NullabilityNodeInfo always) {
44+
data.never = never;
45+
data.always = always;
46+
}
47+
48+
@override
49+
void implicitReturnType(
50+
Source source, AstNode node, DecoratedTypeInfo decoratedReturnType) {
51+
_sourceInfo(source).implicitReturnType[node] = decoratedReturnType;
52+
}
53+
54+
@override
55+
void implicitType(
56+
Source source, AstNode node, DecoratedTypeInfo decoratedType) {
57+
_sourceInfo(source).implicitType[node] = decoratedType;
58+
}
59+
60+
@override
61+
void implicitTypeArguments(
62+
Source source, AstNode node, Iterable<DecoratedTypeInfo> types) {
63+
_sourceInfo(source).implicitTypeArguments[node] = types.toList();
64+
}
65+
66+
@override
67+
void propagationStep(PropagationInfo info) {
68+
data.propagationSteps.add(info);
69+
}
70+
71+
/// Return the source information associated with the given [source], creating
72+
/// it if there has been no previous information for that source.
73+
SourceInformation _sourceInfo(Source source) =>
74+
data.sourceInformation.putIfAbsent(source, () => SourceInformation());
75+
}

0 commit comments

Comments
 (0)