Skip to content

Support [elem, ...] non-empty list shorthand in AbstractCode - #42

Merged
andreashasse merged 2 commits into
mainfrom
andreashasse/intelligent-lederberg-bdd581
Sep 14, 2026
Merged

andreashasse merged 2 commits into
mainfrom
andreashasse/intelligent-lederberg-bdd581

Conversation

@andreashasse

Copy link
Copy Markdown
Owner

Summary

Spectral.AbstractCode.convert_type/1 did not handle the Elixir shorthand for a non-empty list type, [elem_type, ...] (or bare [...]), even though the longhand nonempty_list(elem_type) worked fine. A type like @type t :: %{tags: [String.t(), ...]} failed to compile with:

** (ArgumentError) Spectral.AbstractCode: unsupported type AST: ...

Fix

Added two clauses to convert_type/1 in lib/spectral/abstract_code.ex, matching before the existing single-element [elem] clause:

  • [elem_ast, {:..., _, _}]sp_nonempty_list(type: convert_type(elem_ast))
  • [{:..., _, _}] (bare [...]) → sp_nonempty_list(type: term)

Improper-list shorthands ([elem | tail], [elem, ... | tail]) were considered but not added: Elixir's typespec compiler treats [a | b] as list(a | b) (already handled by the existing [elem] clause) and rejects [a, ... | b] outright at compile time, so there's no AST shape from maybe_improper_list/nonempty_improper_list shorthand to handle here — those types are already only reachable via their named-type form, which already works.

Testing

  • New test/support/nonempty_list_module.ex + test/spectral_nonempty_list_test.exs: encode/decode round-trips a non-empty list, empty list is rejected both ways, and the generated JSON Schema has minItems: 1 and matches nonempty_list(elem)'s schema exactly.
  • Added [integer(), ...] / [...] types to test/support/all_types_module.ex, covered by the existing compile-time vs. runtime type_info equivalence test.
  • make format && make ci passes (217 tests, 0 failures, no Credo or Dialyzer issues).

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes are fully covered by tests and no unresolved issues remain.

Pull request overview

Adds support for Elixir non-empty list shorthand types in Spectral.AbstractCode.

Changes:

  • Converts [...] and [elem, ...] to non-empty list types.
  • Adds encoding, decoding, schema, and equivalence coverage.
File summaries
File Description
test/type_info_equivalence_test.exs Includes shorthand types in equivalence checks.
test/support/nonempty_list_module.ex Adds shorthand and longhand fixtures.
test/support/all_types_module.ex Adds shorthand list fixtures.
test/spectral_nonempty_list_test.exs Tests validation and schema behavior.
lib/spectral/abstract_code.ex Handles non-empty list shorthand ASTs.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@andreashasse
andreashasse merged commit 00f52ce into main Sep 14, 2026
3 checks passed
andreashasse pushed a commit that referenced this pull request Sep 14, 2026
Merges in the non-empty list shorthand fix (#42), which landed on main
after this branch. Folds its CHANGELOG entry into the same 0.14.0
section as the spectra bump, since main is still at 0.13.0 and this
branch is the one cutting that release.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTMXez4gy2fHp6HgovtfGG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants