This is a custom Rubocop cop to detect abbreviations in the names of classes, methods, variables, and constants. It does this by taking each definition, splitting it up into individual words, and running it through a spellchecker. The english spellchecker is from ffi-aspell and the ruby and html dictionaries are from spellr. As a nice side-effect, this helps us to avoid misspellings in our names.
Note that spellr and ffi-aspell are required for this, but could
be swapped out or removed if desired. Small editing will be needed
to remove them from abbreviations.rb
.
Note that this doesn't care about using the abbreviations, only defining them. So for example, these will receive a warning:
abc_thing = ...
class AbcThing
ABC_CONST = ...
def abc_code
but these will not:
abc_thing(code: abc)
AbcCode.call
list.include?(ABC_CONST)
Most repos use a bunch of words that are not in any dictionary but are nonetheless valid. These words can be added in the configuration for the cop which looks like:
require:
- ./path/to/abbreviations.rb
CustomCops/Abbreviations:
AllowedWords:
- cancellable
QuestionableWords:
- aad
AllowedFileWords:
- path: Gemfile
words:
- repo
These words are we expect to not be in the dictionary but are valid
words. Example: arel
, stubber
, dnd
These are words that should likely be removed, fixed, or that are
actually valid but you might not know. The goal would be to remove this
section entirely over time. It's here now because it allows for increment
fixes. Examples: foo
, pll
, authoized
(misspelling of authorized
),
In some cases, we want to only allow the word in a single file because
they are required by some external API or for whatever reason they're
okay in one place but not another. Examples: config
in this custom cop
because cop_config
is a RuboCop value, repo
in the Gemfile.
You can run this cop on the whole codebase or a single file:
bin/rubocop --only CustomCops/Abbreviations path/to/abbreviations.rb