# encoding: utf-8 module RailsBestPractices module Core # A Check class that takes charge of checking the sexp. class Check ALL_FILES = /.*/ CONTROLLER_FILES = /(controllers|cells)\/.*\.rb$/ MIGRATION_FILES = /db\/migrate\/.*\.rb$/ MODEL_FILES = /models\/.*\.rb$/ MAILER_FILES = /models\/.*mailer\.rb$|mailers\/.*mailer\.rb/ VIEW_FILES = /(views|cells)\/.*\.(erb|haml|slim|builder|rxml)$/ PARTIAL_VIEW_FILES = /(views|cells)\/.*\/_.*\.(erb|haml|slim|builder|rxml)$/ ROUTE_FILES = /config\/routes.*\.rb/ SCHEMA_FILE = /db\/schema\.rb/ HELPER_FILES = /helpers\/.*\.rb$/ DEPLOY_FILES = /config\/deploy.*\.rb/ CONFIG_FILES = /config\/(application|environment|environments\/.*)\.rb/ def initialize(options={}) options.each do |key, value| instance_variable_set("@#{key}", value) end end # interesting nodes that the check will parse. def interesting_nodes self.class.interesting_nodes end # interesting files that the check will parse. def interesting_files self.class.interesting_files end # check if the check will need to parse the node file. # # @param [String] the file name of node. # @return [Boolean] true if the check will need to parse the file. def parse_file?(node_file) interesting_files.any? { |pattern| node_file =~ pattern } end # delegate to start_### according to the sexp_type, like # # start_call # start_def # # @param [Sexp] node def node_start(node) @node = node if self.class.debug? ap node end Array(self.class.callbacks["start_#{node.sexp_type}"]).each do |callback| self.instance_exec node, &callback end self.send("start_#{node.sexp_type}", node) end # delegate to end_### according to the sexp_type, like # # end_call # end_def # # @param [Sexp] node def node_end(node) @node = node self.send("end_#{node.sexp_type}", node) Array(self.class.callbacks["end_#{node.sexp_type}"]).each do |callback| self.instance_exec node, &callback end end def after_prepare; end def after_review; end # add error if source code violates rails best practice. # # @param [String] message, is the string message for violation of the rails best practice # @param [String] filename, is the filename of source code # @param [Integer] line_number, is the line number of the source code which is reviewing def add_error(message, filename = @node.file, line_number = @node.line) errors << RailsBestPractices::Core::Error.new( filename: filename, line_number: line_number, message: message, type: self.class.to_s, url: url ) end # errors that vialote the rails best practices. def errors @errors ||= [] end # default url is empty. # # @return [String] the url of rails best practice def url "" end # method_missing to catch all start and end process for each node type, like # # start_def # end_def # start_call # end_call # # if there is a "debug" method defined in check, each node will be output. def method_missing(method_name, *args) if method_name.to_s =~ /^start_/ p args if respond_to?(:debug) elsif method_name.to_s =~ /^end_/ # nothing to do else super end end class <