Write a Rubocop Cop to ensure no `#p` or `#puts` calls get committed to master. (#6615)

Merge pull request 6615
This commit is contained in:
Parker Moore 2018-01-20 17:04:52 -05:00 committed by jekyllbot
parent 707dd03fb0
commit 3f4bb55e07
8 changed files with 63 additions and 5 deletions

View File

@ -1,4 +1,12 @@
--- ---
require:
- ./rubocop/jekyll
Jekyll/NoPutsAllowed:
Exclude:
- rake/*.rake
AllCops: AllCops:
TargetRubyVersion: 2.1 TargetRubyVersion: 2.1
Include: Include:

View File

@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/jekyll/jekyll" s.homepage = "https://github.com/jekyll/jekyll"
all_files = `git ls-files -z`.split("\x0") all_files = `git ls-files -z`.split("\x0")
s.files = all_files.grep(%r!^(exe|lib)/|^.rubocop.yml$!) s.files = all_files.grep(%r!^(exe|lib|rubocop)/|^.rubocop.yml$!)
s.executables = all_files.grep(%r!^exe/!) { |f| File.basename(f) } s.executables = all_files.grep(%r!^exe/!) { |f| File.basename(f) }
s.bindir = "exe" s.bindir = "exe"
s.require_paths = ["lib"] s.require_paths = ["lib"]

View File

@ -12,9 +12,9 @@ module Jekyll
c.action do |args, _| c.action do |args, _|
cmd = (args.first || "").to_sym cmd = (args.first || "").to_sym
if args.empty? if args.empty?
puts prog Jekyll.logger.info prog.to_s
elsif prog.has_command? cmd elsif prog.has_command? cmd
puts prog.commands[cmd] Jekyll.logger.info prog.commands[cmd].to_s
else else
invalid_command(prog, cmd) invalid_command(prog, cmd)
abort abort

View File

@ -77,7 +77,7 @@ module Jekyll
end end
def print_stats def print_stats
puts @liquid_renderer.stats_table Jekyll.logger.info @liquid_renderer.stats_table
end end
# Reset Site details. # Reset Site details.

5
rubocop/jekyll.rb Normal file
View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
Dir[File.join(File.expand_path("jekyll", __dir__), "*.rb")].each do |ruby_file|
require ruby_file
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module Jekyll
class NoPAllowed < Cop
MSG = "Avoid using `p` to print things. Use `Jekyll.logger` instead.".freeze
def_node_search :p_called?, <<-PATTERN
(send _ :p _)
PATTERN
def on_send(node)
if p_called?(node)
add_offense(node, :location => :selector)
end
end
end
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module Jekyll
class NoPutsAllowed < Cop
MSG = "Avoid using `puts` to print things. Use `Jekyll.logger` instead.".freeze
def_node_search :puts_called?, <<-PATTERN
(send nil? :puts _)
PATTERN
def on_send(node)
if puts_called?(node)
add_offense(node, :location => :selector)
end
end
end
end
end
end

View File

@ -779,7 +779,6 @@ class TestFilters < JekyllUnitTest
should "include the size of each grouping" do should "include the size of each grouping" do
grouping = @filter.group_by(@filter.site.pages, "layout") grouping = @filter.group_by(@filter.site.pages, "layout")
grouping.each do |g| grouping.each do |g|
p g
assert_equal( assert_equal(
g["items"].size, g["items"].size,
g["size"], g["size"],