Skip to content

Support new generic function typedef syntax. #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.2

* Support new generic function typedef syntax (#563).

# 1.0.1

* Ensure space between `-` and `--` (#170).
Expand Down
45 changes: 39 additions & 6 deletions lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,25 @@ class SourceVisitor extends ThrowingAstVisitor {
});
}

visitGenericFunctionType(GenericFunctionType node) {
visit(node.returnType, after: space);
token(node.functionKeyword);
_visitParameterSignature(node.typeParameters, node.parameters);
}

visitGenericTypeAlias(GenericTypeAlias node) {
visitNodes(node.metadata, between: newline, after: newline);
_simpleStatement(node, () {
token(node.typedefKeyword);
space();
visit(node.name);
space();
token(node.equals);
space();
visit(node.functionType);
});
}

visitHideCombinator(HideCombinator node) {
_visitCombinator(node.keyword, node.hiddenNames);
}
Expand Down Expand Up @@ -1555,7 +1574,14 @@ class SourceVisitor extends ThrowingAstVisitor {
builder.nestExpression();
modifier(node.covariantKeyword);
modifier(node.keyword);
visit(node.type, after: split);

visit(node.type);

// In function declarations and the old typedef syntax, you can have a
// parameter name without a type. In the new syntax, you can have a type
// without a name. Handle both cases.
if (node.type != null && node.identifier != null) split();

visit(node.identifier);
builder.unnest();
builder.endRule();
Expand Down Expand Up @@ -2016,6 +2042,18 @@ class SourceVisitor extends ThrowingAstVisitor {
builder.startLazyRule(new Rule(Cost.arrow));
}

_visitParameterSignature(typeParameters, parameters);

if (beforeBody != null) beforeBody();
visit(body);

if (body is ExpressionFunctionBody) builder.unnest();
}

/// Visits the type parameters (if any) and formal parameters of a method
/// declaration, function declaration, or generic function type.
void _visitParameterSignature(TypeParameterList typeParameters,
FormalParameterList parameters) {
// Start the nesting for the parameters here, so they wrap around the
// type parameters too, if any.
builder.nestExpression();
Expand All @@ -2026,11 +2064,6 @@ class SourceVisitor extends ThrowingAstVisitor {
}

builder.unnest();

if (beforeBody != null) beforeBody();
visit(body);

if (body is ExpressionFunctionBody) builder.unnest();
}

/// Visits the body statement of a `for`, `for in`, or `while` loop.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ packages:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.29.8"
version: "0.29.9"
ansicolor:
description:
name: ansicolor
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
version: 1.0.1
version: 1.0.2-dev
author: Dart Team <[email protected]>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
environment:
sdk: ">=1.8.0 <2.0.0"
dependencies:
analyzer: '^0.29.5'
analyzer: '^0.29.9'
args: '>=0.12.1 <0.14.0'
path: '>=1.0.0 <2.0.0'
source_span: '>=1.1.1 <2.0.0'
Expand Down
22 changes: 22 additions & 0 deletions test/splitting/function_types.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
40 columns |
>>> split type parameters (skip: published version of analyzer doesn't support function types yet)
typedef G = T Function<TypeOne, TypeTwo, TypeThree>();
<<<
typedef G = T Function<TypeOne, TypeTwo,
TypeThree>();
>>> split all type parameters (skip: published version of analyzer doesn't support function types yet)
typedef G = T Function<TypeOne, TypeTwo, TypeThree, TypeFour, TypeFive, TypeSix>();
<<<
typedef G = T Function<
TypeOne,
TypeTwo,
TypeThree,
TypeFour,
TypeFive,
TypeSix>();
>>> split type and value parameters (skip: published version of analyzer doesn't support function types yet)
typedef G = T Function<TypeOne, TypeTwo, TypeThree>(TypeOne one, TypeTwo two, TypeThree three);
<<<
typedef G = T Function<TypeOne, TypeTwo,
TypeThree>(TypeOne one,
TypeTwo two, TypeThree three);
28 changes: 28 additions & 0 deletions test/whitespace/function_types.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
40 columns |
>>> non-generic in typedef (skip: published version of analyzer doesn't support function types yet)
typedef SomeFunc=ReturnType Function(int param, double other);
<<<
typedef SomeFunc = ReturnType Function(
int param, double other);
>>> generic in typedef (skip: published version of analyzer doesn't support function types yet)
typedef Generic = T Function<T>(T param, double other);
<<<
typedef Generic = T Function<T>(
T param, double other);
>>> no return type (skip: published version of analyzer doesn't support function types yet)
typedef SomeFunc = Function();
<<<
typedef SomeFunc = Function();
>>> nested (skip: published version of analyzer doesn't support function types yet)
typedef SomeFunc = Function(int first, Function(int first, bool second, String third) second, String third);
<<<
typedef SomeFunc = Function(
int first,
Function(int first, bool second,
String third)
second,
String third);
>>> without param names (skip: published version of analyzer doesn't support function types yet)
typedef F = Function(int, bool, String);
<<<
typedef F = Function(int, bool, String);
7 changes: 6 additions & 1 deletion test/whitespace/metadata.unit
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,9 @@ function(@Annotation @VeryLongMetadataAnnotation covariant longParameter) {}
function(
@Annotation
@VeryLongMetadataAnnotation
covariant longParameter) {}
covariant longParameter) {}
>>> metadata on function typedef (skip: published version of analyzer doesn't support function types yet)
@foo typedef Fn = Function();
<<<
@foo
typedef Fn = Function();