Skip to content

Commit 496f21f

Browse files
committed
Support new generic function typedef syntax.
Fix #563.
1 parent 2c216d8 commit 496f21f

File tree

7 files changed

+102
-10
lines changed

7 files changed

+102
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.2
2+
3+
* Support new generic function typedef syntax (#563).
4+
15
# 1.0.1
26

37
* Ensure space between `-` and `--` (#170).

lib/src/source_visitor.dart

+39-6
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,25 @@ class SourceVisitor extends ThrowingAstVisitor {
11811181
});
11821182
}
11831183

1184+
visitGenericFunctionType(GenericFunctionType node) {
1185+
visit(node.returnType, after: space);
1186+
token(node.functionKeyword);
1187+
_visitParameterSignature(node.typeParameters, node.parameters);
1188+
}
1189+
1190+
visitGenericTypeAlias(GenericTypeAlias node) {
1191+
visitNodes(node.metadata, between: newline, after: newline);
1192+
_simpleStatement(node, () {
1193+
token(node.typedefKeyword);
1194+
space();
1195+
visit(node.name);
1196+
space();
1197+
token(node.equals);
1198+
space();
1199+
visit(node.functionType);
1200+
});
1201+
}
1202+
11841203
visitHideCombinator(HideCombinator node) {
11851204
_visitCombinator(node.keyword, node.hiddenNames);
11861205
}
@@ -1555,7 +1574,14 @@ class SourceVisitor extends ThrowingAstVisitor {
15551574
builder.nestExpression();
15561575
modifier(node.covariantKeyword);
15571576
modifier(node.keyword);
1558-
visit(node.type, after: split);
1577+
1578+
visit(node.type);
1579+
1580+
// In function declarations and the old typedef syntax, you can have a
1581+
// parameter name without a type. In the new syntax, you can have a type
1582+
// without a name. Handle both cases.
1583+
if (node.type != null && node.identifier != null) split();
1584+
15591585
visit(node.identifier);
15601586
builder.unnest();
15611587
builder.endRule();
@@ -2016,6 +2042,18 @@ class SourceVisitor extends ThrowingAstVisitor {
20162042
builder.startLazyRule(new Rule(Cost.arrow));
20172043
}
20182044

2045+
_visitParameterSignature(typeParameters, parameters);
2046+
2047+
if (beforeBody != null) beforeBody();
2048+
visit(body);
2049+
2050+
if (body is ExpressionFunctionBody) builder.unnest();
2051+
}
2052+
2053+
/// Visits the type parameters (if any) and formal parameters of a method
2054+
/// declaration, function declaration, or generic function type.
2055+
void _visitParameterSignature(TypeParameterList typeParameters,
2056+
FormalParameterList parameters) {
20192057
// Start the nesting for the parameters here, so they wrap around the
20202058
// type parameters too, if any.
20212059
builder.nestExpression();
@@ -2026,11 +2064,6 @@ class SourceVisitor extends ThrowingAstVisitor {
20262064
}
20272065

20282066
builder.unnest();
2029-
2030-
if (beforeBody != null) beforeBody();
2031-
visit(body);
2032-
2033-
if (body is ExpressionFunctionBody) builder.unnest();
20342067
}
20352068

20362069
/// Visits the body statement of a `for`, `for in`, or `while` loop.

pubspec.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ packages:
66
name: analyzer
77
url: "https://pub.dartlang.org"
88
source: hosted
9-
version: "0.29.8"
9+
version: "0.29.9"
1010
ansicolor:
1111
description:
1212
name: ansicolor

pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 1.0.1
3+
version: 1.0.2-dev
44
author: Dart Team <[email protected]>
55
description: Opinionated, automatic Dart source code formatter.
66
homepage: https://github.com/dart-lang/dart_style
77
environment:
88
sdk: ">=1.8.0 <2.0.0"
99
dependencies:
10-
analyzer: '^0.29.5'
10+
analyzer: '^0.29.9'
1111
args: '>=0.12.1 <0.14.0'
1212
path: '>=1.0.0 <2.0.0'
1313
source_span: '>=1.1.1 <2.0.0'

test/splitting/function_types.unit

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
40 columns |
2+
>>> split type parameters (skip: published version of analyzer doesn't support function types yet)
3+
typedef G = T Function<TypeOne, TypeTwo, TypeThree>();
4+
<<<
5+
typedef G = T Function<TypeOne, TypeTwo,
6+
TypeThree>();
7+
>>> split all type parameters (skip: published version of analyzer doesn't support function types yet)
8+
typedef G = T Function<TypeOne, TypeTwo, TypeThree, TypeFour, TypeFive, TypeSix>();
9+
<<<
10+
typedef G = T Function<
11+
TypeOne,
12+
TypeTwo,
13+
TypeThree,
14+
TypeFour,
15+
TypeFive,
16+
TypeSix>();
17+
>>> split type and value parameters (skip: published version of analyzer doesn't support function types yet)
18+
typedef G = T Function<TypeOne, TypeTwo, TypeThree>(TypeOne one, TypeTwo two, TypeThree three);
19+
<<<
20+
typedef G = T Function<TypeOne, TypeTwo,
21+
TypeThree>(TypeOne one,
22+
TypeTwo two, TypeThree three);

test/whitespace/function_types.unit

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
40 columns |
2+
>>> non-generic in typedef (skip: published version of analyzer doesn't support function types yet)
3+
typedef SomeFunc=ReturnType Function(int param, double other);
4+
<<<
5+
typedef SomeFunc = ReturnType Function(
6+
int param, double other);
7+
>>> generic in typedef (skip: published version of analyzer doesn't support function types yet)
8+
typedef Generic = T Function<T>(T param, double other);
9+
<<<
10+
typedef Generic = T Function<T>(
11+
T param, double other);
12+
>>> no return type (skip: published version of analyzer doesn't support function types yet)
13+
typedef SomeFunc = Function();
14+
<<<
15+
typedef SomeFunc = Function();
16+
>>> nested (skip: published version of analyzer doesn't support function types yet)
17+
typedef SomeFunc = Function(int first, Function(int first, bool second, String third) second, String third);
18+
<<<
19+
typedef SomeFunc = Function(
20+
int first,
21+
Function(int first, bool second,
22+
String third)
23+
second,
24+
String third);
25+
>>> without param names (skip: published version of analyzer doesn't support function types yet)
26+
typedef F = Function(int, bool, String);
27+
<<<
28+
typedef F = Function(int, bool, String);

test/whitespace/metadata.unit

+6-1
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,9 @@ function(@Annotation @VeryLongMetadataAnnotation covariant longParameter) {}
253253
function(
254254
@Annotation
255255
@VeryLongMetadataAnnotation
256-
covariant longParameter) {}
256+
covariant longParameter) {}
257+
>>> metadata on function typedef (skip: published version of analyzer doesn't support function types yet)
258+
@foo typedef Fn = Function();
259+
<<<
260+
@foo
261+
typedef Fn = Function();

0 commit comments

Comments
 (0)