by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Oppen
    @Oppen
    Thanks.
    Oppen
    @Oppen
    Hi all. I'm working in a performance related cop. Should I send it to rubocop, rubocop-performance or both?
    Simon
    @stoivo
    I have a open PR rubocop-hq/rubocop#6649, Could someone take a look at it and give me some feedback
    Lewis Jones
    @LewisARJones_twitter
    Hi, I use Rubocop to write custom cops within my companies code bases to enforce our domain and help aid big refactors. I've got blocked trying to write cops that check if methods are called on instances of our domain object. Basically ruby classes we have created.
    For example I want to write a cop that creates an offence if anyone sends the method expiry_date to and instance of Quote object object.
    Can anyone point in the direction of an example of how to write a cop that can check the class of the object getting the method sent to it?
    Thomas Thomassen
    @thomthom
    @LewisARJones_twitter The dynamic nature of Ruby makes this hard. I've tried to do something similar for rubocop-sketchup - but I found no good way to do so. In some cases I've scanned the parent scope to find the initialisation of the instance. But the moment the instance is passed on to other methods it gets really difficult to track. In the end I fell back on doing simple naive checks like mentioned. Or make educated guesses on variable names.
    You can see this being a challenge even for IDEs like RubyMine or VSCode with Ruby+Solargraph extensions. They tend to make use of YARD to pick up types from doc comments to aid the static analysic. But they also have to analyse the whole project to have chance. If you have good YARD comments you could try to leverage that in your custom cops.
    Jônatas Davi Paganini
    @jonatas
    @LewisARJones_twitter any reason to not deprecate the usage of expiry_date in the Quote object? because it seems more reasonable than try to create a linter for it. Make the interface fail or warn developers about it. When they run tests they will know the api is no longer going to support it.
    Lewis Jones
    @LewisARJones_twitter
    Thanks for your help @jonatas and @thomthom
    Mailo Světel
    @roolo
    Hey guys, I just discovered AllCops are being rewritten by children configs, what is the right way to do it?
    https://gitlab.com/lipoqil/rubocop-mailo/blob/459031ee5e101aa471dca90b202836f8d37025ca/rubocop.yaml#L1-7 If I use AllCops - Exclude in file which inherits from this one, the configs from gem are ignored 🥺
    Rubocop 0.65.0
    Philipp Hansch
    @phansch
    Hi, is there some way to write a cop that detects usage of :[] on Hashes and suggest fetch instead? Specifically, how could I limit this to only work on Hash?
    I suppose this is more difficult as I thought because of Ruby's dynamic nature..
    i.e, I'm currently getting false positives on Dir[pat], some_array[x], etc
    Jonas Arvidsson
    @jonas054
    @phansch The short answer is "No". You'd have to add functionality in RuboCop to guess the type of values, similar to what "intellisense"/auto-completion does in IDEs.
    Colin Swenson-Healey
    @chozandrias76
    Has anyone else recently updated rubocop, added the rubocop-performance gem and found that rubocop no longer inspects any of the files you had it inspecting before?
    It is inspecting only 2 files now when I run rubocop and I don't know which those are or why anything has changed
    Lee-Jon
    @lee-jon
    Hey @chozandrias76 did you solve the issue? I have the same issue jumping from 47 -> 75
    Lee-Jon
    @lee-jon
    I solved it by deleting the yml file and rewriting it line by line.
    Vincent
    @vincent-psarga

    Hi all,
    I'm having some difficulties finding a good solution to my current issue. In the project I work on (cucumber-ruby), I'm getting Rubocop errors on some string formatting. We have enforced annotated style for string formatting:

    Style/FormatStringToken:
      EnforcedStyle: annotated

    but we also use formatting to specify number of decimals to keep in stone functions, eg:

    def format_duration(seconds)
      m, s = seconds.divmod(60)
      "#{m}m#{format('%.3f', s)}s"
    end

    And of course, rubocop complains that this is not annotated style:

    Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s).
            @io.print format_string(format('%.7f', stepdef_key.mean_duration), :skipped) + ' ' unless config.dry_run?

    How to solve this kind of problem ? (hopefully I'm on the right channel :) )

    Vincent
    @vincent-psarga
    ok, well after bit more research, that was pretty simple in fact: format('%<duration>.7f', duration: stepdef_key.mean_duration). I did not know the numbers formatting worked as the string ones :)
    CJ Avilla
    @w1zeman1p

    Anyone happen to have a link handy for where I might learn about how to initialize and run rubocop directly in Ruby? I'm trying to build something to check snippets of ruby in some documentation so the source file is a view template not a ruby file and contains other languages. I want to pass a string in Ruby to some actor and have it return cops or some such.

    For javascript, I'm doing something like

    Eslintrb.lint(raw_code, :eslintrc)

    I'll mostly be checking for Style cops
    halo
    @halo

    Hi! I have a question.

    I have a rubocop.yml on Github, which I inherit_from in all my projects. Some projects are pure Ruby, some Rails, and some use RSpec.

    It is fine to require: rubocop-rails in a Rails project, naturally.

    But in a pure Ruby project I would not want to demand that the rubocop-rails gem is installed (neither should other developers have to install that gem, nor is it needed on CI).

    So, how would I organize my shared rubocop config?

    I cannot just specify any Rails/ cop in it, because that raises an error here

    Similarly, the RSpec cops should only run in projects that use RSpec.

    My idea is to have a rubocop.ruby.yml and a rubocop.rails.yml etc. on Github and inherit only the ones I need in my project.

    Is that the recommended solution? I thought it was nice to have only one yml file with all cops, but I don't see how I could achieve that without the disadvantages I tried to describe.

    Thank you very much.

    Max Schwenk
    @maschwenk

    curious why ruby-parse considers a block that has multiple lines to be nested within a begin. I know this isn't a rubocop question, just trying to figure out how one would generically say:

    this method call within a block of this type

    is it easier to do that or

    this block with this method call within

    create_table :a_table do |z|
      z.goober :name
      z.string :col
    end
    
    create_table :a_table do |z|
      z.goober :name
    end

    parsed

    (begin
      (block
        (send nil :create_table
          (sym :a_table))
        (args
          (procarg0
            (arg :z)))
        (begin
          (send
            (lvar :z) :goober
            (sym :name))
          (send
            (lvar :z) :string
            (sym :col))))
      (block
        (send nil :create_table
          (sym :a_table))
        (args
          (procarg0
            (arg :z)))
        (send
          (lvar :z) :goober
          (sym :name))))
    matrixbot
    @matrixbot

    @myii:matrix.org Hi, loving rubocop, we've deployed it across over 60 of our repos under https://github.com/saltstack-formulas. Found an issue with rubocop -a --safe, the first time it's actually caused a breakage. This is the section, before and after:

    Resulting in:

    test/integration/default/controls/config_spec.rb:158:35: F: Lint/Syntax: unterminated string meets end of file

    Worth a bug report?

    Michael Hagar
    @mehagar

    Hi, I believe I found a bug that has possibly been fixed in a newer version of Rubocop. In version 0.57.2, this code:

    time_period = updated_during ? updated_during : 2.days.ago..Time.now

    is autocorrected to

    time_period = updated_during || 2.days.ago..Time.now

    However, that’s incorrect if updated_during is not nil, because || has a higher precedence than .., whereas ? has a lower precedence than ...

    Ankit Kumar
    @anks333
    how to configure rubocop in vs code, tried installing ruby-rubocop but throwing error env: jruby_executable_hooks: No such file or directory
    can someone help me to configure rubocop in vscode
    Jônatas Davi Paganini
    @jonatas

    Hello friends! Is there any research or ongoing mechanism to allow us to use RuboCop combining autocorrect with user input confirmation?

    I'm asking because today a friend shared a script (https://gist.github.com/jaimerson/57847f25191f58018af06c1c1ea0e4f6) that autocorrects subject names from RSpec/NamedSubject and gives the opportunity to fix it ongoing or accept the suggestions. While cleaning up offenses it would speed up a lot the flow and we could have a default and allow people to edit it if they want through the interactive mode.

    It would also allow us to interactive fix cops that doesn't have autocorrect implementation because of some the dependency of the user input.

    paulgureghian
    @paulgureghian_gitlab
    hi
    paulgureghian
    @paulgureghian_gitlab

    Trying to find the Rubocop yml file. I looked in the .gem and .rvm folders.

    https://imgur.com/a/Hf2htqm

    Andrew Jorgensen
    @ajorgensen
    I have a rubocop question, hopefully this is the right place. I have a custom cop that compares class names to make sure there are no duplicates. The issue that i have is the class names are stored in a class level variable that gets cached in rubocop-daemon between runs so the first run is successful but the second one detects a duplicate class. Is there anything in rubocop that would allow me to clear that cache after the run or a run identifier I can make sure that names are compared within the same run and not across runs?
    The only other way i can think to do it without a before_run, after_run hook or an identifier is to time out the cache after a certain period of time
    Andrew Jorgensen
    @ajorgensen
    It looks like the cops are created new for each source which makes cross file comparison difficult
    Andrew Jorgensen
    @ajorgensen
    sorry the above was actually more of a consequence of using rubocop-daemon rather than rubocop itself
    i was able to work around it by finding the Rubocop::Runner instance and using its object_id as a unique run identifier and then invalidating the cache if the run id is different. This allows us to detect different runs and invalidate the cache. I realize that comparing across all files is probably not a typical use case for Rubocop but in our setup (lots of rails engines) it was important to make sure there were not two classes named the same
    Brandon Weaver
    @baweaver

    @marcandre RE: https://github.com/rubocop-hq/rubocop/pull/7868#issuecomment-642392984

    May not quite be the right place for it but does unlock a lot of interesting potential for really succinct corrections

    Utkarsh Gupta
    @utkarsh2102
    Hiya
    I had a quick question that I am unable to figure out
    My PATTERN can't parse the second call to git, that is, spec.executables.
    Utkarsh Gupta
    @utkarsh2102
    Any clue what am I doing wrong? Or how can I go about fixing this?
    Utkarsh Gupta
    @utkarsh2102
    CC: @marcandre, guess you'd know^ :)
    Marc-André Lafortune
    @marcandre
    @utkarsh2102 The descend operator only looks for the first descendant it encounters. You'll have to split your pattern in two
    Marc-André Lafortune
    @marcandre
    We should consider something do select all that match instead of any that match though.
    Utkarsh Gupta
    @utkarsh2102
    @marcandre, ah, didn't know this, thanks :)
    If it's not much to ask, could you also help me figure out how to do this?
    Marc-André Lafortune
    @marcandre
    I'd first look for the right module, then use def_node_search to find what you're looking for. HTH