erb_check 0.1.0
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 +7 -0
- data/CHANGELOG.md +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +173 -0
- data/docs/controller-ivars.md +93 -0
- data/exe/erb_check +6 -0
- data/lib/erb_check/backends.rb +173 -0
- data/lib/erb_check/baseline.rb +44 -0
- data/lib/erb_check/cli.rb +199 -0
- data/lib/erb_check/code_search.rb +21 -0
- data/lib/erb_check/compiler.rb +349 -0
- data/lib/erb_check/config.rb +125 -0
- data/lib/erb_check/context_registry.rb +102 -0
- data/lib/erb_check/contracts.rb +115 -0
- data/lib/erb_check/diagnostics.rb +144 -0
- data/lib/erb_check/inventory.rb +21 -0
- data/lib/erb_check/ivar_flow.rb +268 -0
- data/lib/erb_check/ivar_types.rb +144 -0
- data/lib/erb_check/lsp.rb +393 -0
- data/lib/erb_check/mapped_writer.rb +82 -0
- data/lib/erb_check/rails_snapshot.rb +225 -0
- data/lib/erb_check/render_graph.rb +373 -0
- data/lib/erb_check/source_map.rb +103 -0
- data/lib/erb_check/template_id.rb +40 -0
- data/lib/erb_check/version.rb +5 -0
- data/lib/erb_check.rb +23 -0
- metadata +157 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "digest"
|
|
5
|
+
|
|
6
|
+
module ErbCheck
|
|
7
|
+
# What a booted app knows that its source doesn't say plainly: which formats Herb compiles, each controller's
|
|
8
|
+
# and mailer's lookup prefixes, layout, helper chain and helper_methods, and each component's sidecar
|
|
9
|
+
# templates and slot methods. Taken once with `erb_check snapshot`; compiling templates never boots Rails.
|
|
10
|
+
class RailsSnapshot
|
|
11
|
+
FORMATS = %w[html turbo_stream text js json xml css].freeze
|
|
12
|
+
|
|
13
|
+
def self.capture(root = ::Rails.root.to_s)
|
|
14
|
+
::Rails.application.eager_load!
|
|
15
|
+
new(root).to_h
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(root)
|
|
19
|
+
@root = root.to_s
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_h
|
|
23
|
+
{
|
|
24
|
+
"version" => 1,
|
|
25
|
+
"erb_check" => VERSION,
|
|
26
|
+
"rails" => ::Rails.version,
|
|
27
|
+
"herb_formats" => herb_formats,
|
|
28
|
+
"view_paths" => view_paths,
|
|
29
|
+
"routed_controllers" => routed_controllers,
|
|
30
|
+
"controllers" => controllers,
|
|
31
|
+
"components" => components,
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
# The formats the app compiles with Herb, found by compiling a probe with its real handler.
|
|
38
|
+
def herb_formats
|
|
39
|
+
handler = ::ActionView::Template::Handlers::ERB.new
|
|
40
|
+
probe = "<p>\n<%= value %>\n</p>\n"
|
|
41
|
+
FORMATS.select do |format|
|
|
42
|
+
template = ::ActionView::Template.new(probe, "probe", handler, locals: [], format: format.to_sym)
|
|
43
|
+
handler.call(template, probe) ==
|
|
44
|
+
Backends.compile(probe, format:, herb_formats: [format], filename: "probe").src
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# controller_paths the app's routes (and its engines' routes) send requests to.
|
|
49
|
+
def routed_controllers
|
|
50
|
+
route_sets = [::Rails.application.routes]
|
|
51
|
+
seen = []
|
|
52
|
+
paths = []
|
|
53
|
+
until route_sets.empty?
|
|
54
|
+
set = route_sets.shift
|
|
55
|
+
next if seen.include?(set)
|
|
56
|
+
|
|
57
|
+
seen << set
|
|
58
|
+
set.routes.each do |route|
|
|
59
|
+
paths << route.defaults[:controller] if route.defaults[:controller]
|
|
60
|
+
app = route.app.respond_to?(:app) ? route.app.app : route.app
|
|
61
|
+
route_sets << app.routes if app.respond_to?(:routes) && app.routes.is_a?(::ActionDispatch::Routing::RouteSet)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
paths.uniq.sort
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def view_paths
|
|
68
|
+
::ActionController::Base.view_paths.map(&:to_s).filter_map { |path| relative(path) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def controllers
|
|
72
|
+
bases = [::ActionController::Base]
|
|
73
|
+
bases << ::ActionMailer::Base if defined?(::ActionMailer::Base)
|
|
74
|
+
bases.flat_map { |base| [base, *base.descendants] }.uniq.select(&:name).sort_by(&:name).map do |klass|
|
|
75
|
+
{
|
|
76
|
+
"name" => klass.name,
|
|
77
|
+
"kind" => klass <= ::ActionController::Base ? "controller" : "mailer",
|
|
78
|
+
"superclass" => klass.superclass.name,
|
|
79
|
+
"abstract" => klass.abstract?,
|
|
80
|
+
"controller_path" => klass.controller_path,
|
|
81
|
+
"prefixes" => klass._prefixes,
|
|
82
|
+
"layout" => (klass._layout if klass._layout.is_a?(String)),
|
|
83
|
+
"actions" => klass.abstract? ? [] : klass.action_methods.to_a.sort,
|
|
84
|
+
"ivar_flow" => ivar_metadata(klass),
|
|
85
|
+
"helper_modules" => helper_modules(klass),
|
|
86
|
+
"helper_methods" => klass._helper_methods.map(&:to_sym).uniq.sort.map { |name| helper_method(klass, name) },
|
|
87
|
+
}
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Use Rails' effective chain, after inheritance, prepend and skip rules have been applied. Never
|
|
92
|
+
# execute a callback or predicate. ActionFilter is Rails' normalized only/except condition; every
|
|
93
|
+
# other predicate remains conditional, including a conditional skip's inverted predicate.
|
|
94
|
+
def ivar_metadata(klass)
|
|
95
|
+
methods = (klass.instance_methods + klass.private_instance_methods + klass.protected_instance_methods).uniq
|
|
96
|
+
locations = methods.filter_map do |name|
|
|
97
|
+
method = klass.instance_method(name)
|
|
98
|
+
if defined?(::T::Utils)
|
|
99
|
+
method = ::T::Utils.signature_for_method(method)&.method || method
|
|
100
|
+
end
|
|
101
|
+
file, line = method.source_location
|
|
102
|
+
path = file && relative(file)
|
|
103
|
+
[name.to_s, [path, line]] if path && File.file?(file)
|
|
104
|
+
rescue StandardError
|
|
105
|
+
nil
|
|
106
|
+
end.to_h
|
|
107
|
+
callbacks = klass._process_action_callbacks.map do |callback|
|
|
108
|
+
conditions = %i[if unless].to_h do |kind|
|
|
109
|
+
values = callback.instance_variable_get("@#{kind}").map do |condition|
|
|
110
|
+
if condition.is_a?(::AbstractController::Callbacks::ActionFilter)
|
|
111
|
+
{ "actions" => condition.instance_variable_get(:@actions).to_a.sort }
|
|
112
|
+
else
|
|
113
|
+
{ "method" => (condition.to_s if condition.is_a?(Symbol)) }
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
[kind.to_s, values]
|
|
117
|
+
end
|
|
118
|
+
conditions.merge("kind" => callback.kind.to_s,
|
|
119
|
+
"method" => (callback.filter.to_s if callback.filter.is_a?(Symbol)))
|
|
120
|
+
end
|
|
121
|
+
@source_digests ||= {}
|
|
122
|
+
owner_files = klass.ancestors.filter_map do |owner|
|
|
123
|
+
file = Object.const_source_location(owner.name)&.first if owner.name
|
|
124
|
+
relative(file) if file
|
|
125
|
+
rescue NameError
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
sources = (locations.values.map(&:first) + owner_files).uniq.to_h do |path|
|
|
129
|
+
[path, @source_digests[path] ||= Digest::SHA256.file(File.join(@root, path)).hexdigest]
|
|
130
|
+
end
|
|
131
|
+
{ "methods" => locations, "callbacks" => callbacks, "sources" => sources }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Named modules in the controller's helper chain, outermost last. Each controller's own
|
|
135
|
+
# `<Controller>::HelperMethods` module only carries its helper_methods, which are listed separately.
|
|
136
|
+
def helper_modules(klass)
|
|
137
|
+
klass._helpers.ancestors.reverse.filter_map do |mod|
|
|
138
|
+
next if mod.is_a?(Class) || mod.name.nil?
|
|
139
|
+
next if mod.name.end_with?("::HelperMethods") && mod.name != "ActionController::Base::HelperMethods"
|
|
140
|
+
|
|
141
|
+
mod.name
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def helper_method(klass, name)
|
|
146
|
+
target = klass.instance_method(name) if klass.method_defined?(name) || klass.private_method_defined?(name)
|
|
147
|
+
return { "name" => name.to_s, "parameters" => nil } unless target
|
|
148
|
+
|
|
149
|
+
owner = target.owner
|
|
150
|
+
# sorbet-runtime replaces a method that has a sig with a wrapper defined in sorbet-runtime; its file and
|
|
151
|
+
# parameters are the wrapper's. Read them from the method the sig was declared on.
|
|
152
|
+
original = original_method(target)
|
|
153
|
+
file = original.source_location&.first
|
|
154
|
+
{
|
|
155
|
+
"name" => name.to_s,
|
|
156
|
+
"owner" => owner.name,
|
|
157
|
+
"owner_is_module" => !owner.is_a?(Class),
|
|
158
|
+
"in_app" => !file.nil? && in_app?(file),
|
|
159
|
+
"parameters" => original.parameters.map { |kind, param| [kind.to_s, param&.to_s] },
|
|
160
|
+
"signature" => signature(target),
|
|
161
|
+
}
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def components
|
|
165
|
+
return [] unless defined?(::ViewComponent::Base)
|
|
166
|
+
|
|
167
|
+
::ViewComponent::Base.descendants.select(&:name).sort_by(&:name).filter_map do |klass|
|
|
168
|
+
next unless klass.identifier && in_app?(klass.identifier)
|
|
169
|
+
|
|
170
|
+
{
|
|
171
|
+
"name" => klass.name,
|
|
172
|
+
"file" => relative(klass.identifier),
|
|
173
|
+
"templates" => klass.sidecar_files(["erb"]).filter_map { |path| relative(path) }.sort,
|
|
174
|
+
"slot_methods" => slot_methods(klass),
|
|
175
|
+
}
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# The methods ViewComponent defined for the component's slots: readers on GeneratedSlotMethods, setters
|
|
180
|
+
# on the class.
|
|
181
|
+
def slot_methods(klass)
|
|
182
|
+
return [] if klass.registered_slots.empty?
|
|
183
|
+
|
|
184
|
+
readers = klass.const_get(:GeneratedSlotMethods, false)
|
|
185
|
+
methods = readers.instance_methods(false).map { |name| readers.instance_method(name) }
|
|
186
|
+
methods += klass.instance_methods(false).map { |name| klass.instance_method(name) }.select do |method|
|
|
187
|
+
method.source_location&.first&.end_with?("view_component/slotable.rb")
|
|
188
|
+
end
|
|
189
|
+
methods.sort_by(&:name).map do |method|
|
|
190
|
+
{ "name" => method.name.to_s, "parameters" => method.parameters.map { |kind, param| [kind.to_s, param&.to_s] } }
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def original_method(method)
|
|
195
|
+
return method unless defined?(::T::Utils) && ::T::Utils.respond_to?(:signature_for_method)
|
|
196
|
+
|
|
197
|
+
::T::Utils.signature_for_method(method)&.method || method
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# A sorbet-runtime signature as type strings, when the method has one.
|
|
201
|
+
def signature(method)
|
|
202
|
+
return unless defined?(::T::Utils) && ::T::Utils.respond_to?(:signature_for_method)
|
|
203
|
+
|
|
204
|
+
sig = ::T::Utils.signature_for_method(method)
|
|
205
|
+
return unless sig
|
|
206
|
+
|
|
207
|
+
params = sig.arg_types.to_h { |name, type| [name.to_s, type.to_s] }
|
|
208
|
+
sig.kwarg_types.each { |name, type| params[name.to_s] = type.to_s }
|
|
209
|
+
params[sig.rest_name.to_s] = sig.rest_type.to_s if sig.rest_name
|
|
210
|
+
params[sig.keyrest_name.to_s] = sig.keyrest_type.to_s if sig.keyrest_name
|
|
211
|
+
params[sig.block_name.to_s] = sig.block_type.to_s if sig.block_name
|
|
212
|
+
{ "params" => params, "returns" => sig.return_type.to_s }
|
|
213
|
+
rescue StandardError
|
|
214
|
+
nil
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def in_app?(file)
|
|
218
|
+
file.start_with?("#{@root}/") && !file.start_with?("#{@root}/vendor/")
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def relative(path)
|
|
222
|
+
path.delete_prefix("#{@root}/") if in_app?(path.to_s) || path.to_s.start_with?("#{@root}/app/")
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
6
|
+
module ErbCheck
|
|
7
|
+
# Which controllers render each template, and with which locals, from the literal render calls in the
|
|
8
|
+
# app's Ruby and templates, resolved the way Rails resolves them: a name without a slash through the
|
|
9
|
+
# rendering controller's lookup prefixes, an action's template implicitly, a layout for every controller
|
|
10
|
+
# that uses it. A call whose target isn't a literal is recorded as unresolved.
|
|
11
|
+
class RenderGraph
|
|
12
|
+
# A template reached from `caller` (a template path or Ruby file:line), rendered for `controller`, given
|
|
13
|
+
# `locals` (nil when the call's locals aren't literal). `guessed` when it comes from a template nobody
|
|
14
|
+
# renders, checked as its directory's controller.
|
|
15
|
+
Usage = Data.define(:controller, :locals, :caller, :guessed) do
|
|
16
|
+
def initialize(controller:, locals:, caller:, guessed: false) = super
|
|
17
|
+
end
|
|
18
|
+
Unresolved = Data.define(:caller, :reason)
|
|
19
|
+
# A followable render call: the name it renders, the templates that name resolves to, the local names it
|
|
20
|
+
# passes (nil when not literal) and the hash node holding them.
|
|
21
|
+
Resolution = Data.define(:name, :targets, :locals, :values)
|
|
22
|
+
|
|
23
|
+
attr_reader :unresolved
|
|
24
|
+
|
|
25
|
+
def initialize(templates:, snapshot:, ruby_files:, sources:, render_wrappers: [], default_controller: "ApplicationController",
|
|
26
|
+
pinned: {})
|
|
27
|
+
@templates = templates # TemplateIds
|
|
28
|
+
@sources = sources # path => template source
|
|
29
|
+
@controllers = snapshot.fetch("controllers").to_h { |c| [c["name"], c] }
|
|
30
|
+
# A controller no route reaches never renders. Mailers aren't routed.
|
|
31
|
+
routed = snapshot["routed_controllers"]&.to_set
|
|
32
|
+
@live = @controllers.values.select do |c|
|
|
33
|
+
!c["abstract"] && (routed.nil? || c["kind"] == "mailer" || routed.include?(c["controller_path"]))
|
|
34
|
+
end.to_set { |c| c["name"] }
|
|
35
|
+
@children = Hash.new { |h, k| h[k] = [] }
|
|
36
|
+
@controllers.each_value { |c| @children[c["superclass"]] << c["name"] }
|
|
37
|
+
@ruby_files = ruby_files
|
|
38
|
+
@render_wrappers = render_wrappers
|
|
39
|
+
@default_controller = default_controller
|
|
40
|
+
@view_roots = snapshot.fetch("view_paths", ["app/views"])
|
|
41
|
+
@pinned = pinned # template path => controller name, from the app's config
|
|
42
|
+
@by_name = templates.group_by(&:name)
|
|
43
|
+
@by_path = templates.to_h { |t| [t.path, t] }
|
|
44
|
+
@usages = Hash.new { |h, k| h[k] = Set.new }
|
|
45
|
+
@unresolved = []
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def usages(path)
|
|
49
|
+
@usages[path].to_a
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def rendering_controllers
|
|
53
|
+
@usages.values.flat_map(&:to_a).reject(&:guessed).map(&:controller).uniq
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def build
|
|
57
|
+
@renders_pages = Set.new
|
|
58
|
+
implicit_renders
|
|
59
|
+
@ruby_files.each { |file| ruby_renders(file) }
|
|
60
|
+
layouts
|
|
61
|
+
@pinned.each { |path, name| add(@by_path.fetch(path), name, nil, "pinned in .erbcheck.yml") if @by_path[path] }
|
|
62
|
+
propagate
|
|
63
|
+
# A template no followable render reaches is checked as its directory's controller, and the partials it
|
|
64
|
+
# renders are followed from there.
|
|
65
|
+
unreached = @templates.reject { |t| @usages.key?(t.path) || !@view_roots.include?(t.root) }
|
|
66
|
+
@guessing = true
|
|
67
|
+
unreached.each { |t| add(t, directory_controller(t), nil, "no caller found") }
|
|
68
|
+
propagate
|
|
69
|
+
self
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Whether a template is only reached through the directory fallback.
|
|
73
|
+
# Each literal local a template's render calls pass when `controller` renders it: [callee path, local name,
|
|
74
|
+
# value node], read from `source` (the file, or an editor's unsaved text), offsets in its bytes. A call that
|
|
75
|
+
# resolves to more than one template is left out, and so is a collection's, whose locals come from its items.
|
|
76
|
+
def arguments(template, source, controller)
|
|
77
|
+
dir = File.dirname(template.name)
|
|
78
|
+
template_calls(template, source).flat_map do |call|
|
|
79
|
+
resolved = resolve(call, controller, in_view: true, caller_dir: dir)
|
|
80
|
+
next [] unless resolved.is_a?(Resolution) && resolved.targets.size == 1 && resolved.values
|
|
81
|
+
next [] if keyword_node(call, "collection") || keyword_node(call, "object")
|
|
82
|
+
|
|
83
|
+
resolved.values.elements.filter_map do |el|
|
|
84
|
+
next unless el.is_a?(Prism::AssocNode) && el.key.is_a?(Prism::SymbolNode) && !el.value.is_a?(Prism::ImplicitNode)
|
|
85
|
+
|
|
86
|
+
[resolved.targets.first.path, el.key.unescaped, el.value]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def fallback?(path)
|
|
92
|
+
@usages[path].all?(&:guessed)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def directory_controller(template)
|
|
96
|
+
@by_controller_path ||= @controllers.values.to_h { |c| [c["controller_path"], c["name"]] }
|
|
97
|
+
dir = File.dirname(template.name)
|
|
98
|
+
until dir == "."
|
|
99
|
+
return @by_controller_path[dir] if @by_controller_path[dir]
|
|
100
|
+
|
|
101
|
+
dir = File.dirname(dir)
|
|
102
|
+
end
|
|
103
|
+
@default_controller
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
# Each action renders its own template, and each controller its layout.
|
|
109
|
+
def implicit_renders
|
|
110
|
+
@controllers.each_value do |c|
|
|
111
|
+
next unless @live.include?(c["name"])
|
|
112
|
+
|
|
113
|
+
c["actions"].each do |action|
|
|
114
|
+
lookup(action, c, partial: false).each { |t| add(t, c["name"], [], "#{c['name']}##{action}") }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# A controller that renders a page renders it in its layout.
|
|
120
|
+
def layouts
|
|
121
|
+
@renders_pages.each do |name|
|
|
122
|
+
layout_templates(@controllers.fetch(name)).each { |t| add(t, name, [], "#{name} layout") }
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def layout_templates(controller)
|
|
127
|
+
names = controller["layout"] ? [controller["layout"]] : ancestors(controller).map { |c| c["controller_path"] }
|
|
128
|
+
names.each do |name|
|
|
129
|
+
found = @by_name["layouts/#{name}"]
|
|
130
|
+
return found if found
|
|
131
|
+
end
|
|
132
|
+
[]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def ancestors(controller)
|
|
136
|
+
chain = []
|
|
137
|
+
while controller
|
|
138
|
+
chain << controller
|
|
139
|
+
controller = @controllers[controller["superclass"]]
|
|
140
|
+
end
|
|
141
|
+
chain
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The concrete controllers a render in `name`'s code runs for: the controller itself and its subclasses.
|
|
145
|
+
def concrete(name)
|
|
146
|
+
queue = [name]
|
|
147
|
+
found = []
|
|
148
|
+
until queue.empty?
|
|
149
|
+
current = queue.shift
|
|
150
|
+
c = @controllers[current]
|
|
151
|
+
next unless c
|
|
152
|
+
|
|
153
|
+
found << current if @live.include?(current)
|
|
154
|
+
queue.concat(@children[current])
|
|
155
|
+
end
|
|
156
|
+
found
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def ruby_renders(file)
|
|
160
|
+
source = File.read(file)
|
|
161
|
+
# Only a file that names a render method can call one; most of app/ doesn't.
|
|
162
|
+
return unless source.include?("render") || @render_wrappers.any? { |w| source.include?(w["method"]) }
|
|
163
|
+
|
|
164
|
+
result = Prism.parse(source, filepath: file)
|
|
165
|
+
walk(result.value, []) do |call, namespace|
|
|
166
|
+
controller = enclosing_controller(namespace)
|
|
167
|
+
receiver_controller = call.receiver && constant_name(call.receiver)
|
|
168
|
+
controller = receiver_controller if receiver_controller && @controllers[receiver_controller]
|
|
169
|
+
where = "#{file}:#{call.location.start_line}"
|
|
170
|
+
if (wrapper = wrapper_for(call, namespace))
|
|
171
|
+
wrapper_render(call, wrapper, where)
|
|
172
|
+
elsif controller
|
|
173
|
+
concrete(controller).each { |name| render_call(call, name, where, in_view: false, caller_dir: nil) }
|
|
174
|
+
else
|
|
175
|
+
@unresolved << Unresolved.new(caller: where, reason: "render outside a controller")
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Templates reached from templates, until nothing new arrives.
|
|
181
|
+
def propagate
|
|
182
|
+
calls = @templates.to_h { |t| [t.path, template_calls(t)] }
|
|
183
|
+
queue = @usages.keys
|
|
184
|
+
@seen ||= Set.new
|
|
185
|
+
seen = @seen
|
|
186
|
+
until queue.empty?
|
|
187
|
+
path = queue.shift
|
|
188
|
+
controllers = @usages[path].map(&:controller).uniq
|
|
189
|
+
controllers.each do |name|
|
|
190
|
+
next if seen.include?([path, name])
|
|
191
|
+
|
|
192
|
+
seen << [path, name]
|
|
193
|
+
(calls[path] || []).each do |call|
|
|
194
|
+
dir = File.dirname(@by_path.fetch(path).name)
|
|
195
|
+
render_call(call, name, "#{path}:#{call.location.start_line}", in_view: true, caller_dir: dir) do |target|
|
|
196
|
+
queue << target.path
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def template_calls(template, source = @sources[template.path])
|
|
204
|
+
return [] unless source.include?("render")
|
|
205
|
+
|
|
206
|
+
calls = []
|
|
207
|
+
walk(Prism.parse(::Herb.extract_ruby(source)).value, []) { |call, _| calls << call }
|
|
208
|
+
calls
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def walk(node, namespace, &block)
|
|
212
|
+
case node
|
|
213
|
+
when Prism::ClassNode, Prism::ModuleNode
|
|
214
|
+
inner = namespace + [node.constant_path.full_name]
|
|
215
|
+
node.compact_child_nodes.each { |child| walk(child, inner, &block) }
|
|
216
|
+
return
|
|
217
|
+
when Prism::CallNode
|
|
218
|
+
yield node, namespace if render?(node)
|
|
219
|
+
end
|
|
220
|
+
node.compact_child_nodes.each { |child| walk(child, namespace, &block) }
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def render?(call)
|
|
224
|
+
call.arguments && (%i[render render_to_string].include?(call.name) ||
|
|
225
|
+
@render_wrappers.any? { |w| w["method"] == call.name.to_s })
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def enclosing_controller(namespace)
|
|
229
|
+
namespace.length.downto(1) do |n|
|
|
230
|
+
name = namespace.first(n).join("::")
|
|
231
|
+
candidates = [name] + (1...n).map { |k| namespace.first(k).join("::") + "::" + namespace[k..n - 1].join("::") }
|
|
232
|
+
found = candidates.find { |c| @controllers[c] }
|
|
233
|
+
return found if found
|
|
234
|
+
end
|
|
235
|
+
nil
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def constant_name(node)
|
|
239
|
+
node.full_name if node.respond_to?(:full_name)
|
|
240
|
+
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
|
|
241
|
+
nil
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# A configured render wrapper: a method like `Labels::Printer#render(path:, variables:)` that renders a template
|
|
245
|
+
# under `root` with the locals in one keyword argument, for `controller`.
|
|
246
|
+
def wrapper_for(call, namespace)
|
|
247
|
+
@render_wrappers.find do |w|
|
|
248
|
+
w["method"] == call.name.to_s && namespace.join("::").end_with?(w["class"]) && keyword_node(call, w["path"])
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def wrapper_render(call, wrapper, where)
|
|
253
|
+
path = keyword(call, wrapper["path"])
|
|
254
|
+
locals = hash_keys(keyword_node(call, wrapper["locals"]))
|
|
255
|
+
return @unresolved << Unresolved.new(caller: where, reason: "non-literal wrapper path") unless path
|
|
256
|
+
|
|
257
|
+
target = @by_path[File.join(wrapper["root"], path)]
|
|
258
|
+
return @unresolved << Unresolved.new(caller: where, reason: "no template #{path}") unless target
|
|
259
|
+
|
|
260
|
+
add(target, wrapper["controller"], locals, where)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# Resolves one render call for one rendering controller; yields each newly reached template.
|
|
264
|
+
def render_call(call, controller, where, in_view:, caller_dir:, &block)
|
|
265
|
+
resolved = resolve(call, controller, in_view:, caller_dir:)
|
|
266
|
+
return unless resolved
|
|
267
|
+
return @unresolved << Unresolved.new(caller: where, reason: resolved) if resolved.is_a?(String)
|
|
268
|
+
|
|
269
|
+
@unresolved << Unresolved.new(caller: where, reason: "no template for #{resolved.name.inspect}") if resolved.targets.empty?
|
|
270
|
+
resolved.targets.each do |target|
|
|
271
|
+
added = add(target, controller, resolved.locals, where)
|
|
272
|
+
yield target if added && block
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# What a render call renders for a controller: a Resolution, the reason it can't be followed, or nil when
|
|
277
|
+
# it renders no template.
|
|
278
|
+
def resolve(call, controller, in_view:, caller_dir:)
|
|
279
|
+
args = call.arguments&.arguments || []
|
|
280
|
+
first = args.first
|
|
281
|
+
options = args.find { |a| a.is_a?(Prism::KeywordHashNode) || a.is_a?(Prism::HashNode) }
|
|
282
|
+
|
|
283
|
+
if first.is_a?(Prism::StringNode) || first.is_a?(Prism::SymbolNode)
|
|
284
|
+
name = first.unescaped
|
|
285
|
+
partial = in_view && first.is_a?(Prism::StringNode)
|
|
286
|
+
# In a view, `render "name", {...}` passes the whole hash as locals, `body:` and `status:` included.
|
|
287
|
+
values = in_view ? options : keyword_node(call, "locals")
|
|
288
|
+
elsif options
|
|
289
|
+
kinds = in_view ? %w[partial template layout] : %w[partial template action]
|
|
290
|
+
name, partial = kinds.filter_map do |kind|
|
|
291
|
+
value = keyword(call, kind)
|
|
292
|
+
[value, kind == "partial" || (kind == "layout" && in_view)] if value
|
|
293
|
+
end.first
|
|
294
|
+
return if name.nil? && !keyword_node(call, "partial") && !keyword_node(call, "template")
|
|
295
|
+
return "non-literal render target" unless name
|
|
296
|
+
|
|
297
|
+
values = keyword_node(call, "locals")
|
|
298
|
+
else
|
|
299
|
+
return "render of an object or component"
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
locals = hash_keys(values)
|
|
303
|
+
locals = collection_locals(call, name, locals) if partial
|
|
304
|
+
Resolution.new(name:, targets: lookup(name, @controllers[controller], partial:, caller_dir:), locals:, values:)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def collection_locals(call, name, locals)
|
|
308
|
+
return locals unless locals && (keyword_node(call, "collection") || keyword_node(call, "object"))
|
|
309
|
+
|
|
310
|
+
as = keyword(call, "as") || File.basename(name)
|
|
311
|
+
extra = [as]
|
|
312
|
+
extra += ["#{as}_counter", "#{as}_iteration"] if keyword_node(call, "collection")
|
|
313
|
+
locals + extra
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# Templates a name resolves to for a controller: with a slash, from the view root; without, through the
|
|
317
|
+
# controller's prefixes (or the calling template's directory when the controller is unknown).
|
|
318
|
+
def lookup(name, controller, partial:, caller_dir: nil)
|
|
319
|
+
name = name.to_s.delete_prefix("/")
|
|
320
|
+
dirs = if name.include?("/")
|
|
321
|
+
[File.dirname(name)]
|
|
322
|
+
else
|
|
323
|
+
(controller ? controller["prefixes"] : []) + [caller_dir].compact
|
|
324
|
+
end
|
|
325
|
+
base = File.basename(name)
|
|
326
|
+
dirs.each do |dir|
|
|
327
|
+
candidates = [partial ? "#{dir}/_#{base}" : "#{dir}/#{base}"]
|
|
328
|
+
candidates << "#{dir}/_#{base}" << "#{dir}/#{base}" if partial.nil?
|
|
329
|
+
candidates.each do |candidate|
|
|
330
|
+
found = @by_name[candidate.delete_prefix("./")]
|
|
331
|
+
return found if found
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
[]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def add(template, controller, locals, caller)
|
|
338
|
+
@renders_pages << controller if !template.partial? && !template.layout? && @controllers.key?(controller) &&
|
|
339
|
+
!caller.include?(".erb:")
|
|
340
|
+
@usages[template.path].add?(Usage.new(controller:, locals: locals&.uniq&.sort, caller:, guessed: !!@guessing))
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def keyword_node(call, key)
|
|
344
|
+
(call.arguments&.arguments || []).each do |arg|
|
|
345
|
+
next unless arg.is_a?(Prism::KeywordHashNode) || arg.is_a?(Prism::HashNode)
|
|
346
|
+
|
|
347
|
+
arg.elements.each do |el|
|
|
348
|
+
next unless el.is_a?(Prism::AssocNode)
|
|
349
|
+
return el.value if el.key.is_a?(Prism::SymbolNode) && el.key.unescaped == key
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
nil
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def keyword(call, key)
|
|
356
|
+
node = keyword_node(call, key)
|
|
357
|
+
node.unescaped if node.is_a?(Prism::StringNode) || node.is_a?(Prism::SymbolNode)
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# The keys of a literal hash; nil when any key isn't a literal symbol or there's a splat.
|
|
361
|
+
def hash_keys(node)
|
|
362
|
+
return [] if node.nil?
|
|
363
|
+
return nil unless node.is_a?(Prism::KeywordHashNode) || node.is_a?(Prism::HashNode)
|
|
364
|
+
|
|
365
|
+
keys = node.elements.map do |el|
|
|
366
|
+
return nil unless el.is_a?(Prism::AssocNode) && el.key.is_a?(Prism::SymbolNode)
|
|
367
|
+
|
|
368
|
+
el.key.unescaped
|
|
369
|
+
end
|
|
370
|
+
keys
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
end
|