Skip to content
This repository has been archived by the owner on Jul 1, 2019. It is now read-only.

Fix autocorrect in the comment cops + linting #11

Open
wants to merge 2 commits 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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
cache: bundler
language: ruby
rvm:
- 2.3.5
- 2.4.2
- 2.4.5
- 2.5.5
- 2.6.3
- ruby-head
matrix:
allow_failures:
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/chef/cookbook_only.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Chef
#
module CookbookOnly
DEFAULT_CONFIGURATION = CONFIG.fetch('AllCops')
COOKBOOK_SEGMENTS = %w{attributes definitions libraries metadata providers recipes resources}
COOKBOOK_SEGMENTS = %w(attributes definitions libraries metadata providers recipes resources).freeze

def relevant_file?(file)
cookbook_pattern =~ file && super
Expand Down Expand Up @@ -62,7 +62,7 @@ def included(klass)
extend ClassMethods
end

def self.CookbookOnly(segments)
def self.CookbookOnly(segments) # rubocop: disable Naming/MethodName
Module.new do |mod|
mod.define_singleton_method(:included) do |klass|
super(klass)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/chef/attribute_keys.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016, Noah Kantrowitz
# Copyright:: 2016, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,7 +82,7 @@ def autocorrect(node)
key_string.to_sym.inspect
else # strings
key_string.inspect
end
end
corrector.replace(node.loc.expression, key_replacement)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/chef/comments_copyright_format.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016, Tim Smith
# Copyright:: 2016, Tim Smith
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,13 +51,13 @@ def investigate(processed_source)
end
end

private

def autocorrect(comment)
correct_comment = "# Copyright:: #{copyright_date_range(comment)}, #{copyright_holder(comment)}"
->(corrector) { corrector.replace(comment.loc.expression, correct_comment) }
end

private

def copyright_date_range(comment)
dates = comment.text.scan(/([0-9]{4})/)

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/chef/comments_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ def investigate(processed_source)
end
end

private

def autocorrect(comment)
# Extract the type and the actual value. Strip out "Name" or "File"
# 'Cookbook Name' should be 'Cookbook'. Also skip a :: if present
Expand All @@ -63,6 +61,8 @@ def autocorrect(comment)
->(corrector) { corrector.replace(comment.loc.expression, correct_comment) }
end

private

def invalid_comment?(comment)
comment_types = %w(Author Cookbook Library Attribute Copyright Recipe Resource Definition License)
comment_types.any? do |comment_type|
Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/chef/file_mode.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016, Noah Kantrowitz
# Copyright:: 2016, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,22 +37,22 @@ class FileMode < Cop

def on_send(node)
resource_mode?(node) do |mode_int|
add_offense(mode_int, location: :expression, message: MSG, severity: is_octal?(mode_int) ? :warning : :error)
add_offense(mode_int, location: :expression, message: MSG, severity: octal?(mode_int) ? :warning : :error)
end
end

def autocorrect(node)
lambda do |corrector|
# If it was an octal literal, make sure we write out the right number.
replacement_base = is_octal?(node) ? 8 : 10
replacement_base = octal?(node) ? 8 : 10
replacement_mode = node.children.first.to_s(replacement_base)
corrector.replace(node.loc.expression, replacement_mode.inspect)
end
end

private

def is_octal?(node)
def octal?(node)
node.source =~ /^0o?\d+/i
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/chef/service_resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016, Chris Henry
# Copyright:: 2016, Chris Henry
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,7 +45,7 @@ def starts_service?(cmd)
(cmd_str.include?('/etc/init.d') || ['service ', '/sbin/service ',
'start ', 'stop ', 'invoke-rc.d '].any? do |service_cmd|
cmd_str.start_with?(service_cmd)
end) && %w[start stop restart reload].any? { |a| cmd_str.include?(a) }
end) && %w(start stop restart reload).any? { |a| cmd_str.include?(a) }
end
end
end
Expand Down