Skip to content
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

Upgrade to later analyzer package, which no longer support isUndefined #67

Merged
merged 15 commits into from
May 9, 2020
Merged
191 changes: 99 additions & 92 deletions code_gen_tester/pubspec.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions code_gen_tester/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: code_gen_tester
version: 0.0.1
environment:
sdk: '>=2.0.0-dev.68.0 <3.0.0'
sdk: ^2.5.0
dependencies:
truongsinh marked this conversation as resolved.
Show resolved Hide resolved
source_gen: ^0.9.4+1
build: '>=1.1.2 <2.0.0'
analyzer: '>=0.35.4 <0.36.0'
test: ^1.5.3
dart_style: '>=1.2.4 <1.2.5'
pedantic: 1.4.0
build_runner: 1.3.1
build_test: ^0.10.4
source_gen: ^0.9.5
build: ^1.2.2
analyzer: ^0.39.8
test: ^1.14.3
dart_style: ^1.3.6
pedantic: ^1.9.0
build_runner: ^1.9.0
build_test: ^1.0.0
10 changes: 5 additions & 5 deletions functional_widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Your `pubspec.yaml` should looks like:

```yaml
dependencies:
functional_widget_annotation: ^0.5.0
functional_widget_annotation: ^0.5.1

builders:
functional_widget: ^0.6.0
functional_widget: ^0.7.2
```

That's it! Flutter will automatically run the code generator when executing `flutter build`, `flutter run` or similar.
Expand All @@ -103,11 +103,11 @@ First add the following to your `pubspec.yaml`:

```yaml
dependencies:
functional_widget_annotation: ^0.5.0
functional_widget_annotation: ^0.5.1

dev_dependencies:
functional_widget: ^0.6.0
build_runner: ^1.3.1
functional_widget: ^0.7.2
build_runner: ^1.9.0
```

Then to run the generator, you must use `build_runner`:
Expand Down
6 changes: 3 additions & 3 deletions functional_widget/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
version: "1.14.12"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -19,7 +19,7 @@ packages:
name: functional_widget_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0"
version: "0.5.1"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -47,4 +47,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.5.0 <3.0.0"
6 changes: 3 additions & 3 deletions functional_widget/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: functional_widget_example
description: A new Flutter project.
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: ^2.5.0

dependencies:
flutter:
sdk: flutter
functional_widget_annotation: ^0.5.0
functional_widget_annotation: ^0.5.1

builders:
functional_widget: ^0.6.0
functional_widget: ^0.7.2
16 changes: 0 additions & 16 deletions functional_widget/lib/findBeginToken.dart

This file was deleted.

39 changes: 25 additions & 14 deletions functional_widget/lib/function_to_widget_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:functional_widget/findBeginToken.dart';
import 'package:functional_widget/src/parameters.dart';
import 'package:functional_widget/src/utils.dart';
import 'package:functional_widget_annotation/functional_widget_annotation.dart';
Expand Down Expand Up @@ -64,7 +63,9 @@ class FunctionalWidgetGenerator
if (function.isAsynchronous ||
function.isExternal ||
function.isGenerator ||
function.returnType?.displayName != 'Widget') {
function.returnType?.getDisplayString(
withNullability: false /* TODO upgrade to true */) !=
'Widget') {
throw InvalidGenerationSourceError(
'Invalid prototype. The function must be synchronous, top level, and return a Widget',
element: function,
Expand Down Expand Up @@ -112,8 +113,9 @@ class FunctionalWidgetGenerator
_defaultOptions.debugFillProperties) {
final overrideDebugFillProperties = _overrideDebugFillProperties(
userDefined, functionElement.parameters);
if (overrideDebugFillProperties != null)
if (overrideDebugFillProperties != null) {
b.methods.add(overrideDebugFillProperties);
}
}
},
);
Expand Down Expand Up @@ -152,14 +154,18 @@ class FunctionalWidgetGenerator
List<Expression> _computeBuildPositionalParametersExpression(
FunctionParameters parameters) {
final positional = <Expression>[];
if (parameters.startsWithContext)
if (parameters.startsWithContext) {
positional.add(const CodeExpression(Code('_context')));
if (parameters.startsWithKey)
}
if (parameters.startsWithKey) {
positional.add(const CodeExpression(Code('key')));
if (parameters.followedByContext)
}
if (parameters.followedByContext) {
positional.add(const CodeExpression(Code('_context')));
if (parameters.followedByKey)
}
if (parameters.followedByKey) {
positional.add(const CodeExpression(Code('key')));
}
positional.addAll(parameters.userDefined
.where((p) => !p.named)
.map((p) => CodeExpression(Code(p.name))));
Expand All @@ -181,9 +187,11 @@ class FunctionalWidgetGenerator
..returns = refer('void')
..lambda = false
..body = Block.of(
[const Code('super.debugFillProperties(properties);')]..addAll(
userFields.map((f) => _parameterToDiagnostic(
f, elements.firstWhere((e) => e.name == f.name)))),
[
const Code('super.debugFillProperties(properties);'),
...userFields.map((f) => _parameterToDiagnostic(
f, elements.firstWhere((e) => e.name == f.name)))
],
));
}

