[cfe] Add CFE test for DartScope

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>
This commit is contained in:
Johnni Winther 2023-11-15 10:17:23 +00:00 committed by Commit Queue
parent 3e89b9b04e
commit 4eed065859
9 changed files with 94 additions and 2 deletions

View file

@ -16,7 +16,6 @@ analyzer:
- node_modules/**
- test/codegen/**
- test/samples/**
- test/scopes/data/**
- test/transformer/hello_app/**
linter:

View file

@ -14,7 +14,8 @@ import 'package:kernel/dart_scope_calculator.dart';
import '../id_testing_helper.dart';
Future<void> main(List<String> args) async {
var dataDir = Directory.fromUri(Platform.script.resolve('data'));
var dataDir = Directory.fromUri(
Platform.script.resolve('../../../front_end/test/scopes/data'));
await runTests<Features>(dataDir,
args: args,
createUriForFileName: createUriForFileName,

View file

@ -17,6 +17,7 @@ analyzer:
- test/macros/incremental/data/**
- test/patching/data/**
- test/predicates/data/**
- test/scopes/data/**
- test/static_types/data/**
- test/text_representation/data/**
- testcases/**

View file

@ -1 +1,2 @@
cfe=pkg/front_end/test/scopes/scope_test.dart
ddc=pkg/dev_compiler/test/scopes/scope_test.dart

View file

@ -0,0 +1,90 @@
// 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);
}
}