Skip to content

Generator: remove dead name checks and duplicated discriminator, Set codec, and schema lookups #277

Backlog

Description

@leoafarias

Follow-up to the 2.0.0 release-readiness PR #274. Do this after the 2.0.0 PRs merge. Each item is internal: generated output must stay byte-identical (cd example && dart run build_runner build && git diff --exit-code), and error messages must not change unless noted. Paths are under packages/ack_generator/lib/src; search by symbol.

Dead checks after the single tokenizer

After schema-first key tokenization, a Dart field name never contains _ or $ and always starts lowercase. These checks can no longer fire:

  • the _ check and the $ack, _fromAckRuntime, _toAckRuntime entries in ackSchemaFirstFieldNameProblem and its reserved set;
  • if (name != node.className) return; in analyzer/schema_model_graph_builder.dart;
  • class-first _reservedMembers, whose only remaining use tests the wire discriminator key.

Duplicated logic

  • Discriminator name: computed in six places through ackSchemaFirstDiscriminatorName (an alias of ackSchemaFirstDartFieldName) and validated three times. This is why builders/model_emitter.dart imports analyzer/. Store the discriminator's Dart name on AckUnionModelNode and validate it once.
  • Set codec text is written in inference/schema_inference.dart and analyzer/class_model_graph_builder.dart. Export one setCodecExpression(list, type).
  • Fallback type names: uniqueName in json_schema/json_schema_emitter.dart produces the same result as ackSchemaFirstTypeNameSegment.
  • Member name validation: _rejectInvalidMemberName now only checks the capture field; reuse ackSchemaFirstFieldNameProblem.
  • Static schema lookups: four separate getGetter('schema')?.isStatic lookups (_validateStaticSchema, _validateUnionStaticSchema, hasStaticSchema, analyzer/custom_schema_resolver.dart). Resolve once into the model metadata.
  • Configured-options list with index(...) is duplicated in model_generator.dart and analyzer/schema_model_graph_builder.dart.

Smaller output and consistency

  • Pass-through class schemas: static final schema = XSchema.schema (the documented pattern) now routes the facade through X.schema for no gain. Detect the pass-through the way the union check already does and keep using XSchema.schema directly.
  • TypeChecker.typeNamedLiterally('AckRule') is inconsistent with the other typeNamed(...) checkers.

Diagnostics worth adding

  • Generated code uses null-aware collection elements (language 3.8 or later) but the generator does not check the input library's language version. Report a clear error instead of emitting code that fails to compile.
  • Library-level declarations named identical, override, or deepUnmodifiableJsonMap can shadow names the generated code uses; only some helper names are checked today.

Done when

The dead checks and duplicates are gone, the example regenerates with no diff, and dart run melos run test:gen --no-select passes (run it once at a time; see AGENTS.md).


Generated by Claude Code

Activity

  1. added
    enhancement
    on Dec 1, 2026
  2. leoafarias commented on Dec 2, 2026

    @leoafarias
    CollaboratorAuthor

    These items come from a review of the generator against v2.0.0-beta.3. They add to the list above; the review also found the Set codec duplication, the discriminator-name alias, and the stale _reservedMembers check, which the issue already lists. Every item here is internal or affects only generated code, so none of them blocks 2.0.0. Paths are under packages/ack_generator/lib/src; search by symbol.

    Names reserved more broadly than needed

    • _generatedReferences (in analyzer/schema_first_names.dart) always reserves bool, double, int, and num. A schema-first key such as double or num is rejected even when no generated field uses that type, and beta.3 accepted it. Reserve a type name only when the generated class refers to it. Accepting these keys later only allows more input, so the change is not breaking.

    Emitted style hidden by // ignore_for_file: type=lint

    If the ignore is removed and package:lints/recommended is applied, the generated output raises these lints:

    • curly_braces_in_flow_control_structures: the braceless if (...) return false; in the generated operator == (builders/data_class_emitter.dart).
    • unnecessary_this: this. on every field (builders/model_emitter.dart, builders/data_class_emitter.dart).
    • no_leading_underscores_for_local_identifiers: _ackDeclaredKeys (builders/model_emitter.dart). The class-first path names the same thing $ackDeclared (builders/class_model_emitter.dart); use one name in both.
    • Identity copies such as .map((item) => item) and MapEntry(key, item).

    Done when the generated example passes package:lints/recommended without the ignore. Keep the ignore itself; it protects consumers' own lint settings.

    Small cleanups

    • ackModelBuilder (builder.dart) is no longer exported, and only tests use it. Move it to the test utilities.
    • _ack() is copied in four classes (class_model_emitter.dart, data_class_emitter.dart, model_emitter.dart, schema_inference.dart). Share one helper.
    • Identifier RegExps are built on every call (class_model_graph_builder.dart, schema_first_names.dart). Make them top-level finals, like _nonAsciiLetter.

    Diagnostics

    • A removed annotation (@IsUrl, @Validate.*) produces source_gen's generic resolution error with no hint. Name the replacement (@IsUri, the Is…/Has… rules) when one of these names fails to resolve.
    • A class-owned schema that calls the facade's own parse/encode, for example in a codec callback, recurses until a StackOverflowError at runtime. Beta.3 accepted this. docs: prepare 2.0 documentation and pin naming conventions #281 documents the rule. A build error is optional, because detecting a call inside a callback needs flow analysis.
    • The class-only-option error for caseStyle ends with an unrelated hint about .describe(...).
    • When a .schema.json falls back to a value model, the generated comment explains only property-name causes. Other causes, such as a missing $defs entry, get no explanation (same at beta.3).

    Note on "Pass-through class schemas" above

    #282 makes the facade's toJsonSchema() and toSchemaModel() use X.schema, the same schema parse and encode use. That fixes a dropped .describe(...) on class-owned schemas. If the pass-through detection lands, apply it to all six facade methods together, so they never use different schemas.


    Generated by Claude Code

  3. leoafarias commented on Dec 2, 2026

    @leoafarias
    CollaboratorAuthor

    One more diagnostic, found while reviewing #282:

    • Nullable class-owned root. Suppose a class declares static final schema = XSchema.schema.nullable(). Then XSchema.toJsonSchema() admits null and safeParse(null) succeeds, but the typed XSchema.parse(null) throws a TypeError at its !. The parse failure also happens on main. Since fix(generator): export the class-owned schema from facades #282, the export advertises null as well. The fix(generator): export the class-owned schema from facades #282 changelog now says to keep a class-owned schema non-nullable.

      A blanket runtime isNullable guard would be wrong: schema.nullable().withDefault(model) reports isNullable == true, yet parse(null) correctly returns the default model. A useful check must either reject a root that accepts null without a default, or recognise .nullable() at build time on a statically resolvable initializer.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions