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

Auto corrected by following Format Ruby Code #387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rails_best_practices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
require 'rails_best_practices/option_parser'
require 'rails_best_practices/cli'

module RailsBestPractices; end
module RailsBestPractices
end
86 changes: 40 additions & 46 deletions lib/rails_best_practices/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,17 @@ def parse_files
files = expand_dirs_to_files(@path)
files = file_sort(files)

if @options['only'].present?
files = file_accept(files, @options['only'])
end
files = file_accept(files, @options['only']) if @options['only'].present?

# By default, tmp, vender, spec, test, features are ignored.
%w[vendor spec test features tmp].each do |dir|
files = file_ignore(files, File.join(@path, dir)) unless @options[dir]
end

# Exclude files based on exclude regexes if the option is set.
@options['exclude'].each do |pattern|
files = file_ignore(files, pattern)
end
@options['exclude'].each { |pattern| files = file_ignore(files, pattern) }

%w[Capfile Gemfile Gemfile.lock].each do |file|
files.unshift File.join(@path, file)
end
%w[Capfile Gemfile Gemfile.lock].each { |file| files.unshift File.join(@path, file) }

files.compact
end
Expand All @@ -138,15 +132,18 @@ def parse_files
def expand_dirs_to_files(*dirs)
extensions = %w[rb erb rake rhtml haml slim builder rxml rabl]

dirs.flatten.map do |entry|
next unless File.exist? entry
dirs
.flatten
.map do |entry|
next unless File.exist? entry

if File.directory? entry
Dir[File.join(entry, '**', "*.{#{extensions.join(',')}}")]
else
entry
if File.directory? entry
Dir[File.join(entry, '**', "*.{#{extensions.join(',')}}")]
else
entry
end
end
end.flatten
.flatten
end

# sort files, models first, mailers, helpers, and then sort other files by characters.
Expand Down Expand Up @@ -234,9 +231,11 @@ def load_git_info
def output_html_errors
require 'erubis'
template =
@options['template'] ?
File.read(File.expand_path(@options['template'])) :
if @options['template']
File.read(File.expand_path(@options['template']))
else
File.read(File.join(File.dirname(__FILE__), '..', '..', 'assets', 'result.html.erb'))
end

if @options['with-github']
last_commit_id = @options['last-commit-id'] || `cd #{@runner.class.base_path} && git rev-parse HEAD`.chomp
Expand Down Expand Up @@ -266,51 +265,46 @@ def output_html_errors
def output_xml_errors
require 'rexml/document'

document =
REXML::Document.new.tap do |d|
d << REXML::XMLDecl.new
end
document = REXML::Document.new.tap { |d| d << REXML::XMLDecl.new }

checkstyle = REXML::Element.new('checkstyle', document)

errors.group_by(&:filename).each do |file, group|
REXML::Element.new('file', checkstyle).tap do |f|
f.attributes['name'] = file
group.each do |error|
REXML::Element.new('error', f).tap do |e|
e.attributes['line'] = error.line_number
e.attributes['column'] = 0
e.attributes['severity'] = 'error'
e.attributes['message'] = error.message
e.attributes['source'] = 'com.puppycrawl.tools.checkstyle.' + error.type
errors
.group_by(&:filename)
.each do |file, group|
REXML::Element
.new('file', checkstyle)
.tap do |f|
f.attributes['name'] = file
group.each do |error|
REXML::Element
.new('error', f)
.tap do |e|
e.attributes['line'] = error.line_number
e.attributes['column'] = 0
e.attributes['severity'] = 'error'
e.attributes['message'] = error.message
e.attributes['source'] = 'com.puppycrawl.tools.checkstyle.' + error.type
end
end
end
end
end
end

formatter = REXML::Formatters::Default.new
File.open(@options['output-file'], 'w+') do |result|
formatter.write(document, result)
end
File.open(@options['output-file'], 'w+') { |result| formatter.write(document, result) }
end

# output errors with yaml format.
def output_yaml_errors
File.open(@options['output-file'], 'w+') do |file|
file.write YAML.dump(errors)
end
File.open(@options['output-file'], 'w+') { |file| file.write YAML.dump(errors) }
end

