ruby-lint 0.0.1a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +5 -0
  2. data/.rbenv-version +1 -0
  3. data/.yardopts +10 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +19 -0
  6. data/MANIFEST +79 -0
  7. data/README.md +48 -0
  8. data/Rakefile +14 -0
  9. data/bin/rlint +6 -0
  10. data/doc/.gitkeep +0 -0
  11. data/doc/build/.gitkeep +0 -0
  12. data/doc/css/.gitkeep +0 -0
  13. data/doc/css/common.css +68 -0
  14. data/lib/rlint/analyze/coding_style.rb +407 -0
  15. data/lib/rlint/analyze/definitions.rb +244 -0
  16. data/lib/rlint/analyze/method_validation.rb +104 -0
  17. data/lib/rlint/analyze/shadowing_variables.rb +37 -0
  18. data/lib/rlint/analyze/undefined_variables.rb +99 -0
  19. data/lib/rlint/analyze/unused_variables.rb +103 -0
  20. data/lib/rlint/callback.rb +67 -0
  21. data/lib/rlint/cli.rb +167 -0
  22. data/lib/rlint/constant_importer.rb +102 -0
  23. data/lib/rlint/definition.rb +230 -0
  24. data/lib/rlint/formatter/text.rb +54 -0
  25. data/lib/rlint/helper/definition_resolver.rb +143 -0
  26. data/lib/rlint/helper/scoping.rb +138 -0
  27. data/lib/rlint/iterator.rb +193 -0
  28. data/lib/rlint/options.rb +58 -0
  29. data/lib/rlint/parser.rb +1252 -0
  30. data/lib/rlint/parser_error.rb +42 -0
  31. data/lib/rlint/report.rb +98 -0
  32. data/lib/rlint/token/assignment_token.rb +46 -0
  33. data/lib/rlint/token/begin_rescue_token.rb +57 -0
  34. data/lib/rlint/token/block_token.rb +17 -0
  35. data/lib/rlint/token/case_token.rb +44 -0
  36. data/lib/rlint/token/class_token.rb +24 -0
  37. data/lib/rlint/token/method_definition_token.rb +64 -0
  38. data/lib/rlint/token/method_token.rb +58 -0
  39. data/lib/rlint/token/parameters_token.rb +99 -0
  40. data/lib/rlint/token/regexp_token.rb +15 -0
  41. data/lib/rlint/token/statement_token.rb +69 -0
  42. data/lib/rlint/token/token.rb +162 -0
  43. data/lib/rlint/token/variable_token.rb +18 -0
  44. data/lib/rlint/version.rb +3 -0
  45. data/lib/rlint.rb +36 -0
  46. data/ruby-lint.gemspec +23 -0
  47. data/spec/benchmarks/memory.rb +52 -0
  48. data/spec/benchmarks/parse_parser.rb +16 -0
  49. data/spec/helper.rb +4 -0
  50. data/spec/rlint/analyze/coding_style.rb +224 -0
  51. data/spec/rlint/analyze/definitions/classes.rb +114 -0
  52. data/spec/rlint/analyze/definitions/methods.rb +91 -0
  53. data/spec/rlint/analyze/definitions/modules.rb +207 -0
  54. data/spec/rlint/analyze/definitions/variables.rb +103 -0
  55. data/spec/rlint/analyze/method_validation.rb +177 -0
  56. data/spec/rlint/analyze/shadowing_variables.rb +30 -0
  57. data/spec/rlint/analyze/undefined_variables.rb +230 -0
  58. data/spec/rlint/analyze/unused_variables.rb +225 -0
  59. data/spec/rlint/callback.rb +28 -0
  60. data/spec/rlint/constant_importer.rb +27 -0
  61. data/spec/rlint/definition.rb +96 -0
  62. data/spec/rlint/formatter/text.rb +21 -0
  63. data/spec/rlint/iterator.rb +452 -0
  64. data/spec/rlint/parser/arrays.rb +147 -0
  65. data/spec/rlint/parser/classes.rb +152 -0
  66. data/spec/rlint/parser/errors.rb +19 -0
  67. data/spec/rlint/parser/hashes.rb +136 -0
  68. data/spec/rlint/parser/methods.rb +249 -0
  69. data/spec/rlint/parser/modules.rb +49 -0
  70. data/spec/rlint/parser/objects.rb +39 -0
  71. data/spec/rlint/parser/operators.rb +75 -0
  72. data/spec/rlint/parser/procs.rb +113 -0
  73. data/spec/rlint/parser/ranges.rb +49 -0
  74. data/spec/rlint/parser/regexp.rb +31 -0
  75. data/spec/rlint/parser/scalars.rb +93 -0
  76. data/spec/rlint/parser/statements.rb +550 -0
  77. data/spec/rlint/parser/variables.rb +181 -0
  78. data/spec/rlint/report.rb +30 -0
  79. data/task/test.rake +6 -0
  80. 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
@@ -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