Skip to content

New cop to detect literal arrays inside a regexp #13323

@dvandersluis

Description

@dvandersluis

Lint/LiteralInInterpolation autocorrects literals inside string like nodes to remove the interpolation. However, this does not make sense for arrays inside a regexp, because the array is converted to a character class that is probably not expected.

For example:

/#{%w[a b c]}/

# compiles to... 
/["a", "b", "c"]/

# but it is much more likely the programmer wanted this instead
/[abc]/

The character class created also adds multiple quotes, spaces and commas depending on the array, which means that the rubocop correction of /#{%w[a b c]}/ becomes /[\"a, bc]/, which looks very different than where it started. As such, even if the programmer was trying to match the array literally, the resulting regexp will not do that.

I would like to suggest removing the ability for Lint/LiteralInInterpolation to deal with arrays inside a regexp, and instead add a new cop to detect literal arrays inside a regexp (and possibly correct them to proper character classes or alternation).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy