diff --git a/jael_web/example/stateful.g.dart b/jael_web/example/stateful.g.dart index 5d26147d..398897a8 100644 --- a/jael_web/example/stateful.g.dart +++ b/jael_web/example/stateful.g.dart @@ -8,6 +8,7 @@ part of 'stateful.dart'; abstract class _StatefulAppJaelTemplate implements Component<_AppState> { Timer get _timer; + void beforeDestroy(); @override DomNode render() { return h('div', {}, [text('Tick count: '), text(state.ticks.toString())]); diff --git a/jael_web/lib/src/builder/builder.dart b/jael_web/lib/src/builder/builder.dart index fc5fe05a..a75a3e57 100644 --- a/jael_web/lib/src/builder/builder.dart +++ b/jael_web/lib/src/builder/builder.dart @@ -1,5 +1,4 @@ import 'dart:async'; - import 'package:analyzer/dart/element/element.dart'; import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; @@ -75,6 +74,21 @@ class JaelComponentGenerator extends GeneratorForAnnotation { })); } + // ... And methods too. + for (var method in element.methods) { + b.methods.add(Method((b) { + b + ..name = method.name + ..returns = convertTypeReference(method.returnType) + ..requiredParameters.addAll(method.parameters + .where(isRequiredParameter) + .map(convertParameter)) + ..optionalParameters.addAll(method.parameters + .where(isOptionalParameter) + .map(convertParameter)); + })); + } + // Add a render() stub b.methods.add(Method((b) { b diff --git a/jael_web/lib/src/builder/util.dart b/jael_web/lib/src/builder/util.dart index 72ac0974..2796cec4 100644 --- a/jael_web/lib/src/builder/util.dart +++ b/jael_web/lib/src/builder/util.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:convert'; +import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; @@ -18,6 +19,25 @@ TypeReference convertTypeReference(DartType t) { }); } +bool isRequiredParameter(ParameterElement e) { + return e.isNotOptional; +} + +bool isOptionalParameter(ParameterElement e) { + return e.isOptional; +} + +Parameter convertParameter(ParameterElement e) { + return Parameter((b) { + b + ..name = e.name + ..type = convertTypeReference(e.type) + ..named = e.isNamed + ..defaultTo = + e.defaultValueCode == null ? null : Code(e.defaultValueCode); + }); +} + UnsupportedError _unsupported() => UnsupportedError('Not support in R/O build file system.'); @@ -183,8 +203,8 @@ class BuildSystemFile extends File { throw _unsupported(); @override - Directory get parent => - BuildSystemDirectory(fileSystem, reader, package, fileSystem.path.dirname(path)); + Directory get parent => BuildSystemDirectory( + fileSystem, reader, package, fileSystem.path.dirname(path)); @override Future> readAsBytes() {