Class: RuboCop::Cop::Lint::NameTypo
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::ProjectIndexHelp,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| Defined in: | lib/rubocop/cop/lint/name_typo.rb |
Overview
Checks for probable typos in constant and method names: a name that does not resolve anywhere in the project, used in a namespace the project does define, with a close-named sibling to suggest instead.
The check is powered by the project-wide index, so it only runs when
AllCops/UseProjectIndex is enabled and the rubydex gem is installed.
Without the index the cop does nothing.
Constants are checked only in qualified references (Foo::Bar) whose
namespace resolves in the index; bare names cannot be distinguished
from constants provided by gems or the standard library. Methods are
checked only in calls on constant receivers (Foo.bar) whose entire
indexed ancestry is resolved, so methods gained through gem classes or
dynamic definitions never produce offenses. In both cases an offense
requires a similarly named alternative to exist — an unknown name
alone is not reported, since the index does not see gems or the
standard library.
Names that appear as symbols or inside string literals in the same
file are never reported, since they usually belong to runtime
definitions the index cannot see (stub_const, const_set,
define_method, and the like).
Constant Summary
-
CONSTANT_MSG =
# File 'lib/rubocop/cop/lint/name_typo.rb', line 63'Possible typo: `%<name>s` is not defined in `%<namespace>s`. ' \ 'Did you mean `%<suggestion>s`?'
-
LITERAL_IDENTIFIER_PATTERN =
# File 'lib/rubocop/cop/lint/name_typo.rb', line 69/[a-zA-Z_]\w*[?!]?/.freeze
-
METHOD_MEMBER_REGEXP =
# File 'lib/rubocop/cop/lint/name_typo.rb', line 68/#([a-zA-Z_]\w*[?!]?)\(\)\z/.freeze
-
METHOD_MSG =
# File 'lib/rubocop/cop/lint/name_typo.rb', line 65'Possible typo: `%<receiver>s` does not respond to `%<name>s`. ' \ 'Did you mean `%<suggestion>s`?'
::RuboCop::Cop::Base - Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::ProjectIndexHelp - Included
Class Attribute Summary
::RuboCop::Cop::Base - Inherited
| .gem_requirements, .lint?, | |
| .support_autocorrect? | Returns if class supports autocorrect. |
| .support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to #on_new_investigation and |
Class Method Summary
::RuboCop::Cop::Base - Inherited
| .autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
| .badge | Naming. |
| .callbacks_needed, .cop_name, .department, | |
| .documentation_url | Returns a url to view this cops documentation online. |
| .exclude_from_registry | Call for abstract Cop classes. |
| .inherited, | |
| .joining_forces | Override and return the Force class(es) you need to join. |
| .match? | Returns true if the cop name or the cop namespace matches any of the given names. |
| .new, | |
| .requires_gem | Register a version requirement for the given gem name. |
| .restrict_on_send | Reserved for Commissioner. |
::RuboCop::ExcludeLimit - Extended
| exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
| transform | |
Instance Attribute Summary
::RuboCop::Cop::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
Instance Method Summary
- #on_const(node)
-
#on_csend(node)
Alias for #on_send.
- #on_new_investigation
- #on_send(node) (also: #on_csend)
- #allowed_name?(name) ⇒ Boolean private
- #check?(key) ⇒ Boolean private
- #checkable_constant?(node) ⇒ Boolean private
- #constant_member_names(namespace) private
- #constant_typo_suggestion(node) private
-
#defined_check?(node) ⇒ Boolean
private
defined?(Foo::Bar)probes whether a name exists; unknown names there are deliberate. -
#definition_identifier?(node) ⇒ Boolean
private
The last segment of a class, module or constant definition is being defined, not referenced.
-
#literal_names
private
Names mentioned as symbols or inside string literals in the current file belong to runtime definitions (
stub_const,const_set,define_method) that the index cannot see. - #method_member_names(declaration) private
-
#method_typo_suggestion(node)
private
Writers are indexed under the reader’s name, so setter calls are verified and spell-checked through the base name.
- #responds_in_index?(declaration, name, base) ⇒ Boolean private
- #setter_call?(node) ⇒ Boolean private
- #spell_check(name, dictionary) private
-
#unknown_method_owner(node, base)
private
The receiver’s declaration when it resolves in the index, its whole ancestry is resolved, and the method is not found — nil otherwise.
::RuboCop::Cop::ProjectIndexHelp - Included
| #external_dependency_checksum, #compute_project_index_signature, | |
| #definitions_in_other_files | Returns the definitions among |
| #fully_resolved_index_ancestry? | Whether every link of the declaration’s ancestry is resolved in the index: no definition of any ancestor has an unresolved superclass or mixin reference. |
| #indexed_singleton_member | A namespace without any singleton method has no singleton-class declaration of its own, so the lookup starts from the first ancestor that has one; its |
| #indexed_singleton_of | The declaration of `declaration’s singleton class, or nil when no singleton method is defined on it anywhere in the project. |
| #inherited_index_member? | Whether an ancestor of |
| #lexical_nesting_of | The lexical nesting the node’s constants resolve through, outermost first. |
| #prior_definition_in_other_file, #project_index_signature, | |
| #resolve_constant_in_index | Resolves a constant node the way Ruby does: the first segment through the lexical nesting and every following segment inside the previous one. |
| #resolved_mixin_references?, #resolved_superclass_reference?, #same_file? | |
::RuboCop::Cop::Base - Inherited
| #add_global_offense | Adds an offense that has no particular location. |
| #add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
| #begin_investigation | Called before any investigation. |
| #callbacks_needed, | |
| #cop_config | Configuration Helpers. |
| #cop_name, #excluded_file?, | |
| #external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
| #inspect, | |
| #message | Gets called if no message is specified when calling |
| #name | Alias for Base#cop_name. |
| #offenses, | |
| #on_investigation_end | Called after all on_… |
| #on_new_investigation | Called before all on_… |
| #on_other_file | Called instead of all on_… |
| #parse | There should be very limited reasons for a Cop to do it’s own parsing. |
| #parser_engine, | |
| #ready | Called between investigations. |
| #relevant_file?, | |
| #target_gem_version | Returns a gems locked versions (i.e. |
| #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
| #callback_argument | Reserved for Cop::Cop. |
| #complete_investigation | Called to complete an investigation. |
| #correct, #current_corrector, | |
| #current_offense_locations | Reserved for Commissioner: |
| #current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #range_for_original, #range_from_node_or_range, | |
| #reset_investigation | Actually private methods. |
| #use_corrector | |
::RuboCop::Cop::AutocorrectLogic - Included
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#allowed_name?(name) ⇒ Boolean (private)
# File 'lib/rubocop/cop/lint/name_typo.rb', line 115
def allowed_name?(name) cop_config.fetch('AllowedNames', []).include?(name.to_s) end
#check?(key) ⇒ Boolean (private)
# File 'lib/rubocop/cop/lint/name_typo.rb', line 105
def check?(key) project_index && defined?(DidYouMean::SpellChecker) && cop_config.fetch(key, true) end
#checkable_constant?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/lint/name_typo.rb', line 109
def checkable_constant?(node) node.namespace&.const_type? && !allowed_name?(node.short_name) && !definition_identifier?(node) && !defined_check?(node) end
#constant_member_names(namespace) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 199
def constant_member_names(namespace) namespace.members.filter_map do |member| name = member.name name.split('::').last unless name.include?('#') || name.include?('<') end end
#constant_typo_suggestion(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 131
def constant_typo_suggestion(node) return nil if literal_names.include?(node.short_name.to_s) namespace = resolve_constant_in_index(node.namespace) return nil unless namespace.is_a?(Rubydex::Namespace) return nil if namespace.find_member(node.short_name.to_s) spell_check(node.short_name, constant_member_names(namespace)) rescue StandardError nil end
#defined_check?(node) ⇒ Boolean (private)
defined?(Foo::Bar) probes whether a name exists; unknown names
there are deliberate.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 127
def defined_check?(node) node.each_ancestor(:defined?).any? end
#definition_identifier?(node) ⇒ Boolean (private)
The last segment of a class, module or constant definition is being defined, not referenced.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 121
def definition_identifier?(node) node.parent&.defined_module end
#literal_names (private)
Names mentioned as symbols or inside string literals in the current
file belong to runtime definitions (stub_const, const_set,
define_method) that the index cannot see.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 187
def literal_names @literal_names ||= processed_source.ast.each_descendant(:sym, :str) .with_object(Set.new) do |literal, names| if literal.sym_type? names << literal.value.to_s else literal.value.scan(LITERAL_IDENTIFIER_PATTERN) { |token| names << token } end end end
#method_member_names(declaration) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 206
def method_member_names(declaration) scopes = declaration.ancestors.filter_map { |ancestor| indexed_singleton_of(ancestor) } scopes << declaration scopes.flat_map do |scope| scope.members.filter_map { |member| member.name[METHOD_MEMBER_REGEXP, 1] } end end
#method_typo_suggestion(node) (private)
Writers are indexed under the reader’s name, so setter calls are verified and spell-checked through the base name.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 145
def method_typo_suggestion(node) setter = setter_call?(node) base = setter ? node.method_name.to_s.delete_suffix('=') : node.method_name.to_s return nil if literal_names.include?(base) declaration = unknown_method_owner(node, base) return nil unless declaration suggestion = spell_check(base, method_member_names(declaration)) suggestion && setter ? "#{suggestion}=" : suggestion rescue StandardError nil end
#on_const(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 71
def on_const(node) return unless check?('CheckConstants') && checkable_constant?(node) suggestion = constant_typo_suggestion(node) return unless suggestion = format(CONSTANT_MSG, name: node.short_name, namespace: node.namespace.const_name, suggestion: suggestion) add_offense(node.loc.name, message: ) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 96
alias on_csend on_send
#on_new_investigation
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 98
def on_new_investigation @literal_names = nil super end
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 83
def on_send(node) return unless check?('CheckMethods') return unless node.receiver&.const_type? return if allowed_name?(node.method_name) || defined_check?(node) suggestion = method_typo_suggestion(node) return unless suggestion = format(METHOD_MSG, receiver: node.receiver.const_name, name: node.method_name, suggestion: suggestion) add_offense(node.loc.selector, message: ) end
#responds_in_index?(declaration, name, base) ⇒ Boolean (private)
# File 'lib/rubocop/cop/lint/name_typo.rb', line 174
def responds_in_index?(declaration, name, base) # Instance methods of a module are also callable on the module # itself when exposed with `module_function`. [name, base].uniq.any? do |candidate| member_name = "#{candidate}()" indexed_singleton_member(declaration, member_name) || declaration.find_member(member_name) end end
#setter_call?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/lint/name_typo.rb', line 170
def setter_call?(node) node.assignment_method? && !node.operator_method? end
#spell_check(name, dictionary) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/name_typo.rb', line 215
def spell_check(name, dictionary) return nil if dictionary.empty? DidYouMean::SpellChecker.new(dictionary: dictionary.uniq).correct(name.to_s).first end
#unknown_method_owner(node, base) (private)
The receiver’s declaration when it resolves in the index, its whole ancestry is resolved, and the method is not found — nil otherwise.
# File 'lib/rubocop/cop/lint/name_typo.rb', line 161
def unknown_method_owner(node, base) declaration = resolve_constant_in_index(node.receiver) return nil unless declaration.is_a?(Rubydex::Namespace) return nil if responds_in_index?(declaration, node.method_name.to_s, base) return nil unless fully_resolved_index_ancestry?(declaration) declaration end