Stop using AstTestFactory and remove it.

Change-Id: Ic0dd0794c2ed3045149d2605c340852b3f187a28
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265184
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-10-24 16:08:12 +00:00 committed by Commit Queue
parent 3fe9cb8bae
commit 79b11174a2
5 changed files with 111 additions and 330 deletions

View file

@ -13252,14 +13252,11 @@ class TypeParameterImpl extends DeclarationImpl implements TypeParameter {
@override
Token get endToken {
if (_bound == null) {
return name;
}
return _bound!.endToken;
return _bound?.endToken ?? name;
}
@override
Token get firstTokenAfterCommentAndMetadata => name;
Token get firstTokenAfterCommentAndMetadata => varianceKeyword ?? name;
@Deprecated('Use name instead')
@override

View file

@ -1,253 +0,0 @@
// Copyright (c) 2014, 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/ast/token.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/ast_factory.dart';
import 'package:analyzer/src/generated/testing/token_factory.dart';
import 'package:meta/meta.dart';
/// The class `AstTestFactory` defines utility methods that can be used to
/// create AST nodes. The nodes that are created are complete in the sense that
/// all of the tokens that would have been
/// associated with the nodes by a parser are also created, but the token stream
/// is not constructed. None of the nodes are resolved.
///
/// The general pattern is for the name of the factory method to be the same as
/// the name of the class of AST node being created. There are two notable
/// exceptions. The first is for methods creating nodes that are part of a
/// cascade expression. These methods are all prefixed with 'cascaded'. The
/// second is places where a shorter name seemed unambiguous and easier to read,
/// such as using 'identifier' rather than 'prefixedIdentifier', or 'integer'
/// rather than 'integerLiteral'.
@internal
class AstTestFactory {
static AssignmentExpressionImpl assignmentExpression(Expression leftHandSide,
TokenType operator, Expression rightHandSide) =>
AssignmentExpressionImpl(
leftHandSide: leftHandSide as ExpressionImpl,
operator: TokenFactory.tokenFromType(operator),
rightHandSide: rightHandSide as ExpressionImpl,
);
static ConstructorDeclarationImpl constructorDeclaration(
Identifier returnType,
String? name,
FormalParameterList parameters,
List<ConstructorInitializer> initializers) =>
ConstructorDeclarationImpl(
comment: null,
metadata: null,
externalKeyword: TokenFactory.tokenFromKeyword(Keyword.EXTERNAL),
constKeyword: null,
factoryKeyword: null,
returnType: returnType as IdentifierImpl,
period:
name == null ? null : TokenFactory.tokenFromType(TokenType.PERIOD),
name: name == null ? null : identifier3(name).token,
parameters: parameters as FormalParameterListImpl,
separator: initializers.isEmpty
? null
: TokenFactory.tokenFromType(TokenType.PERIOD),
initializers: initializers,
redirectedConstructor: null,
body: EmptyFunctionBodyImpl(
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
),
);
static CommentImpl documentationComment(
List<Token> tokens, List<CommentReference> references) {
return astFactory.documentationComment(tokens, references);
}
static ExtensionDeclarationImpl extensionDeclaration(
{required String name,
required bool isExtensionTypeDeclaration,
TypeParameterList? typeParameters,
required TypeAnnotation extendedType,
ShowClause? showClause,
HideClause? hideClause,
List<ClassMember> members = const []}) =>
ExtensionDeclarationImpl(
comment: null,
metadata: null,
extensionKeyword: TokenFactory.tokenFromKeyword(Keyword.EXTENSION),
typeKeyword: isExtensionTypeDeclaration
? TokenFactory.tokenFromString('type')
: null,
name: identifier3(name).token,
typeParameters: typeParameters as TypeParameterListImpl?,
onKeyword: TokenFactory.tokenFromKeyword(Keyword.ON),
extendedType: extendedType as TypeAnnotationImpl,
showClause: showClause as ShowClauseImpl?,
hideClause: hideClause as HideClauseImpl?,
leftBracket: TokenFactory.tokenFromType(TokenType.OPEN_CURLY_BRACKET),
members: members,
rightBracket: TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET),
);
static FieldDeclarationImpl fieldDeclaration(bool isStatic, Keyword? keyword,
TypeAnnotation? type, List<VariableDeclaration> variables,
{bool isAbstract = false, bool isExternal = false}) =>
FieldDeclarationImpl(
comment: null,
metadata: null,
augmentKeyword: null,
covariantKeyword: null,
abstractKeyword:
isAbstract ? TokenFactory.tokenFromKeyword(Keyword.ABSTRACT) : null,
externalKeyword:
isExternal ? TokenFactory.tokenFromKeyword(Keyword.EXTERNAL) : null,
staticKeyword:
isStatic ? TokenFactory.tokenFromKeyword(Keyword.STATIC) : null,
fieldList: variableDeclarationList(keyword, type, variables),
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
);
static FieldDeclarationImpl fieldDeclaration2(bool isStatic, Keyword? keyword,
List<VariableDeclaration> variables) =>
fieldDeclaration(isStatic, keyword, null, variables);
static SimpleIdentifierImpl identifier3(String lexeme) =>
astFactory.simpleIdentifier(
TokenFactory.tokenFromTypeAndString(TokenType.IDENTIFIER, lexeme));
static List<SimpleIdentifier> identifierList(List<String> identifiers) {
return identifiers
.map((String identifier) => identifier3(identifier))
.toList();
}
static LibraryDirectiveImpl libraryDirective(
List<Annotation> metadata, LibraryIdentifier? libraryName) =>
LibraryDirectiveImpl(
comment: null,
metadata: metadata,
libraryKeyword: TokenFactory.tokenFromKeyword(Keyword.LIBRARY),
name: libraryName as LibraryIdentifierImpl?,
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
);
static List list(List<Object> elements) {
return elements;
}
static MethodDeclarationImpl methodDeclaration(
Keyword? modifier,
TypeAnnotation? returnType,
Keyword? property,
Keyword? operator,
SimpleIdentifier name,
FormalParameterList? parameters) =>
MethodDeclarationImpl(
comment: null,
metadata: null,
externalKeyword: TokenFactory.tokenFromKeyword(Keyword.EXTERNAL),
modifierKeyword:
modifier == null ? null : TokenFactory.tokenFromKeyword(modifier),
returnType: returnType as TypeAnnotationImpl?,
propertyKeyword:
property == null ? null : TokenFactory.tokenFromKeyword(property),
operatorKeyword:
operator == null ? null : TokenFactory.tokenFromKeyword(operator),
name: name.token,
typeParameters: null,
parameters: parameters as FormalParameterListImpl?,
body: EmptyFunctionBodyImpl(
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
),
);
static PartOfDirectiveImpl partOfDirective(LibraryIdentifier libraryName) =>
partOfDirective2(<Annotation>[], libraryName);
static PartOfDirectiveImpl partOfDirective2(
List<Annotation> metadata, LibraryIdentifier libraryName) =>
PartOfDirectiveImpl(
comment: null,
metadata: metadata,
partKeyword: TokenFactory.tokenFromKeyword(Keyword.PART),
ofKeyword: TokenFactory.tokenFromString("of"),
uri: null,
libraryName: libraryName as LibraryIdentifierImpl,
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
);
static TopLevelVariableDeclarationImpl topLevelVariableDeclaration(
Keyword? keyword,
TypeAnnotation? type,
List<VariableDeclaration> variables) =>
TopLevelVariableDeclarationImpl(
comment: null,
metadata: null,
externalKeyword: null,
variableList: variableDeclarationList(keyword, type, variables),
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
);
static TopLevelVariableDeclarationImpl topLevelVariableDeclaration2(
Keyword? keyword, List<VariableDeclaration> variables,
{bool isExternal = false}) =>
TopLevelVariableDeclarationImpl(
comment: null,
metadata: null,
variableList: variableDeclarationList(keyword, null, variables),
semicolon: TokenFactory.tokenFromType(TokenType.SEMICOLON),
externalKeyword:
isExternal ? TokenFactory.tokenFromKeyword(Keyword.EXTERNAL) : null,
);
static TypeParameterImpl typeParameter(String name) => TypeParameterImpl(
comment: null,
metadata: null,
name: identifier3(name).token,
extendsKeyword: null,
bound: null,
);
static TypeParameterImpl typeParameter3(String name, String varianceLexeme) =>
// TODO (kallentu) : Clean up AstFactoryImpl casting once variance is
// added to the interface.
TypeParameterImpl(
comment: null,
metadata: null,
name: identifier3(name).token,
extendsKeyword: null,
bound: null,
varianceKeyword: TokenFactory.tokenFromString(varianceLexeme),
);
static VariableDeclarationImpl variableDeclaration(String name) =>
VariableDeclarationImpl(
name: identifier3(name).token,
equals: null,
initializer: null,
);
static VariableDeclarationImpl variableDeclaration2(
String name, Expression initializer) =>
VariableDeclarationImpl(
name: identifier3(name).token,
equals: TokenFactory.tokenFromType(TokenType.EQ),
initializer: initializer as ExpressionImpl,
);
static VariableDeclarationListImpl variableDeclarationList(Keyword? keyword,
TypeAnnotation? type, List<VariableDeclaration> variables) =>
VariableDeclarationListImpl(
comment: null,
metadata: null,
lateKeyword: null,
keyword:
keyword == null ? null : TokenFactory.tokenFromKeyword(keyword),
type: type as TypeAnnotationImpl?,
variables: variables,
);
static VariableDeclarationListImpl variableDeclarationList2(
Keyword? keyword, List<VariableDeclaration> variables) =>
variableDeclarationList(keyword, null, variables);
}

