Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
422 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# This module provides functionality for checking if numbering match the | ||
# configured EnforcedStyle. | ||
module ConfigurableNumbering | ||
include ConfigurableEnforcedStyle | ||
|
||
SNAKE_CASE = /^@{0,2}[-_a-z]+[_\D]*(_\d)*[!?=]?$/ | ||
NORMAL_CASE = /^@{0,2}-{0,1}_{0,1}[a-zA-Z\d]*[_\D]*[!?=]?$/ | ||
NON_INTEGER = /^@{0,2}[-_a-z]+[_\D]*[!?=]?$/ | ||
|
||
def check_name(node, name, name_range) | ||
return if operator?(name) | ||
|
||
if valid_name?(node, name) | ||
correct_style_detected | ||
else | ||
add_offense(node, name_range, message(style)) | ||
end | ||
end | ||
|
||
def valid_name?(node, name) | ||
pattern = | ||
case style | ||
when :snake_case | ||
SNAKE_CASE | ||
when :normalcase | ||
NORMAL_CASE | ||
when :non_integer | ||
NON_INTEGER | ||
end | ||
name.match(pattern) || class_emitter_method?(node, name) | ||
end | ||
|
||
# A class emitter method is a singleton method in a class/module, where | ||
# the method has the same name as a class defined in the class/module. | ||
def class_emitter_method?(node, name) | ||
return false unless node.parent && node.defs_type? | ||
# a class emitter method may be defined inside `def self.included`, | ||
# `def self.extended`, etc. | ||
node = node.parent while node.parent.defs_type? | ||
|
||
node.parent.each_child_node(:class).any? do |c| | ||
c.loc.name.is?(name.to_s) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop makes sure that all numbered variables use the | ||
# configured style, snake_case, normalcase or non_integer, | ||
# for their numbering. | ||
# | ||
# @example | ||
# "EnforcedStyle => 'snake_case'" | ||
# | ||
# # bad | ||
# | ||
# variable1 = 1 | ||
# | ||
# # good | ||
# | ||
# variable_1 = 1 | ||
# | ||
# @example | ||
# "EnforcedStyle => 'normalcase'" | ||
# | ||
# # bad | ||
# | ||
# variable_1 = 1 | ||
# | ||
# # good | ||
# | ||
# variable1 = 1 | ||
# | ||
# @example | ||
# "EnforcedStyle => 'non_integer'" | ||
# | ||
# #bad | ||
# | ||
# variable1 = 1 | ||
# | ||
# variable_1 = 1 | ||
# | ||
# #good | ||
# | ||
# variableone = 1 | ||
# | ||
# variable_one = 1 | ||
# | ||
class VariableNumber < Cop | ||
include ConfigurableNumbering | ||
|
||
def on_lvasgn(node) | ||
name, = *node | ||
check_name(node, name, node.loc.name) | ||
end | ||
|
||
def on_ivasgn(node) | ||
name, = *node | ||
check_name(node, name, node.loc.name) | ||
end | ||
|
||
def on_cvasgn(node) | ||
name, = *node | ||
check_name(node, name, node.loc.name) | ||
end | ||
|
||
def on_arg(node) | ||
name, = *node | ||
check_name(node, name, node.loc.name) | ||
end | ||
|
||
private | ||
|
||
def message(style) | ||
format('Use %s for variable numbers.', style) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.