ruby-lint 0.0.1a
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 +5 -0
- data/.rbenv-version +1 -0
- data/.yardopts +10 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/MANIFEST +79 -0
- data/README.md +48 -0
- data/Rakefile +14 -0
- data/bin/rlint +6 -0
- data/doc/.gitkeep +0 -0
- data/doc/build/.gitkeep +0 -0
- data/doc/css/.gitkeep +0 -0
- data/doc/css/common.css +68 -0
- data/lib/rlint/analyze/coding_style.rb +407 -0
- data/lib/rlint/analyze/definitions.rb +244 -0
- data/lib/rlint/analyze/method_validation.rb +104 -0
- data/lib/rlint/analyze/shadowing_variables.rb +37 -0
- data/lib/rlint/analyze/undefined_variables.rb +99 -0
- data/lib/rlint/analyze/unused_variables.rb +103 -0
- data/lib/rlint/callback.rb +67 -0
- data/lib/rlint/cli.rb +167 -0
- data/lib/rlint/constant_importer.rb +102 -0
- data/lib/rlint/definition.rb +230 -0
- data/lib/rlint/formatter/text.rb +54 -0
- data/lib/rlint/helper/definition_resolver.rb +143 -0
- data/lib/rlint/helper/scoping.rb +138 -0
- data/lib/rlint/iterator.rb +193 -0
- data/lib/rlint/options.rb +58 -0
- data/lib/rlint/parser.rb +1252 -0
- data/lib/rlint/parser_error.rb +42 -0
- data/lib/rlint/report.rb +98 -0
- data/lib/rlint/token/assignment_token.rb +46 -0
- data/lib/rlint/token/begin_rescue_token.rb +57 -0
- data/lib/rlint/token/block_token.rb +17 -0
- data/lib/rlint/token/case_token.rb +44 -0
- data/lib/rlint/token/class_token.rb +24 -0
- data/lib/rlint/token/method_definition_token.rb +64 -0
- data/lib/rlint/token/method_token.rb +58 -0
- data/lib/rlint/token/parameters_token.rb +99 -0
- data/lib/rlint/token/regexp_token.rb +15 -0
- data/lib/rlint/token/statement_token.rb +69 -0
- data/lib/rlint/token/token.rb +162 -0
- data/lib/rlint/token/variable_token.rb +18 -0
- data/lib/rlint/version.rb +3 -0
- data/lib/rlint.rb +36 -0
- data/ruby-lint.gemspec +23 -0
- data/spec/benchmarks/memory.rb +52 -0
- data/spec/benchmarks/parse_parser.rb +16 -0
- data/spec/helper.rb +4 -0
- data/spec/rlint/analyze/coding_style.rb +224 -0
- data/spec/rlint/analyze/definitions/classes.rb +114 -0
- data/spec/rlint/analyze/definitions/methods.rb +91 -0
- data/spec/rlint/analyze/definitions/modules.rb +207 -0
- data/spec/rlint/analyze/definitions/variables.rb +103 -0
- data/spec/rlint/analyze/method_validation.rb +177 -0
- data/spec/rlint/analyze/shadowing_variables.rb +30 -0
- data/spec/rlint/analyze/undefined_variables.rb +230 -0
- data/spec/rlint/analyze/unused_variables.rb +225 -0
- data/spec/rlint/callback.rb +28 -0
- data/spec/rlint/constant_importer.rb +27 -0
- data/spec/rlint/definition.rb +96 -0
- data/spec/rlint/formatter/text.rb +21 -0
- data/spec/rlint/iterator.rb +452 -0
- data/spec/rlint/parser/arrays.rb +147 -0
- data/spec/rlint/parser/classes.rb +152 -0
- data/spec/rlint/parser/errors.rb +19 -0
- data/spec/rlint/parser/hashes.rb +136 -0
- data/spec/rlint/parser/methods.rb +249 -0
- data/spec/rlint/parser/modules.rb +49 -0
- data/spec/rlint/parser/objects.rb +39 -0
- data/spec/rlint/parser/operators.rb +75 -0
- data/spec/rlint/parser/procs.rb +113 -0
- data/spec/rlint/parser/ranges.rb +49 -0
- data/spec/rlint/parser/regexp.rb +31 -0
- data/spec/rlint/parser/scalars.rb +93 -0
- data/spec/rlint/parser/statements.rb +550 -0
- data/spec/rlint/parser/variables.rb +181 -0
- data/spec/rlint/report.rb +30 -0
- data/task/test.rake +6 -0
- metadata +188 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
module Rlint
|
2
|
+
##
|
3
|
+
# Exception class that's raised when the parser {Rlint::Parser} detects
|
4
|
+
# syntax errors.
|
5
|
+
#
|
6
|
+
class ParserError < SyntaxError
|
7
|
+
##
|
8
|
+
# The line number on which the error occured.
|
9
|
+
#
|
10
|
+
# @return [Fixnum|Bignum]
|
11
|
+
#
|
12
|
+
attr_reader :line
|
13
|
+
|
14
|
+
##
|
15
|
+
# The column on which the error occured.
|
16
|
+
#
|
17
|
+
# @return [Fixnum|Bignum]
|
18
|
+
#
|
19
|
+
attr_reader :column
|
20
|
+
|
21
|
+
##
|
22
|
+
# The name of the file in which the error occured.
|
23
|
+
#
|
24
|
+
# @return [String]
|
25
|
+
#
|
26
|
+
attr_reader :file
|
27
|
+
|
28
|
+
##
|
29
|
+
# Creates a new instance of the error class.
|
30
|
+
#
|
31
|
+
# @param [String] message The error message.
|
32
|
+
# @param [Fixnum|Bignum] line The line of the error.
|
33
|
+
# @param [Fixnum|Bignum] column The column of the error.
|
34
|
+
# @param [String] file The file in which the error occured.
|
35
|
+
#
|
36
|
+
def initialize(message, line, column, file)
|
37
|
+
@line, @column, @file = line, column, file
|
38
|
+
|
39
|
+
super(message)
|
40
|
+
end
|
41
|
+
end # ParserError
|
42
|
+
end # Rlint
|
data/lib/rlint/report.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module Rlint
|
2
|
+
##
|
3
|
+
# {Rlint::Report} is a class used for storing error messages, warnings and
|
4
|
+
# informational messages about code processed by {Rlint::Iterator} and
|
5
|
+
# individual callback classes.
|
6
|
+
#
|
7
|
+
# The process of adding data to a report involves two steps:
|
8
|
+
#
|
9
|
+
# 1. setting which levels to enable
|
10
|
+
# 2. adding the data
|
11
|
+
#
|
12
|
+
# The first step is done by creating a new instance of this class and
|
13
|
+
# defining a list of level names in the constructor's second parameter. The
|
14
|
+
# following levels are used by Rlint itself:
|
15
|
+
#
|
16
|
+
# * `:error`
|
17
|
+
# * `:warning`
|
18
|
+
# * `:info`
|
19
|
+
#
|
20
|
+
# The second step is done by calling {Rlint::Report#add}. This method is used
|
21
|
+
# to add data for a specific level. If this level is disabled the data is not
|
22
|
+
# added to the report.
|
23
|
+
#
|
24
|
+
# A basic example of this is the following:
|
25
|
+
#
|
26
|
+
# report = Rlint::Report.new('test_file.rb', [:error])
|
27
|
+
#
|
28
|
+
# report.add(:error, 'This is an error message', 1, 0)
|
29
|
+
#
|
30
|
+
# # This message will not be added since it's not an error.
|
31
|
+
# report.add(:info, 'This is an info message', 2, 0)
|
32
|
+
#
|
33
|
+
class Report
|
34
|
+
##
|
35
|
+
# Array containing the levels that are enabled by default.
|
36
|
+
#
|
37
|
+
# @return [Array]
|
38
|
+
#
|
39
|
+
DEFAULT_LEVELS = [:error, :warning, :info]
|
40
|
+
|
41
|
+
##
|
42
|
+
# A hash containing the various messages stored per level.
|
43
|
+
#
|
44
|
+
# @return [Hash]
|
45
|
+
#
|
46
|
+
attr_reader :messages
|
47
|
+
|
48
|
+
##
|
49
|
+
# An array of levels to use. For example, if `:info` is not included any
|
50
|
+
# message using this level is ignored.
|
51
|
+
#
|
52
|
+
# @return [Array]
|
53
|
+
#
|
54
|
+
attr_reader :levels
|
55
|
+
|
56
|
+
##
|
57
|
+
# String containing the name/path of the file this report belongs to.
|
58
|
+
#
|
59
|
+
# @return [String]
|
60
|
+
#
|
61
|
+
attr_reader :file
|
62
|
+
|
63
|
+
##
|
64
|
+
# Creates a new instance of the class.
|
65
|
+
#
|
66
|
+
# @param [String] file The name/path of the file that this report belongs
|
67
|
+
# to.
|
68
|
+
# @param [Array] levels The message levels to use for this report.
|
69
|
+
#
|
70
|
+
def initialize(file = '(rlint)', levels = DEFAULT_LEVELS)
|
71
|
+
@file = file
|
72
|
+
@levels = levels
|
73
|
+
@messages = {}
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Adds a message to the report.
|
78
|
+
#
|
79
|
+
# @param [#to_sym] level The level of the message.
|
80
|
+
# @param [String] message The message to add.
|
81
|
+
# @param [Fixnum] line The line number of the message.
|
82
|
+
# @param [Fixnum] column The column number of the message.
|
83
|
+
#
|
84
|
+
def add(level, message, line, column)
|
85
|
+
level = level.to_sym
|
86
|
+
|
87
|
+
return unless @levels.include?(level)
|
88
|
+
|
89
|
+
@messages[level] ||= []
|
90
|
+
|
91
|
+
@messages[level] << {
|
92
|
+
:message => message,
|
93
|
+
:line => line,
|
94
|
+
:column => column
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end # Report
|
98
|
+
end # Rlint
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class that is used whenever a value is assigned to a variable,
|
5
|
+
# object attribute or similar.
|
6
|
+
#
|
7
|
+
class AssignmentToken < Token
|
8
|
+
##
|
9
|
+
# The object receiver, set when a value is assigned to an object
|
10
|
+
# attribute.
|
11
|
+
#
|
12
|
+
# @return [Rlint::Token::Token]
|
13
|
+
#
|
14
|
+
attr_accessor :receiver
|
15
|
+
|
16
|
+
##
|
17
|
+
# Symbol containing the operator that was used to separate the receiver
|
18
|
+
# and attribute name.
|
19
|
+
#
|
20
|
+
# @return [Symbol]
|
21
|
+
#
|
22
|
+
attr_accessor :operator
|
23
|
+
|
24
|
+
##
|
25
|
+
# @see Rlint::Token::Token#initialize
|
26
|
+
#
|
27
|
+
def initialize(*args)
|
28
|
+
super
|
29
|
+
|
30
|
+
@type = :local_variable if @type == :identifier
|
31
|
+
@event = :assignment unless @event == :mass_assignment
|
32
|
+
|
33
|
+
# Correct the types for local variable tokens in the name (only used
|
34
|
+
# for mass assignments).
|
35
|
+
if @name and @name.is_a?(Array)
|
36
|
+
@name.each_with_index do |value, index|
|
37
|
+
if @name[index].respond_to?(:type) \
|
38
|
+
and @name[index].type == :identifier
|
39
|
+
@name[index].type = :local_variable
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end # AssignmentToken
|
45
|
+
end # Token
|
46
|
+
end # Rlint
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class used for storing information about begin/rescue/ensure
|
5
|
+
# statements.
|
6
|
+
#
|
7
|
+
class BeginRescueToken < Token
|
8
|
+
##
|
9
|
+
# Array of rescue statements. Each item is an instance of
|
10
|
+
# {Rlint::Token::StatementToken}.
|
11
|
+
#
|
12
|
+
# @return [Array]
|
13
|
+
#
|
14
|
+
attr_accessor :rescue
|
15
|
+
|
16
|
+
##
|
17
|
+
# Attribute containing details about the ensure statement.
|
18
|
+
#
|
19
|
+
# @return [Rlint::Token::StatementToken]
|
20
|
+
#
|
21
|
+
attr_accessor :ensure
|
22
|
+
|
23
|
+
##
|
24
|
+
# Attribute containing details about the else statement.
|
25
|
+
#
|
26
|
+
# @return [Rlint::Token::StatementToken]
|
27
|
+
#
|
28
|
+
attr_accessor :else
|
29
|
+
|
30
|
+
##
|
31
|
+
# @see Rlint::Token#initialize
|
32
|
+
#
|
33
|
+
def initialize(*args)
|
34
|
+
@type = :begin_rescue
|
35
|
+
|
36
|
+
super
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# @see Rlint::Token::Token#child_nodes
|
41
|
+
#
|
42
|
+
def child_nodes
|
43
|
+
nodes = [@rescue]
|
44
|
+
|
45
|
+
if @ensure
|
46
|
+
nodes << [@ensure]
|
47
|
+
end
|
48
|
+
|
49
|
+
if @else
|
50
|
+
nodes << [@else]
|
51
|
+
end
|
52
|
+
|
53
|
+
return super + nodes
|
54
|
+
end
|
55
|
+
end # BeginRescueToken
|
56
|
+
end # Token
|
57
|
+
end # Rlint
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class used for storing information about blocks/procs.
|
5
|
+
#
|
6
|
+
# @since 2012-08-05
|
7
|
+
#
|
8
|
+
class BlockToken < Token
|
9
|
+
##
|
10
|
+
# The parameters of the block.
|
11
|
+
#
|
12
|
+
# @return [Rlint::Token::ParametersToken]
|
13
|
+
#
|
14
|
+
attr_accessor :parameters
|
15
|
+
end
|
16
|
+
end # Token
|
17
|
+
end # Rlint
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class containing details about a case statement.
|
5
|
+
#
|
6
|
+
class CaseToken < StatementToken
|
7
|
+
##
|
8
|
+
# Array containing all the `when` statements sorted in the order of their
|
9
|
+
# appearance.
|
10
|
+
#
|
11
|
+
# @return [Array]
|
12
|
+
#
|
13
|
+
attr_accessor :when
|
14
|
+
|
15
|
+
##
|
16
|
+
# Token containing details about the `else` statement.
|
17
|
+
#
|
18
|
+
# @return [Rlint::Token::StatementToken]
|
19
|
+
#
|
20
|
+
attr_accessor :else
|
21
|
+
|
22
|
+
##
|
23
|
+
# @see Rlint::Token#initialize
|
24
|
+
#
|
25
|
+
def initialize(*args)
|
26
|
+
@when = []
|
27
|
+
@type = :case
|
28
|
+
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# @see Rlint::Token::Token#child_nodes
|
34
|
+
#
|
35
|
+
def child_nodes
|
36
|
+
nodes = super
|
37
|
+
|
38
|
+
nodes.insert(1, @when)
|
39
|
+
|
40
|
+
return nodes.select { |array| array.length > 0 }
|
41
|
+
end
|
42
|
+
end # CaseToken
|
43
|
+
end # Token
|
44
|
+
end # Rlint
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class used for storing data about classes.
|
5
|
+
#
|
6
|
+
class ClassToken < Token
|
7
|
+
##
|
8
|
+
# The name of the parent class, if specified.
|
9
|
+
#
|
10
|
+
# @return [String]
|
11
|
+
#
|
12
|
+
attr_accessor :parent
|
13
|
+
|
14
|
+
##
|
15
|
+
# @see Rlint::Token::Token#initialize
|
16
|
+
#
|
17
|
+
def initialize(*args)
|
18
|
+
super
|
19
|
+
|
20
|
+
@parent = ['Object'] if @parent.empty?
|
21
|
+
end
|
22
|
+
end # ClassToken
|
23
|
+
end # Token
|
24
|
+
end # Rlint
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class used for storing data about a newly defined method.
|
5
|
+
#
|
6
|
+
# @since 2012-08-02
|
7
|
+
#
|
8
|
+
class MethodDefinitionToken < Token
|
9
|
+
##
|
10
|
+
# The object the method is defined on, only set when this is explicitly
|
11
|
+
# stated.
|
12
|
+
#
|
13
|
+
# @return [Rlint::Token::Token]
|
14
|
+
#
|
15
|
+
attr_accessor :receiver
|
16
|
+
|
17
|
+
##
|
18
|
+
# The operator that was used to separate the receiver and method name.
|
19
|
+
#
|
20
|
+
# @return [Rlint::Token::Token]
|
21
|
+
#
|
22
|
+
attr_accessor :operator
|
23
|
+
|
24
|
+
##
|
25
|
+
# The parameters of the method.
|
26
|
+
#
|
27
|
+
# @return [Rlint::Token::ParametersToken]
|
28
|
+
#
|
29
|
+
attr_accessor :parameters
|
30
|
+
|
31
|
+
##
|
32
|
+
# The visibility of the method, set to `:public` by default.
|
33
|
+
#
|
34
|
+
# @since 2012-08-07
|
35
|
+
# @return [Symbol]
|
36
|
+
#
|
37
|
+
attr_accessor :visibility
|
38
|
+
|
39
|
+
##
|
40
|
+
# @see Rlint::Token::Token#initialize
|
41
|
+
#
|
42
|
+
def initialize(*args)
|
43
|
+
@parameters = []
|
44
|
+
@visibility = :public
|
45
|
+
@type = :method_definition
|
46
|
+
|
47
|
+
super
|
48
|
+
|
49
|
+
# Ensure that the parameters attribute always contains an instance of
|
50
|
+
# `Rlint::Token::ParametersToken`.
|
51
|
+
unless @parameters.class == ParametersToken
|
52
|
+
@parameters = ParametersToken.new
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# @see Rlint::Token::Token#child_nodes
|
58
|
+
#
|
59
|
+
def child_nodes
|
60
|
+
return @parameters.child_nodes + super
|
61
|
+
end
|
62
|
+
end # MethodDefinitionToken
|
63
|
+
end # Token
|
64
|
+
end # Rlint
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Rlint
|
2
|
+
module Token
|
3
|
+
##
|
4
|
+
# Token class used for storing methods, their parameters, body, etc.
|
5
|
+
#
|
6
|
+
# @since 2012-07-29
|
7
|
+
#
|
8
|
+
class MethodToken < Token
|
9
|
+
##
|
10
|
+
# The receiver of the method call, if any.
|
11
|
+
#
|
12
|
+
# @since 2012-08-05
|
13
|
+
# @return [Rlint::Token::Token]
|
14
|
+
#
|
15
|
+
attr_accessor :receiver
|
16
|
+
|
17
|
+
##
|
18
|
+
# Symbol containing the method separator, if any.
|
19
|
+
#
|
20
|
+
# @since 2012-08-05
|
21
|
+
# @return [Symbol]
|
22
|
+
#
|
23
|
+
attr_accessor :operator
|
24
|
+
|
25
|
+
##
|
26
|
+
# Array of tokens for the method parameters.
|
27
|
+
#
|
28
|
+
# @since 2012-07-29
|
29
|
+
# @return [Rlint::Token::Parameters]
|
30
|
+
#
|
31
|
+
attr_accessor :parameters
|
32
|
+
|
33
|
+
##
|
34
|
+
# Token containing details about the block passed to the method.
|
35
|
+
#
|
36
|
+
# @since 2012-08-05
|
37
|
+
# @return [Rlint::Token::BlockToken]
|
38
|
+
#
|
39
|
+
attr_accessor :block
|
40
|
+
|
41
|
+
##
|
42
|
+
# @see Rlint::Token::Token#initialize
|
43
|
+
#
|
44
|
+
def initialize(*args)
|
45
|
+
@type = :method
|
46
|
+
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# @see Rlint::Token::Token#child_nodes
|
52
|
+
#
|
53
|
+
def child_nodes
|
54
|
+
return super << @parameters << [@receiver] << [@block]
|
55
|
+
end
|
56
|
+
end # MethodToken
|
57
|
+
end # Token
|
58
|
+
end # Rlint
|