mirror of
https://github.com/dart-lang/sdk
synced 2024-10-03 11:11:05 +00:00
4eed065859
This adds a CFE test for DartScope that shares the data with the corresponding DDC test. The CFE test uses the DartScopeBuilder2 which is currently in development to replace the DartScopeBuilder. Change-Id: I0d6c76957e7a5d28babab14110d430935e686b22 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335821 Reviewed-by: Jens Johansen <jensj@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com> Commit-Queue: Johnni Winther <johnniwinther@google.com>
91 lines
3.2 KiB
Dart
91 lines
3.2 KiB
Dart
// Copyright (c) 2023, 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 'dart:io' show Directory, Platform;
|
|
|
|
import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
|
|
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
|
|
import 'package:_fe_analyzer_shared/src/testing/features.dart';
|
|
import 'package:front_end/src/testing/id_testing_helper.dart';
|
|
import 'package:kernel/ast.dart';
|
|
import 'package:kernel/dart_scope_calculator.dart';
|
|
|
|
Future<void> main(List<String> args) async {
|
|
Directory dataDir = new Directory.fromUri(Platform.script.resolve('data'));
|
|
await runTests<Features>(dataDir,
|
|
args: args,
|
|
createUriForFileName: createUriForFileName,
|
|
onFailure: onFailure,
|
|
runTest: runTestFor(const ScopeDataComputer(), [defaultCfeConfig]));
|
|
}
|
|
|
|
class Tags {
|
|
static const String cls = 'class';
|
|
static const String member = 'member';
|
|
static const String isStatic = 'static';
|
|
static const String typeParameter = 'typeParameters';
|
|
static const String variables = 'variables';
|
|
}
|
|
|
|
class ScopeDataComputer extends CfeDataComputer<Features> {
|
|
const ScopeDataComputer();
|
|
|
|
@override
|
|
void computeMemberData(CfeTestResultData testResultData, Member member,
|
|
Map<Id, ActualData<Features>> actualMap,
|
|
{bool? verbose}) {
|
|
member.accept(ScopeDataExtractor(member.enclosingLibrary,
|
|
member.enclosingClass, testResultData.compilerResult, actualMap));
|
|
}
|
|
|
|
@override
|
|
DataInterpreter<Features> get dataValidator =>
|
|
const FeaturesDataInterpreter();
|
|
}
|
|
|
|
class ScopeDataExtractor extends CfeDataExtractor<Features> {
|
|
final Library library;
|
|
final Class? cls;
|
|
|
|
ScopeDataExtractor(
|
|
this.library, this.cls, super.compilerResult, super.actualMap);
|
|
|
|
Component get component => compilerResult.component!;
|
|
|
|
@override
|
|
Features? computeNodeValue(Id id, TreeNode node) {
|
|
// We use references to a static variable 'x' as the marker for where we
|
|
// want to compute the scope.
|
|
if (node is StaticGet && node.target.name.text == 'x') {
|
|
Location? location = node.location;
|
|
if (location != null) {
|
|
List<DartScope> scopes = DartScopeBuilder2.findScopeFromOffsetAndClass(
|
|
library, location.file, cls, node.fileOffset);
|
|
if (scopes.isNotEmpty) {
|
|
// TODO(johnniwinther,jensj): Support more than one scope.
|
|
DartScope scope = scopes.first;
|
|
Features features = Features();
|
|
if (scope.cls != null) {
|
|
features[Tags.cls] = scope.cls!.name;
|
|
}
|
|
if (scope.member != null) {
|
|
features[Tags.member] = scope.member!.name.text;
|
|
}
|
|
if (scope.isStatic) {
|
|
features.add(Tags.isStatic);
|
|
}
|
|
for (TypeParameter typeParameter in scope.typeParameters) {
|
|
features.addElement(Tags.typeParameter, typeParameter.name!);
|
|
}
|
|
for (String variable in scope.definitions.keys) {
|
|
features.addElement(Tags.variables, variable);
|
|
}
|
|
return features;
|
|
}
|
|
}
|
|
}
|
|
return super.computeNodeValue(id, node);
|
|
}
|
|
}
|