Sha256: 2fd6da2aa4dabda152a3ecb0a2632f0da9430ef3f98d1c4ae19f545212f1a860

Contents?: true

Size: 2 KB

Versions: 11

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true
#
# Copyright:: 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module RuboCop
  module Cop
    module Chef
      module Deprecations
        # Chef Workstation 0.8 and later renamed the ChefDK module used when writing custom cookbook generators from ChefDK to ChefCLI. For compatibility with the latest Chef Workstation releases you'll need to reference the new class names.
        #
        # @example
        #
        #   # bad
        #   ChefDK::CLI
        #   ChefDK::Generator::TemplateHelper
        #   module ChefDK
        #     ...
        #   end
        #
        #   # good
        #   ChefCLI::CLI
        #   ChefCLI::Generator::TemplateHelper
        #   module ChefCLI
        #     ...
        #   end
        #
        class ChefDKGenerators < Base
          extend AutoCorrector
          MSG = 'When writing cookbook generators use the ChefCLI module instead of the ChefDK module which was removed in Chef Workstation 0.8 and later.'

          def on_const(node)
            # We want to catch calls like ChefCLI::CLI.whatever or places where classes are defined in the ChefDK module
            return unless node.const_name == 'ChefDK' && (node.parent&.module_type? || node.parent&.const_type?)

            add_offense(node, message: MSG, severity: :warning) do |corrector|
              corrector.replace(node, node.source.gsub('ChefDK', 'ChefCLI'))
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cookstyle-7.7.2 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.6.1 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.5.3 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.5.2 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.5.1 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.4.0 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.3.11 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.3.10 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.2.1 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.1.2 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb
cookstyle-7.0.0 lib/rubocop/cop/chef/deprecation/chefdk_generators.rb