Expand Down Expand Up @@ -213,7 +221,7 @@ class FunctionalWidgetGenerator
}

String _getFallbackElementDiagnostic(ParameterElement element) =>
'DiagnosticsProperty<${element.type.isUndefined ? findBeginToken(element) : element.type.displayName}>';
'DiagnosticsProperty<${element.type.isDynamic ? tryParseDynamicType(element) : element.type.getDisplayString(withNullability: false /* TODO upgrade to true */)}>';

String _tryParseFunctionToDiagnostic(
ParameterElement element, String propertyType) {
Expand All @@ -232,7 +240,8 @@ class FunctionalWidgetGenerator
if (element.type.element is ClassElement) {
final classElement = element.type.element as ClassElement;
if (classElement.isEnum) {
propertyType = 'EnumProperty<${element.type.displayName}>';
propertyType =
'EnumProperty<${element.type.getDisplayString(withNullability: false /* TODO upgrade to true */)}>';
}
}
return propertyType;
Expand Down Expand Up @@ -328,8 +337,10 @@ class FunctionalWidgetGenerator
List<TypeParameterElement> typeParameters,
) {
return typeParameters.map((e) {
return e.bound?.displayName != null
? refer('${e.displayName} extends ${e.bound.displayName}')
final displayName = e.bound
?.getDisplayString(withNullability: false /* TODO upgrade to true */);
return displayName != null
? refer('${e.displayName} extends $displayName')
: refer(e.displayName);
});
}
Expand Down
28 changes: 21 additions & 7 deletions functional_widget/lib/src/parameters.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart' as element_type;
import 'package:code_builder/code_builder.dart';
import 'package:functional_widget/findBeginToken.dart';
import 'package:analyzer/dart/ast/ast.dart';

class FunctionParameters {
FunctionParameters._(this._parameters);
Expand Down Expand Up @@ -54,9 +55,8 @@ Reference _parameterToReference(ParameterElement element) {
if (element.type == null) {
return null;
}
if (element.type.isUndefined) {
var token = findBeginToken(element);
return refer(token.toString());
if (element.type.isDynamic) {
return refer(tryParseDynamicType(element));
}

return _typeToReference(element.type);
Expand All @@ -71,8 +71,9 @@ Reference _typeToReference(element_type.DartType type) {
final t = _functionTypedElementToFunctionType(type);
return t.type;
}

return type.displayName != null ? refer(type.displayName) : null;
final displayName =
type.getDisplayString(withNullability: false /* TODO upgrade to true */);
return displayName != null ? refer(displayName) : null;
}

FunctionType _functionTypedElementToFunctionType(
Expand All @@ -81,7 +82,9 @@ FunctionType _functionTypedElementToFunctionType(
return FunctionType((b) {
return b
..returnType = _typeToReference(element.returnType)
..types.addAll(element.typeFormals.map((f) => _typeToReference(f.type)))
// TODO set appropriate `nullabilitySuffix`
..types.addAll(element.typeFormals.map((f) => _typeToReference(
f.instantiate(nullabilitySuffix: NullabilitySuffix.none))))
..requiredParameters.addAll(element.parameters
.where((p) => p.isNotOptional)
.map(_parseParameter)
Expand All @@ -96,3 +99,14 @@ FunctionType _functionTypedElementToFunctionType(
.map((p) => p.type));
});
}

String tryParseDynamicType(ParameterElement element) {
final parsedLibrary =
element.session?.getParsedLibraryByElement(element.library);
final node = parsedLibrary?.getElementDeclaration(element)?.node;
final parameter = node is DefaultFormalParameter ? node.parameter : node;
if (parameter is SimpleFormalParameter && parameter.type != null) {
return parameter.type.beginToken.toString();
}
return 'dynamic';
}
Loading