loose_erbs 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea8ec541c2125968aefd0a01e59febda8749e70a9a14b0ad4357167bd244fa02
4
- data.tar.gz: 2c61f02374508721143bd510f840d2a87f3a928ec5b83df04e5eec54a005e228
3
+ metadata.gz: 44d3446a83d21a94d64e15ad5fc6cd41541d1593c38ea6ab9ef4e36ac52e1745
4
+ data.tar.gz: 8529f0d1f25cf62c318b13314fc2fa2ef2bbcf2516ec87f45102c84277426b20
5
5
  SHA512:
6
- metadata.gz: 7a77cde39405160382448bc969e1ad9e8f9f1cbfd9e77d572f0e3f982d14ac3d860d6bd6f4b326b6ae1074ba9d09a91b053f632d2208c2f8109b08ef7e98958e
7
- data.tar.gz: 9987b543cfcf9ea1c22e72ae59731753965da6213994762d4e8c311c139a3ce02d5fd4281c8de55e31e7df7062992e8e108d9cce45d8d938015605f71c0ba673
6
+ metadata.gz: 632d54cf79d18b1bf0a8ed8af9b24c2f19a0f5cfa5f545204a1d3c204596daa5724271684da3cf758dddaaf8588e5b27ed1c4f49635bebcb66d201f02f4d9683
7
+ data.tar.gz: 7281ceae45fb7f0d73a413d15592c9a9ec032af02b373e77497f7b5f87be450946f7e3825f20e572eadf362c374f02993b04a21d43cda645d5dbb78975e80f97
data/README.md CHANGED
@@ -15,7 +15,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
15
  ## Usage
16
16
 
17
17
  ```shell
18
- $ loose_erbs --all
18
+ $ loose_erbs --all --trees
19
19
  /home/hartley/test/dm/app/views/layouts/application.html.erb
20
20
 
21
21
  /home/hartley/test/dm/app/views/posts/edit.html.erb
@@ -29,6 +29,8 @@ $ loose_erbs --all
29
29
 
30
30
  /home/hartley/test/dm/app/views/posts/show.html.erb
31
31
  └── /home/hartley/test/dm/app/views/posts/_post.html.erb
32
+
33
+ /home/hartley/test/dm/app/views/pwa/manifest.json.erb
32
34
  ```
33
35
 
34
36
  ## Development
data/exe/loose_erbs CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  require "loose_erbs"
5
5
 
6
- LooseErbs::Cli.new(ARGV).run
6
+ exit(LooseErbs::Cli.new(ARGV).run)
@@ -36,11 +36,18 @@ module LooseErbs
36
36
  return partial_path if @map.key? partial_path
37
37
  end
38
38
  else
39
- # posts/post
40
39
  @view_paths.each do |view_path|
40
+ # DependencyTracker removes the _ from partial pathishs
41
+ # posts/post
41
42
  partial_path = Pathname.new(view_path).join(pathish).join("../_#{wrapper.basename}.html.erb").to_s
42
43
 
43
44
  return partial_path if @map.key? partial_path
45
+
46
+ # Using the raw RenderParser returns a pathish with the partial _
47
+ # components/_badge
48
+ partial_path = Pathname.new(view_path).join(pathish).join("../#{wrapper.basename}.html.erb").to_s
49
+
50
+ return partial_path if @map.key? partial_path
44
51
  end
45
52
  end
46
53
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LooseErbs
4
+ class Scanner
5
+ singleton_class.attr_accessor :parser_class
6
+
7
+ self.parser_class = if defined?(ActionView::RenderParser::Default)
8
+ ActionView::RenderParser::Default
9
+ else
10
+ ActionView::RenderParser
11
+ end
12
+
13
+ def initialize(app = Rails.application)
14
+ @app = app
15
+ end
16
+
17
+ def renders
18
+ parsed_renders = []
19
+
20
+ each_file_in(helper_paths) do |helper_path|
21
+ parsed_renders.concat parsed_renders_in(helper_path)
22
+ end.uniq
23
+
24
+ parsed_renders
25
+ end
26
+
27
+ private
28
+ attr_reader :app
29
+
30
+ def helper_paths
31
+ app.paths["app/helpers"]
32
+ end
33
+
34
+ def each_file_in(paths, &block)
35
+ paths.each do |root_path|
36
+ Dir["#{Rails.root.join(root_path)}/**/*.rb"].each(&block)
37
+ end
38
+ end
39
+
40
+ def parsed_renders_in(file_path)
41
+ parser = self.class.parser_class.new(file_path, File.read(file_path))
42
+ parser.render_calls
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LooseErbs
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
data/lib/loose_erbs.rb CHANGED
@@ -15,8 +15,7 @@ module LooseErbs
15
15
  ActionView::DependencyTracker::RipperTracker
16
16
  end
17
17
 
18
- NoParentFilter = ->(node) { node.parents.empty? }
19
- PartialFilter = ->(node) { Pathname.new(node.identifier).basename.to_s.start_with?("_") }
18
+ LooseFilter = ->(node) { node.loose? }
20
19
 
21
20
  RegexpIncludeFactory = ->(regexp) {
22
21
  ->(node) { node.identifier.match?(regexp) }
@@ -25,6 +24,18 @@ module LooseErbs
25
24
  ->(node) { !node.identifier.match?(regexp) }
26
25
  }
27
26
 
