Skip to content

Commit

Permalink
Set frozen string literals to true in all files
Browse files Browse the repository at this point in the history
This dropped total memory allocation on a large data set from 1.61GB to 1.55GB

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Jul 14, 2020
1 parent f39ef9b commit 3f3757e
Show file tree
Hide file tree
Showing 420 changed files with 666 additions and 229 deletions.
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ Style/HashEachMethods:

# ChefModernize/FoodcriticComments itslef include an example Foocritic comment which alerts
ChefModernize/FoodcriticComments:
Enabled: false
Enabled: false

# reduces memory usage
Style/FrozenStringLiteralComment:
Enabled: true
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

# Specify your gem's dependencies in cookstyle.gemspec
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'bundler/gem_tasks'

Dir['tasks/**/*.rake'].each { |t| load t }
Expand Down
1 change: 1 addition & 0 deletions bin/cookstyle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), %w(.. lib))

Expand Down
31 changes: 31 additions & 0 deletions bin/cookstyle-profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

if ARGV.include?('-h') || ARGV.include?('--help')
puts 'Usage: same as main `cookstyle` command but gathers profiling info'
puts 'Additional option: `--memory` to print memory usage'
exit(0)
end
with_mem = ARGV.delete('--memory')

require 'stackprof'
if with_mem
require 'memory_profiler'
MemoryProfiler.start
end
StackProf.start
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
load "#{__dir__}/cookstyle"
ensure
delta = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
puts "Finished in #{delta.round(1)} seconds"
StackProf.stop
if with_mem
puts 'Building memory report...'
report = MemoryProfiler.stop
end
Dir.mkdir('tmp') unless File.exist?('tmp')
StackProf.results('tmp/stackprof.dump')
report&.pretty_print(scale_bytes: true)
end
1 change: 1 addition & 0 deletions cookstyle.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'cookstyle/version'
Expand Down
1 change: 1 addition & 0 deletions lib/cookstyle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require_relative 'cookstyle/version'

require 'pathname'
Expand Down
5 changes: 3 additions & 2 deletions lib/cookstyle/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
module Cookstyle
VERSION = "6.12.3".freeze # rubocop: disable Style/StringLiterals
RUBOCOP_VERSION = '0.87.1'.freeze
VERSION = "6.12.3" # rubocop: disable Style/StringLiterals
RUBOCOP_VERSION = '0.87.1'
end
1 change: 1 addition & 0 deletions lib/rubocop/chef.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module RuboCop
# RuboCop Chef project namespace
module Chef
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/chef/autocorrect_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/chef/cookbook_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/chef/platform_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019-2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -37,7 +38,7 @@ module ChefCorrectness
# end
#
class BlockGuardWithOnlyString < Cop
MSG = 'A resource guard (not_if/only_if) that is a string should not be wrapped in {}. Wrapping a guard string in {} causes it be executed as Ruby code which will always returns true instead of a shell command that will actually run.'.freeze
MSG = 'A resource guard (not_if/only_if) that is a string should not be wrapped in {}. Wrapping a guard string in {} causes it be executed as Ruby code which will always returns true instead of a shell command that will actually run.'

def_node_matcher :block_guard_with_only_string?, <<-PATTERN
(block (send nil? ${:not_if :only_if}) (args) (str $_) )
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/cb_depends_on_self.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -33,7 +34,7 @@ module ChefCorrectness
class CookbooksDependsOnSelf < Cop
include RangeHelp

MSG = 'A cookbook cannot depend on itself. This will fail on Chef Infra Client 13+'.freeze
MSG = 'A cookbook cannot depend on itself. This will fail on Chef Infra Client 13+'

def_node_search :dependencies, '(send nil? :depends str ...)'
def_node_matcher :cb_name?, '(send nil? :name str ...)'
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/chef_application_fatal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -29,7 +30,7 @@ module ChefCorrectness
# raise "Something horrible happened!"
#
class ChefApplicationFatal < Cop
MSG = 'Use raise to force Chef Infra Client to fail instead of using Chef::Application.fatal'.freeze
MSG = 'Use raise to force Chef Infra Client to fail instead of using Chef::Application.fatal'

def_node_matcher :application_fatal?, <<-PATTERN
(send
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2020, Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -41,7 +42,7 @@ module ChefCorrectness
#
class ConditionalRubyShellout < Cop
include RuboCop::Chef::CookbookHelpers
MSG = "Don't use Ruby to shellout in an only_if / not_if conditional when you can shellout directly by wrapping the command in quotes.".freeze
MSG = "Don't use Ruby to shellout in an only_if / not_if conditional when you can shellout directly by wrapping the command in quotes."

def_node_matcher :conditional_shellout?, <<-PATTERN
(block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019-2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -37,7 +38,7 @@ class DnfPackageAllowDowngrades < Cop
include RangeHelp
include RuboCop::Chef::CookbookHelpers

MSG = 'dnf_package does not support the allow_downgrades property'.freeze
MSG = 'dnf_package does not support the allow_downgrades property'

def on_block(node)
match_property_in_resource?(:dnf_package, :allow_downgrades, node) do |prop|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019-2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -32,7 +33,7 @@ module ChefCorrectness
# ::Chef::DSL::Recipe.send(:include, Filebeat::Helpers) # covers previous Recipe & Provider classes
#
class IncorrectLibraryInjection < Cop
MSG = 'Libraries should be injected into the Chef::DSL::Recipe class and not Chef::Recipe or Chef::Provider classes directly.'.freeze
MSG = 'Libraries should be injected into the Chef::DSL::Recipe class and not Chef::Recipe or Chef::Provider classes directly.'

def_node_matcher :legacy_class_sends?, <<-PATTERN
(send (const (const (cbase) :Chef) {:Recipe :Provider}) :send (sym :include) ... )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -35,7 +36,7 @@ module ChefCorrectness
# end
#
class InvalidNotificationTiming < Cop
MSG = 'Valid notification timings are :immediately, :immediate (alias for :immediately), :delayed, and :before.'.freeze
MSG = 'Valid notification timings are :immediately, :immediate (alias for :immediately), :delayed, and :before.'

def_node_matcher :notification_with_timing?, <<-PATTERN
(send nil? {:notifies :subscribes} (sym _) (...) $(sym _))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -33,7 +34,7 @@ module ChefCorrectness
class InvalidPlatformFamilyHelper < Cop
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Pass valid platform families to the platform_family? helper.'.freeze
MSG = 'Pass valid platform families to the platform_family? helper.'

def_node_matcher :platform_family_helper?, <<-PATTERN
(send nil? :platform_family? $str*)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -32,7 +33,7 @@ class InvalidPlatformFamilyInCase < Cop
include RangeHelp
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Use valid platform family values in case statements.'.freeze
MSG = 'Use valid platform family values in case statements.'

def_node_matcher :node_platform_family?, <<-PATTERN
(send (send nil? :node) :[] (str "platform_family") )
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/invalid_platform_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -34,7 +35,7 @@ module ChefCorrectness
class InvalidPlatformHelper < Cop
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Pass valid platforms to the platform? helper.'.freeze
MSG = 'Pass valid platforms to the platform? helper.'

def_node_matcher :platform_helper?, <<-PATTERN
(send nil? :platform? $str*)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -34,7 +35,7 @@ module ChefCorrectness
class InvalidPlatformMetadata < Cop
include ::RuboCop::Chef::PlatformHelpers

MSG = 'metadata.rb "supports" platform is invalid'.freeze
MSG = 'metadata.rb "supports" platform is invalid'

def_node_matcher :supports?, '(send nil? :supports $str ...)'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -32,7 +33,7 @@ class InvalidPlatformInCase < Cop
include RangeHelp
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Use valid platform values in case statements.'.freeze
MSG = 'Use valid platform values in case statements.'

def_node_matcher :node_platform?, <<-PATTERN
(send (send nil? :node) :[] (str "platform") )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -37,7 +38,7 @@ module ChefCorrectness
class InvalidPlatformValueForPlatformFamilyHelper < Cop
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Pass valid platform families to the value_for_platform_family helper.'.freeze
MSG = 'Pass valid platform families to the value_for_platform_family helper.'

def_node_matcher :value_for_platform_family?, <<-PATTERN
(send nil? :value_for_platform_family
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -36,7 +37,7 @@ module ChefCorrectness
class InvalidPlatformValueForPlatformHelper < Cop
include ::RuboCop::Chef::PlatformHelpers

MSG = 'Pass valid platforms to the value_for_platform helper.'.freeze
MSG = 'Pass valid platforms to the value_for_platform helper.'

def_node_matcher :value_for_platform?, <<-PATTERN
(send nil? :value_for_platform
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/invalid_version_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -30,7 +31,7 @@ module ChefCorrectness
# version '1.2.3'
#
class InvalidVersionMetadata < Cop
MSG = 'Cookbook metadata.rb version field should follow X.Y.Z version format.'.freeze
MSG = 'Cookbook metadata.rb version field should follow X.Y.Z version format.'

def_node_matcher :version?, '(send nil? :version $str ...)'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -32,7 +33,7 @@ module ChefCorrectness
class LazyEvalNodeAttributeDefaults < Cop
include RuboCop::Chef::CookbookHelpers

MSG = 'When setting a node attribute as the default value for a custom resource property, wrap the node attribute in `lazy {}` so that its value is available when the resource executes.'.freeze
MSG = 'When setting a node attribute as the default value for a custom resource property, wrap the node attribute in `lazy {}` so that its value is available when the resource executes.'

def_node_matcher :non_lazy_node_attribute_default?, <<-PATTERN
(send nil? :property (sym _) ... (hash <(pair (sym :default) $(send (send _ :node) :[] _) ) ...>))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/metadata_missing_name.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -29,7 +30,7 @@ module ChefCorrectness
class MetadataMissingName < Cop
include RangeHelp

MSG = 'metadata.rb needs to include the name method or it will fail on Chef Infra Client 12 and later.'.freeze
MSG = 'metadata.rb needs to include the name method or it will fail on Chef Infra Client 12 and later.'

def investigate(processed_source)
return if processed_source.blank?
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/node_normal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
#
Expand Down Expand Up @@ -37,7 +38,7 @@ module ChefCorrectness
# node.force_override['foo'] = true
#
class NodeNormal < Cop
MSG = 'Do not use node.normal. Replace with default/override/force_default/force_override attribute levels.'.freeze
MSG = 'Do not use node.normal. Replace with default/override/force_default/force_override attribute levels.'

def_node_matcher :node_normal?, <<-PATTERN
(send (send _ :node) :normal)
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/node_normal_unless.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
#
Expand Down Expand Up @@ -37,7 +38,7 @@ module ChefCorrectness
# node.force_override_unless['foo'] = true
#
class NodeNormalUnless < Cop
MSG = 'Do not use node.normal_unless. Replace with default/override/force_default/force_override attribute levels.'.freeze
MSG = 'Do not use node.normal_unless. Replace with default/override/force_default/force_override attribute levels.'

def_node_matcher :node_normal_unless?, <<-PATTERN
(send (send _ :node) :normal_unless)
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/correctness/node_save.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -28,7 +29,7 @@ module ChefCorrectness
# node.save
#
class CookbookUsesNodeSave < Cop
MSG = "Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and increases load on the Chef Infra Server.".freeze
MSG = "Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and increases load on the Chef Infra Server."

def_node_matcher :node_save?, <<-PATTERN
(send (send nil? :node) :save)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#
# Copyright:: 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
Expand Down Expand Up @@ -44,7 +45,7 @@ module ChefCorrectness
class NotifiesActionNotSymbol < Cop
include RuboCop::Chef::CookbookHelpers

MSG = 'Resource notification and subscription actions should be symbols not strings.'.freeze
MSG = 'Resource notification and subscription actions should be symbols not strings.'

def on_block(node)
match_property_in_resource?(nil, %w(notifies subscribes), node) do |notifies_property|
Expand Down
Loading

0 comments on commit 3f3757e

Please sign in to comment.