Repository navigation
fix: withhold changeTo when the JSON surface it calls is absent (#159) #161
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
Changes from 1 commit
c5fd072
291fa1c
7410db0
b5407c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import 'package:code_builder/code_builder.dart'; | ||
|
|
||
| import '../analysis/class_analyzer.dart'; | ||
| import '../ast/ast.dart'; | ||
| import '../helpers.dart' as helpers; | ||
| import 'base_generator.dart'; | ||
|
|
@@ -69,13 +70,40 @@ class CompareToExtensionGenerator extends ConcreteClassGenerator { | |
| /// Generates changeTo extension methods for explicit subtypes. | ||
| /// | ||
| /// Produces a native [Extension] spec. | ||
| /// | ||
| /// A `changeTo<Subtype>` body is a JSON round-trip: it collects the runtime | ||
| /// subtype's `toJson()`, overlays the patch's `toJson()`, and rebuilds the | ||
| /// target with `fromJson`. Those members exist only on a class generated with | ||
| /// `generateJson: true`, so the extension is withheld unless the JSON surface | ||
| /// it calls is really there (issue #159) — otherwise a hierarchy that never | ||
| /// asked for JSON gets a part file that does not compile. | ||
| class ChangeToExtensionGenerator extends UniversalGenerator { | ||
| ChangeToExtensionGenerator(); | ||
|
|
||
| @override | ||
| bool shouldGenerate(GenerationContext context) { | ||
| return context.config.generateChangeTo && | ||
| context.metadata.explicitSubtypes.isNotEmpty; | ||
| if (!context.config.generateChangeTo) return false; | ||
| final metadata = context.metadata; | ||
| if (metadata.explicitSubtypes.isEmpty) return false; | ||
| return _jsonSurfaceExists(context); | ||
| } | ||
|
|
||
| /// Whether every type the generated bodies call has a JSON surface. | ||
| /// | ||
| /// The switch dispatches on the *runtime* subtype, so each declared subtype | ||
| /// needs its own `toJson()` and `fromJson`. A non-sealed base is | ||
| /// instantiable itself, so its `switch` also carries a `_ => this.toJson()` | ||
| /// default that needs the base's own `toJson()`. | ||
| bool _jsonSurfaceExists(GenerationContext context) { | ||
| final metadata = context.metadata; | ||
| if (!metadata.isSealed && !context.config.generateJson) return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠🎯 The gate reads "a non-sealed base is instantiable, so its switch carries
So a base that is not sealed and not Reproduced through the real pipeline — Adding Suggested change — derive the gate and the emitter from ONE helper so they cannot drift: bool _jsonSurfaceExists(GenerationContext context) {
final metadata = context.metadata;
if (!metadata.isSealed && !_baseDeclaresToJson(context)) return false;
for (final subtype in metadata.explicitSubtypes) {
final element = metadata.allAnnotatedClasses[subtype.interfaceName];
// An unresolvable subtype is not evidence that its JSON surface exists.
if (element == null) return false;
if (!ClassAnalyzer.generatesJson(element)) return false;
}
return true;
}
/// The base's own `toJson()` — the target of the `_ => this.toJson()` default
/// case. A polymorphic base declares one only when JSON is on AND the base is
/// `nonSealed` (json_generator.dart:221); a base with `explicitSubTypes` never
/// gets one from JsonExtensionGenerator (json_generator.dart:546-549).
bool _baseDeclaresToJson(GenerationContext context) =>
context.config.generateJson && context.metadata.nonSealed;then pass that same predicate into |
||
| for (final subtype in metadata.explicitSubtypes) { | ||
| final element = metadata.allAnnotatedClasses[subtype.interfaceName]; | ||
| // An unresolvable subtype is not evidence that its JSON surface exists. | ||
| if (element == null) return false; | ||
| if (!ClassAnalyzer.generatesJson(element)) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵📐 The new doc note states a rule the code does not implement — effort ⚡
For a base that is neither sealed nor
nonSealed,generateJson: trueis not enough — such a base declares notoJson()of its own (see the inline finding onextension_generator.dart:99), so the parenthetical is wrong exactly where a reader would rely on it. Once the gate is fixed, something like:The same sentence in
SKILL.md:388("generateJson: trueon the base, or on every declared subtype of a sealed base") needs the same qualifier.