Skip to content

Commit

Permalink
Fixed FunctionTypeAlias with annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggnstone committed Jul 19, 2024
1 parent ad2e414 commit a134c44
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/src/Formatters/FunctionTypeAliasFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class FunctionTypeAliasFormatter extends IFormatter
@override
void format(AstNode node)
{
const String methodName = 'ForPartsWithExpressionFormatter.format';
const String methodName = 'FunctionTypeAliasFormatter.format';
if (Constants.DEBUG_I_FORMATTER) log('START $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', formatState.logIndent++);

if (node is! FunctionTypeAlias)
throw FormatException('Not a FunctionTypeAlias: ${node.runtimeType}');

formatState.acceptList(node.sortedCommentAndAnnotations, astVisitor, '$methodName/node.sortedCommentAndAnnotations');
formatState.copyEntity(node.typedefKeyword, astVisitor, '$methodName/node.typedefKeyword');
formatState.copyEntity(node.returnType, astVisitor, '$methodName/node.returnType');
formatState.copyEntity(node.name, astVisitor, '$methodName/node.name');
Expand Down
34 changes: 34 additions & 0 deletions test/Formatters/FunctionTypeAliasFormatter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dart_format/src/Formatters/FunctionTypeAliasFormatter.dart';

import '../TestTools/AstCreator.dart';
import '../TestTools/TestConfig.dart';
import '../TestTools/TestGroupConfig.dart';
import '../TestTools/TestTools.dart';
import '../TestTools/Visitors/TestVisitor.dart';

void main()
{
TestTools.init();

final List<TestGroupConfig> testGroupConfigs = <TestGroupConfig>[
TestGroupConfig(
inputNodeCreator: AstCreator.createFunctionTypeAlias,
inputLeading: '',
inputMiddle: '@a typedef void f();',
inputTrailing: '',
name: 'FunctionTypeAliasFormatter @a typedef void f();',
astVisitors: <TestVisitor<void>>[
TestVisitor<Annotation>(0, '@a'),
TestVisitor<NamedType>(11, 'void'),
TestVisitor<FormalParameterList>(17, '()')
],
testConfigs: <TestConfig>[
TestConfig.none(),
TestConfig('@a typedef void f();\n')
]
)
];

TestTools.runTestGroupsForFormatter(testGroupConfigs, 'FunctionTypeAliasFormatter', FunctionTypeAliasFormatter.new, StackTrace.current);
}
3 changes: 3 additions & 0 deletions test/TestTools/AstCreator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class AstCreator
static FieldDeclaration createFieldDeclarationInClass(String s)
=> createClassMember(s) as FieldDeclaration;

static FunctionTypeAlias createFunctionTypeAlias(String s)
=> createDeclaration(s) as FunctionTypeAlias;

static Expression createInitializerInVariableDeclarationInClass(String s)
=> createVariableDeclarationInClass(s).initializer!;

Expand Down

0 comments on commit a134c44

Please sign in to comment.