rubocop / rubocop-md

RuboCop for Markdown code snippets
MIT License
73 stars 14 forks source link

Fix complex symbol exception #10

Closed dominicsayers closed 4 years ago

dominicsayers commented 4 years ago

The cop Style/HashSyntax attempts to determine whether a hash has a valid syntax. For complex symbol keys it delegates this to the Ruby parser and calls the parse method on a code snippet with no path context.

By "complex symbol keys" I mean something like this:

{ 'complex_symbol': 0 }

where the key is a symbol but one declared using unusual but valid syntax.

When we run Rubocop, with rubocop-md required, on code like this (in a Ruby script rather than embedded in Markdown) we get the following exception:

An error occurred while Style/HashSyntax cop was inspecting /home/build/Development/dominicsayers/rubocop-hash-syntax-exception/hash_syntax_exception.rb:3:0.
no implicit conversion of nil into String
/home/build/.rvm/gems/ruby-2.6.3/gems/rubocop-md-0.3.2/lib/rubocop/markdown/rubocop_ext.rb:34:in `extname'
/home/build/.rvm/gems/ruby-2.6.3/gems/rubocop-md-0.3.2/lib/rubocop/markdown/rubocop_ext.rb:34:in `markdown_file?'
/home/build/.rvm/gems/ruby-2.6.3/gems/rubocop-md-0.3.2/lib/rubocop/markdown/rubocop_ext.rb:80:in `parse'

This PR fixes that exception.

To get the tests to run I have had to make the following changes, which I have minimised as much as possible:

palkan commented 4 years ago

Thanks! Released in 0.4.0