diff --git a/lib/generator.dart b/lib/generator.dart index 5bdd92e..09dbec3 100644 --- a/lib/generator.dart +++ b/lib/generator.dart @@ -8,8 +8,8 @@ class BuiltReduxGenerator extends Generator { Future generate(LibraryReader library, BuildStep buildStep) async { final result = StringBuffer(); var hasWrittenHeaders = false; - for (final element in library.allElements) { - if (_isReduxActions(element) && element is ClassElement) { + for (final element in library.allElements.whereType()) { + if (_isReduxActions(element)) { if (!hasWrittenHeaders) { hasWrittenHeaders = true; result.writeln(_lintIgnores); @@ -29,44 +29,44 @@ const _lintIgnores = """ // ignore_for_file: type_annotate_public_apis """; -ActionsClass _actionsClassFromElement(ClassElement element) => ActionsClass( +ActionsClass _actionsClassFromElement(InterfaceElement element) => ActionsClass( element.name, _actionsFromElement(element).toSet(), _composedActionClasses(element).toSet(), _actionsClassFromInheritedElements(element).toSet(), ); -Iterable _composedActionClasses(ClassElement element) => - element.fields.where((f) => _isReduxActions(f.type.element)).map((f) => +Iterable _composedActionClasses(InterfaceElement element) => + element.fields.where((f) => _isReduxActions(f.type.element2)).map((f) => ComposedActionClass( f.name, f.type.getDisplayString(withNullability: true))); -Iterable _actionsFromElement(ClassElement element) => element.fields +Iterable _actionsFromElement(InterfaceElement element) => element.fields .where(_isActionDispatcher) .map((field) => _fieldElementToAction(element, field)); Iterable _actionsClassFromInheritedElements( - ClassElement element) => + InterfaceElement element) => element.allSupertypes - .map((s) => s.element) + .map((s) => s.element2) .where(_isReduxActions) .map(_actionsClassFromElement); -Action _fieldElementToAction(ClassElement element, FieldElement field) => +Action _fieldElementToAction(InterfaceElement element, FieldElement field) => Action('${element.name}-${field.name}', field.name, _fieldType(element, field)); // hack to return the generics for the action // this is used so action whose payloads are of generated types // will not result in dynamic -String _fieldType(ClassElement element, FieldElement field) { +String _fieldType(InterfaceElement element, FieldElement field) { if (field.isSynthetic) { return _syntheticFieldType(element, field); } return _getGenerics(field.source!.contents.data, field.nameOffset); } -String _syntheticFieldType(ClassElement element, FieldElement field) { +String _syntheticFieldType(InterfaceElement element, FieldElement field) { final method = element.getGetter(field.name); return _getGenerics(method!.source.contents.data, method.nameOffset); } @@ -81,25 +81,25 @@ String _getGenerics(String source, int nameOffset) { } bool _isReduxActions(Element? element) => - element is ClassElement && _hasSuperType(element, 'ReduxActions'); + element is InterfaceElement && _hasSuperType(element, 'ReduxActions'); bool _isActionDispatcher(FieldElement element) => element.type .getDisplayString(withNullability: true) .startsWith('ActionDispatcher<'); -bool _hasSuperType(ClassElement classElement, String type) => - classElement.allSupertypes - .any((interfaceType) => interfaceType.element.name == type) && - !classElement.displayName.startsWith('_\$'); +bool _hasSuperType(InterfaceElement InterfaceElement, String type) => + InterfaceElement.allSupertypes + .any((interfaceType) => interfaceType.element2.name == type) && + !InterfaceElement.displayName.startsWith('_\$'); -String _generateActions(ClassElement element) { +String _generateActions(InterfaceElement element) { final actionClass = _actionsClassFromElement(element); return _generateDispatchersIfNeeded(element, actionClass) + _actionNamesClassTemplate(actionClass); } String _generateDispatchersIfNeeded( - ClassElement element, ActionsClass actionsClass) => + InterfaceElement element, ActionsClass actionsClass) => element.constructors.length > 1 ? _actionDispatcherClassTemplate(actionsClass) : ''; diff --git a/pubspec.yaml b/pubspec.yaml index 9c97c44..24e2e9c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,21 +1,21 @@ name: built_redux -version: 8.0.0 +version: 9.0.1 description: A state management library written in dart that enforces immutability homepage: https://github.com/davidmarne/built_redux dependencies: - analyzer: ^1.0.0 - build: ">=1.0.0 <3.0.0" - built_collection: ^5.0.0 - built_value: ^8.0.0 - source_gen: ">=0.9.4 <2.0.0" - test: ^1.16.0 + analyzer: ">=4.6.0" + build: ">=2.3.1 <3.0.0" + built_collection: ">=5.1.1 <6.0.0" + built_value: ">=8.4.1 <9.0.0" + source_gen: ">=1.2.5 <2.0.0" + test: ">=0.2.0 <3.0.0" dev_dependencies: - build_runner: ^1.7.1 - build_test: ">=1.2.0 <3.0.0" - built_value_generator: ^8.0.0 - build_web_compilers: ^2.7.1 - workiva_analysis_options: ^1.0.0 + build_runner: ">=2.2.1 <3.0.0" + build_test: + built_value_generator: ">=8.4.1 <9.0.0" + build_web_compilers: ">=3.2.0 <4.0.0" + workiva_analysis_options: ">=1.2.2 <2.0.0" environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.17.0 <3.0.0"