# output errors with json format.
def output_json_errors
errors_as_hashes =
errors.map do |err|
{ filename: err.filename, line_number: err.line_number, message: err.message }
end
errors.map { |err| { filename: err.filename, line_number: err.line_number, message: err.message } }

File.open(@options['output-file'], 'w+') do |file|
file.write JSON.dump(errors_as_hashes)
end
File.open(@options['output-file'], 'w+') { |file| file.write JSON.dump(errors_as_hashes) }
end

# plain output with color.
Expand Down
4 changes: 1 addition & 3 deletions lib/rails_best_practices/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class CLI
# RailsBestPractices::CLI.run(['-d', '-o', 'path/to/file'])
def self.run(argv)
options = OptionParser.parse!(argv)
if !argv.empty? && !File.exist?(argv.first)
raise Errno::ENOENT, "#{argv.first} doesn't exist"
end
raise Errno::ENOENT, "#{argv.first} doesn't exist" if !argv.empty? && !File.exist?(argv.first)

analyzer = Analyzer.new(argv.first, options)
analyzer.analyze
Expand Down
44 changes: 14 additions & 30 deletions lib/rails_best_practices/core/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class Check < CodeAnalyzer::Checker
SKIP_FILES = %r{db/schema.rb}.freeze

def initialize(options = {})
options.each do |key, value|
instance_variable_set("@#{key}", value)
end
options.each { |key, value| instance_variable_set("@#{key}", value) }
end

# check if the check will need to parse the node file.
Expand All @@ -39,11 +37,7 @@ def parse_file?(node_file)

def is_interesting_file?(node_file)
interesting_files.any? do |pattern|
if pattern == ALL_FILES
node_file =~ pattern && node_file !~ SKIP_FILES
else
node_file =~ pattern
end
pattern == ALL_FILES ? node_file =~ pattern && node_file !~ SKIP_FILES : node_file =~ pattern
end
end

Expand All @@ -63,7 +57,11 @@ def regex_ignored_files
def add_error(message, filename = @node.file, line_number = @node.line_number)
errors <<
RailsBestPractices::Core::Error.new(
filename: filename, line_number: line_number, message: message, type: self.class.to_s, url: url
filename: filename,
line_number: line_number,
message: message,
type: self.class.to_s,
url: url
)
end

Expand Down Expand Up @@ -270,9 +268,7 @@ def skip_command_callback_nodes
# super options.merge(exclude: :visible, methods: [:is_discussion_conversation])
# end
add_callback :start_bare_assoc_hash do |node|
if node.hash_keys.include? 'methods'
mark_used(node.hash_value('methods'))
end
mark_used(node.hash_value('methods')) if node.hash_keys.include? 'methods'
end

# remember the first argument for try and send method.
Expand Down Expand Up @@ -306,9 +302,7 @@ def mark_used(method_node)

def call_method(method_name, class_name = nil)
class_name ||= respond_to?(:current_class_name) ? current_class_name : current_module_name
if methods.has_method?(class_name, method_name)
methods.get_method(class_name, method_name).mark_used
end
methods.get_method(class_name, method_name).mark_used if methods.has_method?(class_name, method_name)
methods.mark_parent_class_method_used(class_name, method_name)
methods.mark_subclasses_method_used(class_name, method_name)
methods.possible_public_used(method_name)
Expand All @@ -326,23 +320,17 @@ def self.included(base)

# check if the controller is inherit from InheritedResources::Base.
add_callback :start_class do |_node|
if current_extend_class_name == 'InheritedResources::Base'
@inherited_resources = true
end
@inherited_resources = true if current_extend_class_name == 'InheritedResources::Base'
end

# check if there is a DSL call inherit_resources.
add_callback :start_var_ref do |node|
if node.to_s == 'inherit_resources'
@inherited_resources = true
end
@inherited_resources = true if node.to_s == 'inherit_resources'
end

# check if there is a DSL call inherit_resources.
add_callback :start_vcall do |node|
if node.to_s == 'inherit_resources'
@inherited_resources = true
end
@inherited_resources = true if node.to_s == 'inherit_resources'
end
end
end
Expand Down Expand Up @@ -399,16 +387,12 @@ def self.included(base)

# remember the current access control for methods.
add_callback :start_var_ref do |node|
if %w[public protected private].include? node.to_s
@access_control = node.to_s
end
@access_control = node.to_s if %w[public protected private].include? node.to_s
end