27
+ class FilterChain
28
+ def initialize(filters)
29
+ @filters = filters
30
+ end
31
+
32
+ def filter(elements)
33
+ @filters.reduce(elements) do |filtered_elements, filter|
34
+ filtered_elements.filter(&filter)
35
+ end
36
+ end
37
+ end
38
+
28
39
  class Cli
29
40
  def initialize(argv)
30
41
  @options = {}
@@ -35,23 +46,54 @@ module LooseErbs
35
46
  def run
36
47
  require File.expand_path("./config/environment")
37
48
 
38
- Registry.new(ActionController::Base.view_paths).to_graph.print(filters)
49
+ nodes = registry.to_graph
50
+
51
+ unless options[:all]
52
+ used_erbs = scanner.renders.map { registry.lookup(_1) }.to_set
53
+
54
+ visitor = Graph::LooseVisitor.new
55
+ nodes.select { |node|
56
+ # assume regular templates are good until controller parsing is added
57
+ used_erbs.include?(node.identifier) || !node.partial?
58
+ }.each { _1.accept(visitor) }
59
+ end
60
+
61
+ nodes = FilterChain.new(filters).filter(nodes) unless filters.empty?
62
+
63
+ if options[:trees]
64
+ nodes.each(&:print_tree)
65
+
66
+ true
67
+ else
68
+ puts "\nLoose ERBs:" unless nodes.empty?
69
+ nodes.each(&:print)
70
+
71
+ nodes.empty?
72
+ end
39
73
  end
40
74
 
41
75
  private
42
76
  attr_reader :options
43
77
 
78
+ def registry
79
+ Registry.new(ActionController::Base.view_paths)
80
+ end
81
+
82
+ def scanner
83
+ Scanner.new
84
+ end
85
+
44
86
  def filters
45
87
  [
46
- NoParentFilter,
88
+ LooseFilter,
47
89
  (RegexpIncludeFactory.call(options[:include]) if options[:include]),
48
90
  (RegexpExcludeFactory.call(options[:exclude]) if options[:exclude]),
49
- (PartialFilter unless options[:all]),
50
91
  ].compact
51
92
  end
52
93
 
53
94
  def option_parser
54
95
  OptionParser.new do |parser|
96
+ parser.on("--trees", "Print files and their dependencies")
55
97
  parser.on("--all", "Print all files with no parents (defaults to only partials)")
56
98
  parser.on("--include [REGEXP]", Regexp, "Only print files that match [REGEXP]")
57
99
  parser.on("--exclude [REGEXP]", Regexp, "Do not print files that match [REGEXP]")
@@ -66,7 +108,7 @@ module LooseErbs
66
108
  @seen_nodes = Set.new
67
109
  end
68
110
 
69
- def print
111
+ def print_tree
70
112
  while !@node_stack.empty?
71
113
  node, depth = @node_stack.pop
72
114
 
@@ -86,6 +128,16 @@ module LooseErbs
86
128
  end
87
129
  end
88
130
 
131
+ class LooseVisitor
132
+ def visit(node)
133
+ return unless node.loose?
134
+
135
+ node.not_loose!
136
+
137
+ node.children.each { visit(_1) }
138
+ end
139
+ end
140
+
89
141
  class Node
90
142
  attr_reader :children, :identifier, :parents
91
143
 
@@ -93,24 +145,45 @@ module LooseErbs
93
145
  @identifier = identifier
94
146
  @children = []
95
147
  @parents = []
148
+ @loose = true
149
+ end
150
+
151
+ def accept(visitor)
152
+ visitor.visit(self)
153
+ end
154
+
155
+ def loose?
156
+ @loose
157
+ end
158
+
159
+ def not_loose!
160
+ @loose = false
161
+ end
162
+
163
+ def partial?
164
+ Pathname.new(identifier).basename.to_s.start_with?("_")
165
+ end
166
+
167
+ def print
168
+ puts identifier
169
+ end
170
+
171
+ def print_tree
172
+ Printer.new(self).print_tree
96
173
  end
97
174
  end
98
175
 
176
+ include Enumerable
177
+
99
178
  def initialize(keys, registry)
100
179
  @node_map = keys.to_h { [_1, Node.new(_1)] }
101
180
  @registry = registry
102
181
 
103
- nodes.each { process(_1) }
182
+ each { process(_1) }
104
183
  end
105
184
 
106
- def print(filters)
107
- nodes_to_print = nodes
108
-
109
- filters.each do |filter|
110
- nodes_to_print.filter!(&filter)
111
- end
112
-
113
- nodes_to_print.each { Printer.new(_1).print }
185
+ def each(&block)
186
+ nodes.each(&block)
114
187
  end
115
188
 
116
189
  private
@@ -141,3 +214,4 @@ module LooseErbs
141
214
  end
142
215
 
143
216
  require_relative "loose_erbs/registry"
217
+ require_relative "loose_erbs/scanner"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loose_erbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hartley McGuire
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-01 00:00:00.000000000 Z
11
+ date: 2024-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -39,6 +39,7 @@ files:
39
39
  - exe/loose_erbs
40
40
  - lib/loose_erbs.rb
41
41
  - lib/loose_erbs/registry.rb
42
+ - lib/loose_erbs/scanner.rb
42
43
  - lib/loose_erbs/version.rb
43
44
  homepage: https://github.com/skipkayhil/loose_erbs
44
45
  licenses: