luckyframework / luckyframework/lucky

Add nicer compiler errors when assigning a parameter to type `LuckyAction.class`

Open
#1,243 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

improve error experience
Dominant language
Crystal
Stars
2.7k
Forks
172
PR merge metrics
No merged PRs in 30d

Description

It seems like folks often want to write wrappers that can handle links, and will leverage the link_helper.cr definitions to figure out what to ask for in a parameter.

Something like this:

class UI::Button < BaseComponent
  needs title : String
  needs to : Lucky::Action.class
  def render
    link title, class: "btn", to: to
  end
end

Called like this:

m UI::Button, title: "Button Text", to: App::Show

Generates this cryptic compiler error:

image

Here's why, as noted in our Gitter:

https://github.com/luckyframework/lucky/blob/master/spec/lucky/with_defaults_spec.cr
The error is because it is saying that a Lucky::Action could be any number of Lucky::Actions which have various different arguments. So your component would need to handle all the different kind of actions for it to work correctly. What you could also try to do is make it generic by making the needs route : T and that may work
But I think that with_defaults is nice because you can use any tag method with it like button, submit etc.

Alllllll that context provided, I think it'd be really great if Lucky could step in and help out here when it seems that someone is trying to type-check a parameter with a Lucky::Action.class. Something like:

If you're trying to validate that a Lucky `link_helper` is provided, you should use a generic `needs param : T` or the built-in `with_defaults` helper.

The message needs some work, but the gist is that Lucky could point users towards what they want based on the use cases for checking a LuckyAction.class.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the link_helper.cr definitions and spec/lucky/with_defaults_spec.cr, then reproduce the parameter assignment from the issue using Lucky::Action.class. Trace where the current cryptic compiler error is surfaced and determine how the diagnostic can identify this case. Done means the example produces a clearer message that points users toward a generic needs parameter or with_defaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.