Class: RuboCop::Cop::Isucon::Sinatra::ServeStaticFile

Inherits:
Base
  • Object
show all
Includes:
Mixin::SinatraMethods
Defined in:
lib/rubocop/cop/isucon/sinatra/serve_static_file.rb

Overview

Serve static files on front server (e.g. nginx) instead of sinatra app

Examples:

# bad
class App < Sinatra::Base
  get '/' do
    content_type :html
    File.read(File.join(__dir__, '..', 'public', 'index.html'))
  end
end

# good (e.g. Serve on nginx)
location / {
  try_files $uri $uri/ /index.html;
}

Constant Summary collapse

MSG =
"Serve static files on front server (e.g. nginx) instead of sinatra app"

Instance Method Summary collapse

Methods included from Mixin::SinatraMethods

#parent_is_sinatra_app?, #subclass_of_sinatra_base?, #subclass_of_sinatra_base_contains_logging?

Instance Method Details

#file_read_method?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::Node)

Returns:

  • (Boolean)


31
32
33
# File 'lib/rubocop/cop/isucon/sinatra/serve_static_file.rb', line 31

def_node_matcher :file_read_method?, <<~PATTERN
  (send (const nil? :File) :read ...)
PATTERN

#get_block?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::Node)

Returns:

  • (Boolean)


38
39
40
# File 'lib/rubocop/cop/isucon/sinatra/serve_static_file.rb', line 38

def_node_matcher :get_block?, <<~PATTERN
  (block (send nil? :get ...) ...)
PATTERN

#on_send(node) ⇒ Object

Parameters:

  • node (RuboCop::AST::Node)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/isucon/sinatra/serve_static_file.rb', line 43

def on_send(node)
  return unless parent_is_sinatra_app?(node)
  return unless file_read_method?(node)

  parent = parent_get_node(node)
  return unless parent

  return unless end_of_block?(node: node, parent: parent)

  add_offense(parent)
end