Class: Rigor::CLI::AnnotateCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/cli/annotate_command.rb

Overview

Executes ‘rigor annotate FILE`.

For every source line the command finds the expression the line evaluates to — the last statement that ends on the line (so ‘1; 2; 3` reports `3`), or, for a line that no statement closes, the widest expression ending there (so the `if nil` header reports its condition). It infers that expression’s type and appends a ‘#=> dump_type: <type>` comment.

The annotated source is re-parsed with Prism — a sanity gate, since the appended text is always a comment — and printed to stdout with IRB-style syntax highlighting via PrismColorizer.

Constant Summary collapse

USAGE =
"Usage: rigor annotate [options] FILE"
ANNOTATION_PATTERN =

Appended ‘ #=> dump_type: <type>` suffix. Matched and stripped before re-annotating so re-running is idempotent.

/\s*#=>\s*dump_type:.*\z/

Instance Method Summary collapse

Constructor Details

#initialize(argv:, out:, err:) ⇒ AnnotateCommand

Returns a new instance of AnnotateCommand.



34
35
36
37
38
# File 'lib/rigor/cli/annotate_command.rb', line 34

def initialize(argv:, out:, err:)
  @argv = argv
  @out = out
  @err = err
end

Instance Method Details

#runInteger

Returns CLI exit status.

Returns:

  • (Integer)

    CLI exit status.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rigor/cli/annotate_command.rb', line 41

def run
  options = parse_options
  file = @argv.shift
  if file.nil?
    @err.puts(USAGE)
    return CLI::EXIT_USAGE
  end
  unless File.file?(file)
    @err.puts("annotate: file not found: #{file}")
    return 1
  end

  execute(file, options)
end