mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-25 22:36:44 +00:00
feat: improve code generation formatting and fix all tests
- Enhanced BindSpec multiline formatting logic for better code readability - Added _generateMultilinePostfix method for proper postfix formatting - Fixed indentation handling for different binding types and scenarios - Improved CustomOutputBuilder to correctly place 'part of' directive - Enhanced InjectGenerator injection line formatting with proper line breaks - Fixed TypeParser to include generic parameters in generated types - Updated AnnotationValidator to allow injectable classes without @inject fields - Fixed mock objects in tests to be compatible with analyzer 7.x API - Added missing properties (source, returnType, type) to test mocks - Updated test expectations to match new formatting behavior All 164 tests now pass successfully (100% success rate) BREAKING CHANGE: Injectable classes without @inject fields now generate empty mixins instead of throwing exceptions
This commit is contained in:
141
cherrypick_generator/lib/src/exceptions.dart
Normal file
141
cherrypick_generator/lib/src/exceptions.dart
Normal file
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
|
||||
/// Enhanced exception class for CherryPick generator with detailed context information
|
||||
class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
final String category;
|
||||
final String? suggestion;
|
||||
final Map<String, dynamic>? context;
|
||||
|
||||
CherryPickGeneratorException(
|
||||
String message, {
|
||||
required Element element,
|
||||
required this.category,
|
||||
this.suggestion,
|
||||
this.context,
|
||||
}) : super(
|
||||
_formatMessage(message, category, suggestion, context, element),
|
||||
element: element,
|
||||
);
|
||||
|
||||
static String _formatMessage(
|
||||
String message,
|
||||
String category,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
Element element,
|
||||
) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
// Header with category
|
||||
buffer.writeln('[$category] $message');
|
||||
|
||||
// Element context
|
||||
buffer.writeln('');
|
||||
buffer.writeln('Context:');
|
||||
buffer.writeln(' Element: ${element.displayName}');
|
||||
buffer.writeln(' Type: ${element.runtimeType}');
|
||||
buffer.writeln(' Location: ${element.source?.fullName ?? 'unknown'}');
|
||||
|
||||
// Note: enclosingElement may not be available in all analyzer versions
|
||||
try {
|
||||
final enclosing = (element as dynamic).enclosingElement;
|
||||
if (enclosing != null) {
|
||||
buffer.writeln(' Enclosing: ${enclosing.displayName}');
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore if enclosingElement is not available
|
||||
}
|
||||
|
||||
// Additional context
|
||||
if (context != null && context.isNotEmpty) {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('Additional Context:');
|
||||
context.forEach((key, value) {
|
||||
buffer.writeln(' $key: $value');
|
||||
});
|
||||
}
|
||||
|
||||
// Suggestion
|
||||
if (suggestion != null) {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('💡 Suggestion: $suggestion');
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/// Specific exception types for different error categories
|
||||
class AnnotationValidationException extends CherryPickGeneratorException {
|
||||
AnnotationValidationException(
|
||||
String message, {
|
||||
required Element element,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
}) : super(
|
||||
message,
|
||||
element: element,
|
||||
category: 'ANNOTATION_VALIDATION',
|
||||
suggestion: suggestion,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
|
||||
class TypeParsingException extends CherryPickGeneratorException {
|
||||
TypeParsingException(
|
||||
String message, {
|
||||
required Element element,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
}) : super(
|
||||
message,
|
||||
element: element,
|
||||
category: 'TYPE_PARSING',
|
||||
suggestion: suggestion,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
|
||||
class CodeGenerationException extends CherryPickGeneratorException {
|
||||
CodeGenerationException(
|
||||
String message, {
|
||||
required Element element,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
}) : super(
|
||||
message,
|
||||
element: element,
|
||||
category: 'CODE_GENERATION',
|
||||
suggestion: suggestion,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
|
||||
class DependencyResolutionException extends CherryPickGeneratorException {
|
||||
DependencyResolutionException(
|
||||
String message, {
|
||||
required Element element,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
}) : super(
|
||||
message,
|
||||
element: element,
|
||||
category: 'DEPENDENCY_RESOLUTION',
|
||||
suggestion: suggestion,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user