Module: Deprewriter

Defined in:
lib/deprewriter.rb,
lib/deprewriter/diff.rb,
lib/deprewriter/version.rb,
lib/deprewriter/rewriter.rb,
lib/deprewriter/transformer.rb,
lib/deprewriter/configuration.rb,
lib/deprewriter/call_site_finder.rb

Defined Under Namespace

Modules: Rewriter Classes: CallSiteFinder, Configuration, Diff, Transformer

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject



14
15
16
# File 'lib/deprewriter.rb', line 14

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/deprewriter.rb', line 18

def configure
  yield config if block_given?
end

.processed_location_of_callersObject



22
23
24
# File 'lib/deprewriter.rb', line 22

def processed_location_of_callers
  @processed_location_of_callers ||= Set.new
end

Instance Method Details

#deprewrite(method_name, to:, from: nil) ⇒ Object

Marks a method as deprecated and sets up automatic code rewriting

Parameters:

  • method_name (Symbol)

    The name of the method to deprecate

  • from (String, nil) (defaults to: nil)

    Pattern to match for transformation

  • to (String)

    Pattern to transform to



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/deprewriter.rb', line 31

def deprewrite(method_name, to:, from: nil)
  return if !Deprewriter.config.enabled?

  class_eval do
    old = "_deprecated_#{method_name}"
    alias_method old, method_name

    define_method method_name do |*args, &block|
      filepath, line = Gem.location_of_caller
      location_of_caller = "#{filepath}:#{line}"
      skippable = Deprewriter.config.skip_redundant_rewrite && Deprewriter.processed_location_of_callers.include?(location_of_caller)

      if File.exist?(filepath) && !skippable
        source = File.read(filepath)
        rewritten_source = Rewriter.transform_source(source, method_name, line, to: to, from: from)
        diff = Diff.new(source, rewritten_source, File.join(".", filepath))

        if diff.different?
          target = is_a?(Module) ? "#{self}." : "#{self.class}#"

          case Deprewriter.config.mode
          in :rewrite
            if File.writable?(filepath)
              Deprewriter.config.logger.warn "DEPREWRITER: Dangerously trying to rewrite. It will rewrite a file to apply the deprecation but won't load the file"
              File.write(filepath, diff.rewritten_source)
            else
              Deprewriter.config.logger.error "DEPREWRITER: Failed to rewrite #{filepath} because it is not writable."
            end
          in :diff
            File.write(File.join(Dir.pwd, diff.id), diff.to_s)
          in :log
            Deprewriter.config.logger.warn "DEPREWRITER: #{target}#{method_name} usage at #{filepath}:#{line} is deprecated." \
              " You can apply the diff below to resolve the deprecation.\n#{diff}"
          end
        end

        Deprewriter.processed_location_of_callers << location_of_caller if Deprewriter.config.skip_redundant_rewrite
      end

      send old, *args, &block
    end
  end
end