kspurgin / almost_standard

Shared almost standard rubocop linting config

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AlmostStandard

Use in your project

1. Add dependency

Add almost_standard to your Gemfile in a development (or other non-default) group. Example:

group :development do
  gem "pry", "~> 0.14"
  gem "rake", "~> 13.0"
  gem "almost_standard", github: "kspurgin/almost_standard", branch: "main"
end

OR:

gem "almost_standard", github: "kspurgin/almost_standard", branch: "main", group: :development

2. Inherit rubocop config

Create a .rubocop.yml file in the base directory of your project. The contents of the file should simply be:

  inherit_gem:
    almost_standard: .rubocop.yml

Theoretically you can add further overrides in your projects’s .rubocop file, but that takes us even further away from the spirit of things.

What’s not Standard?

Runs Standard with the following overrides/customizations:

Style/FrozenStringLiteralComment enabled, with enforced style always

This means that all files should have the magic comment

# frozen_string_literal: true

or

# frozen_string_literal: false

This cop was added in Ruby 2.3, when it was on the roadmap to make all string literals frozen by default in Ruby 3. This plan was abandoned, leading some to question the continued relevance of enabling this cop by default.

The cop is still enabled by default in Rubocop.

  • "matz said default frozen string literals were never going to happen."

  • not liking two extra lines at the top of every file

I’m overriding this because:

  1. That’s not what matz said. Matz said it wasn’t going to happen in Ruby 3.

I officially abandon making frozen-string-literals default (for Ruby3). --https://bugs.ruby-lang.org/issues/11473#note-53[matz]

It is reported that Matz would like to implement default frozen string literals in the future.

I would like my code to be ready if/when this happens. If it never happens #2 is still salient.

  1. In the discussion of this cop for Standard, a number of experienceD and trusted Ruby community members attest to the performance benefits of freezing string literals by default.

A number of Ruby libraries such as CSV and Psych (yaml) default to frozen string literals already.

In the kind of data-processing code I tend to write, mutable strings can be a real bug liability.

Layout/LineContinuationSpacing enforced style no_space

This cop is enabled by Standard with the default enforced style space.

Given that line continuation is used to break long lines, it seems space-wasteful to require an extra space be inserted between end-of-string segment and \ (line continuation backslash)

That extra space could cause a string that otherwise fit on the line to need to be broken into multiple lines.

Layout/LineEndStringConcatenationIndentation enabled with enforced style indented

Disabled in Standard.

I’m overriding this because Standard seems to rather consistently prefer indentation of continued code elements/ideas. Leaving this disabled seems inconsistent.

Layout/LineLength enabled with max=80

The other configurable values of this cop are left at default.

This seems to be ignored by Standard because it is "a personal preference" and/or "editors wrap lines automatically".

I am overriding this because:

  • We have accessibility standards, and personal preferences should not override accessibility. Code is almost always read on screen, and WCAG guidelines specify a maximum line width of 80 characters.

  • Editor text wrapping often produces bizarre-looking Ruby that is visually difficult to follow.

  • Laptop screens are not getting bigger, and if you need to regularly code in one window while referring to something else in another window, 80 character lines just work.

Implementation notes

💡

rubocop -a fill fix many line length offenses, however it is conservative in which lines it tries to correct.

If you are left with many long line offenses, run the following to mark them as "rubocop:todo" in your code. They will no longer show up as offenses, and you can deal with them as you work in the code base over time.

rubocop --disable-uncorrectable --only Layout/LineLength

Rubocop recommends a number of other cops be enabled if you wish to use autocorrection for Layout/LineLength. The cops below are in that list of recommendations, but are disabled by Standard (1.31.0).

These are enabled by default by Rubocop, and enabled here (with their default settings) as per that recommendation:

  • Style/BlockDelimiters

  • Layout/FirstParameterIndentation

These are disabled by default by Rubocop and left disabled here:

  • Layout/MultilineArrayLineBreaks

  • Layout/MultilineHashKeyLineBreaks

  • Layout/MultilineMethodArgumentLineBreaks

  • Layout/MultilineMethodParameterLineBreaks

About

Shared almost standard rubocop linting config

License:MIT License


Languages

Language:Ruby 94.8%Language:Shell 5.2%