Rubocop: Error occurring with Style/PercentLiteralDelimiters

Created on 27 Sep 2016  路  6Comments  路  Source: rubocop-hq/rubocop

With this configuration:

Lint/UnneededSplatExpansion:
  Enabled: false

Style/IndentArray:
  EnforcedStyle: consistent

Style/PercentLiteralDelimiters:
  PreferredDelimiters:
    "%i": []

This code:

attributes(*%i[
  foo
  bar
])

Throws an exception:

Default configuration from /Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/config/default.yml
Inheriting configuration from /Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/config/enabled.yml
Inheriting configuration from /Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/config/disabled.yml
Inspecting 1 file
Scanning /Users/brandon/Desktop/rubocop/test.rb
An error occurred while Style/PercentLiteralDelimiters cop was inspecting /Users/brandon/Desktop/rubocop/test.rb.
undefined method `split' for []:Array
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/style/percent_literal_delimiters.rb:62:in `preferred_delimiters'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/style/percent_literal_delimiters.rb:66:in `uses_preferred_delimiter?'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/style/percent_literal_delimiters.rb:55:in `on_percent_literal'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/mixin/percent_literal.rb:15:in `process'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/style/percent_literal_delimiters.rb:12:in `on_array'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:42:in `block (2 levels) in on_array'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:97:in `with_cop_error_handling'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:41:in `block in on_array'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:40:in `each'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:40:in `on_array'
(eval):3:in `on_splat'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:46:in `on_splat'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/ast_node/traversal.rb:102:in `block in on_send'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/ast_node/traversal.rb:100:in `each'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/ast_node/traversal.rb:100:in `each_with_index'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/ast_node/traversal.rb:100:in `on_send'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:46:in `on_send'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/ast_node/traversal.rb:13:in `walk'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/commissioner.rb:59:in `investigate'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/team.rb:121:in `investigate'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/team.rb:109:in `offenses'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cop/team.rb:52:in `inspect_file'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:244:in `inspect_file'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:191:in `block in do_inspection_loop'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:223:in `block in iterate_until_no_changes'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:216:in `loop'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:216:in `iterate_until_no_changes'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:187:in `do_inspection_loop'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:102:in `block in file_offenses'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:112:in `file_offense_cache'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:100:in `file_offenses'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:91:in `process_file'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:69:in `block in each_inspected_file'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:66:in `each'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:66:in `reduce'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:66:in `each_inspected_file'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:58:in `inspect_files'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/runner.rb:37:in `run'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cli.rb:72:in `execute_runner'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/lib/rubocop/cli.rb:28:in `run'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/bin/rubocop:14:in `block in <top (required)>'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/Users/brandon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.43.0/bin/rubocop:13:in `<top (required)>'
/Users/brandon/.rbenv/versions/2.3.1/bin/rubocop:23:in `load'
/Users/brandon/.rbenv/versions/2.3.1/bin/rubocop:23:in `<main>'

The versions are 0.43.0 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-darwin15).

Most helpful comment

The YAML parser is seeing an array rather than the string "[]" which is then breaking RuboCop. You can fix it like so:

Style/PercentLiteralDelimiters:
  PreferredDelimiters:
    "%i": '[]'

Note how the default config has to do this for {} brackets too:
https://github.com/bbatsov/rubocop/blob/f122024/config/default.yml#L785-L795

I think this is a bit too easy to mess up. I don't know if there is a stage during startup where the cop could check its config and at least ensure they're all strings, that'd be good.

All 6 comments

The YAML parser is seeing an array rather than the string "[]" which is then breaking RuboCop. You can fix it like so:

Style/PercentLiteralDelimiters:
  PreferredDelimiters:
    "%i": '[]'

Note how the default config has to do this for {} brackets too:
https://github.com/bbatsov/rubocop/blob/f122024/config/default.yml#L785-L795

I think this is a bit too easy to mess up. I don't know if there is a stage during startup where the cop could check its config and at least ensure they're all strings, that'd be good.

Oh, wow, yeah I didn't notice that. Thanks for pointing that out!

Should I close this or leave it open for a possible improvement to validating the configuration?

@bbatsov

How would you feel about something like this?

Or is there already a system in place to detect things like this that we can implement for this cop?

Looks reasonable. We don't have anything generic in place. Some cops check their configuration ad-hoc but we can certainly do better.

I think we only have some unified validation for supported styles.

I'll close this for now. Hopefully @savef will work on the config validation improvements.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirrmann picture kirrmann  路  3Comments

tedPen picture tedPen  路  3Comments

david942j picture david942j  路  3Comments

Aqualon picture Aqualon  路  3Comments

lepieru picture lepieru  路  3Comments