tailor 0.1.5 → 1.0.0.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,31 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
class MaxLineLengthRuler < Tailor::Ruler
|
6
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
7
|
+
log "<#{self.class}> Line length: #{lexed_line.line_length}"
|
8
|
+
measure(lexed_line, lineno, column)
|
9
|
+
end
|
10
|
+
|
11
|
+
def nl_update(lexed_line, lineno, column)
|
12
|
+
ignored_nl_update(lexed_line, lineno, column)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Checks to see if the line has more characters that given at +@config+.
|
16
|
+
#
|
17
|
+
# @param [Fixnum] lexed_line The line to measure.
|
18
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
19
|
+
# @param [Fixnum] column Column the potential problem is on
|
20
|
+
def measure(lexed_line, lineno, column)
|
21
|
+
if lexed_line.line_length > @config
|
22
|
+
options = {
|
23
|
+
actual_length: lexed_line.line_length,
|
24
|
+
should_be_at: @config
|
25
|
+
}
|
26
|
+
@problems << Problem.new(:line_length, lineno, column, options)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Looks for spaces after a ',' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * the char after it is a '\n'.
|
9
|
+
# * it's at the end of a line, followed by a trailing comment.
|
10
|
+
class SpacesAfterCommaRuler < Tailor::Ruler
|
11
|
+
def initialize(config)
|
12
|
+
super(config)
|
13
|
+
@comma_columns = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def comma_update(text_line, lineno, column)
|
17
|
+
@comma_columns << column
|
18
|
+
end
|
19
|
+
|
20
|
+
def comment_update(token, lexed_line, file_text, lineno, column)
|
21
|
+
if token =~ /\n$/
|
22
|
+
log "Found comment with trailing newline."
|
23
|
+
ignored_nl_update(lexed_line, lineno, column)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
28
|
+
check_spaces_after_comma(lexed_line, lineno)
|
29
|
+
end
|
30
|
+
|
31
|
+
def nl_update(lexed_line, lineno, column)
|
32
|
+
ignored_nl_update(lexed_line, lineno, column)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Checks to see if the actual_spaces after a comma equals the value
|
36
|
+
# at +@config+.
|
37
|
+
#
|
38
|
+
# @param [Fixnum] actual_spaces The number of spaces after the comma.
|
39
|
+
# @param [Fixnum] lineno Line the problem is on.
|
40
|
+
# @param [Fixnum] column Column the potential problem is on.
|
41
|
+
def measure(actual_spaces, lineno, column)
|
42
|
+
if actual_spaces != @config
|
43
|
+
@problems << Problem.new(:spaces_after_comma, lineno, column + 1,
|
44
|
+
{ actual_spaces: actual_spaces, should_have: @config })
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def check_spaces_after_comma(lexed_line, lineno)
|
49
|
+
log "Commas found at: #{@comma_columns}" unless @comma_columns.empty?
|
50
|
+
|
51
|
+
@comma_columns.each do |c|
|
52
|
+
event_index = lexed_line.event_index(c)
|
53
|
+
if event_index.nil?
|
54
|
+
log "Event index is nil. Weird..."
|
55
|
+
break
|
56
|
+
end
|
57
|
+
|
58
|
+
next_event = lexed_line.at(event_index + 1)
|
59
|
+
if next_event.nil?
|
60
|
+
log "Looks like there is no next event (this is last in the line)."
|
61
|
+
break
|
62
|
+
end
|
63
|
+
|
64
|
+
if next_event[1] == :on_nl || next_event[1] == :on_ignored_nl
|
65
|
+
log "Next event is a newline."
|
66
|
+
break
|
67
|
+
end
|
68
|
+
|
69
|
+
second_next_event = lexed_line.at(event_index + 2)
|
70
|
+
if second_next_event[1] == :on_comment
|
71
|
+
log "Event + 2 is a comment."
|
72
|
+
next
|
73
|
+
end
|
74
|
+
|
75
|
+
actual_spaces = next_event[1] != :on_sp ? 0 : next_event.last.size
|
76
|
+
measure(actual_spaces, lineno, c)
|
77
|
+
end
|
78
|
+
|
79
|
+
@comma_columns.clear
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Checks for spaces after a '{' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's at the end of a line.
|
9
|
+
# * the next char is a '}'
|
10
|
+
# * it's at the end of a line, followed by a trailing comment.
|
11
|
+
class SpacesAfterLbraceRuler < Tailor::Ruler
|
12
|
+
def initialize(config)
|
13
|
+
super(config)
|
14
|
+
@lbrace_columns = []
|
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
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
25
|
+
check_spaces_after_lbrace(lexed_line, lineno)
|
26
|
+
end
|
27
|
+
|
28
|
+
def lbrace_update(lexed_line, lineno, column)
|
29
|
+
@lbrace_columns << column
|
30
|
+
end
|
31
|
+
|
32
|
+
def nl_update(lexed_line, lineno, column)
|
33
|
+
ignored_nl_update(lexed_line, lineno, column)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Checks to see if the number of spaces after an lbrace equals the value
|
37
|
+
# at +@config+.
|
38
|
+
#
|
39
|
+
# @param [Fixnum] actual_spaces The number of spaces after the lbrace.
|
40
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
41
|
+
# @param [Fixnum] column Column the potential problem is on.
|
42
|
+
def measure(actual_spaces, lineno, column)
|
43
|
+
if actual_spaces != @config
|
44
|
+
@problems << Problem.new(:spaces_after_lbrace, lineno, column + 1,
|
45
|
+
{ actual_spaces: actual_spaces, should_have: @config })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def check_spaces_after_lbrace(lexed_line, lineno)
|
50
|
+
log "lbraces found at: #{@lbrace_columns}" unless @lbrace_columns.empty?
|
51
|
+
|
52
|
+
@lbrace_columns.each do |column|
|
53
|
+
actual_spaces = count_spaces(lexed_line, column)
|
54
|
+
next if actual_spaces.nil?
|
55
|
+
|
56
|
+
if @do_measurement == false
|
57
|
+
log "Skipping measurement."
|
58
|
+
else
|
59
|
+
measure(actual_spaces, lineno, column)
|
60
|
+
end
|
61
|
+
|
62
|
+
@do_measurement = true
|
63
|
+
end
|
64
|
+
|
65
|
+
@lbrace_columns.clear
|
66
|
+
end
|
67
|
+
|
68
|
+
# Counts the number of spaces after the lbrace.
|
69
|
+
#
|
70
|
+
# @param [LexedLine] lexed_line The LexedLine that contains the context
|
71
|
+
# the lbrace was found in.
|
72
|
+
# @param [Fixnum] column Column the lbrace was found at.
|
73
|
+
# @return [Fixnum] The number of spaces found after the lbrace.
|
74
|
+
def count_spaces(lexed_line, column)
|
75
|
+
event_index = lexed_line.event_index(column)
|
76
|
+
|
77
|
+
if event_index.nil?
|
78
|
+
log "No lbrace in this line. Moving on..."
|
79
|
+
@do_measurement = false
|
80
|
+
return
|
81
|
+
end
|
82
|
+
|
83
|
+
next_event = lexed_line.at(event_index + 1)
|
84
|
+
|
85
|
+
if next_event.nil?
|
86
|
+
log "lbrace must be at the end of the line. Moving on."
|
87
|
+
@do_measurement = false
|
88
|
+
return 0
|
89
|
+
end
|
90
|
+
|
91
|
+
if next_event[1] == :on_nl || next_event[1] == :on_ignored_nl
|
92
|
+
log "lbrace is followed by a '#{next_event[1]}'. Moving on."
|
93
|
+
@do_measurement = false
|
94
|
+
return 0
|
95
|
+
end
|
96
|
+
|
97
|
+
if next_event[1] == :on_rbrace
|
98
|
+
log "lbrace is followed by an rbrace. Moving on."
|
99
|
+
@do_measurement = false
|
100
|
+
return 0
|
101
|
+
end
|
102
|
+
|
103
|
+
second_next_event = lexed_line.at(event_index + 2)
|
104
|
+
if second_next_event[1] == :on_comment
|
105
|
+
log "Event + 2 is a comment."
|
106
|
+
@do_measurement = false
|
107
|
+
return next_event.last.size
|
108
|
+
end
|
109
|
+
|
110
|
+
next_event[1] != :on_sp ? 0 : next_event.last.size
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
|
6
|
+
# Detects spaces after a '[' as given by +@config+. It skips checking
|
7
|
+
# when:
|
8
|
+
# * it's the last char in line.
|
9
|
+
# * the char after it is a ']'.
|
10
|
+
# * the char after it is space, then a '{'.
|
11
|
+
class SpacesAfterLbracketRuler < Tailor::Ruler
|
12
|
+
def initialize(config)
|
13
|
+
super(config)
|
14
|
+
@lbracket_columns = []
|
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
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
25
|
+
check_spaces_after_lbracket(lexed_line, lineno)
|
26
|
+
end
|
27
|
+
|
28
|
+
def lbracket_update(lexed_line, lineno, column)
|
29
|
+
@lbracket_columns << column
|
30
|
+
end
|
31
|
+
|
32
|
+
def nl_update(lexed_line, lineno, column)
|
33
|
+
ignored_nl_update(lexed_line, lineno, column)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Checks to see if the actual_spaces after an lbracket equals the value
|
37
|
+
# at +@config+.
|
38
|
+
#
|
39
|
+
# @param [Fixnum] actual_spaces The number of spaces after the lbracket.
|
40
|
+
# @param [Fixnum] lineno Line the problem was found on.
|
41
|
+
# @param [Fixnum] column Column the problem was found on.
|
42
|
+
def measure(actual_spaces, lineno, column)
|
43
|
+
if actual_spaces != @config
|
44
|
+
@problems << Problem.new(:spaces_after_lbracket, lineno, column + 1,
|
45
|
+
{ actual_spaces: actual_spaces, should_have: @config })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def check_spaces_after_lbracket(lexed_line, lineno)
|
50
|
+
unless @lbracket_columns.empty?
|
51
|
+
log "lbracket found at: #{@lbracket_columns}"
|
52
|
+
end
|
53
|
+
|
54
|
+
@lbracket_columns.each do |column|
|
55
|
+
actual_spaces = count_spaces(lexed_line, column)
|
56
|
+
next if actual_spaces.nil?
|
57
|
+
|
58
|
+
if @do_measurement == false
|
59
|
+
log "Skipping measurement."
|
60
|
+
else
|
61
|
+
measure(actual_spaces, lineno, column)
|
62
|
+
end
|
63
|
+
|
64
|
+
@do_measurement = true
|
65
|
+
end
|
66
|
+
|
67
|
+
@lbracket_columns.clear
|
68
|
+
end
|
69
|
+
|
70
|
+
# Counts the number of spaces after the lbracket.
|
71
|
+
#
|
72
|
+
# @param [LexedLine] lexed_line The LexedLine that contains the context
|
73
|
+
# the lbracket was found in.
|
74
|
+
# @param [Fixnum] column Column the lbracket was found at.
|
75
|
+
# @return [Fixnum] The number of spaces found after the lbracket.
|
76
|
+
def count_spaces(lexed_line, column)
|
77
|
+
event_index = lexed_line.event_index(column)
|
78
|
+
|
79
|
+
if event_index.nil?
|
80
|
+
log "No lbracket in this line. Moving on..."
|
81
|
+
@do_measurement = false
|
82
|
+
return
|
83
|
+
end
|
84
|
+
|
85
|
+
next_event = lexed_line.at(event_index + 1)
|
86
|
+
log "Next event: #{next_event}"
|
87
|
+
|
88
|
+
if next_event.nil?
|
89
|
+
log "lbracket must be at the end of the line."
|
90
|
+
@do_measurement = false
|
91
|
+
return 0
|
92
|
+
end
|
93
|
+
|
94
|
+
[:on_nl, :on_ignored_nl].each do |event|
|
95
|
+
if next_event[1] == event
|
96
|
+
log "lbracket is followed by a '#{event}'. Moving on."
|
97
|
+
@do_measurement = false
|
98
|
+
return 0
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
if next_event[1] == :on_rbracket
|
103
|
+
log "lbracket is followed by a rbracket. Moving on."
|
104
|
+
@do_measurement = false
|
105
|
+
return 0
|
106
|
+
end
|
107
|
+
|
108
|
+
second_next_event = lexed_line.at(event_index + 2)
|
109
|
+
log "Event + 2: #{second_next_event}"
|
110
|
+
|
111
|
+
[:on_comment, :on_lbrace].each do |event|
|
112
|
+
if second_next_event[1] == event
|
113
|
+
log "Event + 2 is a #{event}. Moving on."
|
114
|
+
@do_measurement = false
|
115
|
+
return next_event.last.size
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
next_event[1] != :on_sp ? 0 : next_event.last.size
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require_relative '../ruler'
|
2
|
+
|
3
|
+
class Tailor
|
4
|
+
module Rulers
|
5
|
+
class SpacesAfterLparenRuler < Tailor::Ruler
|
6
|
+
def initialize(config)
|
7
|
+
super(config)
|
8
|
+
@lparen_columns = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def comment_update(token, lexed_line, file_text, lineno, column)
|
12
|
+
if token =~ /\n$/
|
13
|
+
log "Found comment with trailing newline."
|
14
|
+
ignored_nl_update(lexed_line, lineno, column)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def ignored_nl_update(lexed_line, lineno, column)
|
19
|
+
check_spaces_after_lparen(lexed_line, lineno)
|
20
|
+
end
|
21
|
+
|
22
|
+
def lparen_update(lineno, column)
|
23
|
+
@lparen_columns << column
|
24
|
+
end
|
25
|
+
|
26
|
+
def nl_update(lexed_line, lineno, column)
|
27
|
+
ignored_nl_update(lexed_line, lineno, column)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Checks to see if the number of spaces after an lparen equals the value
|
31
|
+
# at +@config+.
|
32
|
+
#
|
33
|
+
# @param [Fixnum] actual_spaces The number of spaces after the lparen.
|
34
|
+
# @param [Fixnum] lineno Line the potential problem is on.
|
35
|
+
# @param [Fixnum] column Column the potential problem is on.
|
36
|
+
def measure(actual_spaces, lineno, column)
|
37
|
+
if actual_spaces != @config
|
38
|
+
@problems << Problem.new(:spaces_after_lparen, lineno, column + 1,
|
39
|
+
{ actual_spaces: actual_spaces, should_have: @config })
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def check_spaces_after_lparen(lexed_line, lineno)
|
44
|
+
unless @lparen_columns.empty?
|
45
|
+
log "lparens found at: #{@lparen_columns}"
|
46
|
+
end
|
47
|
+
|
48
|
+
@lparen_columns.each do |column|
|
49
|
+
actual_spaces = count_spaces(lexed_line, column)
|
50
|
+
next if actual_spaces.nil?
|
51
|
+
|
52
|
+
if @do_measurement == false
|
53
|
+
log "Skipping measurement."
|
54
|
+
else
|
55
|
+
measure(actual_spaces, lineno, column)
|
56
|
+
end
|
57
|
+
|
58
|
+
@do_measurement = true
|
59
|
+
end
|
60
|
+
|
61
|
+
@lparen_columns.clear
|
62
|
+
end
|
63
|
+
|
64
|
+
# Counts the number of spaces after the lparen.
|
65
|
+
#
|
66
|
+
# @param [LexedLine] lexed_line The LexedLine that contains the context
|
67
|
+
# the lparen was found in.
|
68
|
+
# @param [Fixnum] column Column the lparen was found at.
|
69
|
+
# @return [Fixnum] The number of spaces found after the lparen.
|
70
|
+
def count_spaces(lexed_line, column)
|
71
|
+
event_index = lexed_line.event_index(column)
|
72
|
+
|
73
|
+
if event_index.nil?
|
74
|
+
log "No lparen in this line. Moving on..."
|
75
|
+
@do_measurement = false
|
76
|
+
return
|
77
|
+
end
|
78
|
+
|
79
|
+
next_event = lexed_line.at(event_index + 1)
|
80
|
+
log "Next event: #{next_event}"
|
81
|
+
|
82
|
+
if next_event.nil?
|
83
|
+
log "lparen must be at the end of the line."
|
84
|
+
@do_measurement = false
|
85
|
+
return 0
|
86
|
+
end
|
87
|
+
|
88
|
+
[:on_nl, :on_ignored_nl].each do |event|
|
89
|
+
if next_event[1] == event
|
90
|
+
log "lparen is followed by a '#{event}'. Moving on."
|
91
|
+
return 0
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if next_event[1] == :on_rparen
|
96
|
+
log "lparen is followed by an rparen. Moving on."
|
97
|
+
@do_measurement = false
|
98
|
+
return 0
|
99
|
+
end
|
100
|
+
|
101
|
+
second_next_event = lexed_line.at(event_index + 2)
|
102
|
+
log "Event + 2: #{second_next_event}"
|
103
|
+
|
104
|
+
[:on_comment, :on_lbrace, :on_lparen].each do |event|
|
105
|
+
if second_next_event[1] == event
|
106
|
+
log "Event + 2 is a #{event}. Moving on."
|
107
|
+
@do_measurement = false
|
108
|
+
return next_event.last.size
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
next_event[1] != :on_sp ? 0 : next_event.last.size
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|