mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 18:31:32 +00:00
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 <srawlins@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
bd28bad820
commit
74af5f3496
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/src/generated/source.dart';
|
||||
import 'package:nnbd_migration/instrumentation.dart';
|
||||
import 'package:nnbd_migration/nnbd_migration.dart';
|
||||
|
||||
/// The instrumentation information gathered from the migration engine.
|
||||
class InstrumentationInformation {
|
||||
/// The node used for type sources that are always `null`.
|
||||
NullabilityNodeInfo always;
|
||||
|
||||
/// A map from elements outside of the code being migrated, to the nullability
|
||||
/// nodes associated with the type of the element.
|
||||
final Map<Element, DecoratedTypeInfo> externalDecoratedType = {};
|
||||
|
||||
/// A map from the graph edges between nullability nodes, to information about
|
||||
/// the edge that was created and why it was created.
|
||||
final Map<EdgeInfo, EdgeOriginInfo> edgeOrigin = {};
|
||||
|
||||
/// The node used for type sources that are never `null`.
|
||||
NullabilityNodeInfo never;
|
||||
|
||||
/// A list of the steps in the propagation of nullability information through
|
||||
/// the nullability graph, to report details of the step that was performed
|
||||
/// and why it was performed.
|
||||
final List<PropagationInfo> propagationSteps = [];
|
||||
|
||||
/// The instrumentation information that is specific to a single source.
|
||||
final Map<Source, SourceInformation> sourceInformation = {};
|
||||
|
||||
/// Initialize a newly created holder of instrumentation information.
|
||||
InstrumentationInformation();
|
||||
}
|
||||
|
||||
/// The instrumentation information gathered from the migration engine that is
|
||||
/// specific to a single source.
|
||||
class SourceInformation {
|
||||
/// A map from the type annotations found in the source code, to the
|
||||
/// nullability nodes that are associated with that type.
|
||||
final Map<TypeAnnotation, NullabilityNodeInfo> explicitTypeNullability = {};
|
||||
|
||||
/// A map from the fixes that were decided on to the reasons for the fix.
|
||||
final Map<SingleNullabilityFix, List<FixReasonInfo>> fixes = {};
|
||||
|
||||
/// A map from AST nodes that have an implicit return type to the nullability
|
||||
/// node associated with the implicit return type of the AST node. The node
|
||||
/// can be an
|
||||
/// - executable declaration,
|
||||
/// - function-typed formal parameter declaration,
|
||||
/// - function type alias declaration,
|
||||
/// - generic function type, or
|
||||
/// - function expression.
|
||||
final Map<AstNode, DecoratedTypeInfo> implicitReturnType = {};
|
||||
|
||||
/// A map from AST nodes that have an implicit type to the nullability node
|
||||
/// associated with the implicit type of the AST node. The node can be a
|
||||
/// - formal parameter,
|
||||
/// - declared identifier, or
|
||||
/// - variable in a variable declaration list.
|
||||
final Map<AstNode, DecoratedTypeInfo> implicitType = {};
|
||||
|
||||
/// Called whenever the migration engine encounters an AST node with implicit
|
||||
/// type arguments, to report the nullability nodes associated with the
|
||||
/// implicit type arguments of the AST node.
|
||||
///
|
||||
/// A map from AST nodes that have implicit type arguments to the nullability
|
||||
/// nodes associated with the implicit type arguments of the AST node. The
|
||||
/// node can be a
|
||||
/// - constructor redirection,
|
||||
/// - function expression invocation,
|
||||
/// - method invocation,
|
||||
/// - instance creation expression,
|
||||
/// - list/map/set literal, or
|
||||
/// - type annotation.
|
||||
final Map<AstNode, List<DecoratedTypeInfo>> implicitTypeArguments = {};
|
||||
|
||||
/// Initialize a newly created holder of instrumentation information that is
|
||||
/// specific to a single source.
|
||||
SourceInformation();
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analysis_server/src/edit/nnbd_migration/instrumentation_information.dart';
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/src/generated/source.dart';
|
||||
import 'package:nnbd_migration/instrumentation.dart';
|
||||
import 'package:nnbd_migration/nnbd_migration.dart';
|
||||
|
||||
/// A listener used to gather instrumentation information from the migration
|
||||
/// engine.
|
||||
class InstrumentationListener implements NullabilityMigrationInstrumentation {
|
||||
/// The instrumentation information being gathered.
|
||||
InstrumentationInformation data = InstrumentationInformation();
|
||||
|
||||
/// Initialize a newly created listener.
|
||||
InstrumentationListener();
|
||||
|
||||
@override
|
||||
void explicitTypeNullability(
|
||||
Source source, TypeAnnotation typeAnnotation, NullabilityNodeInfo node) {
|
||||
_sourceInfo(source).explicitTypeNullability[typeAnnotation] = node;
|
||||
}
|
||||
|
||||
@override
|
||||
void externalDecoratedType(Element element, DecoratedTypeInfo decoratedType) {
|
||||
data.externalDecoratedType[element] = decoratedType;
|
||||
}
|
||||
|
||||
@override
|
||||
void fix(SingleNullabilityFix fix, Iterable<FixReasonInfo> reasons) {
|
||||
_sourceInfo(fix.source).fixes[fix] = reasons.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
void graphEdge(EdgeInfo edge, EdgeOriginInfo originInfo) {
|
||||
data.edgeOrigin[edge] = originInfo;
|
||||
}
|
||||
|
||||
@override
|
||||
void immutableNodes(NullabilityNodeInfo never, NullabilityNodeInfo always) {
|
||||
data.never = never;
|
||||
data.always = always;
|
||||
}
|
||||
|
||||
@override
|
||||
void implicitReturnType(
|
||||
Source source, AstNode node, DecoratedTypeInfo decoratedReturnType) {
|
||||
_sourceInfo(source).implicitReturnType[node] = decoratedReturnType;
|
||||
}
|
||||
|
||||
@override
|
||||
void implicitType(
|
||||
Source source, AstNode node, DecoratedTypeInfo decoratedType) {
|
||||
_sourceInfo(source).implicitType[node] = decoratedType;
|
||||
}
|
||||
|
||||
@override
|
||||
void implicitTypeArguments(
|
||||
Source source, AstNode node, Iterable<DecoratedTypeInfo> types) {
|
||||
_sourceInfo(source).implicitTypeArguments[node] = types.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
void propagationStep(PropagationInfo info) {
|
||||
data.propagationSteps.add(info);
|
||||
}
|
||||
|
||||
/// Return the source information associated with the given [source], creating
|
||||
/// it if there has been no previous information for that source.
|
||||
SourceInformation _sourceInfo(Source source) =>
|
||||
data.sourceInformation.putIfAbsent(source, () => SourceInformation());
|
||||
}
|
Loading…
Reference in a new issue