Skip to content

Commit

Permalink
Improve needs error reporting (#1303)
Browse files Browse the repository at this point in the history
* Raise helpful error if the same needs is not unique

* Improve error message if duplicate needs found

* Removed test that only used to force a compilation failure

* fixed formatting

* Removed Changelog comments

* Removed deliberately failing test

Co-authored-by: David Simmons <davidsimmons@davids-mbp.home>
  • Loading branch information
shortly-portly and David Simmons committed Nov 9, 2020
1 parent c094df3 commit fb880fb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lucky/assignable.cr
Expand Up @@ -30,6 +30,17 @@ module Lucky::Assignable
{% raise "Using '?' in a 'needs' var name is no longer supported. Now Lucky generates a method ending in '?' if the type is 'Bool'." %}
{% end %}

# Ensure that the needs variable name has not been previously defined.
{% previous_declaration = ASSIGNS.find { |d| d.var == declaration.var } %}
{% if previous_declaration %}
{% raise <<-ERROR
\n
Duplicate needs definition: '#{declaration}' defined in #{declaration.filename}:#{declaration.line_number}:#{declaration.column_number}
This needs is already defined as '#{previous_declaration}' in #{previous_declaration.filename}:#{previous_declaration.line_number}:#{previous_declaration.column_number}
ERROR
%}
{% end %}

{% if declaration.type.stringify == "Bool" %}
def {{ declaration.var }}?
@{{ declaration.var }}
Expand Down

0 comments on commit fb880fb

Please sign in to comment.