tailor 0.1.5 → 1.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -1
- data/.rspec +2 -1
- data/.tailor +6 -0
- data/Gemfile.lock +47 -78
- data/{ChangeLog.rdoc → History.rdoc} +0 -0
- data/README.rdoc +157 -24
- data/Rakefile +0 -9
- data/bin/tailor +16 -69
- data/features/configurable.feature +78 -0
- data/features/horizontal_spacing.feature +262 -0
- data/features/indentation.feature +17 -21
- data/features/indentation/bad_files_with_no_trailing_newline.feature +90 -0
- data/features/indentation/good_files_with_no_trailing_newline.feature +206 -0
- data/features/name_detection.feature +72 -0
- data/features/step_definitions/indentation_steps.rb +10 -133
- data/features/support/env.rb +7 -15
- data/features/support/file_cases/horizontal_spacing_cases.rb +265 -0
- data/features/support/file_cases/indentation_cases.rb +972 -0
- data/features/support/file_cases/naming_cases.rb +52 -0
- data/features/support/file_cases/vertical_spacing_cases.rb +70 -0
- data/features/support/hooks.rb +8 -0
- data/features/support/{1_file_with_bad_operator_spacing → legacy}/bad_op_spacing.rb +0 -0
- data/features/support/{1_file_with_bad_ternary_colon_spacing → legacy}/bad_ternary_colon_spacing.rb +0 -0
- data/features/support/{1_long_file_with_indentation/my_project.rb → legacy/long_file_with_indentation.rb} +1 -1
- data/features/support/world.rb +14 -0
- data/features/vertical_spacing.feature +114 -0
- data/lib/ext/string_ext.rb +5 -0
- data/lib/tailor.rb +6 -252
- data/lib/tailor/cli.rb +49 -0
- data/lib/tailor/cli/options.rb +251 -0
- data/lib/tailor/composite_observable.rb +56 -0
- data/lib/tailor/configuration.rb +263 -0
- data/lib/tailor/critic.rb +162 -0
- data/lib/tailor/formatters/text.rb +126 -0
- data/lib/tailor/lexed_line.rb +246 -0
- data/lib/tailor/lexer.rb +428 -0
- data/lib/tailor/lexer/token.rb +103 -0
- data/lib/tailor/lexer_constants.rb +75 -0
- data/lib/tailor/logger.rb +28 -0
- data/lib/tailor/problem.rb +100 -0
- data/lib/tailor/reporter.rb +48 -0
- data/lib/tailor/ruler.rb +39 -0
- data/lib/tailor/rulers.rb +7 -0
- data/lib/tailor/rulers/allow_camel_case_methods_ruler.rb +30 -0
- data/lib/tailor/rulers/allow_hard_tabs_ruler.rb +22 -0
- data/lib/tailor/rulers/allow_screaming_snake_case_classes_ruler.rb +32 -0
- data/lib/tailor/rulers/allow_trailing_line_spaces_ruler.rb +33 -0
- data/lib/tailor/rulers/indentation_spaces_ruler.rb +199 -0
- data/lib/tailor/rulers/indentation_spaces_ruler/indentation_manager.rb +362 -0
- data/lib/tailor/rulers/max_code_lines_in_class_ruler.rb +84 -0
- data/lib/tailor/rulers/max_code_lines_in_method_ruler.rb +84 -0
- data/lib/tailor/rulers/max_line_length_ruler.rb +31 -0
- data/lib/tailor/rulers/spaces_after_comma_ruler.rb +83 -0
- data/lib/tailor/rulers/spaces_after_lbrace_ruler.rb +114 -0
- data/lib/tailor/rulers/spaces_after_lbracket_ruler.rb +123 -0
- data/lib/tailor/rulers/spaces_after_lparen_ruler.rb +116 -0
- data/lib/tailor/rulers/spaces_before_comma_ruler.rb +67 -0
- data/lib/tailor/rulers/spaces_before_lbrace_ruler.rb +93 -0
- data/lib/tailor/rulers/spaces_before_rbrace_ruler.rb +98 -0
- data/lib/tailor/rulers/spaces_before_rbracket_ruler.rb +70 -0
- data/lib/tailor/rulers/spaces_before_rparen_ruler.rb +70 -0
- data/lib/tailor/rulers/spaces_in_empty_braces_ruler.rb +94 -0
- data/lib/tailor/rulers/trailing_newlines_ruler.rb +36 -0
- data/lib/tailor/runtime_error.rb +3 -0
- data/lib/tailor/tailorrc.erb +88 -0
- data/lib/tailor/version.rb +2 -2
- data/spec/spec_helper.rb +7 -5
- data/spec/tailor/cli_spec.rb +94 -0
- data/spec/tailor/configuration_spec.rb +147 -0
- data/spec/tailor/critic_spec.rb +63 -0
- data/spec/tailor/lexed_line_spec.rb +569 -0
- data/spec/tailor/lexer/token_spec.rb +46 -0
- data/spec/tailor/lexer_spec.rb +181 -0
- data/spec/tailor/options_spec.rb +6 -0
- data/spec/tailor/problem_spec.rb +74 -0
- data/spec/tailor/reporter_spec.rb +53 -0
- data/spec/tailor/ruler_spec.rb +56 -0
- data/spec/tailor/rulers/indentation_spaces_ruler/indentation_manager_spec.rb +454 -0
- data/spec/tailor/rulers/indentation_spaces_ruler_spec.rb +128 -0
- data/spec/tailor/rulers/spaces_after_comma_spec.rb +31 -0
- data/spec/tailor/rulers/spaces_after_lbrace_ruler_spec.rb +145 -0
- data/spec/tailor/rulers/spaces_before_lbrace_ruler_spec.rb +63 -0
- data/spec/tailor/rulers/spaces_before_rbrace_ruler_spec.rb +63 -0
- data/spec/tailor/rulers_spec.rb +9 -0
- data/spec/tailor/version_spec.rb +6 -0
- data/spec/tailor_spec.rb +9 -21
- data/tailor.gemspec +22 -35
- data/tasks/features.rake +7 -0
- data/tasks/roodi.rake +9 -0
- data/tasks/roodi_config.yaml +14 -0
- data/tasks/spec.rake +16 -0
- data/tasks/yard.rake +14 -0
- metadata +224 -77
- data/features/case_checking.feature +0 -38
- data/features/spacing.feature +0 -97
- data/features/spacing/commas.feature +0 -44
- data/features/step_definitions/case_checking_steps.rb +0 -42
- data/features/step_definitions/spacing_steps.rb +0 -156
- data/features/support/1_file_with_bad_comma_spacing/bad_comma_spacing.rb +0 -43
- data/features/support/1_file_with_bad_curly_brace_spacing/bad_curly_brace_spacing.rb +0 -60
- data/features/support/1_file_with_bad_parenthesis/bad_parenthesis.rb +0 -4
- data/features/support/1_file_with_bad_square_brackets/bad_square_brackets.rb +0 -62
- data/features/support/1_file_with_camel_case_class/camel_case_class.rb +0 -5
- data/features/support/1_file_with_camel_case_method/camel_case_method.rb +0 -3
- data/features/support/1_file_with_hard_tabs/hard_tab.rb +0 -3
- data/features/support/1_file_with_long_lines/long_lines.rb +0 -5
- data/features/support/1_file_with_snake_case_class/snake_case_class.rb +0 -5
- data/features/support/1_file_with_snake_case_method/snake_case_method.rb +0 -3
- data/features/support/1_file_with_trailing_whitespace/trailing_whitespace.rb +0 -5
- data/features/support/1_good_simple_file/simple_project.rb +0 -5
- data/features/support/common.rb +0 -102
- data/features/support/matchers.rb +0 -11
- data/lib/tailor/file_line.rb +0 -220
- data/lib/tailor/indentation.rb +0 -245
- data/lib/tailor/spacing.rb +0 -237
- data/spec/file_line_spec.rb +0 -70
- data/spec/indentation_spec.rb +0 -259
- data/spec/spacing/colon_spacing_spec.rb +0 -71
- data/spec/spacing/comma_spacing_spec.rb +0 -159
- data/spec/spacing/curly_brace_spacing_spec.rb +0 -257
- data/spec/spacing/parentheses_spacing_spec.rb +0 -28
- data/spec/spacing/square_bracket_spacing_spec.rb +0 -116
- data/spec/spacing_spec.rb +0 -167
- data/tasks/metrics.rake +0 -23
@@ -0,0 +1,67 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces before a ',' as given by +@config+.
|
7
|
+
class SpacesBeforeCommaRuler < Tailor::Ruler
|
8
|
+
def initialize(config)
|
9
|
+
super(config)
|
10
|
+
@comma_columns = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def comma_update(text_line, lineno, column)
|
14
|
+
@comma_columns << column
|
15
|
+
end
|
16
|
+
|
17
|
+
def comment_update(token, lexed_line, file_text, lineno, column)
|
18
|
+
if token =~ /\n$/
|
19
|
+
log "Found comment with trailing newline."
|
20
|
+
ignored_nl_update(lexed_line, lineno, column)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Checks to see if the number of spaces before a comma equals the value
|
25
|
+
# at +@config+.
|
26
|
+
#
|
27
|
+
# @param [Fixnum] actual_spaces The number of spaces before the comma.
|
28
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
29
|
+
# @param [Fixnum] column Column the potential problem is on.
|
30
|
+
def measure(actual_spaces, lineno, column)
|
31
|
+
if actual_spaces != @config
|
32
|
+
@problems << Problem.new(:spaces_before_comma, lineno, column - 1,
|
33
|
+
{ actual_spaces: actual_spaces, should_have: @config })
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def check_spaces_before_comma(lexed_line, lineno)
|
38
|
+
@comma_columns.each do |c|
|
39
|
+
event_index = lexed_line.event_index(c)
|
40
|
+
if event_index.nil?
|
41
|
+
log "Event index is nil. Weird..."
|
42
|
+
next
|
43
|
+
end
|
44
|
+
|
45
|
+
previous_event = lexed_line.at(event_index - 1)
|
46
|
+
actual_spaces = if previous_event[1] != :on_sp
|
47
|
+
0
|
48
|
+
else
|
49
|
+
previous_event.last.size
|
50
|
+
end
|
51
|
+
|
52
|
+
measure(actual_spaces, lineno, c)
|
53
|
+
end
|
54
|
+
|
55
|
+
@comma_columns.clear
|
56
|
+
end
|
57
|
+
|
58
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
59
|
+
check_spaces_before_comma(lexed_line, lineno)
|
60
|
+
end
|
61
|
+
|
62
|
+
def nl_update(lexed_line, lineno, column)
|
63
|
+
ignored_nl_update(lexed_line, lineno, column)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Detects spaces before a '{' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's the first char in the line.
|
9
|
+
# * the char before it is a '#{'.
|
10
|
+
# * the char before it is a '('.
|
11
|
+
# * the char before it is a '['.
|
12
|
+
# * it's only preceded by spaces.
|
13
|
+
class SpacesBeforeLbraceRuler < Tailor::Ruler
|
14
|
+
|
15
|
+
# Counts the spaces before the '{'.
|
16
|
+
#
|
17
|
+
# @param [LexedLine] lexed_line
|
18
|
+
# @param [Fixnum] column
|
19
|
+
# @return [Fixnum] The number of spaces before the lbrace.
|
20
|
+
def count_spaces(lexed_line, column)
|
21
|
+
current_index = lexed_line.event_index(column)
|
22
|
+
log "Current event index: #{current_index}"
|
23
|
+
previous_event = lexed_line.at(current_index - 1)
|
24
|
+
log "Previous event: #{previous_event}"
|
25
|
+
|
26
|
+
if column.zero? || previous_event.nil?
|
27
|
+
log "lbrace must be at the beginning of the line."
|
28
|
+
@do_measurement = false
|
29
|
+
return 0
|
30
|
+
end
|
31
|
+
|
32
|
+
if previous_event[1] == :on_embexpr_beg
|
33
|
+
log "lbrace comes after a '\#{'."
|
34
|
+
@do_measurement = false
|
35
|
+
return 0
|
36
|
+
end
|
37
|
+
|
38
|
+
if previous_event[1] == :on_lparen
|
39
|
+
log "lbrace comes after a '('."
|
40
|
+
@do_measurement = false
|
41
|
+
return 0
|
42
|
+
end
|
43
|
+
|
44
|
+
if previous_event[1] == :on_lbracket
|
45
|
+
log "lbrace comes after a '['."
|
46
|
+
@do_measurement = false
|
47
|
+
return 0
|
48
|
+
end
|
49
|
+
|
50
|
+
return 0 if previous_event[1] != :on_sp
|
51
|
+
|
52
|
+
if current_index - 2 < 0
|
53
|
+
log "lbrace comes at the beginning of an indented line."
|
54
|
+
@do_measurement = false
|
55
|
+
return previous_event.last.size
|
56
|
+
end
|
57
|
+
|
58
|
+
previous_event.last.size
|
59
|
+
end
|
60
|
+
|
61
|
+
# Called by {Lexer} when :on_lbrace is encountered.
|
62
|
+
#
|
63
|
+
# @param [LexedLine] lexed_line
|
64
|
+
# @param [Fixnum] lineno
|
65
|
+
# @param [Fixnum] column
|
66
|
+
def lbrace_update(lexed_line, lineno, column)
|
67
|
+
count = count_spaces(lexed_line, column)
|
68
|
+
log "Found #{count} space(s) before lbrace."
|
69
|
+
|
70
|
+
if @do_measurement == false
|
71
|
+
log "Skipping measurement."
|
72
|
+
else
|
73
|
+
measure(count, lineno, column)
|
74
|
+
end
|
75
|
+
|
76
|
+
@do_measurement = true
|
77
|
+
end
|
78
|
+
|
79
|
+
# Checks to see if the counted spaces before an lbrace equals the value
|
80
|
+
# at +@config+.
|
81
|
+
#
|
82
|
+
# @param [Fixnum] count The number of spaces before the lbrace.
|
83
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
84
|
+
# @param [Fixnum] column Column the potential problem is on.
|
85
|
+
def measure(count, lineno, column)
|
86
|
+
if count != @config
|
87
|
+
@problems << Problem.new(:spaces_before_lbrace, lineno, column,
|
88
|
+
{ actual_spaces: count, should_have: @config })
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces before a '}' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's the first char in the line.
|
9
|
+
# * it's the first char in the line, preceded by spaces.
|
10
|
+
# * it's directly preceded by a '{'.
|
11
|
+
class SpacesBeforeRbraceRuler < Tailor::Ruler
|
12
|
+
def initialize(config)
|
13
|
+
super(config)
|
14
|
+
@lbrace_nesting = []
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [LexedLine] lexed_line
|
18
|
+
# @param [Fixnum] column
|
19
|
+
# @return [Fixnum] The number of spaces before the rbrace.
|
20
|
+
def count_spaces(lexed_line, column)
|
21
|
+
current_index = lexed_line.event_index(column)
|
22
|
+
log "Current event index: #{current_index}"
|
23
|
+
previous_event = lexed_line.at(current_index - 1)
|
24
|
+
log "Previous event: #{previous_event}"
|
25
|
+
|
26
|
+
if column.zero? || previous_event.nil?
|
27
|
+
log "rbrace is at the beginning of the line."
|
28
|
+
@do_measurement = false
|
29
|
+
return 0
|
30
|
+
end
|
31
|
+
|
32
|
+
if previous_event[1] == :on_lbrace
|
33
|
+
log "rbrace comes after a '{'"
|
34
|
+
@do_measurement = false
|
35
|
+
return 0
|
36
|
+
end
|
37
|
+
|
38
|
+
return 0 if previous_event[1] != :on_sp
|
39
|
+
|
40
|
+
if current_index - 2 < 0
|
41
|
+
log "rbrace is at the beginning of an indented line. Moving on."
|
42
|
+
@do_measurement = false
|
43
|
+
return previous_event.last.size
|
44
|
+
end
|
45
|
+
|
46
|
+
previous_event.last.size
|
47
|
+
end
|
48
|
+
|
49
|
+
def embexpr_beg_update
|
50
|
+
@lbrace_nesting << :embexpr_beg
|
51
|
+
end
|
52
|
+
|
53
|
+
def lbrace_update(lexed_line, lineno, column)
|
54
|
+
@lbrace_nesting << :lbrace
|
55
|
+
end
|
56
|
+
|
57
|
+
# Checks to see if the number of spaces before an rbrace equals the value
|
58
|
+
# at +@config+.
|
59
|
+
#
|
60
|
+
# @param [Fixnum] count The number of spaces after the rbrace.
|
61
|
+
# @param [Fixnum] lineno Line the problem was found on.
|
62
|
+
# @param [Fixnum] column Column the problem was found on.
|
63
|
+
def measure(count, lineno, column)
|
64
|
+
if count != @config
|
65
|
+
@problems << Problem.new(:spaces_before_rbrace, lineno, column,
|
66
|
+
{ actual_spaces: count, should_have: @config })
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# This has to keep track of '{'s and only follow through with the check
|
71
|
+
# if the '{' was an lbrace because Ripper doesn't scan the '}' of an
|
72
|
+
# embedded expression (embexpr_end) as such.
|
73
|
+
#
|
74
|
+
# @param [Tailor::LexedLine] lexed_line
|
75
|
+
# @param [Fixnum] lineno
|
76
|
+
# @param [Fixnum] column
|
77
|
+
def rbrace_update(lexed_line, lineno, column)
|
78
|
+
if @lbrace_nesting.last == :embexpr_beg
|
79
|
+
@lbrace_nesting.pop
|
80
|
+
return
|
81
|
+
end
|
82
|
+
|
83
|
+
@lbrace_nesting.pop
|
84
|
+
|
85
|
+
count = count_spaces(lexed_line, column)
|
86
|
+
log "Found #{count} space(s) before rbrace."
|
87
|
+
|
88
|
+
if @do_measurement == false
|
89
|
+
log "Skipping measurement."
|
90
|
+
else
|
91
|
+
measure(count, lineno, column)
|
92
|
+
end
|
93
|
+
|
94
|
+
@do_measurement = true
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces before a ']' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's the first char in the line.
|
9
|
+
# * it's directly preceded by a '['.
|
10
|
+
# * it's directly preceded by spaces, then a '['.
|
11
|
+
class SpacesBeforeRbracketRuler < Tailor::Ruler
|
12
|
+
|
13
|
+
# @param [LexedLine] lexed_line
|
14
|
+
# @param [Fixnum] column
|
15
|
+
# @return [Fixnum] The number of spaces before the rbracket.
|
16
|
+
def count_spaces(lexed_line, column)
|
17
|
+
current_index = lexed_line.event_index(column)
|
18
|
+
log "Current event index: #{current_index}"
|
19
|
+
previous_event = lexed_line.at(current_index - 1)
|
20
|
+
log "Previous event: #{previous_event}"
|
21
|
+
|
22
|
+
if column.zero? || previous_event.nil? ||
|
23
|
+
previous_event[1] == :on_lbracket
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
return 0 if previous_event[1] != :on_sp
|
28
|
+
return nil if current_index - 2 < 0
|
29
|
+
|
30
|
+
second_previous_event = lexed_line.at(current_index - 2)
|
31
|
+
return nil if second_previous_event[1] == :on_lbracket
|
32
|
+
|
33
|
+
previous_event.last.size
|
34
|
+
end
|
35
|
+
|
36
|
+
# Checks to see if the counted spaces before an rbracket equals the value
|
37
|
+
# at +@config+.
|
38
|
+
#
|
39
|
+
# @param [Fixnum] count The number of spaces before the rbracket.
|
40
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
41
|
+
# @param [Fixnum] column Column the potential problem is on.
|
42
|
+
def measure(count, lineno, column)
|
43
|
+
if count != @config
|
44
|
+
@problems << Problem.new(:spaces_before_rbracket, lineno, column,
|
45
|
+
{ actual_spaces: count, should_have: @config })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# This has to keep track of '{'s and only follow through with the check
|
50
|
+
# if the '{' was an lbrace because Ripper doesn't scan the '}' of an
|
51
|
+
# embedded expression (embexpr_end) as such.
|
52
|
+
#
|
53
|
+
# @param [Tailor::LexedLine] lexed_line
|
54
|
+
# @param [Fixnum] lineno
|
55
|
+
# @param [Fixnum] column
|
56
|
+
def rbracket_update(lexed_line, lineno, column)
|
57
|
+
count = count_spaces(lexed_line, column)
|
58
|
+
|
59
|
+
if count.nil?
|
60
|
+
log "rbracket must be at the beginning of the line."
|
61
|
+
return
|
62
|
+
else
|
63
|
+
log "Found #{count} space(s) before rbracket."
|
64
|
+
end
|
65
|
+
|
66
|
+
measure(count, lineno, column)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces before a ')' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's the first char in the line.
|
9
|
+
# * it's directly preceded by a '('.
|
10
|
+
# * it's directly preceded by spaces, then a '('.
|
11
|
+
class SpacesBeforeRparenRuler < Tailor::Ruler
|
12
|
+
|
13
|
+
# @param [LexedLine] lexed_line
|
14
|
+
# @param [Fixnum] column
|
15
|
+
# @return [Fixnum] The number of spaces before the rparen.
|
16
|
+
def count_spaces(lexed_line, column)
|
17
|
+
current_index = lexed_line.event_index(column)
|
18
|
+
log "Current event index: #{current_index}"
|
19
|
+
previous_event = lexed_line.at(current_index - 1)
|
20
|
+
log "Previous event: #{previous_event}"
|
21
|
+
|
22
|
+
if column.zero? || previous_event.nil? ||
|
23
|
+
previous_event[1] == :on_lparen
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
return 0 if previous_event[1] != :on_sp
|
28
|
+
return nil if current_index - 2 < 0
|
29
|
+
|
30
|
+
second_previous_event = lexed_line.at(current_index - 2)
|
31
|
+
return nil if second_previous_event[1] == :on_lparen
|
32
|
+
|
33
|
+
previous_event.last.size
|
34
|
+
end
|
35
|
+
|
36
|
+
# Checks to see if the counted spaces before an rparen equals the value
|
37
|
+
# at +@config+.
|
38
|
+
#
|
39
|
+
# @param [Fixnum] count The number of spaces before the rparen.
|
40
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
41
|
+
# @param [Fixnum] column Column the potential problem is on.
|
42
|
+
def measure(count, lineno, column)
|
43
|
+
if count != @config
|
44
|
+
@problems << Problem.new(:spaces_before_rparen, lineno, column,
|
45
|
+
{ actual_spaces: count, should_have: @config })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# This has to keep track of '{'s and only follow through with the check
|
50
|
+
# if the '{' was an lbrace because Ripper doesn't scan the '}' of an
|
51
|
+
# embedded expression (embexpr_end) as such.
|
52
|
+
#
|
53
|
+
# @param [Tailor::LexedLine] lexed_line
|
54
|
+
# @param [Fixnum] lineno
|
55
|
+
# @param [Fixnum] column
|
56
|
+
def rparen_update(lexed_line, lineno, column)
|
57
|
+
count = count_spaces(lexed_line, column)
|
58
|
+
|
59
|
+
if count.nil?
|
60
|
+
log "rparen must be at the beginning of the line."
|
61
|
+
return
|
62
|
+
else
|
63
|
+
log "Found #{count} space(s) before rparen."
|
64
|
+
end
|
65
|
+
|
66
|
+
measure(count, lineno, column)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces that exist between a '{' and '}' when there is only
|
7
|
+
# space in between them.
|
8
|
+
class SpacesInEmptyBracesRuler < Tailor::Ruler
|
9
|
+
def initialize(config)
|
10
|
+
super(config)
|
11
|
+
@lbrace_nesting = []
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param [LexedLine] lexed_line
|
15
|
+
# @param [Fixnum] column
|
16
|
+
# @return [Fixnum] The number of spaces before the rbrace.
|
17
|
+
def count_spaces(lexed_line, column)
|
18
|
+
current_index = lexed_line.event_index(column)
|
19
|
+
log "Current event index: #{current_index}"
|
20
|
+
previous_event = lexed_line.at(current_index - 1)
|
21
|
+
log "Previous event: #{previous_event}"
|
22
|
+
|
23
|
+
if column.zero? || previous_event.nil?
|
24
|
+
return
|
25
|
+
end
|
26
|
+
|
27
|
+
if previous_event[1] == :on_lbrace
|
28
|
+
return 0
|
29
|
+
end
|
30
|
+
|
31
|
+
if previous_event[1] == :on_sp
|
32
|
+
second_previous_event = lexed_line.at(current_index - 2)
|
33
|
+
|
34
|
+
if second_previous_event[1] == :on_lbrace
|
35
|
+
previous_event.last.size
|
36
|
+
else
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def embexpr_beg_update
|
43
|
+
@lbrace_nesting << :embexpr_beg
|
44
|
+
end
|
45
|
+
|
46
|
+
def lbrace_update(lexed_line, lineno, column)
|
47
|
+
@lbrace_nesting << :lbrace
|
48
|
+
end
|
49
|
+
|
50
|
+
def nl_update(lexed_line, lineno, column)
|
51
|
+
ignored_nl_update(lexed_line, lineno, column)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Checks to see if the counted spaces between an lbrace and an rbrace
|
55
|
+
# equals the value at +@config+.
|
56
|
+
#
|
57
|
+
# @param [Fixnum] count The number of spaces before the lbrace.
|
58
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
59
|
+
# @param [Fixnum] column Column the potential problem is on.
|
60
|
+
def measure(count, lineno, column)
|
61
|
+
if count != @config
|
62
|
+
@problems << Problem.new(:spaces_in_empty_braces, lineno, column,
|
63
|
+
{ actual_spaces: count, should_have: @config })
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# This has to keep track of '{'s and only follow through with the check
|
68
|
+
# if the '{' was an lbrace because Ripper doesn't scan the '}' of an
|
69
|
+
# embedded expression (embexpr_end) as such.
|
70
|
+
#
|
71
|
+
# @param [Tailor::LexedLine] lexed_line
|
72
|
+
# @param [Fixnum] lineno
|
73
|
+
# @param [Fixnum] column
|
74
|
+
def rbrace_update(lexed_line, lineno, column)
|
75
|
+
if @lbrace_nesting.last == :embexpr_beg
|
76
|
+
@lbrace_nesting.pop
|
77
|
+
return
|
78
|
+
end
|
79
|
+
|
80
|
+
@lbrace_nesting.pop
|
81
|
+
count = count_spaces(lexed_line, column)
|
82
|
+
|
83
|
+
if count.nil?
|
84
|
+
log "Braces aren't empty. Moving on."
|
85
|
+
return
|
86
|
+
else
|
87
|
+
log "Found #{count} space(s) before rbrace."
|
88
|
+
end
|
89
|
+
|
90
|
+
measure(count, lineno, column)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|