ruby-lint 0.0.2 → 0.0.3
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.tar.gz.asc +17 -0
- data/.gitignore +1 -0
- data/.travis.yml +25 -0
- data/.yardopts +4 -0
- data/Gemfile +1 -1
- data/LICENSE +1 -1
- data/MANIFEST +238 -49
- data/README.md +84 -131
- data/Rakefile +6 -0
- data/bin/ruby-lint +2 -2
- data/checksum/.gitkeep +0 -0
- data/doc/DCO.md +26 -0
- data/doc/architecture.md +63 -0
- data/doc/code_analysis.md +90 -0
- data/doc/configuration.md +86 -0
- data/doc/contributing.md +16 -0
- data/doc/graphviz/flow.dot +7 -0
- data/doc/images/.gitkeep +0 -0
- data/doc/images/flow.png +0 -0
- data/lib/ruby-lint.rb +35 -35
- data/lib/ruby-lint/analyze/argument_amount.rb +73 -0
- data/lib/ruby-lint/analyze/shadowing_variables.rb +19 -24
- data/lib/ruby-lint/analyze/undefined_methods.rb +68 -0
- data/lib/ruby-lint/analyze/undefined_variables.rb +42 -69
- data/lib/ruby-lint/analyze/unused_variables.rb +23 -78
- data/lib/ruby-lint/base.rb +85 -0
- data/lib/ruby-lint/cli.rb +23 -167
- data/lib/ruby-lint/cli/analyze.rb +99 -0
- data/lib/ruby-lint/cli/ast.rb +35 -0
- data/lib/ruby-lint/cli/base.rb +120 -0
- data/lib/ruby-lint/configuration.rb +112 -0
- data/lib/ruby-lint/constant_loader.rb +92 -0
- data/lib/ruby-lint/definition/ruby_method.rb +248 -0
- data/lib/ruby-lint/definition/ruby_object.rb +757 -0
- data/lib/ruby-lint/definition_generator.rb +155 -0
- data/lib/ruby-lint/definitions/core.rb +5 -0
- data/lib/ruby-lint/definitions/core/arg0.rb +7 -0
- data/lib/ruby-lint/definitions/core/argf.rb +7 -0
- data/lib/ruby-lint/definitions/core/argument_error.rb +12 -0
- data/lib/ruby-lint/definitions/core/argv.rb +7 -0
- data/lib/ruby-lint/definitions/core/array.rb +414 -0
- data/lib/ruby-lint/definitions/core/autoload.rb +39 -0
- data/lib/ruby-lint/definitions/core/basic_object.rb +46 -0
- data/lib/ruby-lint/definitions/core/bignum.rb +128 -0
- data/lib/ruby-lint/definitions/core/binding.rb +52 -0
- data/lib/ruby-lint/definitions/core/class.rb +23 -0
- data/lib/ruby-lint/definitions/core/comparable.rb +38 -0
- data/lib/ruby-lint/definitions/core/complex.rb +195 -0
- data/lib/ruby-lint/definitions/core/condition_variable.rb +19 -0
- data/lib/ruby-lint/definitions/core/continuation.rb +8 -0
- data/lib/ruby-lint/definitions/core/data.rb +8 -0
- data/lib/ruby-lint/definitions/core/date.rb +706 -0
- data/lib/ruby-lint/definitions/core/date_time.rb +381 -0
- data/lib/ruby-lint/definitions/core/default_record_separator.rb +7 -0
- data/lib/ruby-lint/definitions/core/digest.rb +166 -0
- data/lib/ruby-lint/definitions/core/dir.rb +496 -0
- data/lib/ruby-lint/definitions/core/encoding.rb +2030 -0
- data/lib/ruby-lint/definitions/core/encoding_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/enumerable.rb +352 -0
- data/lib/ruby-lint/definitions/core/enumerator.rb +37 -0
- data/lib/ruby-lint/definitions/core/env.rb +7 -0
- data/lib/ruby-lint/definitions/core/eoferror.rb +8 -0
- data/lib/ruby-lint/definitions/core/erb.rb +304 -0
- data/lib/ruby-lint/definitions/core/errno.rb +3331 -0
- data/lib/ruby-lint/definitions/core/etc.rb +138 -0
- data/lib/ruby-lint/definitions/core/exception.rb +72 -0
- data/lib/ruby-lint/definitions/core/false.rb +7 -0
- data/lib/ruby-lint/definitions/core/false_class.rb +30 -0
- data/lib/ruby-lint/definitions/core/fatal_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/fiber.rb +35 -0
- data/lib/ruby-lint/definitions/core/fiber_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/file.rb +1277 -0
- data/lib/ruby-lint/definitions/core/file_list.rb +727 -0
- data/lib/ruby-lint/definitions/core/file_test.rb +106 -0
- data/lib/ruby-lint/definitions/core/file_utils.rb +1027 -0
- data/lib/ruby-lint/definitions/core/fixnum.rb +156 -0
- data/lib/ruby-lint/definitions/core/float.rb +307 -0
- data/lib/ruby-lint/definitions/core/float_domain_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/gc.rb +57 -0
- data/lib/ruby-lint/definitions/core/gem.rb +3161 -0
- data/lib/ruby-lint/definitions/core/hash.rb +512 -0
- data/lib/ruby-lint/definitions/core/immediate_value.rb +19 -0
- data/lib/ruby-lint/definitions/core/index_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/integer.rb +100 -0
- data/lib/ruby-lint/definitions/core/interrupt.rb +14 -0
- data/lib/ruby-lint/definitions/core/io.rb +928 -0
- data/lib/ruby-lint/definitions/core/ioerror.rb +8 -0
- data/lib/ruby-lint/definitions/core/kernel.rb +504 -0
- data/lib/ruby-lint/definitions/core/key_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/load_error.rb +28 -0
- data/lib/ruby-lint/definitions/core/local_jump_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/main.rb +25 -0
- data/lib/ruby-lint/definitions/core/marshal.rb +466 -0
- data/lib/ruby-lint/definitions/core/match_data.rb +73 -0
- data/lib/ruby-lint/definitions/core/math.rb +205 -0
- data/lib/ruby-lint/definitions/core/memory_segmention_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/method.rb +61 -0
- data/lib/ruby-lint/definitions/core/module.rb +262 -0
- data/lib/ruby-lint/definitions/core/monitor.rb +39 -0
- data/lib/ruby-lint/definitions/core/monitor_mixin.rb +59 -0
- data/lib/ruby-lint/definitions/core/mutex.rb +32 -0
- data/lib/ruby-lint/definitions/core/name_error.rb +16 -0
- data/lib/ruby-lint/definitions/core/nil.rb +7 -0
- data/lib/ruby-lint/definitions/core/nil_class.rb +46 -0
- data/lib/ruby-lint/definitions/core/no_memory_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/no_method_error.rb +18 -0
- data/lib/ruby-lint/definitions/core/not_implemented_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/numeric.rb +123 -0
- data/lib/ruby-lint/definitions/core/object.rb +31 -0
- data/lib/ruby-lint/definitions/core/object_space.rb +41 -0
- data/lib/ruby-lint/definitions/core/open_struct.rb +49 -0
- data/lib/ruby-lint/definitions/core/option_parser.rb +1355 -0
- data/lib/ruby-lint/definitions/core/precision.rb +21 -0
- data/lib/ruby-lint/definitions/core/primitive_failure.rb +8 -0
- data/lib/ruby-lint/definitions/core/proc.rb +109 -0
- data/lib/ruby-lint/definitions/core/process.rb +602 -0
- data/lib/ruby-lint/definitions/core/psych.rb +2231 -0
- data/lib/ruby-lint/definitions/core/queue.rb +44 -0
- data/lib/ruby-lint/definitions/core/rake.rb +4784 -0
- data/lib/ruby-lint/definitions/core/rake_file_utils.rb +203 -0
- data/lib/ruby-lint/definitions/core/rakeversion.rb +7 -0
- data/lib/ruby-lint/definitions/core/random.rb +38 -0
- data/lib/ruby-lint/definitions/core/range.rb +104 -0
- data/lib/ruby-lint/definitions/core/range_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/rational.rb +96 -0
- data/lib/ruby-lint/definitions/core/rb_config.rb +36 -0
- data/lib/ruby-lint/definitions/core/regexp.rb +396 -0
- data/lib/ruby-lint/definitions/core/regexp_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/rubinius.rb +16637 -0
- data/lib/ruby-lint/definitions/core/ruby_copyright.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_description.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_engine.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_lint.rb +93 -0
- data/lib/ruby-lint/definitions/core/ruby_patchlevel.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_platform.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_release_date.rb +7 -0
- data/lib/ruby-lint/definitions/core/ruby_version.rb +7 -0
- data/lib/ruby-lint/definitions/core/runtime_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/scan_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/script_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/security_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/shellwords.rb +37 -0
- data/lib/ruby-lint/definitions/core/signal.rb +37 -0
- data/lib/ruby-lint/definitions/core/signal_exception.rb +19 -0
- data/lib/ruby-lint/definitions/core/singleton.rb +37 -0
- data/lib/ruby-lint/definitions/core/sized_queue.rb +42 -0
- data/lib/ruby-lint/definitions/core/standard_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/stderr.rb +7 -0
- data/lib/ruby-lint/definitions/core/stdin.rb +7 -0
- data/lib/ruby-lint/definitions/core/stdout.rb +7 -0
- data/lib/ruby-lint/definitions/core/stop_iteration.rb +8 -0
- data/lib/ruby-lint/definitions/core/string.rb +713 -0
- data/lib/ruby-lint/definitions/core/string_io.rb +287 -0
- data/lib/ruby-lint/definitions/core/string_scanner.rb +158 -0
- data/lib/ruby-lint/definitions/core/struct.rb +357 -0
- data/lib/ruby-lint/definitions/core/syck.rb +30 -0
- data/lib/ruby-lint/definitions/core/symbol.rb +90 -0
- data/lib/ruby-lint/definitions/core/syntax_error.rb +44 -0
- data/lib/ruby-lint/definitions/core/system_call_error.rb +31 -0
- data/lib/ruby-lint/definitions/core/system_exit.rb +19 -0
- data/lib/ruby-lint/definitions/core/system_stack_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/thread.rb +209 -0
- data/lib/ruby-lint/definitions/core/thread_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/thread_group.rb +22 -0
- data/lib/ruby-lint/definitions/core/time.rb +233 -0
- data/lib/ruby-lint/definitions/core/toplevel_binding.rb +7 -0
- data/lib/ruby-lint/definitions/core/true.rb +7 -0
- data/lib/ruby-lint/definitions/core/true_class.rb +30 -0
- data/lib/ruby-lint/definitions/core/type_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/unbound_method.rb +51 -0
- data/lib/ruby-lint/definitions/core/unmarshalable.rb +13 -0
- data/lib/ruby-lint/definitions/core/unsupported_library_error.rb +8 -0
- data/lib/ruby-lint/definitions/core/weak_ref.rb +42 -0
- data/lib/ruby-lint/definitions/core/zero_division_error.rb +8 -0
- data/lib/ruby-lint/definitions_builder.rb +692 -0
- data/lib/ruby-lint/extensions/string.rb +15 -0
- data/lib/ruby-lint/helper/constant_paths.rb +41 -0
- data/lib/ruby-lint/helper/conversion.rb +33 -0
- data/lib/ruby-lint/helper/current_scope.rb +98 -0
- data/lib/ruby-lint/helper/methods.rb +91 -0
- data/lib/ruby-lint/inspector.rb +191 -0
- data/lib/ruby-lint/iterator.rb +187 -127
- data/lib/ruby-lint/node.rb +107 -0
- data/lib/ruby-lint/parser.rb +510 -1137
- data/lib/ruby-lint/parser_error.rb +15 -27
- data/lib/ruby-lint/presenter/json.rb +19 -0
- data/lib/ruby-lint/presenter/text.rb +37 -0
- data/lib/ruby-lint/report.rb +95 -53
- data/lib/ruby-lint/report/entry.rb +71 -0
- data/lib/ruby-lint/template/definition.erb +24 -0
- data/lib/ruby-lint/template/scope.rb +25 -0
- data/lib/ruby-lint/variable_predicates.rb +109 -0
- data/lib/ruby-lint/version.rb +1 -1
- data/ruby-lint.gemspec +19 -8
- data/spec/helper.rb +10 -2
- data/spec/ruby-lint/analyze/argument_amount.rb +91 -0
- data/spec/ruby-lint/analyze/shadowing_variables.rb +69 -14
- data/spec/ruby-lint/analyze/undefined_methods.rb +174 -0
- data/spec/ruby-lint/analyze/undefined_variables.rb +70 -179
- data/spec/ruby-lint/analyze/unused_variables.rb +63 -183
- data/spec/ruby-lint/configuration.rb +15 -0
- data/spec/ruby-lint/constant_loader.rb +32 -0
- data/spec/ruby-lint/definition/dsl.rb +142 -0
- data/spec/ruby-lint/definition/method_calls.rb +26 -0
- data/spec/ruby-lint/definition/ruby_method.rb +175 -0
- data/spec/ruby-lint/definition/ruby_object.rb +228 -0
- data/spec/ruby-lint/definitions_builder/assignments/arrays.rb +71 -0
- data/spec/ruby-lint/definitions_builder/assignments/hashes.rb +65 -0
- data/spec/ruby-lint/definitions_builder/assignments/objects.rb +23 -0
- data/spec/ruby-lint/definitions_builder/assignments/optional.rb +22 -0
- data/spec/ruby-lint/definitions_builder/assignments/return_values.rb +78 -0
- data/spec/ruby-lint/definitions_builder/assignments/variables.rb +71 -0
- data/spec/ruby-lint/definitions_builder/associate_nodes.rb +17 -0
- data/spec/ruby-lint/definitions_builder/blocks.rb +40 -0
- data/spec/ruby-lint/definitions_builder/classes.rb +230 -0
- data/spec/ruby-lint/definitions_builder/for.rb +16 -0
- data/spec/ruby-lint/definitions_builder/methods.rb +147 -0
- data/spec/ruby-lint/definitions_builder/modules.rb +175 -0
- data/spec/ruby-lint/definitions_builder/reference_amount.rb +31 -0
- data/spec/ruby-lint/definitions_builder/unused.rb +15 -0
- data/spec/ruby-lint/extensions/string.rb +7 -0
- data/spec/ruby-lint/iterator.rb +42 -417
- data/spec/ruby-lint/node.rb +38 -0
- data/spec/ruby-lint/parser/assignments.rb +225 -0
- data/spec/ruby-lint/parser/classes.rb +80 -122
- data/spec/ruby-lint/parser/errors.rb +7 -14
- data/spec/ruby-lint/parser/metadata.rb +17 -0
- data/spec/ruby-lint/parser/method_definitions.rb +111 -0
- data/spec/ruby-lint/parser/methods.rb +184 -216
- data/spec/ruby-lint/parser/modules.rb +54 -33
- data/spec/ruby-lint/parser/operators.rb +30 -65
- data/spec/ruby-lint/parser/statements/begin.rb +55 -0
- data/spec/ruby-lint/parser/statements/case.rb +34 -0
- data/spec/ruby-lint/parser/statements/defined.rb +11 -0
- data/spec/ruby-lint/parser/statements/for.rb +34 -0
- data/spec/ruby-lint/parser/statements/if.rb +46 -0
- data/spec/ruby-lint/parser/statements/return.rb +14 -0
- data/spec/ruby-lint/parser/statements/super.rb +49 -0
- data/spec/ruby-lint/parser/statements/unless.rb +42 -0
- data/spec/ruby-lint/parser/statements/until.rb +25 -0
- data/spec/ruby-lint/parser/statements/while.rb +25 -0
- data/spec/ruby-lint/parser/statements/yield.rb +18 -0
- data/spec/ruby-lint/parser/types/arrays.rb +47 -0
- data/spec/ruby-lint/parser/types/booleans.rb +11 -0
- data/spec/ruby-lint/parser/types/constants.rb +32 -0
- data/spec/ruby-lint/parser/types/hashes.rb +55 -0
- data/spec/ruby-lint/parser/types/nil.rb +7 -0
- data/spec/ruby-lint/parser/types/numbers.rb +11 -0
- data/spec/ruby-lint/parser/types/procs.rb +11 -0
- data/spec/ruby-lint/parser/types/ranges.rb +11 -0
- data/spec/ruby-lint/parser/types/regexp.rb +27 -0
- data/spec/ruby-lint/parser/types/strings.rb +44 -0
- data/spec/ruby-lint/parser/types/symbols.rb +15 -0
- data/spec/ruby-lint/presenter/json.rb +31 -0
- data/spec/ruby-lint/presenter/text.rb +22 -0
- data/spec/ruby-lint/report.rb +45 -15
- data/spec/ruby-lint/report/entry.rb +24 -0
- data/spec/support/bacon.rb +33 -0
- data/spec/support/building.rb +43 -0
- data/spec/support/definitions.rb +23 -0
- data/spec/support/parsing.rb +23 -0
- data/spec/support/simplecov.rb +16 -0
- data/task/build.rake +9 -0
- data/task/checksum.rake +13 -0
- data/task/coverage.rake +6 -0
- data/task/doc.rake +5 -0
- data/task/generate.rake +34 -0
- data/task/graphviz.rake +12 -0
- data/task/stdlib.rake +2 -9
- data/task/tag.rake +6 -0
- metadata +337 -68
- metadata.gz.asc +17 -0
- data/.rbenv-version +0 -1
- data/lib/ruby-lint/analyze/coding_style.rb +0 -407
- data/lib/ruby-lint/analyze/definitions.rb +0 -244
- data/lib/ruby-lint/analyze/method_validation.rb +0 -104
- data/lib/ruby-lint/callback.rb +0 -67
- data/lib/ruby-lint/constant_importer.rb +0 -112
- data/lib/ruby-lint/definition.rb +0 -230
- data/lib/ruby-lint/formatter/text.rb +0 -54
- data/lib/ruby-lint/helper/definition_resolver.rb +0 -143
- data/lib/ruby-lint/helper/scoping.rb +0 -138
- data/lib/ruby-lint/options.rb +0 -58
- data/lib/ruby-lint/token/assignment_token.rb +0 -35
- data/lib/ruby-lint/token/begin_rescue_token.rb +0 -57
- data/lib/ruby-lint/token/block_token.rb +0 -26
- data/lib/ruby-lint/token/case_token.rb +0 -44
- data/lib/ruby-lint/token/class_token.rb +0 -24
- data/lib/ruby-lint/token/keyword_token.rb +0 -43
- data/lib/ruby-lint/token/method_definition_token.rb +0 -64
- data/lib/ruby-lint/token/method_token.rb +0 -56
- data/lib/ruby-lint/token/parameters_token.rb +0 -99
- data/lib/ruby-lint/token/regexp_token.rb +0 -15
- data/lib/ruby-lint/token/statement_token.rb +0 -69
- data/lib/ruby-lint/token/token.rb +0 -176
- data/lib/ruby-lint/token/variable_token.rb +0 -18
- data/spec/benchmarks/memory.rb +0 -52
- data/spec/benchmarks/parse_parser.rb +0 -16
- data/spec/fixtures/stdlib/un.rb +0 -348
- data/spec/ruby-lint/analyze/coding_style.rb +0 -224
- data/spec/ruby-lint/analyze/complex/un.rb +0 -29
- data/spec/ruby-lint/analyze/definitions/classes.rb +0 -114
- data/spec/ruby-lint/analyze/definitions/methods.rb +0 -91
- data/spec/ruby-lint/analyze/definitions/modules.rb +0 -207
- data/spec/ruby-lint/analyze/definitions/variables.rb +0 -103
- data/spec/ruby-lint/analyze/method_validation.rb +0 -177
- data/spec/ruby-lint/callback.rb +0 -28
- data/spec/ruby-lint/constant_importer.rb +0 -27
- data/spec/ruby-lint/definition.rb +0 -96
- data/spec/ruby-lint/formatter/text.rb +0 -21
- data/spec/ruby-lint/parser/arrays.rb +0 -147
- data/spec/ruby-lint/parser/expander_assignments.rb +0 -183
- data/spec/ruby-lint/parser/hashes.rb +0 -136
- data/spec/ruby-lint/parser/keywords.rb +0 -89
- data/spec/ruby-lint/parser/objects.rb +0 -39
- data/spec/ruby-lint/parser/procs.rb +0 -113
- data/spec/ruby-lint/parser/ranges.rb +0 -49
- data/spec/ruby-lint/parser/regexp.rb +0 -31
- data/spec/ruby-lint/parser/scalars.rb +0 -93
- data/spec/ruby-lint/parser/statements.rb +0 -591
- data/spec/ruby-lint/parser/variables.rb +0 -230
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe 'Building variable definitions using for loops' do
|
|
4
|
+
should 'create local variables' do
|
|
5
|
+
code = <<-CODE
|
|
6
|
+
for key, value in something
|
|
7
|
+
|
|
8
|
+
end
|
|
9
|
+
CODE
|
|
10
|
+
|
|
11
|
+
defs = build_definitions(code)
|
|
12
|
+
|
|
13
|
+
defs.lookup(:local_variable, 'key').is_a?(ruby_object).should == true
|
|
14
|
+
defs.lookup(:local_variable, 'value').is_a?(ruby_object).should == true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe 'Building method definitions' do
|
|
4
|
+
describe 'scoping method definitions' do
|
|
5
|
+
should 'process a global method' do
|
|
6
|
+
defs = build_definitions('def example; end')
|
|
7
|
+
example = defs.lookup(:instance_method, 'example')
|
|
8
|
+
|
|
9
|
+
example.is_a?(ruby_method).should == true
|
|
10
|
+
|
|
11
|
+
example.type.should == :method_definition
|
|
12
|
+
example.method_type.should == :instance_method
|
|
13
|
+
example.name.should == 'example'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
should 'process a nested method' do
|
|
17
|
+
code = <<-CODE
|
|
18
|
+
def first
|
|
19
|
+
def second
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
CODE
|
|
23
|
+
|
|
24
|
+
defs = build_definitions(code)
|
|
25
|
+
first = defs.lookup(:instance_method, 'first')
|
|
26
|
+
|
|
27
|
+
first.is_a?(ruby_method).should == true
|
|
28
|
+
|
|
29
|
+
first.lookup(:instance_method, 'second') \
|
|
30
|
+
.is_a?(ruby_method) \
|
|
31
|
+
.should == true
|
|
32
|
+
|
|
33
|
+
defs.lookup(:instance_method, 'second').nil?.should == true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
should 'process a global and nested method' do
|
|
37
|
+
code = <<-CODE
|
|
38
|
+
def first
|
|
39
|
+
def second
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def third
|
|
44
|
+
end
|
|
45
|
+
CODE
|
|
46
|
+
|
|
47
|
+
defs = build_definitions(code)
|
|
48
|
+
first = defs.lookup(:instance_method, 'first')
|
|
49
|
+
|
|
50
|
+
first.lookup(:instance_method, 'second') \
|
|
51
|
+
.is_a?(ruby_method) \
|
|
52
|
+
.should == true
|
|
53
|
+
|
|
54
|
+
first.lookup(:instance_method, 'second') \
|
|
55
|
+
.lookup(:instance_method, 'third') \
|
|
56
|
+
.is_a?(ruby_method) \
|
|
57
|
+
.should == true
|
|
58
|
+
|
|
59
|
+
defs.lookup(:instance_method, 'third') \
|
|
60
|
+
.is_a?(ruby_method) \
|
|
61
|
+
.should == true
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
should 'process a definition with a variable assignment in the body' do
|
|
66
|
+
code = <<-CODE
|
|
67
|
+
def example
|
|
68
|
+
number = 10
|
|
69
|
+
end
|
|
70
|
+
CODE
|
|
71
|
+
|
|
72
|
+
defs = build_definitions(code)
|
|
73
|
+
|
|
74
|
+
defs.lookup(:instance_method, 'example') \
|
|
75
|
+
.lookup(:local_variable, 'number') \
|
|
76
|
+
.is_a?(ruby_object) \
|
|
77
|
+
.should == true
|
|
78
|
+
|
|
79
|
+
defs.lookup(:local_variable, 'number').nil?.should == true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
should 'process a definition with a receiver' do
|
|
83
|
+
code = <<-CODE
|
|
84
|
+
def String.example
|
|
85
|
+
number = 10
|
|
86
|
+
end
|
|
87
|
+
CODE
|
|
88
|
+
|
|
89
|
+
defs = build_definitions(code)
|
|
90
|
+
|
|
91
|
+
defs.lookup(:constant, 'String') \
|
|
92
|
+
.lookup(:method, 'example') \
|
|
93
|
+
.is_a?(ruby_method) \
|
|
94
|
+
.should == true
|
|
95
|
+
|
|
96
|
+
defs.lookup(:method, 'example').nil?.should == true
|
|
97
|
+
defs.lookup(:instance_method, 'example').nil?.should == true
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe 'creating variables for method parameters' do
|
|
101
|
+
should 'create local variables' do
|
|
102
|
+
code = <<-CODE
|
|
103
|
+
def example(number)
|
|
104
|
+
return number
|
|
105
|
+
end
|
|
106
|
+
CODE
|
|
107
|
+
|
|
108
|
+
defs = build_definitions(code)
|
|
109
|
+
|
|
110
|
+
defs.lookup(:instance_method, 'example') \
|
|
111
|
+
.lookup(:local_variable, 'number') \
|
|
112
|
+
.is_a?(ruby_object) \
|
|
113
|
+
.should == true
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
should 'allow the assignment using parameters' do
|
|
117
|
+
code = <<-CODE
|
|
118
|
+
def example(number)
|
|
119
|
+
other_number = number
|
|
120
|
+
end
|
|
121
|
+
CODE
|
|
122
|
+
|
|
123
|
+
defs = build_definitions(code)
|
|
124
|
+
|
|
125
|
+
defs.lookup(:instance_method, 'example') \
|
|
126
|
+
.lookup(:local_variable, 'other_number') \
|
|
127
|
+
.is_a?(ruby_object) \
|
|
128
|
+
.should == true
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
describe 'exporting variables' do
|
|
133
|
+
should 'export variables to the outer scope' do
|
|
134
|
+
code = <<-CODE
|
|
135
|
+
def example
|
|
136
|
+
@number = 10
|
|
137
|
+
end
|
|
138
|
+
CODE
|
|
139
|
+
|
|
140
|
+
defs = build_definitions(code)
|
|
141
|
+
|
|
142
|
+
defs.lookup(:instance_variable, '@number') \
|
|
143
|
+
.is_a?(ruby_object) \
|
|
144
|
+
.should == true
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe 'Building module definitions' do
|
|
4
|
+
describe 'scoping definitions' do
|
|
5
|
+
should 'process a global module' do
|
|
6
|
+
defs = build_definitions('module Example; end')
|
|
7
|
+
example = defs.lookup(:constant, 'Example')
|
|
8
|
+
|
|
9
|
+
example.is_a?(ruby_object).should == true
|
|
10
|
+
|
|
11
|
+
example.type.should == :module
|
|
12
|
+
example.name.should == 'Example'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
should 'process a nested module' do
|
|
16
|
+
code = <<-CODE
|
|
17
|
+
module First
|
|
18
|
+
module Second
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
CODE
|
|
22
|
+
|
|
23
|
+
defs = build_definitions(code)
|
|
24
|
+
first = defs.lookup(:constant, 'First')
|
|
25
|
+
|
|
26
|
+
first.is_a?(ruby_object).should == true
|
|
27
|
+
|
|
28
|
+
defs.lookup(:constant, 'Second').nil?.should == true
|
|
29
|
+
|
|
30
|
+
first.lookup(:constant, 'Second') \
|
|
31
|
+
.is_a?(ruby_object) \
|
|
32
|
+
.should == true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
should 'process a global and nested module' do
|
|
36
|
+
code = <<-CODE
|
|
37
|
+
module First
|
|
38
|
+
module Second
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module Third
|
|
43
|
+
end
|
|
44
|
+
CODE
|
|
45
|
+
|
|
46
|
+
defs = build_definitions(code)
|
|
47
|
+
first = defs.lookup(:constant, 'First')
|
|
48
|
+
|
|
49
|
+
first.lookup(:constant, 'Second') \
|
|
50
|
+
.is_a?(ruby_object) \
|
|
51
|
+
.should == true
|
|
52
|
+
|
|
53
|
+
# Due to "First" and "Third" being defined in the same scope the "Third"
|
|
54
|
+
# constant is available inside the "First" module.
|
|
55
|
+
first.lookup(:constant, 'Third') \
|
|
56
|
+
.is_a?(ruby_object) \
|
|
57
|
+
.should == true
|
|
58
|
+
|
|
59
|
+
first.lookup(:constant, 'Second') \
|
|
60
|
+
.lookup(:constant, 'Third') \
|
|
61
|
+
.is_a?(ruby_object) \
|
|
62
|
+
.should == true
|
|
63
|
+
|
|
64
|
+
defs.lookup(:constant, 'Third') \
|
|
65
|
+
.is_a?(ruby_object) \
|
|
66
|
+
.should == true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe 'redefining modules' do
|
|
71
|
+
should 'update a module when it is redefined' do
|
|
72
|
+
code = <<-CODE
|
|
73
|
+
module First
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
module First
|
|
77
|
+
def example
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
CODE
|
|
81
|
+
|
|
82
|
+
defs = build_definitions(code)
|
|
83
|
+
|
|
84
|
+
defs.lookup(:constant, 'First') \
|
|
85
|
+
.lookup(:instance_method, 'example') \
|
|
86
|
+
.is_a?(ruby_method) \
|
|
87
|
+
.should == true
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
describe 'including modules' do
|
|
92
|
+
should 'include a module' do
|
|
93
|
+
code = <<-CODE
|
|
94
|
+
module First
|
|
95
|
+
def example
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
module Second
|
|
100
|
+
include First
|
|
101
|
+
end
|
|
102
|
+
CODE
|
|
103
|
+
|
|
104
|
+
defs = build_definitions(code)
|
|
105
|
+
|
|
106
|
+
defs.lookup(:constant, 'Second') \
|
|
107
|
+
.lookup(:instance_method, 'example') \
|
|
108
|
+
.is_a?(ruby_method) \
|
|
109
|
+
.should == true
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
should 'extend a module' do
|
|
113
|
+
code = <<-CODE
|
|
114
|
+
module First
|
|
115
|
+
def example
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
module Second
|
|
120
|
+
extend First
|
|
121
|
+
end
|
|
122
|
+
CODE
|
|
123
|
+
|
|
124
|
+
defs = build_definitions(code)
|
|
125
|
+
|
|
126
|
+
defs.lookup(:constant, 'Second') \
|
|
127
|
+
.lookup(:method, 'example') \
|
|
128
|
+
.is_a?(ruby_method) \
|
|
129
|
+
.should == true
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
should 'include a module using a constant path' do
|
|
133
|
+
code = <<-CODE
|
|
134
|
+
module First
|
|
135
|
+
module Second
|
|
136
|
+
def example
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
module Third
|
|
142
|
+
include First::Second
|
|
143
|
+
end
|
|
144
|
+
CODE
|
|
145
|
+
|
|
146
|
+
defs = build_definitions(code)
|
|
147
|
+
|
|
148
|
+
defs.lookup(:constant, 'Third') \
|
|
149
|
+
.lookup(:instance_method, 'example') \
|
|
150
|
+
.is_a?(ruby_method) \
|
|
151
|
+
.should == true
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
should 'include a module using a variable' do
|
|
155
|
+
code = <<-CODE
|
|
156
|
+
module First
|
|
157
|
+
def example
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
module Second
|
|
162
|
+
first = First
|
|
163
|
+
include first
|
|
164
|
+
end
|
|
165
|
+
CODE
|
|
166
|
+
|
|
167
|
+
defs = build_definitions(code)
|
|
168
|
+
|
|
169
|
+
defs.lookup(:constant, 'Second') \
|
|
170
|
+
.lookup(:instance_method, 'example') \
|
|
171
|
+
.is_a?(ruby_method) \
|
|
172
|
+
.should == true
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe 'Count the reference amount of variables' do
|
|
4
|
+
should 'count the amount of references for a variable' do
|
|
5
|
+
code = <<-CODE
|
|
6
|
+
number = 10
|
|
7
|
+
number
|
|
8
|
+
number
|
|
9
|
+
CODE
|
|
10
|
+
|
|
11
|
+
defs = build_definitions(code)
|
|
12
|
+
|
|
13
|
+
defs.lookup(:local_variable, 'number').reference_amount.should == 2
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
should 'count the amount of references for a constant path' do
|
|
17
|
+
code = <<-CODE
|
|
18
|
+
module A; end
|
|
19
|
+
A::B = 10
|
|
20
|
+
A::B
|
|
21
|
+
A::B
|
|
22
|
+
CODE
|
|
23
|
+
|
|
24
|
+
defs = build_definitions(code)
|
|
25
|
+
|
|
26
|
+
defs.lookup(:constant, 'A') \
|
|
27
|
+
.lookup(:constant, 'B') \
|
|
28
|
+
.reference_amount \
|
|
29
|
+
.should == 2
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require File.expand_path('../../../helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
describe 'Marking variables as used/unused' do
|
|
4
|
+
should 'mark variables as unused by default' do
|
|
5
|
+
defs = build_definitions('number = 10')
|
|
6
|
+
|
|
7
|
+
defs.lookup(:local_variable, 'number').used?.should == false
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
should 'mark variables as used' do
|
|
11
|
+
defs = build_definitions('number = 10; number')
|
|
12
|
+
|
|
13
|
+
defs.lookup(:local_variable, 'number').used?.should == true
|
|
14
|
+
end
|
|
15
|
+
end
|
data/spec/ruby-lint/iterator.rb
CHANGED
|
@@ -1,452 +1,77 @@
|
|
|
1
1
|
require File.expand_path('../../helper', __FILE__)
|
|
2
2
|
|
|
3
3
|
describe 'RubyLint::Iterator' do
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
number = 10
|
|
7
|
-
number
|
|
8
|
-
CODE
|
|
9
|
-
|
|
10
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
11
|
-
iterator = RubyLint::Iterator.new
|
|
12
|
-
|
|
13
|
-
callback = Class.new(RubyLint::Callback) do
|
|
14
|
-
attr_reader :assigned
|
|
15
|
-
attr_reader :referenced
|
|
16
|
-
|
|
17
|
-
def on_assignment(token)
|
|
18
|
-
@assigned = true
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def on_local_variable(token)
|
|
22
|
-
@referenced = true
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
iterator.bind(callback)
|
|
27
|
-
|
|
28
|
-
iterator.run(tokens)
|
|
29
|
-
|
|
30
|
-
iterator.callbacks[0].assigned.should == true
|
|
31
|
-
iterator.callbacks[0].referenced.should == true
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'Iterate over a multi dimensional AST' do
|
|
35
|
-
code = <<-CODE
|
|
36
|
-
class Foo
|
|
37
|
-
def initialize
|
|
38
|
-
@number = 10
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
CODE
|
|
42
|
-
|
|
43
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
44
|
-
iterator = RubyLint::Iterator.new
|
|
45
|
-
callback = Class.new(RubyLint::Callback) do
|
|
46
|
-
attr_reader :class_name
|
|
47
|
-
attr_reader :method_name
|
|
48
|
-
attr_reader :assigned
|
|
49
|
-
|
|
50
|
-
def on_class(token)
|
|
51
|
-
@class_name = token.name[0]
|
|
52
|
-
end
|
|
4
|
+
should 'iterate over a simple AST' do
|
|
5
|
+
ast = parse('10; 20; 30', false)
|
|
53
6
|
|
|
54
|
-
|
|
55
|
-
|
|
7
|
+
iterator = Class.new(RubyLint::Iterator) do
|
|
8
|
+
def on_root(node)
|
|
9
|
+
@options[:events] << :on_root
|
|
56
10
|
end
|
|
57
11
|
|
|
58
|
-
def
|
|
59
|
-
@
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
iterator.bind(callback)
|
|
64
|
-
|
|
65
|
-
iterator.run(tokens)
|
|
66
|
-
|
|
67
|
-
iterator.callbacks[0].class_name.should == 'Foo'
|
|
68
|
-
iterator.callbacks[0].method_name.should == 'initialize'
|
|
69
|
-
iterator.callbacks[0].assigned.should == true
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it 'Call an event after iterating over a node' do
|
|
73
|
-
code = <<-CODE
|
|
74
|
-
def some_method
|
|
75
|
-
return 10
|
|
76
|
-
end
|
|
77
|
-
CODE
|
|
78
|
-
|
|
79
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
80
|
-
iterator = RubyLint::Iterator.new
|
|
81
|
-
callback = Class.new(RubyLint::Callback) do
|
|
82
|
-
attr_reader :before
|
|
83
|
-
attr_reader :after
|
|
84
|
-
|
|
85
|
-
def on_method_definition(token)
|
|
86
|
-
@before = token.name
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def after_method_definition(token)
|
|
90
|
-
@after = token.name
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
iterator.bind(callback)
|
|
95
|
-
iterator.run(tokens)
|
|
96
|
-
|
|
97
|
-
iterator.callbacks[0].before.should == 'some_method'
|
|
98
|
-
iterator.callbacks[0].after.should == 'some_method'
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
it 'Iterate over a begin/rescue statement' do
|
|
102
|
-
code = <<-CODE
|
|
103
|
-
begin
|
|
104
|
-
raise
|
|
105
|
-
rescue RuntimeError => e
|
|
106
|
-
puts 'runtime error!'
|
|
107
|
-
end
|
|
108
|
-
CODE
|
|
109
|
-
|
|
110
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
111
|
-
iterator = RubyLint::Iterator.new
|
|
112
|
-
callback = Class.new(RubyLint::Callback) do
|
|
113
|
-
attr_reader :method_names
|
|
114
|
-
attr_reader :exception
|
|
115
|
-
attr_reader :exception_var
|
|
116
|
-
|
|
117
|
-
def initialize(*args)
|
|
118
|
-
super
|
|
119
|
-
|
|
120
|
-
@method_names = []
|
|
12
|
+
def after_root(node)
|
|
13
|
+
@options[:events] << :after_root
|
|
121
14
|
end
|
|
122
15
|
|
|
123
|
-
def
|
|
124
|
-
@
|
|
125
|
-
|
|
16
|
+
def on_integer(node)
|
|
17
|
+
unless @options[:events].include?(:on_integer)
|
|
18
|
+
@options[:events] << :on_integer
|
|
19
|
+
end
|
|
126
20
|
|
|
127
|
-
|
|
128
|
-
@exception = token.name
|
|
21
|
+
@options[:numbers] << node.children[0]
|
|
129
22
|
end
|
|
130
23
|
|
|
131
|
-
def
|
|
132
|
-
@
|
|
24
|
+
def after_integer(node)
|
|
25
|
+
unless @options[:events].include?(:after_integer)
|
|
26
|
+
@options[:events] << :after_integer
|
|
27
|
+
end
|
|
133
28
|
end
|
|
134
29
|
end
|
|
135
30
|
|
|
136
|
-
iterator.
|
|
137
|
-
iterator.run(tokens)
|
|
31
|
+
iterator = iterator.new(:numbers => [], :events => [])
|
|
138
32
|
|
|
139
|
-
|
|
33
|
+
iterator.iterate(ast)
|
|
140
34
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
35
|
+
iterator.options[:numbers].should == ['10', '20', '30']
|
|
36
|
+
iterator.options[:events].should == [
|
|
37
|
+
:on_root,
|
|
38
|
+
:on_integer,
|
|
39
|
+
:after_integer,
|
|
40
|
+
:after_root
|
|
41
|
+
]
|
|
144
42
|
end
|
|
145
43
|
|
|
146
|
-
|
|
44
|
+
should 'iterate over a multi dimensional AST' do
|
|
147
45
|
code = <<-CODE
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
first
|
|
152
|
-
elsif c == d
|
|
153
|
-
second
|
|
154
|
-
elsif e == f
|
|
155
|
-
third
|
|
156
|
-
else
|
|
157
|
-
fourth
|
|
158
|
-
end
|
|
159
|
-
CODE
|
|
160
|
-
|
|
161
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
162
|
-
iterator = RubyLint::Iterator.new
|
|
163
|
-
callback = Class.new(RubyLint::Callback) do
|
|
164
|
-
attr_reader :variables
|
|
165
|
-
attr_reader :methods
|
|
166
|
-
|
|
167
|
-
def initialize(*args)
|
|
168
|
-
super
|
|
169
|
-
|
|
170
|
-
@variables = []
|
|
171
|
-
@methods = []
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def on_local_variable(token)
|
|
175
|
-
@variables << token.name
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def on_method(token)
|
|
179
|
-
@methods << token.name
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
iterator.bind(callback)
|
|
184
|
-
iterator.run(tokens)
|
|
185
|
-
|
|
186
|
-
obj = iterator.callbacks[0]
|
|
187
|
-
|
|
188
|
-
obj.variables.should == %w{a b c d e f}
|
|
189
|
-
obj.methods.should == %w{first second third fourth}
|
|
46
|
+
class Example
|
|
47
|
+
def some_method
|
|
48
|
+
puts '10'
|
|
190
49
|
end
|
|
191
|
-
|
|
192
|
-
it 'Iterate over a case statement' do
|
|
193
|
-
code = <<-CODE
|
|
194
|
-
case number
|
|
195
|
-
when 10
|
|
196
|
-
first
|
|
197
|
-
when 20
|
|
198
|
-
second
|
|
199
|
-
when 30
|
|
200
|
-
third
|
|
201
|
-
else
|
|
202
|
-
fourth
|
|
203
50
|
end
|
|
204
51
|
CODE
|
|
205
52
|
|
|
206
|
-
|
|
207
|
-
iterator = RubyLint::Iterator.new
|
|
208
|
-
callback = Class.new(RubyLint::Callback) do
|
|
209
|
-
attr_reader :methods
|
|
210
|
-
attr_reader :numbers
|
|
53
|
+
ast = parse(code, false)
|
|
211
54
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
@methods = []
|
|
216
|
-
@numbers = []
|
|
55
|
+
iterator = Class.new(RubyLint::Iterator) do
|
|
56
|
+
def on_class(node)
|
|
57
|
+
@options[:class] = node.children[0].children[0]
|
|
217
58
|
end
|
|
218
59
|
|
|
219
|
-
def
|
|
220
|
-
@
|
|
60
|
+
def on_method_definition(node)
|
|
61
|
+
@options[:method] = node.children[0]
|
|
221
62
|
end
|
|
222
63
|
|
|
223
|
-
def
|
|
224
|
-
@
|
|
64
|
+
def on_method(node)
|
|
65
|
+
@options[:call] = node.children[0]
|
|
225
66
|
end
|
|
226
67
|
end
|
|
227
68
|
|
|
228
|
-
iterator.
|
|
229
|
-
iterator.run(tokens)
|
|
230
|
-
|
|
231
|
-
obj = iterator.callbacks[0]
|
|
232
|
-
|
|
233
|
-
obj.numbers.should == %w{10 20 30}
|
|
234
|
-
obj.methods.should == %w{number first second third fourth}
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
it 'Iterate over a for loop' do
|
|
238
|
-
code = <<-CODE
|
|
239
|
-
for key, value in {:name => 'Ruby'}
|
|
240
|
-
puts key
|
|
241
|
-
end
|
|
242
|
-
CODE
|
|
243
|
-
|
|
244
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
245
|
-
iterator = RubyLint::Iterator.new
|
|
246
|
-
callback = Class.new(RubyLint::Callback) do
|
|
247
|
-
attr_reader :symbol
|
|
248
|
-
attr_reader :string
|
|
249
|
-
attr_reader :names
|
|
250
|
-
|
|
251
|
-
def initialize(*args)
|
|
252
|
-
super
|
|
253
|
-
|
|
254
|
-
@names = []
|
|
255
|
-
end
|
|
256
|
-
|
|
257
|
-
def on_local_variable(token)
|
|
258
|
-
@names << token.name
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
def on_identifier(token)
|
|
262
|
-
@names << token.name
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
def on_symbol(token)
|
|
266
|
-
@symbol = token.name
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
def on_string(token)
|
|
270
|
-
@string = token.value
|
|
271
|
-
end
|
|
272
|
-
end
|
|
273
|
-
|
|
274
|
-
iterator.bind(callback)
|
|
275
|
-
iterator.run(tokens)
|
|
276
|
-
|
|
277
|
-
obj = iterator.callbacks[0]
|
|
278
|
-
|
|
279
|
-
obj.symbol.should == 'name'
|
|
280
|
-
obj.string.should == 'Ruby'
|
|
281
|
-
obj.names.should == %w{key value key}
|
|
282
|
-
end
|
|
283
|
-
|
|
284
|
-
it 'Iterate over a while statement' do
|
|
285
|
-
code = <<-CODE
|
|
286
|
-
while foo == 10
|
|
287
|
-
puts 20
|
|
288
|
-
end
|
|
289
|
-
CODE
|
|
290
|
-
|
|
291
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
292
|
-
iterator = RubyLint::Iterator.new
|
|
293
|
-
callback = Class.new(RubyLint::Callback) do
|
|
294
|
-
attr_reader :numbers
|
|
295
|
-
attr_reader :methods
|
|
296
|
-
|
|
297
|
-
def initialize(*args)
|
|
298
|
-
super
|
|
299
|
-
|
|
300
|
-
@numbers = []
|
|
301
|
-
@methods = []
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
def on_identifier(token)
|
|
305
|
-
@methods << token.name
|
|
306
|
-
end
|
|
307
|
-
|
|
308
|
-
def on_method(token)
|
|
309
|
-
@methods << token.name
|
|
310
|
-
end
|
|
311
|
-
|
|
312
|
-
def on_integer(token)
|
|
313
|
-
@numbers << token.value
|
|
314
|
-
end
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
iterator.bind(callback)
|
|
318
|
-
iterator.run(tokens)
|
|
319
|
-
|
|
320
|
-
obj = iterator.callbacks[0]
|
|
321
|
-
|
|
322
|
-
obj.methods.should == %w{foo puts}
|
|
323
|
-
obj.numbers.should == %w{10 20}
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
it 'Iterate over a method definition' do
|
|
327
|
-
code = <<-CODE
|
|
328
|
-
def foobar(required, optional = 10, *rest, more, &block)
|
|
329
|
-
return 20
|
|
330
|
-
end
|
|
331
|
-
CODE
|
|
332
|
-
|
|
333
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
334
|
-
iterator = RubyLint::Iterator.new
|
|
335
|
-
callback = Class.new(RubyLint::Callback) do
|
|
336
|
-
attr_reader :variables
|
|
337
|
-
attr_reader :numbers
|
|
338
|
-
|
|
339
|
-
def initialize(*args)
|
|
340
|
-
super
|
|
341
|
-
|
|
342
|
-
@variables = []
|
|
343
|
-
@numbers = []
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
def on_local_variable(token)
|
|
347
|
-
@variables << token.name
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
def on_integer(token)
|
|
351
|
-
@numbers << token.value
|
|
352
|
-
end
|
|
353
|
-
end
|
|
354
|
-
|
|
355
|
-
iterator.bind(callback)
|
|
356
|
-
iterator.run(tokens)
|
|
357
|
-
|
|
358
|
-
obj = iterator.callbacks[0]
|
|
359
|
-
|
|
360
|
-
obj.numbers.should == %w{10 20}
|
|
361
|
-
obj.variables.should == %w{required optional rest more block}
|
|
362
|
-
end
|
|
363
|
-
|
|
364
|
-
it 'Iterate over a method call' do
|
|
365
|
-
tokens = RubyLint::Parser.new('puts "Foo", "Bar"').parse
|
|
366
|
-
iterator = RubyLint::Iterator.new
|
|
367
|
-
callback = Class.new(RubyLint::Callback) do
|
|
368
|
-
attr_reader :params
|
|
369
|
-
|
|
370
|
-
def initialize(*args)
|
|
371
|
-
@params = []
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
def on_string(token)
|
|
375
|
-
@params << token.value
|
|
376
|
-
end
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
iterator.bind(callback)
|
|
380
|
-
iterator.run(tokens)
|
|
381
|
-
|
|
382
|
-
obj = iterator.callbacks[0]
|
|
383
|
-
|
|
384
|
-
obj.params.should == %w{Foo Bar}
|
|
385
|
-
end
|
|
386
|
-
|
|
387
|
-
it 'Call an event before and after iterating over all nodes' do
|
|
388
|
-
code = <<-CODE
|
|
389
|
-
class Person
|
|
390
|
-
def initialize
|
|
391
|
-
@name = 'Matz'
|
|
392
|
-
end
|
|
393
|
-
end
|
|
394
|
-
CODE
|
|
395
|
-
|
|
396
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
397
|
-
iterator = RubyLint::Iterator.new
|
|
398
|
-
callback = Class.new(RubyLint::Callback) do
|
|
399
|
-
attr_reader :start
|
|
400
|
-
attr_reader :finish
|
|
401
|
-
|
|
402
|
-
def initialize(*args)
|
|
403
|
-
super
|
|
404
|
-
|
|
405
|
-
@start = 0
|
|
406
|
-
@finish = 0
|
|
407
|
-
end
|
|
408
|
-
|
|
409
|
-
def on_start
|
|
410
|
-
@start += 1
|
|
411
|
-
end
|
|
412
|
-
|
|
413
|
-
def on_finish
|
|
414
|
-
@finish += 1
|
|
415
|
-
end
|
|
416
|
-
end
|
|
417
|
-
|
|
418
|
-
iterator.bind(callback)
|
|
419
|
-
iterator.run(tokens)
|
|
420
|
-
|
|
421
|
-
iterator.callbacks[0].start.should == 1
|
|
422
|
-
iterator.callbacks[0].finish.should == 1
|
|
423
|
-
end
|
|
424
|
-
|
|
425
|
-
it 'Share data between callback classes' do
|
|
426
|
-
code = <<-CODE
|
|
427
|
-
def example_method
|
|
428
|
-
return 10
|
|
429
|
-
end
|
|
430
|
-
CODE
|
|
431
|
-
|
|
432
|
-
tokens = RubyLint::Parser.new(code).parse
|
|
433
|
-
iterator = RubyLint::Iterator.new
|
|
434
|
-
|
|
435
|
-
setter = Class.new(RubyLint::Callback) do
|
|
436
|
-
def on_method_definition(token)
|
|
437
|
-
@storage[:method_name] = token.name
|
|
438
|
-
end
|
|
439
|
-
end
|
|
440
|
-
|
|
441
|
-
getter = Class.new(RubyLint::Callback) do
|
|
442
|
-
attr_reader :storage
|
|
443
|
-
end
|
|
69
|
+
iterator = iterator.new
|
|
444
70
|
|
|
445
|
-
iterator.
|
|
446
|
-
iterator.bind(getter)
|
|
447
|
-
iterator.run(tokens)
|
|
71
|
+
iterator.iterate(ast)
|
|
448
72
|
|
|
449
|
-
iterator.
|
|
450
|
-
iterator.
|
|
73
|
+
iterator.options[:class].should == 'Example'
|
|
74
|
+
iterator.options[:method].should == 'some_method'
|
|
75
|
+
iterator.options[:call].should == 'puts'
|
|
451
76
|
end
|
|
452
77
|
end
|