View file

@ -6,10 +6,8 @@ import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/ast_factory.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
import 'package:analyzer/src/generated/testing/ast_test_factory.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -1809,31 +1807,22 @@ class A {
}
@reflectiveTest
class VariableDeclarationTest extends ParserTestCase {
class VariableDeclarationTest extends _AstTest {
void test_getDocumentationComment_onGrandParent() {
VariableDeclaration varDecl = AstTestFactory.variableDeclaration("a");
var decl =
AstTestFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]);
Comment comment = astFactory.documentationComment([]);
expect(varDecl.documentationComment, isNull);
decl.documentationComment = comment;
expect(varDecl.documentationComment, isNotNull);
expect(decl.documentationComment, isNotNull);
}
void test_getDocumentationComment_onNode() {
var decl = AstTestFactory.variableDeclaration("a");
Comment comment = astFactory.documentationComment([]);
decl.documentationComment = comment;
expect(decl.documentationComment, isNotNull);
var parseResult = parseStringWithErrors(r'''
/// text
var a = 0;
''');
parseResult.assertNoErrors();
var node = parseResult.findNode.variableDeclaration('a =');
expect(node.documentationComment, isNotNull);
}
test_sortedCommentAndAnnotations_noComment() {
var result = parseString(content: '''
int i = 0;
var parseResult = parseStringWithErrors('''
var a = 0;
''');
var variables = result.unit.declarations[0] as TopLevelVariableDeclaration;
var variable = variables.variables.variables[0];
var variable = parseResult.findNode.variableDeclaration('a =');
expect(variable.sortedCommentAndAnnotations, isEmpty);
}
}

View file

@ -2,12 +2,12 @@
// 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/analysis/features.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/ast_factory.dart';
import 'package:analyzer/src/dart/ast/to_source_visitor.dart';
import 'package:analyzer/src/generated/testing/ast_test_factory.dart';
import 'package:analyzer/src/generated/testing/token_factory.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:test/test.dart';
@ -104,10 +104,13 @@ void f() {
}
void test_visitAssignmentExpression() {
_assertSource(
"a = b",
AstTestFactory.assignmentExpression(AstTestFactory.identifier3("a"),
TokenType.EQ, AstTestFactory.identifier3("b")));
final code = 'a = b';
final findNode = _parseStringToFindNode('''
void f() {
$code;
}
''');
_assertSource(code, findNode.assignment(code));
}
void test_visitAugmentationImportDirective() {
@ -1299,33 +1302,43 @@ void f(x) {
}
void test_visitFieldDeclaration_abstract() {
_assertSource(
"abstract var a;",
AstTestFactory.fieldDeclaration(
false, Keyword.VAR, null, [AstTestFactory.variableDeclaration("a")],
isAbstract: true));
final code = 'abstract var a;';
final findNode = _parseStringToFindNode('''
class A {
$code
}
''');
_assertSource(code, findNode.fieldDeclaration(code));
}
void test_visitFieldDeclaration_external() {
_assertSource(
"external var a;",
AstTestFactory.fieldDeclaration(
false, Keyword.VAR, null, [AstTestFactory.variableDeclaration("a")],
isExternal: true));
final code = 'external var a;';
final findNode = _parseStringToFindNode('''
class A {
$code
}
''');
_assertSource(code, findNode.fieldDeclaration(code));
}
void test_visitFieldDeclaration_instance() {
_assertSource(
"var a;",
AstTestFactory.fieldDeclaration2(
false, Keyword.VAR, [AstTestFactory.variableDeclaration("a")]));
final code = 'var a;';
final findNode = _parseStringToFindNode('''
class A {
$code
}
''');
_assertSource(code, findNode.fieldDeclaration(code));
}
void test_visitFieldDeclaration_static() {
_assertSource(
"static var a;",
AstTestFactory.fieldDeclaration2(
true, Keyword.VAR, [AstTestFactory.variableDeclaration("a")]));
final code = 'static var a;';
final findNode = _parseStringToFindNode('''
class A {
$code
}
''');
_assertSource(code, findNode.fieldDeclaration(code));
}
void test_visitFieldDeclaration_withMetadata() {
@ -3163,7 +3176,11 @@ class A {
}
void test_visitSimpleIdentifier() {
_assertSource("a", AstTestFactory.identifier3("a"));
final code = 'foo';
final findNode = _parseStringToFindNode('''
var x = $code;
''');
_assertSource(code, findNode.simple(code));
}
void test_visitSimpleStringLiteral() {
@ -3520,27 +3537,27 @@ void f() {
}
void test_visitTopLevelVariableDeclaration_external() {
_assertSource(
"external var a;",
AstTestFactory.topLevelVariableDeclaration2(
Keyword.VAR, [AstTestFactory.variableDeclaration("a")],
isExternal: true));
final code = 'external var a;';
final findNode = _parseStringToFindNode('''
$code
''');
_assertSource(code, findNode.topLevelVariableDeclaration(code));
}
void test_visitTopLevelVariableDeclaration_multiple() {
_assertSource(
"var a;",
AstTestFactory.topLevelVariableDeclaration2(
Keyword.VAR, [AstTestFactory.variableDeclaration("a")]));
final code = 'var a = 0, b = 1;';
final findNode = _parseStringToFindNode('''
$code
''');
_assertSource(code, findNode.topLevelVariableDeclaration(code));
}
void test_visitTopLevelVariableDeclaration_single() {
_assertSource(
"var a, b;",
AstTestFactory.topLevelVariableDeclaration2(Keyword.VAR, [
AstTestFactory.variableDeclaration("a"),
AstTestFactory.variableDeclaration("b")
]));
final code = 'var a;';
final findNode = _parseStringToFindNode('''
$code
''');
_assertSource(code, findNode.topLevelVariableDeclaration(code));
}
void test_visitTryStatement_catch() {
@ -3648,15 +3665,27 @@ final x = <$code>[];
}
void test_visitTypeParameter_variance_contravariant() {
_assertSource("in E", AstTestFactory.typeParameter3("E", "in"));
final code = 'in T';
final findNode = _parseStringToFindNode('''
class A<$code> {}
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.typeParameter(code));
}
void test_visitTypeParameter_variance_covariant() {
_assertSource("out E", AstTestFactory.typeParameter3("E", "out"));
final code = 'out T';
final findNode = _parseStringToFindNode('''
class A<$code> {}
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.typeParameter(code));
}
void test_visitTypeParameter_variance_invariant() {
_assertSource("inout E", AstTestFactory.typeParameter3("E", "inout"));
final code = 'inout T';
final findNode = _parseStringToFindNode('''
class A<$code> {}
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.typeParameter(code));
}
void test_visitTypeParameter_withExtends() {
@ -3676,7 +3705,11 @@ class A<$code> {}
}
void test_visitTypeParameter_withoutExtends() {
_assertSource("E", AstTestFactory.typeParameter("E"));
final code = 'T';
final findNode = _parseStringToFindNode('''
class A<$code> {}
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.typeParameter(code));
}
void test_visitTypeParameterList_multiple() {
@ -3696,14 +3729,19 @@ class A$code {}
}
void test_visitVariableDeclaration_initialized() {
_assertSource(
"a = b",
AstTestFactory.variableDeclaration2(
"a", AstTestFactory.identifier3("b")));
final code = 'foo = bar';
final findNode = _parseStringToFindNode('''
var $code;
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.variableDeclaration(code));
}
void test_visitVariableDeclaration_uninitialized() {
_assertSource("a", AstTestFactory.variableDeclaration("a"));
final code = 'foo';
final findNode = _parseStringToFindNode('''
var $code;
''', featureSet: FeatureSets.latestWithVariance);
_assertSource(code, findNode.variableDeclaration(code));
}
void test_visitVariableDeclarationList_const_type() {
@ -3816,10 +3854,13 @@ void f() sync* {
}
/// TODO(scheglov) Use [parseStringWithErrors] everywhere? Or just there?
FindNode _parseStringToFindNode(String content) {
FindNode _parseStringToFindNode(
String content, {
FeatureSet? featureSet,
}) {
var parseResult = parseString(
content: content,
featureSet: FeatureSets.latestWithExperiments,
featureSet: featureSet ?? FeatureSets.latestWithExperiments,
);
return FindNode(parseResult.content, parseResult.unit);
}

View file

@ -53,5 +53,12 @@ class FeatureSets {
],
);
static final FeatureSet latestWithVariance = FeatureSet.fromEnableFlags2(
sdkLanguageVersion: ExperimentStatus.currentVersion,
flags: [
EnableString.variance,
],
);
FeatureSets._();
}