Skip to content

RuboCop gives false positive Lint/Syntax errors for valid Ruby 3.1 syntax in slim files #154

@kreintjes

Description

@kreintjes

Ruby 3.1 includes new syntax to leave the hash value if it is a variable called the same as the hash key: https://bugs.ruby-lang.org/issues/14579. However there sometimes are cases this new syntax can be applied (as also suggested by RuboCop) in Slim files while doing so results in false positive RuboCop Lint/Syntax errors afterwards. For example the following code:

= simple_form_for(@object) do |f|
  = render 'fields', f:

  - content_for :something do
    | Some content

Results in the following error:

file.html.slim:4 [W] RuboCop: Lint/Syntax: unexpected token tSYMBOL
(Using Ruby 3.1 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

While this code is actually valid, parses and runs. I therefore would expect slim-lint/RuboCop not to produce an error for it though. Interestingly, similar code in Ruby also gives a RuboCop Lint/Syntax error for it, but in that case the code is actually invalid (Ruby itself gives a syntax error for it as well) and doesn't run. While in slim-lint the code is valid (and does run) and the error is a false positive.

It can be fixed by using parentheses around the render call:

= simple_form_for(@chart_configurator) do |f|
  = render('fields', f:)

  - content_for :something do
    | Some content

Or by including the omitted hash value f:

= simple_form_for(@chart_configurator) do |f|
  = render 'fields', f: f

  - content_for :something do
    | Some content

Interestingly the (false) syntax error is only produced when the omitted hash value was last and is followed immediately by some other Ruby code with a block on the same indenting level. The following examples all work correctly (no error is produced):

= simple_form_for(@chart_configurator) do |f|
  = render 'fields', f:, foo: 'bar'

  - content_for :something do
    | Some content
= simple_form_for(@chart_configurator) do |f|
  = render 'fields', f:

  div = 'Hi'

  - content_for :something do
    | Some content
= simple_form_for(@chart_configurator) do |f|
  = render 'fields', f:

- content_for :something do
  | Some content
= simple_form_for(@chart_configurator) do |f|
  = render 'fields', f:

  = f.submit

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions