-
Notifications
You must be signed in to change notification settings - Fork 14
Support naked braces/brackets/parens, invert labels on banner #89
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
Merged
schneems
merged 7 commits into
main
from
mauro-oto/support-naked-braces-brackets-and-invert-labels
Oct 19, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
244d95a
Support naked braces/brackets/parens, invert labels on banner
mauro-oto 58e5ef2
Fix linting issues
mauro-oto 78303df
Added changelog entry
mauro-oto 17b4151
Skip } check for Ruby 2.6
mauro-oto a46d865
Move symbol invert logic to whodis, re-unified unmatched syntax/symbo…
mauro-oto 04c94ff
Get rid of key check, we need only check the hash values now
mauro-oto d86613f
Update lib/dead_end/banner.rb
schneems File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DeadEnd | ||
| class Banner | ||
| attr_reader :invalid_obj | ||
|
|
||
| def initialize(invalid_obj:) | ||
| @invalid_obj = invalid_obj | ||
| end | ||
|
|
||
| def call | ||
| case invalid_obj.error_symbol | ||
| when :missing_end | ||
| <<~EOM | ||
| DeadEnd: Missing `end` detected | ||
|
|
||
| This code has a missing `end`. Ensure that all | ||
| syntax keywords (`def`, `do`, etc.) have a matching `end`. | ||
| EOM | ||
| when :unmatched_syntax | ||
| case unmatched_symbol | ||
| when :end | ||
| <<~EOM | ||
| DeadEnd: Unmatched `end` detected | ||
|
|
||
| This code has an unmatched `end`. Ensure that all `end` lines | ||
| in your code have a matching syntax keyword (`def`, `do`, etc.) | ||
| and that you don't have any extra `end` lines. | ||
| EOM | ||
| when :| | ||
| <<~EOM | ||
| DeadEnd: Unmatched `|` character detected | ||
|
|
||
| Example: | ||
|
|
||
| `do |x` should be `do |x|` | ||
| EOM | ||
| when *WhoDisSyntaxError::CHARACTERS.keys | ||
| <<~EOM | ||
| DeadEnd: Unmatched `#{unmatched_symbol}` character detected | ||
|
|
||
| It appears a `#{missing_character}` might be missing. | ||
| EOM | ||
| else | ||
| "DeadEnd: Unmatched `#{unmatched_symbol}` detected" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private def unmatched_symbol | ||
| invalid_obj.unmatched_symbol | ||
| end | ||
|
|
||
| private def missing_character | ||
| WhoDisSyntaxError::CHARACTERS[unmatched_symbol] | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "banner" | ||
| require_relative "capture_code_context" | ||
| require_relative "display_code_with_line_numbers" | ||
|
|
||
|
|
@@ -54,43 +55,7 @@ def call | |
| end | ||
|
|
||
| def banner | ||
| case @invalid_obj.error_symbol | ||
| when :missing_end | ||
| <<~EOM | ||
| DeadEnd: Missing `end` detected | ||
|
|
||
| This code has a missing `end`. Ensure that all | ||
| syntax keywords (`def`, `do`, etc.) have a matching `end`. | ||
| EOM | ||
| when :unmatched_syntax | ||
| case @invalid_obj.unmatched_symbol | ||
| when :end | ||
| <<~EOM | ||
| DeadEnd: Unmatched `end` detected | ||
|
|
||
| This code has an unmatched `end`. Ensure that all `end` lines | ||
| in your code have a matching syntax keyword (`def`, `do`, etc.) | ||
| and that you don't have any extra `end` lines. | ||
| EOM | ||
| when :| | ||
| <<~EOM | ||
| DeadEnd: Unmatched `|` character detected | ||
|
|
||
| Example: | ||
|
|
||
| `do |x` should be `do |x|` | ||
| EOM | ||
| when :"}" | ||
| <<~EOM | ||
| DeadEnd: Unmatched `}` character detected | ||
|
|
||
| This code has an unmatched `}`. Ensure that opening curly braces are | ||
| closed: `{ }`. | ||
| EOM | ||
| else | ||
| "DeadEnd: Unmatched `#{@invalid_obj.unmatched_symbol}` detected" | ||
| end | ||
| end | ||
| Banner.new(invalid_obj: @invalid_obj).call | ||
|
Collaborator
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. Love this. Extracting this logic to it's own class makes so much more sense. |
||
| end | ||
|
|
||
| def indent(string, with: " ") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../spec_helper" | ||
|
|
||
| module DeadEnd | ||
| RSpec.describe Banner do | ||
| it "Unmatched | banner" do | ||
| source = <<~EOM | ||
| Foo.call do | | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("Unmatched `|` character detected") | ||
| end | ||
|
|
||
| it "Unmatched { banner" do | ||
| source = <<~EOM | ||
| class Cat | ||
| lol = { | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("Unmatched `{` character detected") | ||
| end | ||
|
|
||
| it "Unmatched } banner" do | ||
| skip("Unsupported ruby version") unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7") | ||
mauro-oto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| source = <<~EOM | ||
| def foo | ||
| lol = } | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("Unmatched `}` character detected") | ||
| end | ||
|
|
||
| it "Unmatched [ banner" do | ||
| source = <<~EOM | ||
| class Cat | ||
| lol = [ | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("Unmatched `[` character detected") | ||
| end | ||
|
|
||
| it "Unmatched ] banner" do | ||
| source = <<~EOM | ||
| def foo | ||
| lol = ] | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("Unmatched `]` character detected") | ||
| end | ||
|
|
||
| it "Unmatched end banner" do | ||
| source = <<~EOM | ||
| class Cat | ||
| end | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("DeadEnd: Unmatched `end` detected") | ||
| end | ||
|
|
||
| it "Unmatched unknown banner" do | ||
| source = <<~EOM | ||
| class Cat | ||
| def meow | ||
| 1 * | ||
| end | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("DeadEnd: Unmatched `unknown` detected") | ||
| end | ||
|
|
||
| it "missing end banner" do | ||
| source = <<~EOM | ||
| class Cat | ||
| def meow | ||
| end | ||
| EOM | ||
|
|
||
| invalid_obj = WhoDisSyntaxError.new(source) | ||
| banner = Banner.new(invalid_obj: invalid_obj) | ||
| expect(banner.call).to include("DeadEnd: Missing `end` detected") | ||
| end | ||
|
|
||
| it "naked (closing) parenthesis" do | ||
| invalid_obj = WhoDisSyntaxError.new("def initialize; ); end").call | ||
|
|
||
| expect( | ||
| Banner.new(invalid_obj: invalid_obj).call | ||
| ).to include("Unmatched `)` character detected") | ||
| end | ||
|
|
||
| it "naked (opening) parenthesis" do | ||
| invalid_obj = WhoDisSyntaxError.new("def initialize; (; end").call | ||
|
|
||
| expect( | ||
| Banner.new(invalid_obj: invalid_obj).call | ||
| ).to include("Unmatched `(` character detected") | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm realizing that
unmatched_syntaxandunexpected_syntaxare very similar. I'm thinking that we should make them more unique at a glance maybemissing_symbolandunexpected_syntax? What do you think?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.
Actually scratch that, If we normalize and reverse characters in "who dis" instead then we won't need to add this extra state. I would prefer it that way instead.
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.
Done! There's still one reversal I do here, in
missing_characterto show which character might be missing, see the following example:On 2.0 release:
On this branch:
(For the
It appears a ']' is missing.message, which is part of the banner)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.
Cool, works for me