Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect top-level constant usage like ::Const #8245

Merged
merged 25 commits into from Jul 6, 2020

Conversation

biinari
Copy link
Contributor

@biinari biinari commented Jul 6, 2020

[Fix #8215]

Detect top-level constant usage (in offenses or allowed forms) like ::Const in the following cops:

  • Gemspec/RubyVersionGlobalsUsage - ::RUBY_VERSION
  • Lint/NestedMethodDefinition - ::Class.new, ::Module.new, ::Struct.new
  • Lint/NonDeterministicRequireOrder - ::Dir
  • Lint/RandOne - ::Kernel.rand(1)
  • Lint/RedundantSplatExpansion - ::Array.new
  • Lint/UnusedMethodArgument - ::NotImplementedError
  • Lint/UselessAccessModifier - ::Class.new, ::Module.new, ::Struct.new
  • Metrics/ClassLength - ::Class.new
  • Metrics/ModuleLength - ::Module.new
  • Style/DateTime - Allow using ::Date constants for historic date
  • Style/Dir - ::File
  • Style/EmptyLiteral - ::Array.new, ::Hash.new, ::String.new
  • Style/ExpandPathArguments - ::File.expand_path, ::Pathname.new
  • Style/MutableConstant - ::ENV, ::Struct
  • Style/Proc - ::Proc.new
  • Style/RandomWithOffset - ::Kernel.rand, ::Random.rand
  • Style/RedundantException - ::RuntimeError
  • Style/RedundantFreeze - ::ENV
  • Style/RescueStandardError - ::StandardError
  • Style/SignalException - ::Kernel.raise, ::Kernel.fail
  • Style/StderrPuts - ::STDERR
  • Style/StructInheritance - ::Struct
  • Style/SymbolProc - ::Proc.new
  • Style/ZeroLengthPredicate - ::File.stat, ::Tempfile.new, ::StringIO.new

Also in Mixin/EnforceSuperclass used by rubocop-rails ApplicationRecord and maybe other gems:

  • Detect ::Class.new and Class.new with or without a block.
  • Added a spec using a stubbed cop class to include the mixin.

If any of these changes are unwanted or desired to be split out, I'm happy to do so. That is why I have not yet squashed my commits.

I have not added any examples to documentation as they would not aid the reader's understanding of what the cops are looking for.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

biinari added 25 commits July 6, 2020 05:46
Detect `::Class.new` and `Class.new` in EnforceSuperclass mixin, with or
without a block.
Detect `::RUBY_VERSION` in Gemspec/RubyVersionGlobalsUsage cop.
Detect `::Class.new`, `::Module.new`, `::Struct.new` in top-level namespace for
Lint/NestedMethodDefinition.
Detect top-level constant `::Dir` in Lint/NonDeterministicRequireOrder.
Detect top-level constant `::Kernel.rand(1)` in Lint/RandOne cop.
Detect redundant array splat with top-level constant `::Array`.
Detect useless access modifier in `::Class.new`, `::Module.new`, `::Struct.new` blocks.
Detect `::Class.new`, `::Module.new` in Metrics/ClassLength and
Metrics/ModuleLength cops.
Allow historic DateTime using `::Date` constants in Style/DateTime cop.
Detect `::Array.new`, `::Hash.new`, `::String.new` producing empty literals.
Detect `::Proc.new` with a block in Style/Proc.
Detect with top-level constant as `::Kernel.raise` and `::Kernel.fail`
Allow `::File.stat`, `::Tempfile.new`, `::StringIO.new`
Detect `::Random.rand`, `::Kernel.rand` in Style/RandomWithOffset.
Detect `::File.expand_path`, `::Pathname.new` in Style/ExpandPathArguments cop.
Allow top-level `::ENV`, `::Struct` in Style/MutableConstant cop.
Detect top-level constants
Copy link
Collaborator

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's one massive PR! The changes look good to me. Thanks for tacking this!

@bbatsov bbatsov requested review from marcandre and koic July 6, 2020 05:30
@marcandre
Copy link
Contributor

This is great! I've been noticing the same mistake too, e.g.: rubocop/rubocop-ast#30

I was wondering if having a function for this wouldn't be a good idea:

def_node_matcher :top_level_constant?, "(const {nil? (cbase)} %1)"

# example of use:
def_node_matcher :unsorted_dir_each?, <<~PATTERN
    (send (send (top_level_constant?(:Dir) {:[] :glob} ...) :each)
PATTERN

We could also add an internal cop against use of (const nil? ...)

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 6, 2020

We could also add an internal cop against use of (const nil? ...)

Good idea!

@marcandre
Copy link
Contributor

So should we merge this as is, or introduce top_level_constant? ?

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 6, 2020

So should we merge this as is, or introduce top_level_constant? ?

I think we should add the helper method, but we can do this after this PR has been merged. I was thinking of cutting a new release today, so I can squeeze this PR in it.

@marcandre
Copy link
Contributor

I'm 👍 to merge as is.

Are you sure you want to release another 0.x with the Cop refactoring merged in? Or if you were thinking of 1.0.0rc, there are a few issues still waiting for your decision that would be great to resolve first

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 6, 2020

Are you sure you want to release another 0.x with the Cop refactoring merged in?

We better get any bad feedback before 1.0. :-)

Or if you were thinking of 1.0.0rc, there are a few issues still waiting for your decision that would be great to resolve first

Probably we should open some ticket with everything outstanding, as I might have missed some of those. Anyways, we'll figure it out.

@bbatsov bbatsov merged commit 040e829 into rubocop:master Jul 6, 2020
@biinari
Copy link
Contributor Author

biinari commented Jul 6, 2020

That's one massive PR!

Sorry it might have been big to look at but it was just repetitions of the same thing in multiple cops (and some spec coverage).

Thanks @bbatsov and @marcandre for reviewing and merging so quickly. Hope everything is going smoothly towards the 1.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect top-level qualified constant lookups (cbase)
3 participants