# remember the current access control for methods.
add_callback :start_vcall do |node|
if %w[public protected private].include? node.to_s
@access_control = node.to_s
end
@access_control = node.to_s if %w[public protected private].include? node.to_s
end

# set access control to "public" by default.
Expand Down
3 changes: 2 additions & 1 deletion lib/rails_best_practices/core/configs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module RailsBestPractices
module Core
class Configs < Hash; end
class Configs < Hash
end
end
end
3 changes: 2 additions & 1 deletion lib/rails_best_practices/core/controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module RailsBestPractices
module Core
# Controller classes.
class Controllers < Klasses; end
class Controllers < Klasses
end
end
end
3 changes: 2 additions & 1 deletion lib/rails_best_practices/core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module RailsBestPractices
module Core
# Helper moduels.
class Helpers < Modules; end
class Helpers < Modules
end
end
end
4 changes: 1 addition & 3 deletions lib/rails_best_practices/core/klasses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def initialize(class_name, extend_class_name, modules)
@modules = modules.dup
base = @modules.map { |modu| "#{modu}::" }.join('')
@class_name = base + class_name
if extend_class_name
@extend_class_name = base + extend_class_name
end
@extend_class_name = base + extend_class_name if extend_class_name
end

def to_s
Expand Down
3 changes: 2 additions & 1 deletion lib/rails_best_practices/core/mailers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module RailsBestPractices
module Core
# Mailer classes.
class Mailers < Klasses; end
class Mailers < Klasses
end
end
end
35 changes: 19 additions & 16 deletions lib/rails_best_practices/core/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def add_method(class_name, method_name, meta = {}, access_control = 'public')
return if has_method?(class_name, method_name)

methods(class_name) << Method.new(class_name, method_name, access_control, meta)
if access_control == 'public'
@possible_methods[method_name] = false
end
@possible_methods[method_name] = false if access_control == 'public'
end

# Get methods of a class.
Expand Down Expand Up @@ -72,11 +70,14 @@ def mark_parent_class_method_used(class_name, method_name)
# @param [String] class name
# @param [String] method name
def mark_subclasses_method_used(class_name, method_name)
Prepares.klasses.select { |klass| klass.extend_class_name == class_name }.each do |klass|
mark_subclasses_method_used(klass.to_s, method_name)
method = get_method(klass.to_s, method_name)
method&.mark_used
end
Prepares
.klasses
.select { |klass| klass.extend_class_name == class_name }
.each do |klass|
mark_subclasses_method_used(klass.to_s, method_name)
method = get_method(klass.to_s, method_name)
method&.mark_used
end
end

# Mark the method as public.
Expand Down Expand Up @@ -128,14 +129,16 @@ def get_method(class_name, method_name, access_control = nil)
# @param [String] access control
# @return [Array] array of Method
def get_all_unused_methods(access_control = nil)
@methods.inject([]) do |unused_methods, (_class_name, methods)|
unused_methods +=
if access_control
methods.select { |method| method.access_control == access_control && !method.used }
else
methods.reject(&:used)
end
end.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
@methods
.inject([]) do |unused_methods, (_class_name, methods)|
unused_methods +=
if access_control
methods.select { |method| method.access_control == access_control && !method.used }
else
methods.reject(&:used)
end
end
.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
end

private
Expand Down
7 changes: 5 additions & 2 deletions lib/rails_best_practices/core/model_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def initialize
def add_association(model_name, association_name, association_meta, association_class = nil)
@associations[model_name] ||= {}
@associations[model_name][association_name] = {
'meta' => association_meta, 'class_name' => association_class || association_name.classify
'meta' => association_meta,
'class_name' => association_class || association_name.classify
}
end

Expand Down Expand Up @@ -53,7 +54,9 @@ def each
def get_association_class_name(table_name, association_name)
(
associations =
@associations.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }.values
@associations
.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }
.values
.first
) && (association_meta = associations.select { |name, _meta| name == association_name }.values.first) &&
association_meta['class_name']
Expand Down
3 changes: 2 additions & 1 deletion lib/rails_best_practices/core/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module RailsBestPractices
module Core
# Model classes.
class Models < Klasses; end
class Models < Klasses
end
end
end
Loading