Skip to content

Commit f5da6bc

Browse files
committed
Use typed _visitFunctionOrMethodDeclaration(), stop using 'name2'.
1 parent 1ee1976 commit f5da6bc

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Require `package:analyzer` `^5.1.0`.
44
* Format unnamed libraries.
55
* Require Dart 2.18.
6+
* Use typed `_visitFunctionOrMethodDeclaration` instead of dynamically typed.
67

78
# 2.2.4
89

lib/src/source_visitor.dart

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,18 @@ class SourceVisitor extends ThrowingAstVisitor {
15561556

15571557
@override
15581558
void visitFunctionDeclaration(FunctionDeclaration node) {
1559-
_visitMemberDeclaration(node, node.functionExpression);
1559+
_visitFunctionOrMethodDeclaration(
1560+
metadata: node.metadata,
1561+
externalKeyword: node.externalKeyword,
1562+
propertyKeyword: node.propertyKeyword,
1563+
modifierKeyword: null,
1564+
operatorKeyword: null,
1565+
name: node.name,
1566+
returnType: node.returnType,
1567+
typeParameters: node.functionExpression.typeParameters,
1568+
formalParameters: node.functionExpression.parameters,
1569+
body: node.functionExpression.body,
1570+
);
15601571
}
15611572

15621573
@override
@@ -2054,7 +2065,18 @@ class SourceVisitor extends ThrowingAstVisitor {
20542065

20552066
@override
20562067
void visitMethodDeclaration(MethodDeclaration node) {
2057-
_visitMemberDeclaration(node, node);
2068+
_visitFunctionOrMethodDeclaration(
2069+
metadata: node.metadata,
2070+
externalKeyword: node.externalKeyword,
2071+
propertyKeyword: node.propertyKeyword,
2072+
modifierKeyword: node.modifierKeyword,
2073+
operatorKeyword: node.operatorKeyword,
2074+
name: node.name,
2075+
returnType: node.returnType,
2076+
typeParameters: node.typeParameters,
2077+
formalParameters: node.parameters,
2078+
body: node.body,
2079+
);
20582080
}
20592081

20602082
@override
@@ -2795,41 +2817,41 @@ class SourceVisitor extends ThrowingAstVisitor {
27952817
}
27962818

27972819
/// Visits a top-level function or method declaration.
2798-
///
2799-
/// The two AST node types are very similar but, alas, share no common
2800-
/// interface type in analyzer, hence the dynamic typing.
2801-
void _visitMemberDeclaration(/* FunctionDeclaration|MethodDeclaration */ node,
2802-
/* FunctionExpression|MethodDeclaration */ function) {
2803-
visitMetadata(node.metadata as NodeList<Annotation>);
2820+
void _visitFunctionOrMethodDeclaration({
2821+
required NodeList<Annotation> metadata,
2822+
required Token? externalKeyword,
2823+
required Token? propertyKeyword,
2824+
required Token? modifierKeyword,
2825+
required Token? operatorKeyword,
2826+
required Token name,
2827+
required TypeAnnotation? returnType,
2828+
required TypeParameterList? typeParameters,
2829+
required FormalParameterList? formalParameters,
2830+
required FunctionBody body,
2831+
}) {
2832+
visitMetadata(metadata);
28042833

28052834
// Nest the signature in case we have to split between the return type and
28062835
// name.
28072836
builder.nestExpression();
28082837
builder.startSpan();
2809-
modifier(node.externalKeyword);
2810-
if (node is MethodDeclaration) modifier(node.modifierKeyword);
2811-
visit(node.returnType, after: soloSplit);
2812-
modifier(node.propertyKeyword);
2813-
if (node is MethodDeclaration) modifier(node.operatorKeyword);
2814-
token(node.name2);
2838+
modifier(externalKeyword);
2839+
modifier(modifierKeyword);
2840+
visit(returnType, after: soloSplit);
2841+
modifier(propertyKeyword);
2842+
modifier(operatorKeyword);
2843+
token(name);
28152844
builder.endSpan();
28162845

2817-
TypeParameterList? typeParameters;
2818-
if (node is FunctionDeclaration) {
2819-
typeParameters = node.functionExpression.typeParameters;
2820-
} else {
2821-
typeParameters = (node as MethodDeclaration).typeParameters;
2822-
}
2823-
2824-
_visitFunctionBody(typeParameters, function.parameters, function.body, () {
2846+
_visitFunctionBody(typeParameters, formalParameters, body, () {
28252847
// If the body is a block, we need to exit nesting before we hit the body
28262848
// indentation, but we do want to wrap it around the parameters.
2827-
if (function.body is! ExpressionFunctionBody) builder.unnest();
2849+
if (body is! ExpressionFunctionBody) builder.unnest();
28282850
});
28292851

28302852
// If it's an expression, we want to wrap the nesting around that so that
28312853
// the body gets nested farther.
2832-
if (function.body is ExpressionFunctionBody) builder.unnest();
2854+
if (body is ExpressionFunctionBody) builder.unnest();
28332855
}
28342856

28352857
/// Visit the given function [parameters] followed by its [body], printing a

0 commit comments

Comments
 (0)