-
Notifications
You must be signed in to change notification settings - Fork 127
Allow for anonymous typed functions #1506
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
Changes from all commits
bee012e
4d8a8c9
dd7cf66
6040a09
b79efa2
05ddca0
6096113
9580743
f21a28b
7c57b80
071b199
386c8a3
2f16d26
588dc93
7c98297
dedf617
c22ce9b
11573b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,42 +21,38 @@ class ElementType { | |
|
||
bool get isFunctionType => (_type is FunctionType); | ||
|
||
bool get isParameterizedType { | ||
if (_type is FunctionType) { | ||
return typeArguments.isNotEmpty; | ||
} else if (_type is ParameterizedType) { | ||
return (_type as ParameterizedType).typeArguments.isNotEmpty; | ||
} | ||
return false; | ||
} | ||
bool get isParameterizedType => (_type is ParameterizedType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches the above, but the parans could be dropped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dart style guide has no opinion, so leaving to match style. |
||
|
||
bool get isParameterType => (_type is TypeParameterType); | ||
|
||
String get linkedName { | ||
if (_linkedName != null) return _linkedName; | ||
|
||
StringBuffer buf = new StringBuffer(); | ||
|
||
if (isParameterType) { | ||
buf.write(name); | ||
} else { | ||
buf.write(element.linkedName); | ||
} | ||
if (_linkedName == null) { | ||
StringBuffer buf = new StringBuffer(); | ||
|
||
// not TypeParameterType or Void or Union type | ||
if (isParameterizedType) { | ||
if (typeArguments.every((t) => t.linkedName == 'dynamic')) { | ||
_linkedName = buf.toString(); | ||
return _linkedName; | ||
if (isParameterType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all code from before that was indented. Leaving. |
||
buf.write(name); | ||
} else { | ||
buf.write(element.linkedName); | ||
} | ||
if (typeArguments.isNotEmpty) { | ||
buf.write('<'); | ||
buf.writeAll(typeArguments.map((t) => t.linkedName), ', '); | ||
buf.write('>'); | ||
|
||
// not TypeParameterType or Void or Union type | ||
if (isParameterizedType) { | ||
if (!typeArguments.every((t) => t.linkedName == 'dynamic') && | ||
typeArguments.isNotEmpty) { | ||
buf.write('<'); | ||
buf.writeAll(typeArguments.map((t) => t.linkedName), ', '); | ||
buf.write('>'); | ||
} | ||
// Hide parameters if there's a an explicit typedef behind this | ||
// element, but if there is no typedef, be explicit. | ||
if (element is ModelFunctionAnonymous) { | ||
buf.write('('); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you're avoiding string interpolation here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to maintain a consistent style with what was previously there. |
||
buf.write(element.linkedParams()); | ||
buf.write(')'); | ||
} | ||
} | ||
_linkedName = buf.toString(); | ||
} | ||
_linkedName = buf.toString(); | ||
|
||
return _linkedName; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -769,7 +769,7 @@ class Class extends ModelElement implements EnclosedElement { | |
|
||
@override | ||
String get nameWithGenerics { | ||
if (!modelType.isParameterizedType) return name; | ||
if (!modelType.isParameterizedType || _typeParameters.isEmpty) return name; | ||
return '$name<${_typeParameters.map((t) => t.name).join(', ')}>'; | ||
} | ||
|
||
|
@@ -2216,6 +2216,14 @@ abstract class ModelElement extends Nameable | |
} | ||
if (e is FunctionElement) { | ||
newModelElement = new ModelFunction(e, library); | ||
} else if (e is GenericFunctionTypeElement) { | ||
if (e is FunctionTypeAliasElement) { | ||
assert(e.name != ''); | ||
newModelElement = new ModelFunctionTypedef(e, library); | ||
} else { | ||
assert(e.name == ''); | ||
newModelElement = new ModelFunctionAnonymous(e, library); | ||
} | ||
} | ||
if (e is FunctionTypeAliasElement) { | ||
newModelElement = new Typedef(e, library); | ||
|
@@ -2285,10 +2293,7 @@ abstract class ModelElement extends Nameable | |
} | ||
} | ||
} | ||
// TODO(jcollins-g): Consider subclass for ModelFunctionTyped. | ||
if (e is GenericFunctionTypeElement) { | ||
newModelElement = new ModelFunctionTyped(e, library); | ||
} | ||
|
||
|
||
if (newModelElement == null) throw "Unknown type ${e.runtimeType}"; | ||
if (enclosingClass != null) assert(newModelElement is Inheritable); | ||
|
@@ -3203,6 +3208,7 @@ abstract class ModelElement extends Nameable | |
} | ||
} | ||
|
||
/// A [ModelElement] for a [FunctionElement] that isn't part of a type definition. | ||
class ModelFunction extends ModelFunctionTyped { | ||
ModelFunction(FunctionElement element, Library library) | ||
: super(element, library); | ||
|
@@ -3212,10 +3218,52 @@ class ModelFunction extends ModelFunctionTyped { | |
return _func.isStatic; | ||
} | ||
|
||
@override | ||
String get name { | ||
if (element.enclosingElement is ParameterElement && super.name.isEmpty) | ||
return element.enclosingElement.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure of the context of this code, so it might not matter, but parameters are no longer required to have a name (when inside a generic function type). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's OK here. With this CL, it's now more OK than it used to be, in fact. |
||
return super.name; | ||
} | ||
|
||
@override | ||
FunctionElement get _func => (element as FunctionElement); | ||
} | ||
|
||
/// A [ModelElement] for a [GenericModelFunctionElement] that is an | ||
/// explicit typedef. | ||
/// | ||
/// Distinct from ModelFunctionTypedef in that it doesn't | ||
/// have a name, but we document it as "Function" to match how these are | ||
/// written in declarations. | ||
class ModelFunctionAnonymous extends ModelFunctionTyped { | ||
ModelFunctionAnonymous(FunctionTypedElement element, Library library) | ||
: super(element, library) {} | ||
|
||
@override | ||
String get name => 'Function'; | ||
|
||
@override | ||
bool get isPublic => false; | ||
} | ||
|
||
/// A [ModelElement] for a [GenericModelFunctionElement] that is part of an | ||
/// explicit typedef. | ||
class ModelFunctionTypedef extends ModelFunctionTyped { | ||
ModelFunctionTypedef(FunctionTypedElement element, Library library) | ||
: super(element, library) {} | ||
|
||
@override | ||
String get name { | ||
Element e = element; | ||
while (e != null) { | ||
if (e is FunctionTypeAliasElement) return e.name; | ||
e = e.enclosingElement; | ||
} | ||
assert(false); | ||
return super.name; | ||
} | ||
} | ||
|
||
class ModelFunctionTyped extends ModelElement | ||
with SourceCodeMixin | ||
implements EnclosedElement { | ||
|
@@ -3238,13 +3286,6 @@ class ModelFunctionTyped extends ModelElement | |
|
||
String get fileName => "$name.html"; | ||
|
||
@override | ||
String get name { | ||
if (element.enclosingElement is ParameterElement && super.name.isEmpty) | ||
return element.enclosingElement.name; | ||
return super.name; | ||
} | ||
|
||
@override | ||
String get href { | ||
if (canonicalLibrary == null) return null; | ||
|
@@ -4590,7 +4631,7 @@ class Typedef extends ModelElement | |
|
||
@override | ||
String get nameWithGenerics { | ||
if (!modelType.isParameterizedType) return name; | ||
if (!modelType.isParameterizedType || _typeParameters.isEmpty) return name; | ||
return '$name<${_typeParameters.map((t) => t.name).join(', ')}>'; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(cd testing/test_package; pub get)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Windows, so that won't work. I deliberately wrote this in the most generic way possible for clarity.