Class: RuboCop::Cop::SketchupSuggestions::OperationName

Inherits:
SketchUp::Cop
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/sketchup/cop/suggestions/operation_name.rb

Overview

Operation name should be a short capitalized description. It will be visible to the user in the Edit > Undo menu. Make sure to give it a short human readable name, similar to SketchUp’s own operation names.

This cop make some very naive assumptions and will have more false positives than most of the other cops. It’s purpose is mainly to enable awareness.

Examples:

Bad - Ends with a punctuation. Inconsistent with SketchUp.

model.start_operation('Example.', true)

Good - Doesn’t end with a punctuation.

model.start_operation('Example', true)

Bad - Starts with whitespace. Inconsistent with SketchUp.

model.start_operation('  Example', true)

Good - Doesn’t start with whitespace.

model.start_operation('Example', true)

Bad - Tabs doesn’t belong in operation titles.

model.start_operation("example\tName", true)

Good - Stick to space for whitespace.

model.start_operation('Example Name', true)

Bad - No capitalization. Inconsistent with SketchUp.

model.start_operation('example name', true)

Good - Each word capitalized.

model.start_operation('Example Name', true)

Bad - Underscore instead of space. Inconsistent with SketchUp.

model.start_operation('example_name', true)

Good - Name is for human reading.

model.start_operation('Example Name', true)

Constant Summary collapse

MSG =
'Operation name should be a short capitalized description.'
MSG_MAX =
'Operation names should not be short and concise. [%d/%d]'

Constants inherited from SketchUp::Cop

SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubocop/sketchup/cop/suggestions/operation_name.rb', line 54

def on_send(node)
  _, method_name, *args = *node
  return unless method_name == :start_operation
  return if args.empty? || !args.first.str_type?

  # Ignore transparent operations.
  return if args.size == 4 && args.last.true_type?

  operation_name = args.first.str_content
  # We can only inspect string literals.
  return unless operation_name.is_a?(String)

  # Check the format of the operation name.
  unless acceptable_operation_name?(operation_name)
    msg = %(#{MSG} Expected: `"#{titleize(operation_name)}"`)
    add_offense(args.first, location: :expression, message: msg)
  end
  # Check the length of the operation name.
  unless operation_name.size <= max_operation_name_length
    message = format(MSG_MAX, operation_name.size, max_operation_name_length)
    add_offense(args.first,
                location: excess_range(args.first, operation_name),
                message: message)
  end
  # Ensure operation name is not empty.
  if operation_name.empty?
    msg = 'Operation names should not be empty.'
    add_offense(args.first, location: :expression, message: msg)
  end
end