mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
refactor(generator): migrate cherrypick_generator to analyzer element2 API
- Fully migrated core cherrypick_generator and submodules to new analyzer element2 system: - Updated all GeneratorForAnnotation overrides to use Element2, ClassElement2, MethodElement2, FieldElement2 and new annotation/metadata access patterns. - Migrated signature and bodies for helpers, parsers, annotation validators, meta utils, and type parsers. - Fixed tests to use readerWriter instead of deprecated reader argument. - Refactored usage of now-absent 'metadata', 'parameters', 'fields', 'methods', 'source', and similar members to use correct *.firstFragment.* or API alternatives. - Cleaned up old imports and unused code. test(generator): update generator integration tests - Updated test calls to use correct TestReaderWriter type and bring test infra in line with current build_runner/testing API. build: update dependencies and pubspec to support latest analyzer/build ecosystem - Raised Dart SDK and package constraints as required for generated code and codegen plugins. - Updated pubspecs in root/examples as needed by build warnings. docs: add plots and assets (new files) BREAKING CHANGE: - Requires Dart 3.8+ and analyzer that supports element2. - All downstream codegen/tests depending on Element API must migrate to Element2 signatures and data model.
This commit is contained in:
@@ -11,13 +11,12 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
|
||||
/// CherryPick DI field injector generator for codegen.
|
||||
@@ -100,12 +99,12 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
/// }
|
||||
/// ```
|
||||
@override
|
||||
FutureOr<String> generateForAnnotatedElement(
|
||||
Element element,
|
||||
dynamic generateForAnnotatedElement(
|
||||
Element2 element,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
if (element is! ClassElement) {
|
||||
if (element is! ClassElement2) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@injectable() can only be applied to classes.',
|
||||
element: element,
|
||||
@@ -113,7 +112,7 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
}
|
||||
|
||||
final classElement = element;
|
||||
final className = classElement.name;
|
||||
final className = classElement.firstFragment.name2;
|
||||
final mixinName = '_\$$className';
|
||||
|
||||
final buffer = StringBuffer()
|
||||
@@ -121,8 +120,9 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
..writeln(' void _inject($className instance) {');
|
||||
|
||||
// Collect and process all @inject fields
|
||||
final injectFields =
|
||||
classElement.fields.where(_isInjectField).map(_parseInjectField);
|
||||
final injectFields = classElement.fields2
|
||||
.where((f) => _isInjectField(f))
|
||||
.map((f) => _parseInjectField(f));
|
||||
|
||||
for (final parsedField in injectFields) {
|
||||
buffer.writeln(_generateInjectionLine(parsedField));
|
||||
@@ -138,8 +138,8 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
/// Returns true if a field is annotated with `@inject`.
|
||||
///
|
||||
/// Used to detect which fields should be processed for injection.
|
||||
static bool _isInjectField(FieldElement field) {
|
||||
return field.metadata.any(
|
||||
static bool _isInjectField(FieldElement2 field) {
|
||||
return field.firstFragment.metadata2.annotations.any(
|
||||
(m) => m.computeConstantValue()?.type?.getDisplayString() == 'inject',
|
||||
);
|
||||
}
|
||||
@@ -149,11 +149,11 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
///
|
||||
/// Converts Dart field declaration and all parameterizing injection-related
|
||||
/// annotations into a [_ParsedInjectField] which is used for codegen.
|
||||
static _ParsedInjectField _parseInjectField(FieldElement field) {
|
||||
static _ParsedInjectField _parseInjectField(FieldElement2 field) {
|
||||
String? scopeName;
|
||||
String? namedValue;
|
||||
|
||||
for (final meta in field.metadata) {
|
||||
for (final meta in field.firstFragment.metadata2.annotations) {
|
||||
final DartObject? obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
@@ -177,15 +177,15 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
}
|
||||
|
||||
// Determine nullability for field types like T? or Future<T?>
|
||||
bool isNullable = dartType.nullabilitySuffix ==
|
||||
NullabilitySuffix.question ||
|
||||
bool isNullable =
|
||||
dartType.nullabilitySuffix == NullabilitySuffix.question ||
|
||||
(dartType is ParameterizedType &&
|
||||
(dartType)
|
||||
.typeArguments
|
||||
.any((t) => t.nullabilitySuffix == NullabilitySuffix.question));
|
||||
(dartType).typeArguments.any(
|
||||
(t) => t.nullabilitySuffix == NullabilitySuffix.question,
|
||||
));
|
||||
|
||||
return _ParsedInjectField(
|
||||
fieldName: field.name,
|
||||
fieldName: field.firstFragment.name2 ?? '',
|
||||
coreType: coreTypeName.replaceAll('?', ''), // удаляем "?" на всякий
|
||||
isFuture: isFuture,
|
||||
isNullable: isNullable,
|
||||
@@ -207,11 +207,11 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
String _generateInjectionLine(_ParsedInjectField field) {
|
||||
final resolveMethod = field.isFuture
|
||||
? (field.isNullable
|
||||
? 'tryResolveAsync<${field.coreType}>'
|
||||
: 'resolveAsync<${field.coreType}>')
|
||||
? 'tryResolveAsync<${field.coreType}>'
|
||||
: 'resolveAsync<${field.coreType}>')
|
||||
: (field.isNullable
|
||||
? 'tryResolve<${field.coreType}>'
|
||||
: 'resolve<${field.coreType}>');
|
||||
? 'tryResolve<${field.coreType}>'
|
||||
: 'resolve<${field.coreType}>');
|
||||
|
||||
final openCall = (field.scopeName != null && field.scopeName!.isNotEmpty)
|
||||
? "CherryPick.openScope(scopeName: '${field.scopeName}')"
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
@@ -79,12 +79,12 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
||||
///
|
||||
/// See file-level docs for usage and generated output example.
|
||||
@override
|
||||
String generateForAnnotatedElement(
|
||||
Element element,
|
||||
dynamic generateForAnnotatedElement(
|
||||
Element2 element,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
if (element is! ClassElement) {
|
||||
if (element is! ClassElement2) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@module() can only be applied to classes.',
|
||||
element: element,
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'exceptions.dart';
|
||||
import 'metadata_utils.dart';
|
||||
|
||||
@@ -52,8 +53,10 @@ class AnnotationValidator {
|
||||
/// - Parameter validation for method arguments.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] on any violation.
|
||||
static void validateMethodAnnotations(MethodElement method) {
|
||||
final annotations = _getAnnotationNames(method.metadata);
|
||||
static void validateMethodAnnotations(MethodElement2 method) {
|
||||
final annotations = _getAnnotationNames(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateMutuallyExclusiveAnnotations(method, annotations);
|
||||
_validateAnnotationCombinations(method, annotations);
|
||||
@@ -68,8 +71,10 @@ class AnnotationValidator {
|
||||
/// - Correct scope naming if present.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] if checks fail.
|
||||
static void validateFieldAnnotations(FieldElement field) {
|
||||
final annotations = _getAnnotationNames(field.metadata);
|
||||
static void validateFieldAnnotations(FieldElement2 field) {
|
||||
final annotations = _getAnnotationNames(
|
||||
field.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateInjectFieldAnnotations(field, annotations);
|
||||
}
|
||||
@@ -82,8 +87,10 @@ class AnnotationValidator {
|
||||
/// - Provides helpful context for error/warning reporting.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] if checks fail.
|
||||
static void validateClassAnnotations(ClassElement classElement) {
|
||||
final annotations = _getAnnotationNames(classElement.metadata);
|
||||
static void validateClassAnnotations(ClassElement2 classElement) {
|
||||
final annotations = _getAnnotationNames(
|
||||
classElement.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateModuleClassAnnotations(classElement, annotations);
|
||||
_validateInjectableClassAnnotations(classElement, annotations);
|
||||
@@ -104,7 +111,7 @@ class AnnotationValidator {
|
||||
///
|
||||
/// For example, `@instance` and `@provide` cannot both be present.
|
||||
static void _validateMutuallyExclusiveAnnotations(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// @instance and @provide are mutually exclusive
|
||||
@@ -127,7 +134,7 @@ class AnnotationValidator {
|
||||
/// - One of `@instance` or `@provide` must be present for a registration method
|
||||
/// - Validates singleton usage
|
||||
static void _validateAnnotationCombinations(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// @params can only be used with @provide
|
||||
@@ -165,7 +172,7 @@ class AnnotationValidator {
|
||||
|
||||
/// Singleton-specific method annotation checks.
|
||||
static void _validateSingletonUsage(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// Singleton with params might not make sense in some contexts
|
||||
@@ -181,18 +188,17 @@ class AnnotationValidator {
|
||||
'Singleton methods cannot return void',
|
||||
element: method,
|
||||
suggestion: 'Remove @singleton annotation or change return type',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'return_type': returnType,
|
||||
},
|
||||
context: {'method_name': method.displayName, 'return_type': returnType},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates extra requirements or syntactic rules for annotation arguments, like @named.
|
||||
static void _validateAnnotationParameters(MethodElement method) {
|
||||
static void _validateAnnotationParameters(MethodElement2 method) {
|
||||
// Validate @named annotation parameters
|
||||
final namedValue = MetadataUtils.getNamedValue(method.metadata);
|
||||
final namedValue = MetadataUtils.getNamedValue(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (namedValue != null) {
|
||||
if (namedValue.isEmpty) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -222,8 +228,10 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Validate method parameters for @params usage
|
||||
for (final param in method.parameters) {
|
||||
final paramAnnotations = _getAnnotationNames(param.metadata);
|
||||
for (final param in method.formalParameters) {
|
||||
final paramAnnotations = _getAnnotationNames(
|
||||
param.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (paramAnnotations.contains('params')) {
|
||||
_validateParamsParameter(param, method);
|
||||
}
|
||||
@@ -232,7 +240,9 @@ class AnnotationValidator {
|
||||
|
||||
/// Checks that @params is used with compatible parameter type.
|
||||
static void _validateParamsParameter(
|
||||
ParameterElement param, MethodElement method) {
|
||||
FormalParameterElement param,
|
||||
MethodElement2 method,
|
||||
) {
|
||||
// @params parameter should typically be dynamic or Map<String, dynamic>
|
||||
final paramType = param.type.getDisplayString();
|
||||
if (paramType != 'dynamic' &&
|
||||
@@ -256,7 +266,7 @@ class AnnotationValidator {
|
||||
|
||||
/// Checks field-level annotation for valid injectable fields.
|
||||
static void _validateInjectFieldAnnotations(
|
||||
FieldElement field,
|
||||
FieldElement2 field,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('inject')) {
|
||||
@@ -270,15 +280,12 @@ class AnnotationValidator {
|
||||
'Cannot inject void type',
|
||||
element: field,
|
||||
suggestion: 'Use a concrete type instead of void',
|
||||
context: {
|
||||
'field_name': field.displayName,
|
||||
'field_type': fieldType,
|
||||
},
|
||||
context: {'field_name': field.displayName, 'field_type': fieldType},
|
||||
);
|
||||
}
|
||||
|
||||
// Validate scope annotation if present
|
||||
for (final meta in field.metadata) {
|
||||
for (final meta in field.firstFragment.metadata2.annotations) {
|
||||
final obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
@@ -290,7 +297,7 @@ class AnnotationValidator {
|
||||
|
||||
/// Checks @module usage: must have at least one DI method, each with DI-annotation.
|
||||
static void _validateModuleClassAnnotations(
|
||||
ClassElement classElement,
|
||||
ClassElement2 classElement,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('module')) {
|
||||
@@ -298,8 +305,9 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Check if class has public methods
|
||||
final publicMethods =
|
||||
classElement.methods.where((m) => m.isPublic).toList();
|
||||
final publicMethods = classElement.methods2
|
||||
.where((m) => m.isPublic)
|
||||
.toList();
|
||||
if (publicMethods.isEmpty) {
|
||||
throw AnnotationValidationException(
|
||||
'Module class must have at least one public method',
|
||||
@@ -314,7 +322,9 @@ class AnnotationValidator {
|
||||
|
||||
// Validate that public methods have appropriate annotations
|
||||
for (final method in publicMethods) {
|
||||
final methodAnnotations = _getAnnotationNames(method.metadata);
|
||||
final methodAnnotations = _getAnnotationNames(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (!methodAnnotations.contains('instance') &&
|
||||
!methodAnnotations.contains('provide')) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -332,7 +342,7 @@ class AnnotationValidator {
|
||||
|
||||
/// Checks @injectable usage on classes and their fields.
|
||||
static void _validateInjectableClassAnnotations(
|
||||
ClassElement classElement,
|
||||
ClassElement2 classElement,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('injectable')) {
|
||||
@@ -340,8 +350,10 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Check if class has injectable fields
|
||||
final injectFields = classElement.fields.where((f) {
|
||||
final fieldAnnotations = _getAnnotationNames(f.metadata);
|
||||
final injectFields = classElement.fields2.where((f) {
|
||||
final fieldAnnotations = _getAnnotationNames(
|
||||
f.firstFragment.metadata2.annotations,
|
||||
);
|
||||
return fieldAnnotations.contains('inject');
|
||||
}).toList();
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
|
||||
import 'bind_parameters_spec.dart';
|
||||
import 'metadata_utils.dart';
|
||||
@@ -25,7 +25,7 @@ enum BindingType {
|
||||
instance,
|
||||
|
||||
/// Provider/factory function (@provide).
|
||||
provide;
|
||||
provide,
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
@@ -155,7 +155,8 @@ class BindSpec {
|
||||
switch (bindingType) {
|
||||
case BindingType.instance:
|
||||
throw StateError(
|
||||
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.');
|
||||
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.',
|
||||
);
|
||||
//return isAsyncInstance
|
||||
// ? '.toInstanceAsync(($fnArgs) => $methodName($fnArgs))'
|
||||
// : '.toInstance(($fnArgs) => $methodName($fnArgs))';
|
||||
@@ -189,20 +190,24 @@ class BindSpec {
|
||||
case BindingType.provide:
|
||||
if (isAsyncProvide) {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
final lambdaIndent = (isSingleton || named != null)
|
||||
? indent + 6
|
||||
: indent + 2;
|
||||
final closingIndent = (isSingleton || named != null)
|
||||
? indent + 4
|
||||
: indent;
|
||||
return '.toProvideAsync(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
|
||||
} else {
|
||||
return '.toProvideAsync(() => $methodName($argsStr))';
|
||||
}
|
||||
} else {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
final lambdaIndent = (isSingleton || named != null)
|
||||
? indent + 6
|
||||
: indent + 2;
|
||||
final closingIndent = (isSingleton || named != null)
|
||||
? indent + 4
|
||||
: indent;
|
||||
return '.toProvide(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
|
||||
} else {
|
||||
return '.toProvide(() => $methodName($argsStr))';
|
||||
@@ -246,7 +251,7 @@ class BindSpec {
|
||||
/// print(bindSpec.returnType); // e.g., 'Logger'
|
||||
/// ```
|
||||
/// Throws [AnnotationValidationException] or [CodeGenerationException] if invalid.
|
||||
static BindSpec fromMethod(MethodElement method) {
|
||||
static BindSpec fromMethod(MethodElement2 method) {
|
||||
try {
|
||||
// Validate method annotations
|
||||
AnnotationValidator.validateMethodAnnotations(method);
|
||||
@@ -254,28 +259,44 @@ class BindSpec {
|
||||
// Parse return type using improved type parser
|
||||
final parsedReturnType = TypeParser.parseType(method.returnType, method);
|
||||
|
||||
final methodName = method.displayName;
|
||||
final methodName = method.firstFragment.name2 ?? '';
|
||||
|
||||
// Check for @singleton annotation.
|
||||
final isSingleton = MetadataUtils.anyMeta(method.metadata, 'singleton');
|
||||
final isSingleton = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'singleton',
|
||||
);
|
||||
|
||||
// Get @named value if present.
|
||||
final named = MetadataUtils.getNamedValue(method.metadata);
|
||||
final named = MetadataUtils.getNamedValue(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
// Parse each method parameter.
|
||||
final params = <BindParameterSpec>[];
|
||||
bool hasParams = false;
|
||||
for (final p in method.parameters) {
|
||||
for (final p in method.formalParameters) {
|
||||
final typeStr = p.type.getDisplayString();
|
||||
final paramNamed = MetadataUtils.getNamedValue(p.metadata);
|
||||
final isParams = MetadataUtils.anyMeta(p.metadata, 'params');
|
||||
final paramNamed = MetadataUtils.getNamedValue(
|
||||
p.firstFragment.metadata2.annotations,
|
||||
);
|
||||
final isParams = MetadataUtils.anyMeta(
|
||||
p.firstFragment.metadata2.annotations,
|
||||
'params',
|
||||
);
|
||||
if (isParams) hasParams = true;
|
||||
params.add(BindParameterSpec(typeStr, paramNamed, isParams: isParams));
|
||||
}
|
||||
|
||||
// Determine bindingType: @instance or @provide.
|
||||
final hasInstance = MetadataUtils.anyMeta(method.metadata, 'instance');
|
||||
final hasProvide = MetadataUtils.anyMeta(method.metadata, 'provide');
|
||||
final hasInstance = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'instance',
|
||||
);
|
||||
final hasProvide = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'provide',
|
||||
);
|
||||
|
||||
if (!hasInstance && !hasProvide) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -290,8 +311,9 @@ class BindSpec {
|
||||
);
|
||||
}
|
||||
|
||||
final bindingType =
|
||||
hasInstance ? BindingType.instance : BindingType.provide;
|
||||
final bindingType = hasInstance
|
||||
? BindingType.instance
|
||||
: BindingType.provide;
|
||||
|
||||
// PROHIBIT @params with @instance bindings!
|
||||
if (bindingType == BindingType.instance && hasParams) {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
@@ -48,21 +48,21 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
|
||||
CherryPickGeneratorException(
|
||||
String message, {
|
||||
required Element element,
|
||||
required Element2 element,
|
||||
required this.category,
|
||||
this.suggestion,
|
||||
this.context,
|
||||
}) : super(
|
||||
_formatMessage(message, category, suggestion, context, element),
|
||||
element: element,
|
||||
);
|
||||
_formatMessage(message, category, suggestion, context, element),
|
||||
element: element,
|
||||
);
|
||||
|
||||
static String _formatMessage(
|
||||
String message,
|
||||
String category,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
Element element,
|
||||
Element2 element,
|
||||
) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
@@ -74,7 +74,9 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
buffer.writeln('Context:');
|
||||
buffer.writeln(' Element: ${element.displayName}');
|
||||
buffer.writeln(' Type: ${element.runtimeType}');
|
||||
buffer.writeln(' Location: ${element.source?.fullName ?? 'unknown'}');
|
||||
buffer.writeln(
|
||||
' Location: ${element.firstFragment.libraryFragment?.source.fullName ?? 'unknown'}',
|
||||
);
|
||||
|
||||
// Try to show enclosing element info for extra context
|
||||
try {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'bind_spec.dart';
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
@@ -75,14 +76,11 @@ class GeneratedClass {
|
||||
/// final gen = GeneratedClass.fromClassElement(classElement);
|
||||
/// print(gen.generatedClassName); // e.g. $AppModule
|
||||
/// ```
|
||||
static GeneratedClass fromClassElement(ClassElement element) {
|
||||
final className = element.displayName;
|
||||
// Generated class name with '$' prefix (standard for generated Dart code).
|
||||
static GeneratedClass fromClassElement(ClassElement2 element) {
|
||||
final className = element.firstFragment.name2 ?? '';
|
||||
final generatedClassName = r'$' + className;
|
||||
// Get source file name
|
||||
final sourceFile = element.source.shortName;
|
||||
// Collect bindings for all non-abstract methods.
|
||||
final binds = element.methods
|
||||
final sourceFile = element.firstFragment.libraryFragment.source.shortName;
|
||||
final binds = element.methods2
|
||||
.where((m) => !m.isAbstract)
|
||||
.map(BindSpec.fromMethod)
|
||||
.toList();
|
||||
|
||||
@@ -41,14 +41,16 @@ class MetadataUtils {
|
||||
/// bool isSingleton = MetadataUtils.anyMeta(myMethod.metadata, 'singleton');
|
||||
/// ```
|
||||
static bool anyMeta(List<ElementAnnotation> meta, String typeName) {
|
||||
return meta.any((m) =>
|
||||
m
|
||||
.computeConstantValue()
|
||||
?.type
|
||||
?.getDisplayString()
|
||||
.toLowerCase()
|
||||
.contains(typeName.toLowerCase()) ??
|
||||
false);
|
||||
return meta.any(
|
||||
(m) =>
|
||||
m
|
||||
.computeConstantValue()
|
||||
?.type
|
||||
?.getDisplayString()
|
||||
.toLowerCase()
|
||||
.contains(typeName.toLowerCase()) ??
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
/// Extracts the string value from a `@named('value')` annotation if present in [meta].
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'exceptions.dart';
|
||||
@@ -45,7 +45,7 @@ class TypeParser {
|
||||
/// final parsed = TypeParser.parseType(field.type, field);
|
||||
/// if (parsed.isNullable) print('Field is nullable');
|
||||
/// ```
|
||||
static ParsedType parseType(DartType dartType, Element context) {
|
||||
static ParsedType parseType(DartType dartType, Element2 context) {
|
||||
try {
|
||||
return _parseTypeInternal(dartType, context);
|
||||
} catch (e) {
|
||||
@@ -61,7 +61,7 @@ class TypeParser {
|
||||
}
|
||||
}
|
||||
|
||||
static ParsedType _parseTypeInternal(DartType dartType, Element context) {
|
||||
static ParsedType _parseTypeInternal(DartType dartType, Element2 context) {
|
||||
final displayString = dartType.getDisplayString();
|
||||
final isNullable = dartType.nullabilitySuffix == NullabilitySuffix.question;
|
||||
|
||||
@@ -87,7 +87,10 @@ class TypeParser {
|
||||
}
|
||||
|
||||
static ParsedType _parseFutureType(
|
||||
DartType dartType, Element context, bool isNullable) {
|
||||
DartType dartType,
|
||||
Element2 context,
|
||||
bool isNullable,
|
||||
) {
|
||||
if (dartType is! ParameterizedType || dartType.typeArguments.isEmpty) {
|
||||
throw TypeParsingException(
|
||||
'Future type must have a type argument',
|
||||
@@ -112,7 +115,10 @@ class TypeParser {
|
||||
}
|
||||
|
||||
static ParsedType _parseGenericType(
|
||||
ParameterizedType dartType, Element context, bool isNullable) {
|
||||
ParameterizedType dartType,
|
||||
Element2 context,
|
||||
bool isNullable,
|
||||
) {
|
||||
final typeArguments = dartType.typeArguments
|
||||
.map((arg) => _parseTypeInternal(arg, context))
|
||||
.toList();
|
||||
@@ -138,7 +144,7 @@ class TypeParser {
|
||||
/// final parsed = TypeParser.parseType(field.type, field);
|
||||
/// TypeParser.validateInjectableType(parsed, field);
|
||||
/// ```
|
||||
static void validateInjectableType(ParsedType parsedType, Element context) {
|
||||
static void validateInjectableType(ParsedType parsedType, Element2 context) {
|
||||
// Check for void type
|
||||
if (parsedType.coreType == 'void') {
|
||||
throw TypeParsingException(
|
||||
|
||||
Reference in New Issue
Block a user