solargraph-rails 0.2.2.pre.4 → 1.0.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +33 -0
- data/.github/workflows/ruby.yml +36 -0
- data/.gitignore +8 -0
- data/.solargraph.yml +19 -0
- data/CHANGELOG.md +19 -0
- data/DEVELOPMENT.md +83 -0
- data/README.md +5 -3
- data/lib/solargraph/rails/annotate.rb +44 -0
- data/lib/solargraph/rails/annotations.rb +50 -0
- data/lib/solargraph/rails/autoload.rb +53 -0
- data/lib/solargraph/rails/debug.rb +30 -0
- data/lib/solargraph/rails/delegate.rb +42 -0
- data/lib/solargraph/rails/devise.rb +48 -0
- data/lib/solargraph/rails/model.rb +100 -0
- data/lib/solargraph/rails/rails_api.rb +98 -0
- data/lib/solargraph/rails/schema.rb +100 -0
- data/lib/solargraph/rails/storage.rb +48 -0
- data/lib/solargraph/rails/types.yml +24 -0
- data/lib/solargraph/rails/util.rb +68 -0
- data/lib/solargraph/rails/version.rb +1 -1
- data/lib/solargraph/rails/walker.rb +89 -0
- data/lib/solargraph-rails.rb +57 -22
- data/script/generate_definitions.rb +134 -0
- data/solargraph-rails.gemspec +24 -22
- metadata +26 -16
- data/Gemfile.lock +0 -109
- data/lib/solargraph/rails/files_loader.rb +0 -16
- data/lib/solargraph/rails/meta_source/association/belongs_to_matcher.rb +0 -24
- data/lib/solargraph/rails/meta_source/association/has_and_belongs_to_many_matcher.rb +0 -24
- data/lib/solargraph/rails/meta_source/association/has_many_matcher.rb +0 -24
- data/lib/solargraph/rails/meta_source/association/has_one_matcher.rb +0 -24
- data/lib/solargraph/rails/pin_creator.rb +0 -117
- data/lib/solargraph/rails/ruby_parser.rb +0 -77
data/solargraph-rails.gemspec
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'solargraph/rails/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
6
|
+
spec.name = 'solargraph-rails'
|
7
|
+
spec.version = Solargraph::Rails::VERSION
|
8
|
+
spec.authors = ['Fritz Meissner']
|
9
|
+
spec.email = ['fritz.meissner@gmail.com']
|
11
10
|
|
12
|
-
spec.summary
|
13
|
-
|
14
|
-
spec.
|
15
|
-
|
11
|
+
spec.summary =
|
12
|
+
'Solargraph plugin that adds Rails-specific code through a Convention'
|
13
|
+
spec.description =
|
14
|
+
'Add reflection on ActiveModel dynamic attributes that will be created at runtime'
|
15
|
+
spec.homepage = 'https://github.com/iftheshoefritz/solargraph-rails'
|
16
|
+
spec.license = 'MIT'
|
16
17
|
|
17
|
-
spec.files
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
spec.
|
22
|
-
spec.
|
18
|
+
spec.files =
|
19
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
23
25
|
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.3'
|
27
|
+
spec.add_development_dependency 'rake', '~> 12.3.3'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
29
|
|
28
|
-
spec.add_runtime_dependency
|
29
|
-
spec.add_runtime_dependency
|
30
|
+
spec.add_runtime_dependency 'solargraph', '~> 0.44.2'
|
31
|
+
spec.add_runtime_dependency 'activesupport'
|
30
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: solargraph-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.pre.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fritz Meissner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: '2.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
26
|
+
version: '2.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: solargraph
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 0.44.2
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 0.44.2
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activesupport
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,12 +88,15 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
+
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
92
|
+
- ".github/workflows/ruby.yml"
|
91
93
|
- ".gitignore"
|
92
94
|
- ".rspec"
|
95
|
+
- ".solargraph.yml"
|
93
96
|
- ".travis.yml"
|
94
97
|
- CHANGELOG.md
|
98
|
+
- DEVELOPMENT.md
|
95
99
|
- Gemfile
|
96
|
-
- Gemfile.lock
|
97
100
|
- LICENSE.txt
|
98
101
|
- README.md
|
99
102
|
- Rakefile
|
@@ -104,14 +107,21 @@ files:
|
|
104
107
|
- bin/console
|
105
108
|
- bin/setup
|
106
109
|
- lib/solargraph-rails.rb
|
107
|
-
- lib/solargraph/rails/
|
108
|
-
- lib/solargraph/rails/
|
109
|
-
- lib/solargraph/rails/
|
110
|
-
- lib/solargraph/rails/
|
111
|
-
- lib/solargraph/rails/
|
112
|
-
- lib/solargraph/rails/
|
113
|
-
- lib/solargraph/rails/
|
110
|
+
- lib/solargraph/rails/annotate.rb
|
111
|
+
- lib/solargraph/rails/annotations.rb
|
112
|
+
- lib/solargraph/rails/autoload.rb
|
113
|
+
- lib/solargraph/rails/debug.rb
|
114
|
+
- lib/solargraph/rails/delegate.rb
|
115
|
+
- lib/solargraph/rails/devise.rb
|
116
|
+
- lib/solargraph/rails/model.rb
|
117
|
+
- lib/solargraph/rails/rails_api.rb
|
118
|
+
- lib/solargraph/rails/schema.rb
|
119
|
+
- lib/solargraph/rails/storage.rb
|
120
|
+
- lib/solargraph/rails/types.yml
|
121
|
+
- lib/solargraph/rails/util.rb
|
114
122
|
- lib/solargraph/rails/version.rb
|
123
|
+
- lib/solargraph/rails/walker.rb
|
124
|
+
- script/generate_definitions.rb
|
115
125
|
- solargraph-rails.gemspec
|
116
126
|
homepage: https://github.com/iftheshoefritz/solargraph-rails
|
117
127
|
licenses:
|
data/Gemfile.lock
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
solargraph-rails (0.2.2.pre.4)
|
5
|
-
activesupport
|
6
|
-
solargraph (>= 0.41.1)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: https://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activesupport (6.1.4)
|
12
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
-
i18n (>= 1.6, < 2)
|
14
|
-
minitest (>= 5.1)
|
15
|
-
tzinfo (~> 2.0)
|
16
|
-
zeitwerk (~> 2.3)
|
17
|
-
ast (2.4.2)
|
18
|
-
backport (1.2.0)
|
19
|
-
benchmark (0.1.1)
|
20
|
-
bundler-audit (0.8.0)
|
21
|
-
bundler (>= 1.2.0, < 3)
|
22
|
-
thor (~> 1.0)
|
23
|
-
byebug (11.1.3)
|
24
|
-
concurrent-ruby (1.1.9)
|
25
|
-
diff-lcs (1.4.4)
|
26
|
-
e2mmap (0.1.0)
|
27
|
-
i18n (1.8.10)
|
28
|
-
concurrent-ruby (~> 1.0)
|
29
|
-
jaro_winkler (1.5.4)
|
30
|
-
kramdown (2.3.1)
|
31
|
-
rexml
|
32
|
-
kramdown-parser-gfm (1.1.0)
|
33
|
-
kramdown (~> 2.0)
|
34
|
-
mini_portile2 (2.5.3)
|
35
|
-
minitest (5.14.4)
|
36
|
-
nokogiri (1.11.7)
|
37
|
-
mini_portile2 (~> 2.5.0)
|
38
|
-
racc (~> 1.4)
|
39
|
-
parallel (1.20.1)
|
40
|
-
parser (3.0.1.1)
|
41
|
-
ast (~> 2.4.1)
|
42
|
-
racc (1.5.2)
|
43
|
-
rainbow (3.0.0)
|
44
|
-
rake (12.3.3)
|
45
|
-
regexp_parser (2.1.1)
|
46
|
-
reverse_markdown (2.0.0)
|
47
|
-
nokogiri
|
48
|
-
rexml (3.2.5)
|
49
|
-
rspec (3.10.0)
|
50
|
-
rspec-core (~> 3.10.0)
|
51
|
-
rspec-expectations (~> 3.10.0)
|
52
|
-
rspec-mocks (~> 3.10.0)
|
53
|
-
rspec-core (3.10.0)
|
54
|
-
rspec-support (~> 3.10.0)
|
55
|
-
rspec-expectations (3.10.0)
|
56
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
57
|
-
rspec-support (~> 3.10.0)
|
58
|
-
rspec-mocks (3.10.0)
|
59
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
-
rspec-support (~> 3.10.0)
|
61
|
-
rspec-support (3.10.0)
|
62
|
-
rubocop (1.17.0)
|
63
|
-
parallel (~> 1.10)
|
64
|
-
parser (>= 3.0.0.0)
|
65
|
-
rainbow (>= 2.2.2, < 4.0)
|
66
|
-
regexp_parser (>= 1.8, < 3.0)
|
67
|
-
rexml
|
68
|
-
rubocop-ast (>= 1.7.0, < 2.0)
|
69
|
-
ruby-progressbar (~> 1.7)
|
70
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
71
|
-
rubocop-ast (1.7.0)
|
72
|
-
parser (>= 3.0.1.1)
|
73
|
-
ruby-progressbar (1.11.0)
|
74
|
-
solargraph (0.42.3)
|
75
|
-
backport (~> 1.2)
|
76
|
-
benchmark
|
77
|
-
bundler (>= 1.17.2)
|
78
|
-
diff-lcs (~> 1.4)
|
79
|
-
e2mmap
|
80
|
-
jaro_winkler (~> 1.5)
|
81
|
-
kramdown (~> 2.3)
|
82
|
-
kramdown-parser-gfm (~> 1.1)
|
83
|
-
parser (~> 3.0)
|
84
|
-
reverse_markdown (>= 1.0.5, < 3)
|
85
|
-
rubocop (>= 0.52)
|
86
|
-
thor (~> 1.0)
|
87
|
-
tilt (~> 2.0)
|
88
|
-
yard (~> 0.9, >= 0.9.24)
|
89
|
-
thor (1.1.0)
|
90
|
-
tilt (2.0.10)
|
91
|
-
tzinfo (2.0.4)
|
92
|
-
concurrent-ruby (~> 1.0)
|
93
|
-
unicode-display_width (2.0.0)
|
94
|
-
yard (0.9.26)
|
95
|
-
zeitwerk (2.4.2)
|
96
|
-
|
97
|
-
PLATFORMS
|
98
|
-
ruby
|
99
|
-
|
100
|
-
DEPENDENCIES
|
101
|
-
bundler (~> 2.2.10)
|
102
|
-
bundler-audit
|
103
|
-
byebug
|
104
|
-
rake (~> 12.3.3)
|
105
|
-
rspec (~> 3.0)
|
106
|
-
solargraph-rails!
|
107
|
-
|
108
|
-
BUNDLED WITH
|
109
|
-
2.2.16
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module Solargraph
|
2
|
-
module Rails
|
3
|
-
class FilesLoader
|
4
|
-
def initialize(file_names)
|
5
|
-
@file_names = file_names
|
6
|
-
end
|
7
|
-
|
8
|
-
def each(&blk)
|
9
|
-
@file_names.each do |file_name|
|
10
|
-
Solargraph::Logging.logger.info "loading from #{file_name}"
|
11
|
-
blk.call(file_name, File.read(file_name))
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module MetaSource
|
2
|
-
module Association
|
3
|
-
class BelongsToMatcher
|
4
|
-
attr_reader :name
|
5
|
-
|
6
|
-
def match?(line)
|
7
|
-
line =~ /belongs_to\s+:([a-z_]*)/
|
8
|
-
@name = Regexp.last_match(1)
|
9
|
-
|
10
|
-
return unless @name
|
11
|
-
|
12
|
-
return @name unless line =~ /class_name:\s+["']([A-Za-z0-9]+)/
|
13
|
-
|
14
|
-
@type = Regexp.last_match(1)
|
15
|
-
|
16
|
-
@name
|
17
|
-
end
|
18
|
-
|
19
|
-
def type
|
20
|
-
@type || name&.camelize
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module MetaSource
|
2
|
-
module Association
|
3
|
-
class HasAndBelongsToManyMatcher
|
4
|
-
attr_reader :name
|
5
|
-
|
6
|
-
def match?(line)
|
7
|
-
line =~ /has_and_belongs_to_many\s+:([a-z_]*)/
|
8
|
-
@name = Regexp.last_match(1)
|
9
|
-
|
10
|
-
return unless @name
|
11
|
-
|
12
|
-
if line =~ /class_name:\s+["']([A-Za-z0-9]+)/
|
13
|
-
@type = Regexp.last_match(1)
|
14
|
-
end
|
15
|
-
|
16
|
-
@name
|
17
|
-
end
|
18
|
-
|
19
|
-
def type
|
20
|
-
"ActiveRecord::Associations::CollectionProxy<#{@type || name.singularize.camelize}>"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module MetaSource
|
2
|
-
module Association
|
3
|
-
class HasManyMatcher
|
4
|
-
attr_reader :name
|
5
|
-
|
6
|
-
def match?(line)
|
7
|
-
line =~ /has_many\s+:([a-z_]*)/
|
8
|
-
@name = Regexp.last_match(1)
|
9
|
-
|
10
|
-
return unless @name
|
11
|
-
|
12
|
-
if line =~ /class_name:\s+["']([A-Za-z0-9]+)/
|
13
|
-
@type = Regexp.last_match(1)
|
14
|
-
end
|
15
|
-
|
16
|
-
@name
|
17
|
-
end
|
18
|
-
|
19
|
-
def type
|
20
|
-
"ActiveRecord::Associations::CollectionProxy<#{@type || name.singularize.camelize}>"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module MetaSource
|
2
|
-
module Association
|
3
|
-
class HasOneMatcher
|
4
|
-
attr_reader :name
|
5
|
-
|
6
|
-
def match?(line)
|
7
|
-
line =~ /has_one\s+:([a-z_]*)/
|
8
|
-
@name = Regexp.last_match(1)
|
9
|
-
|
10
|
-
return unless @name
|
11
|
-
|
12
|
-
if line =~ /class_name:\s+["']([A-Za-z0-9]+)/
|
13
|
-
@type = Regexp.last_match(1)
|
14
|
-
end
|
15
|
-
|
16
|
-
@name
|
17
|
-
end
|
18
|
-
|
19
|
-
def type
|
20
|
-
@type || name.camelize
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_support/core_ext/string'
|
4
|
-
|
5
|
-
module Solargraph
|
6
|
-
module Rails
|
7
|
-
class PinCreator
|
8
|
-
attr_reader :contents, :path
|
9
|
-
|
10
|
-
def initialize(path, contents)
|
11
|
-
@path = path
|
12
|
-
@contents = contents
|
13
|
-
end
|
14
|
-
|
15
|
-
def create_pins
|
16
|
-
model_attrs = []
|
17
|
-
model_name = nil
|
18
|
-
module_names = []
|
19
|
-
parser = RubyParser.new(file_contents: contents)
|
20
|
-
|
21
|
-
parser.on_comment do |comment|
|
22
|
-
Solargraph::Logging.logger.info "found comment #{comment}"
|
23
|
-
col_name, col_type = col_with_type(comment)
|
24
|
-
if type_translation.keys.include?(col_type)
|
25
|
-
loc = Solargraph::Location.new(
|
26
|
-
path,
|
27
|
-
Solargraph::Range.from_to(
|
28
|
-
parser.current_line_number,
|
29
|
-
0,
|
30
|
-
parser.current_line_number,
|
31
|
-
parser.current_line_length - 1
|
32
|
-
)
|
33
|
-
)
|
34
|
-
model_attrs << { name: col_name, type: col_type, location: loc }
|
35
|
-
else
|
36
|
-
Solargraph::Logging.logger.info 'could not find annotation in comment'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
parser.on_module do |mod_name|
|
41
|
-
Solargraph::Logging.logger.info "found module #{mod_name}"
|
42
|
-
module_names << mod_name
|
43
|
-
end
|
44
|
-
|
45
|
-
parser.on_class do |klass, superklass|
|
46
|
-
Solargraph::Logging.logger.info "found class: #{klass} < #{superklass}"
|
47
|
-
if ['ActiveRecord::Base', 'ApplicationRecord'].include?(superklass)
|
48
|
-
model_name = klass
|
49
|
-
else
|
50
|
-
Solargraph::Logging.logger.info "Unable to find ActiveRecord model from #{klass} #{superklass}"
|
51
|
-
model_attrs = [] # don't include anything from this file
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
parser.on_ruby_line do |line|
|
56
|
-
matcher = ruby_matchers.find do |m|
|
57
|
-
m.match?(line)
|
58
|
-
end
|
59
|
-
|
60
|
-
if matcher
|
61
|
-
loc = Solargraph::Location.new(
|
62
|
-
path,
|
63
|
-
Solargraph::Range.from_to(
|
64
|
-
parser.current_line_number,
|
65
|
-
0,
|
66
|
-
parser.current_line_number,
|
67
|
-
parser.current_line_length - 1
|
68
|
-
)
|
69
|
-
)
|
70
|
-
model_attrs << { name: matcher.name, type: matcher.type, location: loc }
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
parser.parse
|
75
|
-
|
76
|
-
Solargraph::Logging.logger.info "Adding #{model_attrs.count} attributes as pins"
|
77
|
-
model_attrs.map do |attr|
|
78
|
-
Solargraph::Pin::Method.new(
|
79
|
-
name: attr[:name],
|
80
|
-
comments: "@return [#{type_translation.fetch(attr[:type], attr[:type])}]",
|
81
|
-
location: attr[:location],
|
82
|
-
closure: Solargraph::Pin::Namespace.new(name: module_names.join('::') + "::#{model_name}"),
|
83
|
-
scope: :instance,
|
84
|
-
attribute: true
|
85
|
-
)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def ruby_matchers
|
90
|
-
[
|
91
|
-
MetaSource::Association::BelongsToMatcher.new,
|
92
|
-
MetaSource::Association::HasManyMatcher.new,
|
93
|
-
MetaSource::Association::HasOneMatcher.new,
|
94
|
-
MetaSource::Association::HasAndBelongsToManyMatcher.new
|
95
|
-
]
|
96
|
-
end
|
97
|
-
def col_with_type(line)
|
98
|
-
line
|
99
|
-
.gsub(/[\(\),:\d]/, '')
|
100
|
-
.split
|
101
|
-
.first(2)
|
102
|
-
end
|
103
|
-
|
104
|
-
def type_translation
|
105
|
-
{
|
106
|
-
'decimal' => 'BigDecimal',
|
107
|
-
'integer' => 'Integer',
|
108
|
-
'date' => 'Date',
|
109
|
-
'datetime' => 'ActiveSupport::TimeWithZone',
|
110
|
-
'string' => 'String',
|
111
|
-
'boolean' => 'Boolean',
|
112
|
-
'text' => 'String'
|
113
|
-
}
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
module Solargraph
|
2
|
-
module Rails
|
3
|
-
class RubyParser
|
4
|
-
attr_reader :current_line_number, :current_line_length
|
5
|
-
|
6
|
-
def initialize(file_contents: '')
|
7
|
-
@lines = file_contents.lines
|
8
|
-
@comment_handlers = []
|
9
|
-
@non_comment_handlers = []
|
10
|
-
@class_handlers = []
|
11
|
-
@module_handlers = []
|
12
|
-
end
|
13
|
-
|
14
|
-
def on_comment(&blk)
|
15
|
-
@comment_handlers << blk
|
16
|
-
end
|
17
|
-
|
18
|
-
def on_class(&blk)
|
19
|
-
@class_handlers << blk
|
20
|
-
end
|
21
|
-
|
22
|
-
def on_module(&blk)
|
23
|
-
@module_handlers << blk
|
24
|
-
end
|
25
|
-
|
26
|
-
def on_ruby_line(&blk)
|
27
|
-
@non_comment_handlers << blk
|
28
|
-
end
|
29
|
-
|
30
|
-
def parse
|
31
|
-
@lines
|
32
|
-
.map(&:rstrip)
|
33
|
-
.each_with_index do |line, i|
|
34
|
-
@current_line_number = i
|
35
|
-
@current_line_length = line.length
|
36
|
-
|
37
|
-
if is_comment?(line)
|
38
|
-
comment_content = line.gsub(/#\s*/, '')
|
39
|
-
@comment_handlers.each { |handler| handler.call(comment_content) }
|
40
|
-
else
|
41
|
-
@non_comment_handlers.each { |handler| handler.call(line) }
|
42
|
-
end
|
43
|
-
|
44
|
-
if is_class?(line)
|
45
|
-
line.scan(/(?:(?<!<\s)(?:(\b\w+\b)\:\:))/).flatten.each do |inline_module_name|
|
46
|
-
@module_handlers.each { | handler| handler.call(inline_module_name) }
|
47
|
-
end
|
48
|
-
line.match(/class\s+(?:\w*?(?:\:\:))*([A-Z]\w*)/)
|
49
|
-
klass_name = $1
|
50
|
-
line.match(/(?:<\s+)((?:[A-Z]\w*(?:\:\:)?)*)/)
|
51
|
-
superklass_name = $1
|
52
|
-
@class_handlers.each { |handler| handler.call(klass_name, superklass_name) }
|
53
|
-
end
|
54
|
-
|
55
|
-
if is_module?(line)
|
56
|
-
module_name = line.match(/^\s*module\s*?([A-Z]\w+)/)[1]
|
57
|
-
@module_handlers.each { |handler| handler.call(module_name) }
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def is_comment?(line)
|
65
|
-
line =~ (/^\s*#/)
|
66
|
-
end
|
67
|
-
|
68
|
-
def is_class?(line)
|
69
|
-
line =~ /^[^#]?.*?class\s+?[A-Z]/
|
70
|
-
end
|
71
|
-
|
72
|
-
def is_module?(line)
|
73
|
-
line =~ (/^\s*module\s*?([A-Z]\w+)/)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|