rails-ai-context 5.30.2 → 5.32.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 +4 -4
- data/CHANGELOG.md +868 -0
- data/CONTEXT.md +7 -3
- data/README.md +1 -1
- data/docs/ARCHITECTURE.md +3 -3
- data/docs/CLI.md +1 -1
- data/docs/COMPATIBILITY.md +50 -14
- data/docs/CONFIGURATION.md +4 -1
- data/docs/FAQ.md +1 -1
- data/docs/GUIDE.md +14 -8
- data/docs/INTROSPECTORS.md +50 -23
- data/docs/SECURITY.md +56 -1
- data/docs/TOOLS.md +58 -12
- data/lib/generators/rails_ai_context/install/install_generator.rb +1 -0
- data/lib/rails_ai_context/action_filters.rb +121 -48
- data/lib/rails_ai_context/app_commands.rb +2 -2
- data/lib/rails_ai_context/app_kind.rb +65 -8
- data/lib/rails_ai_context/ast_cache.rb +53 -18
- data/lib/rails_ai_context/base_mixins.rb +46 -16
- data/lib/rails_ai_context/boot_manager.rb +5 -1
- data/lib/rails_ai_context/cli/entry_boot.rb +17 -2
- data/lib/rails_ai_context/cli/tool_runner.rb +15 -1
- data/lib/rails_ai_context/concern_macros.rb +791 -111
- data/lib/rails_ai_context/concern_membership.rb +25 -5
- data/lib/rails_ai_context/concern_paths.rb +63 -17
- data/lib/rails_ai_context/config_yaml.rb +40 -0
- data/lib/rails_ai_context/configuration.rb +3 -3
- data/lib/rails_ai_context/data/docs/index.json +324 -0
- data/lib/rails_ai_context/database_yml.rb +136 -0
- data/lib/rails_ai_context/doctor.rb +30 -37
- data/lib/rails_ai_context/erb_source.rb +40 -0
- data/lib/rails_ai_context/fingerprinter.rb +48 -22
- data/lib/rails_ai_context/fixture_keys.rb +42 -1
- data/lib/rails_ai_context/gem_lock.rb +275 -47
- data/lib/rails_ai_context/git_ignore.rb +13 -11
- data/lib/rails_ai_context/hydrators/hydration_formatter.rb +8 -2
- data/lib/rails_ai_context/hydrators/schema_hint_builder.rb +10 -5
- data/lib/rails_ai_context/introspector.rb +10 -1
- data/lib/rails_ai_context/introspectors/action_mailbox_introspector.rb +27 -19
- data/lib/rails_ai_context/introspectors/action_presence.rb +11 -8
- data/lib/rails_ai_context/introspectors/action_resolver.rb +38 -26
- data/lib/rails_ai_context/introspectors/action_text_introspector.rb +2 -2
- data/lib/rails_ai_context/introspectors/active_record_settings.rb +56 -0
- data/lib/rails_ai_context/introspectors/active_storage_introspector.rb +1 -1
- data/lib/rails_ai_context/introspectors/active_support_introspector.rb +98 -22
- data/lib/rails_ai_context/introspectors/admin_resources.rb +101 -0
- data/lib/rails_ai_context/introspectors/anyway_configs.rb +64 -0
- data/lib/rails_ai_context/introspectors/apartment_config.rb +47 -0
- data/lib/rails_ai_context/introspectors/api_introspector.rb +125 -26
- data/lib/rails_ai_context/introspectors/asset_pipeline_introspector.rb +24 -19
- data/lib/rails_ai_context/introspectors/ast_walk.rb +18 -0
- data/lib/rails_ai_context/introspectors/auth_introspector.rb +71 -18
- data/lib/rails_ai_context/introspectors/autoload_introspector.rb +62 -15
- data/lib/rails_ai_context/introspectors/base.rb +38 -0
- data/lib/rails_ai_context/introspectors/call_site_expansion.rb +349 -39
- data/lib/rails_ai_context/introspectors/component_introspector.rb +26 -14
- data/lib/rails_ai_context/introspectors/config_introspector.rb +22 -9
- data/lib/rails_ai_context/introspectors/connection_pool_introspector.rb +1 -4
- data/lib/rails_ai_context/introspectors/controller_filters.rb +335 -31
- data/lib/rails_ai_context/introspectors/controller_introspector.rb +315 -129
- data/lib/rails_ai_context/introspectors/controller_settings.rb +182 -0
- data/lib/rails_ai_context/introspectors/convention_introspector.rb +18 -15
- data/lib/rails_ai_context/introspectors/database_stats_introspector.rb +1 -1
- data/lib/rails_ai_context/introspectors/declared_constant.rb +108 -23
- data/lib/rails_ai_context/introspectors/eager_load.rb +12 -7
- data/lib/rails_ai_context/introspectors/engine_introspector.rb +13 -5
- data/lib/rails_ai_context/introspectors/env_config_introspector.rb +62 -17
- data/lib/rails_ai_context/introspectors/env_introspector.rb +8 -6
- data/lib/rails_ai_context/introspectors/env_references.rb +21 -4
- data/lib/rails_ai_context/introspectors/frontend_framework_introspector.rb +37 -22
- data/lib/rails_ai_context/introspectors/gem_introspector.rb +3 -1
- data/lib/rails_ai_context/introspectors/gemfile_gems.rb +53 -5
- data/lib/rails_ai_context/introspectors/grape_endpoints.rb +109 -0
- data/lib/rails_ai_context/introspectors/helper_names.rb +12 -1
- data/lib/rails_ai_context/introspectors/i18n_introspector.rb +101 -46
- data/lib/rails_ai_context/introspectors/includers.rb +6 -1
- data/lib/rails_ai_context/introspectors/initializer_introspector.rb +2 -5
- data/lib/rails_ai_context/introspectors/job_introspector.rb +548 -103
- data/lib/rails_ai_context/introspectors/listener_registration.rb +11 -1
- data/lib/rails_ai_context/introspectors/listeners/associations_listener.rb +51 -4
- data/lib/rails_ai_context/introspectors/listeners/autoload_ignore_listener.rb +31 -0
- data/lib/rails_ai_context/introspectors/listeners/autoload_paths_listener.rb +4 -47
- data/lib/rails_ai_context/introspectors/listeners/base_listener.rb +80 -14
- data/lib/rails_ai_context/introspectors/listeners/branch_conditions.rb +156 -0
- data/lib/rails_ai_context/introspectors/listeners/callbacks_listener.rb +85 -23
- data/lib/rails_ai_context/introspectors/listeners/class_definition_listener.rb +35 -6
- data/lib/rails_ai_context/introspectors/listeners/component_structure_listener.rb +30 -2
- data/lib/rails_ai_context/introspectors/listeners/conditional_macro_listener.rb +19 -0
- data/lib/rails_ai_context/introspectors/listeners/config_assignment_listener.rb +255 -10
- data/lib/rails_ai_context/introspectors/listeners/constant_reference_listener.rb +34 -0
- data/lib/rails_ai_context/introspectors/listeners/constructor_macro_listener.rb +123 -0
- data/lib/rails_ai_context/introspectors/listeners/definition_file_paths_listener.rb +66 -0
- data/lib/rails_ai_context/introspectors/listeners/enums_listener.rb +2 -2
- data/lib/rails_ai_context/introspectors/listeners/env_access_listener.rb +31 -0
- data/lib/rails_ai_context/introspectors/listeners/filter_macro_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/fixture_paths_listener.rb +62 -0
- data/lib/rails_ai_context/introspectors/listeners/gemfile_dsl_listener.rb +42 -1
- data/lib/rails_ai_context/introspectors/listeners/generic_macro_listener.rb +28 -2
- data/lib/rails_ai_context/introspectors/listeners/grape_api_listener.rb +137 -0
- data/lib/rails_ai_context/introspectors/listeners/http_client_call_listener.rb +50 -0
- data/lib/rails_ai_context/introspectors/listeners/i18n_load_path_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/literal_paths.rb +123 -0
- data/lib/rails_ai_context/introspectors/listeners/macros_listener.rb +248 -4
- data/lib/rails_ai_context/introspectors/listeners/mailbox_routing_listener.rb +2 -1
- data/lib/rails_ai_context/introspectors/listeners/method_call_listener.rb +7 -1
- data/lib/rails_ai_context/introspectors/listeners/methods_listener.rb +352 -69
- data/lib/rails_ai_context/introspectors/listeners/migration_dsl_listener.rb +1 -1
- data/lib/rails_ai_context/introspectors/listeners/migration_replay_listener.rb +35 -26
- data/lib/rails_ai_context/introspectors/listeners/mixins_listener.rb +62 -29
- data/lib/rails_ai_context/introspectors/listeners/mount_listener.rb +8 -0
- data/lib/rails_ai_context/introspectors/listeners/namespaced_roots_listener.rb +35 -0
- data/lib/rails_ai_context/introspectors/listeners/nested_constants_listener.rb +32 -0
- data/lib/rails_ai_context/introspectors/listeners/owner_scope.rb +67 -0
- data/lib/rails_ai_context/introspectors/listeners/preview_paths_listener.rb +16 -7
- data/lib/rails_ai_context/introspectors/listeners/proc_literal_listener.rb +42 -0
- data/lib/rails_ai_context/introspectors/listeners/queue_assignment_listener.rb +54 -0
- data/lib/rails_ai_context/introspectors/listeners/rake_task_dsl_listener.rb +21 -8
- data/lib/rails_ai_context/introspectors/listeners/route_files_listener.rb +4 -11
- data/lib/rails_ai_context/introspectors/listeners/routes_dsl_listener.rb +461 -65
- data/lib/rails_ai_context/introspectors/listeners/schema_dsl_listener.rb +76 -14
- data/lib/rails_ai_context/introspectors/listeners/scopes_listener.rb +40 -1
- data/lib/rails_ai_context/introspectors/listeners/validations_listener.rb +10 -1
- data/lib/rails_ai_context/introspectors/listeners/view_paths_listener.rb +37 -0
- data/lib/rails_ai_context/introspectors/middleware_introspector.rb +47 -3
- data/lib/rails_ai_context/introspectors/migration_introspector.rb +3 -3
- data/lib/rails_ai_context/introspectors/migration_replay/helpers.rb +4 -3
- data/lib/rails_ai_context/introspectors/migration_replay/statements.rb +72 -12
- data/lib/rails_ai_context/introspectors/migration_replay.rb +110 -14
- data/lib/rails_ai_context/introspectors/model_introspector.rb +708 -214
- data/lib/rails_ai_context/introspectors/module_aliases.rb +11 -3
- data/lib/rails_ai_context/introspectors/multi_database_introspector.rb +9 -57
- data/lib/rails_ai_context/introspectors/observability_introspector.rb +5 -8
- data/lib/rails_ai_context/introspectors/performance_introspector.rb +28 -8
- data/lib/rails_ai_context/introspectors/rake_task_introspector.rb +118 -51
- data/lib/rails_ai_context/introspectors/recurring_schedules.rb +150 -0
- data/lib/rails_ai_context/introspectors/rendered_record.rb +87 -0
- data/lib/rails_ai_context/introspectors/retry_policy.rb +8 -5
- data/lib/rails_ai_context/introspectors/route_introspector.rb +153 -42
- data/lib/rails_ai_context/introspectors/schema_conventions.rb +127 -40
- data/lib/rails_ai_context/introspectors/schema_dump_path.rb +80 -0
- data/lib/rails_ai_context/introspectors/schema_introspector.rb +346 -112
- data/lib/rails_ai_context/introspectors/schema_reader.rb +69 -29
- data/lib/rails_ai_context/introspectors/service_classes.rb +21 -15
- data/lib/rails_ai_context/introspectors/source_calls.rb +1 -1
- data/lib/rails_ai_context/introspectors/source_introspector.rb +17 -2
- data/lib/rails_ai_context/introspectors/source_scan.rb +174 -26
- data/lib/rails_ai_context/introspectors/sqlite_virtual_tables.rb +28 -0
- data/lib/rails_ai_context/introspectors/stimulus_introspector.rb +16 -5
- data/lib/rails_ai_context/introspectors/structure_sql_reader.rb +372 -98
- data/lib/rails_ai_context/introspectors/superclass_chain.rb +14 -15
- data/lib/rails_ai_context/introspectors/table_name.rb +46 -45
- data/lib/rails_ai_context/introspectors/test_introspector.rb +289 -78
- data/lib/rails_ai_context/introspectors/turbo_introspector.rb +10 -5
- data/lib/rails_ai_context/introspectors/view_introspector.rb +5 -40
- data/lib/rails_ai_context/introspectors/view_template_introspector.rb +12 -10
- data/lib/rails_ai_context/method_name.rb +31 -0
- data/lib/rails_ai_context/migration_status.rb +5 -5
- data/lib/rails_ai_context/output_guard.rb +6 -4
- data/lib/rails_ai_context/package_json.rb +96 -5
- data/lib/rails_ai_context/path_resolver.rb +170 -15
- data/lib/rails_ai_context/payload.rb +48 -0
- data/lib/rails_ai_context/pending_migrations.rb +15 -18
- data/lib/rails_ai_context/portable_path.rb +76 -4
- data/lib/rails_ai_context/redaction.rb +8 -2
- data/lib/rails_ai_context/route_coverage.rb +5 -1
- data/lib/rails_ai_context/safe_file.rb +4 -1
- data/lib/rails_ai_context/safe_path.rb +61 -10
- data/lib/rails_ai_context/schema_hint.rb +8 -2
- data/lib/rails_ai_context/schema_version.rb +7 -9
- data/lib/rails_ai_context/serializers/claude_rules_serializer.rb +1 -1
- data/lib/rails_ai_context/serializers/markdown_serializer.rb +13 -4
- data/lib/rails_ai_context/serializers/section_facts.rb +5 -1
- data/lib/rails_ai_context/serializers/stack_overview_helper.rb +2 -2
- data/lib/rails_ai_context/serializers/tool_guide_helper.rb +7 -4
- data/lib/rails_ai_context/server.rb +26 -7
- data/lib/rails_ai_context/test_framework.rb +32 -5
- data/lib/rails_ai_context/tools/analyze_feature.rb +22 -10
- data/lib/rails_ai_context/tools/base_tool.rb +69 -30
- data/lib/rails_ai_context/tools/diagnose.rb +1 -1
- data/lib/rails_ai_context/tools/generate_test.rb +8 -9
- data/lib/rails_ai_context/tools/get_active_support.rb +19 -4
- data/lib/rails_ai_context/tools/get_api.rb +17 -11
- data/lib/rails_ai_context/tools/get_autoload.rb +10 -5
- data/lib/rails_ai_context/tools/get_callbacks.rb +65 -14
- data/lib/rails_ai_context/tools/get_component_catalog.rb +7 -4
- data/lib/rails_ai_context/tools/get_concern.rb +31 -18
- data/lib/rails_ai_context/tools/get_config.rb +52 -18
- data/lib/rails_ai_context/tools/get_controllers.rb +57 -13
- data/lib/rails_ai_context/tools/get_conventions.rb +44 -22
- data/lib/rails_ai_context/tools/get_engines.rb +2 -0
- data/lib/rails_ai_context/tools/get_env.rb +209 -52
- data/lib/rails_ai_context/tools/get_env_config.rb +38 -7
- data/lib/rails_ai_context/tools/get_frontend_stack.rb +56 -7
- data/lib/rails_ai_context/tools/get_helper_methods.rb +136 -26
- data/lib/rails_ai_context/tools/get_job_pattern.rb +143 -87
- data/lib/rails_ai_context/tools/get_mailers.rb +59 -2
- data/lib/rails_ai_context/tools/get_model_details.rb +178 -28
- data/lib/rails_ai_context/tools/get_partial_interface.rb +101 -62
- data/lib/rails_ai_context/tools/get_routes.rb +35 -9
- data/lib/rails_ai_context/tools/get_schema.rb +160 -61
- data/lib/rails_ai_context/tools/get_service_pattern.rb +121 -25
- data/lib/rails_ai_context/tools/get_stimulus.rb +1 -1
- data/lib/rails_ai_context/tools/get_test_info.rb +152 -82
- data/lib/rails_ai_context/tools/get_view.rb +37 -20
- data/lib/rails_ai_context/tools/migration_advisor.rb +64 -24
- data/lib/rails_ai_context/tools/onboard.rb +174 -88
- data/lib/rails_ai_context/tools/performance_check.rb +5 -13
- data/lib/rails_ai_context/tools/query.rb +122 -28
- data/lib/rails_ai_context/tools/read_logs.rb +45 -17
- data/lib/rails_ai_context/tools/runtime_info.rb +1 -1
- data/lib/rails_ai_context/tools/safe_call.rb +20 -15
- data/lib/rails_ai_context/tools/search_code.rb +114 -40
- data/lib/rails_ai_context/tools/search_docs.rb +22 -11
- data/lib/rails_ai_context/tools/security_scan.rb +51 -4
- data/lib/rails_ai_context/tools/validate.rb +21 -4
- data/lib/rails_ai_context/tools/validate_semantics.rb +18 -6
- data/lib/rails_ai_context/version.rb +1 -1
- data/lib/rails_ai_context/vfs.rb +2 -2
- data/lib/rails_ai_context/view_file.rb +53 -3
- data/lib/rails_ai_context.rb +10 -2
- data/server.json +1 -1
- metadata +34 -3
|
@@ -12,7 +12,8 @@ module RailsAiContext
|
|
|
12
12
|
UNKNOWN = Object.new.freeze
|
|
13
13
|
# `pairs` is set for a literal hash: its `key: value` text, which reads
|
|
14
14
|
# as keyword arguments where the parameter is passed as the last one.
|
|
15
|
-
|
|
15
|
+
# `items` is set for a list of literals: each one's source.
|
|
16
|
+
Binding = Struct.new(:value, :source, :pairs, :value_sources, :items)
|
|
16
17
|
|
|
17
18
|
# The rewritten body, and for each of its lines the line of the mixin's
|
|
18
19
|
# file it came from, so what the body declares keeps its real location.
|
|
@@ -28,13 +29,12 @@ module RailsAiContext
|
|
|
28
29
|
@foreign = []
|
|
29
30
|
end
|
|
30
31
|
|
|
31
|
-
# A line takes the source line of
|
|
32
|
-
|
|
33
|
-
def append(chunk, source_line)
|
|
32
|
+
# A line takes the source line of its first code; text the call supplies keeps its parameter's line.
|
|
33
|
+
def append(chunk, source_line, from_call: false)
|
|
34
34
|
chunk.each_char do |char|
|
|
35
35
|
@text << char
|
|
36
36
|
if char == "\n"
|
|
37
|
-
source_line += 1
|
|
37
|
+
source_line += 1 unless from_call
|
|
38
38
|
@code = false
|
|
39
39
|
elsif !@code && !char.match?(/\s/)
|
|
40
40
|
@lines[line] = source_line
|
|
@@ -58,14 +58,12 @@ module RailsAiContext
|
|
|
58
58
|
# @param definition [Prism::DefNode] the method called
|
|
59
59
|
# @param call [Prism::CallNode, nil] the call site, nil when unknown
|
|
60
60
|
# @param listeners [Hash] the listener map to read the body with
|
|
61
|
+
# @param includer [Set, nil] ids of the call's argument nodes that are the including class
|
|
61
62
|
# @return [Hash{Symbol => Array<Hash>}]
|
|
62
|
-
def entries(definition, call, listeners)
|
|
63
|
+
def entries(definition, call, listeners, includer: nil)
|
|
63
64
|
return {} unless definition.body
|
|
64
65
|
|
|
65
|
-
bindings =
|
|
66
|
-
out = Output.new
|
|
67
|
-
undecided = []
|
|
68
|
-
emit(definition.body, bindings, out, undecided, 0, [])
|
|
66
|
+
out, undecided, bindings = rewrite(definition, call, includer)
|
|
69
67
|
data = SourceIntrospector.walk_source(out.text, listeners)
|
|
70
68
|
conditional = []
|
|
71
69
|
if undecided.any?
|
|
@@ -79,17 +77,56 @@ module RailsAiContext
|
|
|
79
77
|
[ key, kept ]
|
|
80
78
|
end
|
|
81
79
|
end
|
|
80
|
+
unbound = bindings.filter_map { |name, binding| name if name.is_a?(Symbol) && binding.source.nil? }
|
|
81
|
+
data = data.transform_values do |found|
|
|
82
|
+
Array(found).reject do |entry|
|
|
83
|
+
held = names_unbound?(entry, out, unbound)
|
|
84
|
+
conditional << declaration(entry, out, []) if held
|
|
85
|
+
held
|
|
86
|
+
end
|
|
87
|
+
end
|
|
82
88
|
data = data.transform_values { |found| Array(found).map { |entry| relocated(entry, out) } }
|
|
83
89
|
data.merge(conditional: conditional, foreign: foreign_entries(out, listeners))
|
|
84
90
|
end
|
|
85
91
|
|
|
92
|
+
# The body as this call runs it; nil when a condition the call cannot decide holds part of it back.
|
|
93
|
+
def rewritten(definition, call)
|
|
94
|
+
return nil unless definition.body
|
|
95
|
+
|
|
96
|
+
out, undecided, = rewrite(definition, call, nil)
|
|
97
|
+
out.text if undecided.empty?
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def rewrite(definition, call, includer)
|
|
101
|
+
bound = includer_bound(bind(definition.parameters, call), definition.parameters, call, includer)
|
|
102
|
+
bindings = derived_bindings(rest_bindings(unwritten(bound, definition.body), definition, call), definition.body)
|
|
103
|
+
out = Output.new
|
|
104
|
+
undecided = []
|
|
105
|
+
emit(definition.body, bindings, out, undecided, 0, [])
|
|
106
|
+
[ out, undecided, bindings ]
|
|
107
|
+
end
|
|
108
|
+
|
|
86
109
|
EVALS = %i[instance_eval class_eval class_exec instance_exec module_eval module_exec].freeze
|
|
87
110
|
|
|
111
|
+
# The including class a mixin hook hands on (`enhance_controller(base)`), which a block evaluated on runs as the class.
|
|
112
|
+
INCLUDER = Object.new.freeze
|
|
113
|
+
|
|
88
114
|
# `other.instance_eval { validates ... }` runs its block with `other` as
|
|
89
115
|
# self, so what it declares is `other`'s, not the calling class's.
|
|
90
|
-
def foreign_eval?(node)
|
|
116
|
+
def foreign_eval?(node, bindings = {}, depth = 0)
|
|
91
117
|
node.is_a?(Prism::CallNode) && EVALS.include?(node.name) && node.block.is_a?(Prism::BlockNode) &&
|
|
92
|
-
node.receiver && !node.receiver.is_a?(Prism::SelfNode)
|
|
118
|
+
node.receiver && !node.receiver.is_a?(Prism::SelfNode) &&
|
|
119
|
+
!(node.receiver.is_a?(Prism::LocalVariableReadNode) && bound(bindings, node.receiver, depth)&.value.equal?(INCLUDER))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# A required parameter the call passes the including class to stands for that class.
|
|
123
|
+
def includer_bound(bindings, parameters, call, includer)
|
|
124
|
+
return bindings unless parameters && call && includer&.any?
|
|
125
|
+
|
|
126
|
+
Array(call.arguments&.arguments).zip(parameters.requireds).each do |argument, param|
|
|
127
|
+
bindings[param.name] = Binding.new(INCLUDER, "self") if param.respond_to?(:name) && includer.include?(argument.__id__)
|
|
128
|
+
end
|
|
129
|
+
bindings
|
|
93
130
|
end
|
|
94
131
|
|
|
95
132
|
# What each block evaluated on another receiver declares, named with the
|
|
@@ -128,14 +165,33 @@ module RailsAiContext
|
|
|
128
165
|
# The call written at the entry's line, less any block, with the
|
|
129
166
|
# conditions it runs under.
|
|
130
167
|
def declaration(entry, out, conditions)
|
|
131
|
-
call =
|
|
132
|
-
node.is_a?(Prism::CallNode) && node.location.start_line == entry[:location]
|
|
133
|
-
end
|
|
168
|
+
call = call_at(out, entry[:location])
|
|
134
169
|
{ declaration: call ? call_source(call) : out.text.lines[entry[:location] - 1].to_s.strip,
|
|
135
170
|
condition: (conditions.join(" and ") if conditions.any?),
|
|
136
171
|
location: out.source_line(entry[:location]) || entry[:location] }.compact
|
|
137
172
|
end
|
|
138
173
|
|
|
174
|
+
def call_at(out, line)
|
|
175
|
+
AstWalk.each(AstCache.parse_string(out.text).value).find do |node|
|
|
176
|
+
node.is_a?(Prism::CallNode) && node.location.start_line == line
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# A parameter the expansion cannot bind is written as its bare name, and a name it
|
|
181
|
+
# could not fold keeps its `#{...}`; a listener would read either as what the call declares.
|
|
182
|
+
def names_unbound?(entry, out, names)
|
|
183
|
+
return false unless entry.is_a?(Hash) && entry[:location]
|
|
184
|
+
|
|
185
|
+
Array(call_at(out, entry[:location])&.arguments&.arguments).any? do |argument|
|
|
186
|
+
argument = argument.expression if argument.is_a?(Prism::SplatNode)
|
|
187
|
+
next true if (argument.is_a?(Prism::InterpolatedSymbolNode) || argument.is_a?(Prism::InterpolatedStringNode)) &&
|
|
188
|
+
argument.parts.any?(Prism::EmbeddedStatementsNode)
|
|
189
|
+
|
|
190
|
+
(argument.is_a?(Prism::LocalVariableReadNode) || (argument.is_a?(Prism::CallNode) && argument.variable_call?)) &&
|
|
191
|
+
names.include?(argument.name)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
139
195
|
def call_source(call)
|
|
140
196
|
text = call.slice
|
|
141
197
|
text = text.byteslice(0, call.block.location.start_offset - call.location.start_offset) if call.block.is_a?(Prism::BlockNode)
|
|
@@ -145,7 +201,7 @@ module RailsAiContext
|
|
|
145
201
|
def relocated(entry, out)
|
|
146
202
|
return entry unless entry.is_a?(Hash) && entry[:location]
|
|
147
203
|
|
|
148
|
-
|
|
204
|
+
ConcernMacros.moved(entry) { |line| out.source_line(line) || line }
|
|
149
205
|
end
|
|
150
206
|
|
|
151
207
|
def identity(key, entry)
|
|
@@ -185,9 +241,22 @@ module RailsAiContext
|
|
|
185
241
|
else unknown
|
|
186
242
|
end
|
|
187
243
|
end
|
|
244
|
+
rest = parameters.keyword_rest
|
|
245
|
+
bindings[rest.name] = keyword_rest_binding(parameters, arguments, keywords) if rest.respond_to?(:name) && rest.name
|
|
188
246
|
bindings
|
|
189
247
|
end
|
|
190
248
|
|
|
249
|
+
# `**options` holds the call's keywords the named ones leave.
|
|
250
|
+
def keyword_rest_binding(parameters, arguments, keywords)
|
|
251
|
+
return unknown if arguments.nil?
|
|
252
|
+
return hash_binding({}, {}) unless keywords
|
|
253
|
+
return unknown unless symbol_keyed?(keywords)
|
|
254
|
+
|
|
255
|
+
named = parameters.keywords.map(&:name)
|
|
256
|
+
kept = keywords.elements.reject { |pair| named.include?(key_of(pair)) }
|
|
257
|
+
hash_binding(kept.to_h { |pair| [ key_of(pair), value_of(pair.value) ] }, kept.to_h { |pair| [ key_of(pair), pair.value.slice ] })
|
|
258
|
+
end
|
|
259
|
+
|
|
191
260
|
def unknown
|
|
192
261
|
Binding.new(UNKNOWN, nil)
|
|
193
262
|
end
|
|
@@ -197,19 +266,181 @@ module RailsAiContext
|
|
|
197
266
|
Prism::LocalVariableOrWriteNode, Prism::LocalVariableAndWriteNode
|
|
198
267
|
].freeze
|
|
199
268
|
|
|
200
|
-
# A parameter the body assigns, in a block too, no
|
|
201
|
-
# call's argument wherever it is read, so it is bound to nothing.
|
|
269
|
+
# A parameter the body assigns or changes in place, in a block too, no
|
|
270
|
+
# longer holds the call's argument wherever it is read, so it is bound to nothing.
|
|
202
271
|
def unwritten(bindings, body)
|
|
272
|
+
changed(body, deleted_keys(bindings, body)).each { |name| bindings[name] = unknown if bindings.key?(name) }
|
|
273
|
+
bindings
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Leading `options.delete(:key)` on a literal hash parameter leave it bound to the other keys,
|
|
277
|
+
# and `local = options.delete(:key)` binds the local to that key's value, nil when the call left it out.
|
|
278
|
+
def deleted_keys(bindings, body)
|
|
279
|
+
return [] unless body.is_a?(Prism::StatementsNode)
|
|
280
|
+
|
|
281
|
+
body.body.take_while do |node|
|
|
282
|
+
call = node.is_a?(Prism::LocalVariableWriteNode) ? node.value : node
|
|
283
|
+
next false unless call.is_a?(Prism::CallNode) && call.name == :delete && call.block.nil?
|
|
284
|
+
|
|
285
|
+
receiver = call.receiver
|
|
286
|
+
key = Array(call.arguments&.arguments)
|
|
287
|
+
binding = receiver.is_a?(Prism::LocalVariableReadNode) && receiver.depth.zero? && bindings[receiver.name]
|
|
288
|
+
next false unless binding&.value_sources && key.one? && key.first.is_a?(Prism::SymbolNode)
|
|
289
|
+
next false if node.is_a?(Prism::LocalVariableWriteNode) && node.name == receiver.name
|
|
290
|
+
|
|
291
|
+
key = key.first.unescaped.to_sym
|
|
292
|
+
if node.is_a?(Prism::LocalVariableWriteNode)
|
|
293
|
+
bindings[node.name] =
|
|
294
|
+
if !binding.value.is_a?(Hash) then unknown
|
|
295
|
+
elsif binding.value.key?(key) then Binding.new(binding.value[key], binding.value_sources[key])
|
|
296
|
+
else Binding.new(nil, "nil")
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
value = binding.value.is_a?(Hash) ? binding.value.except(key) : binding.value
|
|
300
|
+
bindings[receiver.name] = hash_binding(value, binding.value_sources.except(key))
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# `local[key] ||= v`, `&&=` and `+=` change the local in place too.
|
|
305
|
+
INDEX_WRITES = [ Prism::IndexOrWriteNode, Prism::IndexAndWriteNode, Prism::IndexOperatorWriteNode ].freeze
|
|
306
|
+
|
|
307
|
+
# Methods that change their receiver in place.
|
|
308
|
+
MUTATORS = %i[<< []= push append unshift prepend insert concat pop shift delete delete_at delete_if keep_if
|
|
309
|
+
clear replace store update fill].freeze
|
|
310
|
+
|
|
311
|
+
# Readers that return an object the receiver holds, not a copy.
|
|
312
|
+
HELD_READERS = %i[[] fetch dig].freeze
|
|
313
|
+
|
|
314
|
+
# The method's own locals the body writes or changes in place, outside the `kept` statements.
|
|
315
|
+
def changed(body, kept = [])
|
|
316
|
+
found = Set.new
|
|
203
317
|
stack = [ [ body, 0 ] ]
|
|
204
318
|
until stack.empty?
|
|
205
319
|
node, blocks = stack.pop
|
|
206
|
-
|
|
320
|
+
next if kept.any? { |statement| statement.equal?(node) }
|
|
321
|
+
|
|
322
|
+
found << node.name if WRITES.include?(node.class) && node.depth == blocks
|
|
323
|
+
receiver = node.receiver if INDEX_WRITES.include?(node.class) ||
|
|
324
|
+
(node.is_a?(Prism::CallNode) && (MUTATORS.include?(node.name) || node.name.match?(/\w!\z/)))
|
|
325
|
+
# `options[:a][:b] = v` changes what `options` holds; `options.dup[:a] = v` changes a copy.
|
|
326
|
+
receiver = receiver.receiver while receiver.is_a?(Prism::CallNode) && HELD_READERS.include?(receiver.name)
|
|
327
|
+
found << receiver.name if receiver.is_a?(Prism::LocalVariableReadNode) && receiver.depth == blocks
|
|
207
328
|
blocks += 1 if node.is_a?(Prism::BlockNode) || node.is_a?(Prism::LambdaNode)
|
|
208
329
|
stack.concat(node.compact_child_nodes.map { |child| [ child, blocks ] })
|
|
209
330
|
end
|
|
331
|
+
found
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# `*args` holds the call's positionals past the other parameters, less a trailing hash the body takes
|
|
335
|
+
# (`extract_options!`), plus literals the leading statements push; a local changed elsewhere is unknown.
|
|
336
|
+
def rest_bindings(bindings, definition, call)
|
|
337
|
+
rest = definition.parameters&.rest
|
|
338
|
+
return bindings unless call && rest.is_a?(Prism::RestParameterNode) && definition.body.is_a?(Prism::StatementsNode)
|
|
339
|
+
return anonymous_rest(bindings, definition, call) unless rest.name
|
|
340
|
+
|
|
341
|
+
given = rest_arguments(definition.parameters, call)
|
|
342
|
+
taken = [ "#{rest.name}.extract_options!", "#{rest.name}.last.is_a?(Hash)?#{rest.name}.pop:{}" ]
|
|
343
|
+
kept = []
|
|
344
|
+
list = given
|
|
345
|
+
leading = true
|
|
346
|
+
definition.body.body.each do |node|
|
|
347
|
+
if node.is_a?(Prism::LocalVariableWriteNode) && taken.include?(node.value.slice.delete(" "))
|
|
348
|
+
last = given&.last
|
|
349
|
+
bindings[node.name] =
|
|
350
|
+
if given.nil? then unknown
|
|
351
|
+
elsif symbol_keyed?(last) then literal(last)
|
|
352
|
+
elsif last.nil? || literal_source?(last) then hash_binding({}, {})
|
|
353
|
+
else unknown
|
|
354
|
+
end
|
|
355
|
+
list = leading && list && (symbol_keyed?(list.last) ? list[0...-1] : list)
|
|
356
|
+
kept << node
|
|
357
|
+
elsif leading && (grown = pushed(node, rest.name, list, bindings))
|
|
358
|
+
list = grown
|
|
359
|
+
kept << node
|
|
360
|
+
else
|
|
361
|
+
leading = false
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
changed_names = changed(definition.body, kept)
|
|
365
|
+
kept.each { |node| bindings[node.name] = unknown if node.is_a?(Prism::LocalVariableWriteNode) && changed_names.include?(node.name) }
|
|
366
|
+
# A trailing keyword hash no statement took stays in the rest, as Ruby leaves it.
|
|
367
|
+
items = list && (symbol_keyed?(list.last) ? list[0...-1] : list)
|
|
368
|
+
list = nil if !list || changed_names.include?(rest.name) || items.any? { |item| !literal_source?(item) }
|
|
369
|
+
bindings[rest.name] = list ? list_binding(list) : unknown
|
|
370
|
+
bindings
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# A local the body's own statements set once from what the call binds
|
|
374
|
+
# (`plural_name = list_type.to_s`) stands for that value wherever it is read.
|
|
375
|
+
def derived_bindings(bindings, body)
|
|
376
|
+
return bindings unless body.is_a?(Prism::StatementsNode)
|
|
377
|
+
|
|
378
|
+
body.body.each do |node|
|
|
379
|
+
next unless node.is_a?(Prism::LocalVariableWriteNode) && !bindings.key?(node.name)
|
|
380
|
+
next if changed(body, [ node ]).include?(node.name)
|
|
381
|
+
|
|
382
|
+
value = evaluate(node.value, bindings, 0)
|
|
383
|
+
bindings[node.name] = Binding.new(value, value.inspect) if value.is_a?(String) || value.is_a?(Symbol)
|
|
384
|
+
end
|
|
385
|
+
bindings
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
# A bare `*` holds what the call passes there, which the body can only pass on whole.
|
|
389
|
+
def anonymous_rest(bindings, definition, call)
|
|
390
|
+
given = rest_arguments(definition.parameters, call)
|
|
391
|
+
last = given&.last
|
|
392
|
+
options = symbol_keyed?(last) ? [ last ] : []
|
|
393
|
+
items = given && (given - options)
|
|
394
|
+
bindings[ANONYMOUS_REST] = items&.all? { |item| literal_source?(item) } ? list_binding(items + options) : unknown
|
|
210
395
|
bindings
|
|
211
396
|
end
|
|
212
397
|
|
|
398
|
+
ANONYMOUS_REST = :*
|
|
399
|
+
|
|
400
|
+
# The call's arguments `*rest` takes, nil when they cannot be told (a splat, too few).
|
|
401
|
+
def rest_arguments(parameters, call)
|
|
402
|
+
arguments = Array(call.arguments&.arguments)
|
|
403
|
+
return nil if arguments.any? { |argument| argument.is_a?(Prism::SplatNode) }
|
|
404
|
+
|
|
405
|
+
takes_keywords = parameters.keywords.any? || parameters.keyword_rest
|
|
406
|
+
arguments = arguments[0...-1] if takes_keywords && arguments.last.is_a?(Prism::KeywordHashNode)
|
|
407
|
+
before = parameters.requireds.size
|
|
408
|
+
after = parameters.posts.size
|
|
409
|
+
return nil if arguments.size < before + after
|
|
410
|
+
|
|
411
|
+
before += [ parameters.optionals.size, arguments.size - before - after ].min
|
|
412
|
+
arguments[before...(arguments.size - after)]
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
# The list `args << :x` or `args.push(:x)` leaves, under a modifier `if` the bindings decide; nil for any other statement.
|
|
416
|
+
def pushed(node, name, list, bindings)
|
|
417
|
+
return nil unless list
|
|
418
|
+
|
|
419
|
+
if node.is_a?(Prism::IfNode) || node.is_a?(Prism::UnlessNode)
|
|
420
|
+
return nil if node.is_a?(Prism::IfNode) ? node.subsequent : node.else_clause
|
|
421
|
+
return nil unless node.statements&.body&.one? && (grown = pushed(node.statements.body.first, name, list, bindings))
|
|
422
|
+
|
|
423
|
+
truth = truth_of(evaluate(node.predicate, bindings.merge(name => list_binding(list)), 0))
|
|
424
|
+
return nil if truth == UNKNOWN
|
|
425
|
+
|
|
426
|
+
return (node.is_a?(Prism::UnlessNode) ? !truth : truth) ? grown : list
|
|
427
|
+
end
|
|
428
|
+
return nil unless node.is_a?(Prism::CallNode) && %i[<< push].include?(node.name) && node.block.nil?
|
|
429
|
+
return nil unless node.receiver.is_a?(Prism::LocalVariableReadNode) && node.receiver.name == name
|
|
430
|
+
|
|
431
|
+
items = Array(node.arguments&.arguments)
|
|
432
|
+
list + items if items.all? { |item| literal_source?(item) }
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def list_binding(items)
|
|
436
|
+
Binding.new(items.map { |item| value_of(item) }, "[#{items.map(&:slice).join(", ")}]", nil, nil, items.map { |item| item_source(item) })
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# A trailing `key: value` hash passes on as the keywords it spells.
|
|
440
|
+
def item_source(item)
|
|
441
|
+
item.is_a?(Prism::HashNode) && symbol_keyed?(item) ? item.elements.map(&:slice).join(", ") : item.slice
|
|
442
|
+
end
|
|
443
|
+
|
|
213
444
|
# A hash with symbol keys binds by its pairs' own source, whatever
|
|
214
445
|
# the values are: `length: { in: 4..Proposal.max }` stands in for itself.
|
|
215
446
|
def literal(node)
|
|
@@ -270,11 +501,16 @@ module RailsAiContext
|
|
|
270
501
|
def emit(node, bindings, out, undecided, depth, conditions)
|
|
271
502
|
case node
|
|
272
503
|
when Prism::LocalVariableReadNode
|
|
273
|
-
source = node
|
|
274
|
-
return out.append(source, node.location.start_line) if source
|
|
504
|
+
source = bound(bindings, node, depth)&.source
|
|
505
|
+
return out.append(source, node.location.start_line, from_call: true) if source
|
|
506
|
+
when Prism::InterpolatedSymbolNode, Prism::InterpolatedStringNode
|
|
507
|
+
folded = !heredoc?(node) && interpolated(node, bindings, depth)
|
|
508
|
+
return out.append(folded.inspect, node.location.start_line) if folded
|
|
275
509
|
when Prism::CallNode
|
|
510
|
+
return emit_each(node, bindings, out, undecided, depth, conditions) if unrolled?(node, bindings, depth)
|
|
511
|
+
|
|
276
512
|
looked_up = hash_lookup(node, bindings, depth)
|
|
277
|
-
return out.append(looked_up, node.location.start_line) if looked_up
|
|
513
|
+
return out.append(looked_up, node.location.start_line, from_call: true) if looked_up
|
|
278
514
|
when Prism::IfNode, Prism::UnlessNode
|
|
279
515
|
return emit_branch(node, bindings, out, undecided, depth, conditions)
|
|
280
516
|
when Prism::CaseNode
|
|
@@ -284,7 +520,7 @@ module RailsAiContext
|
|
|
284
520
|
when Prism::BlockNode, Prism::LambdaNode
|
|
285
521
|
depth += 1
|
|
286
522
|
end
|
|
287
|
-
if foreign_eval?(node)
|
|
523
|
+
if foreign_eval?(node, bindings, depth)
|
|
288
524
|
block_out = Output.new
|
|
289
525
|
emit(node.block.body, bindings, block_out, [], depth + 1, conditions) if node.block.body
|
|
290
526
|
out.foreign << [ node.receiver.slice, block_out, conditions ]
|
|
@@ -310,49 +546,119 @@ module RailsAiContext
|
|
|
310
546
|
out.append(text.byteslice(position, text.bytesize - position), line_at.call(position))
|
|
311
547
|
end
|
|
312
548
|
|
|
549
|
+
# The binding a read reaches: the method's parameter, or the parameter of a block written once per item.
|
|
550
|
+
def bound(bindings, node, depth)
|
|
551
|
+
level = depth - node.depth
|
|
552
|
+
level.zero? ? bindings[node.name] : bindings[[ node.name, level ]]
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# `"#{field}_changed?"` with every part a literal or a bound literal, as the value it makes.
|
|
556
|
+
def interpolated(node, bindings, depth)
|
|
557
|
+
text = node.parts.map do |part|
|
|
558
|
+
next part.unescaped if part.is_a?(Prism::StringNode)
|
|
559
|
+
|
|
560
|
+
statements = part.is_a?(Prism::EmbeddedStatementsNode) && part.statements&.body
|
|
561
|
+
return nil unless statements&.one?
|
|
562
|
+
|
|
563
|
+
inner = statements.first
|
|
564
|
+
value = inner.is_a?(Prism::LocalVariableReadNode) ? bound(bindings, inner, depth)&.value : value_of(inner)
|
|
565
|
+
return nil if value.nil? || value == UNKNOWN || value.is_a?(Hash) || value.is_a?(Array)
|
|
566
|
+
|
|
567
|
+
value.to_s
|
|
568
|
+
end.join
|
|
569
|
+
node.is_a?(Prism::InterpolatedSymbolNode) ? text.to_sym : text
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
# `args.each do |field| ... end` over a bound list of literals.
|
|
573
|
+
def unrolled?(node, bindings, depth)
|
|
574
|
+
receiver = node.receiver
|
|
575
|
+
return false unless node.name == :each && node.arguments.nil? && node.block.is_a?(Prism::BlockNode) && node.block.body
|
|
576
|
+
return false unless receiver.is_a?(Prism::LocalVariableReadNode) && bound(bindings, receiver, depth)&.items
|
|
577
|
+
|
|
578
|
+
params = node.block.parameters&.parameters
|
|
579
|
+
params && params.requireds.one? && params.requireds.first.respond_to?(:name) && params.optionals.empty? && params.rest.nil? && params.posts.empty?
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
# The block's body written once per item, its parameter bound to that item.
|
|
583
|
+
def emit_each(node, bindings, out, undecided, depth, conditions)
|
|
584
|
+
name = node.block.parameters.parameters.requireds.first.name
|
|
585
|
+
binding = bound(bindings, node.receiver, depth)
|
|
586
|
+
binding.items.zip(binding.value).each do |source, value|
|
|
587
|
+
emit(node.block.body, bindings.merge([ name, depth + 1 ] => Binding.new(value, source)), out, undecided, depth + 1, conditions)
|
|
588
|
+
out.append("\n", node.location.end_line)
|
|
589
|
+
end
|
|
590
|
+
out
|
|
591
|
+
end
|
|
592
|
+
|
|
313
593
|
# A call's arguments, each written from its own node and joined with a
|
|
314
594
|
# comma: an argument that stands for no keywords at all (an options hash
|
|
315
595
|
# left at `{}`) is left out whole, with no separator to dangle.
|
|
316
596
|
def emit_arguments(node, bindings, out, undecided, depth, conditions)
|
|
317
597
|
written = false
|
|
318
598
|
node.arguments.each do |argument|
|
|
319
|
-
pairs = keyword_argument(node, argument, bindings, depth)
|
|
599
|
+
pairs = keyword_argument(node, argument, bindings, depth) || splatted_items(argument, bindings, depth)
|
|
320
600
|
next if pairs&.empty?
|
|
321
601
|
|
|
322
602
|
out.append(", ", argument.location.start_line) if written
|
|
323
|
-
pairs ? out.append(pairs, argument.location.start_line) : emit(argument, bindings, out, undecided, depth, conditions)
|
|
603
|
+
pairs ? out.append(pairs, argument.location.start_line, from_call: true) : emit(argument, bindings, out, undecided, depth, conditions)
|
|
324
604
|
written = true
|
|
325
605
|
end
|
|
326
606
|
out
|
|
327
607
|
end
|
|
328
608
|
|
|
329
|
-
# `
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
return
|
|
609
|
+
# `*names` over a bound list of literals is its items.
|
|
610
|
+
def splatted_items(argument, bindings, depth)
|
|
611
|
+
return unless argument.is_a?(Prism::SplatNode)
|
|
612
|
+
return bindings[ANONYMOUS_REST]&.items&.join(", ") if argument.expression.nil?
|
|
613
|
+
return unless argument.expression.is_a?(Prism::LocalVariableReadNode)
|
|
333
614
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return unless sources && key.one? && key.first.is_a?(Prism::SymbolNode)
|
|
615
|
+
bound(bindings, argument.expression, depth)&.items&.join(", ")
|
|
616
|
+
end
|
|
337
617
|
|
|
338
|
-
|
|
618
|
+
# `options[:length]` or `options.fetch(:length, default)` on a hash
|
|
619
|
+
# parameter the call passed as a literal: the value's own source, or for
|
|
620
|
+
# a key the call left out, `nil` or the default.
|
|
621
|
+
def hash_lookup(node, bindings, depth)
|
|
622
|
+
return unless %i[[] fetch].include?(node.name) && node.receiver.is_a?(Prism::LocalVariableReadNode) && node.block.nil?
|
|
623
|
+
|
|
624
|
+
sources = bound(bindings, node.receiver, depth)&.value_sources
|
|
625
|
+
key, default, *rest = Array(node.arguments&.arguments)
|
|
626
|
+
return unless sources && rest.empty? && key.is_a?(Prism::SymbolNode)
|
|
627
|
+
|
|
628
|
+
name = key.unescaped.to_sym
|
|
629
|
+
if node.name == :[]
|
|
630
|
+
sources.fetch(name, "nil") unless default
|
|
631
|
+
elsif sources.key?(name)
|
|
632
|
+
sources[name]
|
|
633
|
+
elsif default
|
|
634
|
+
emit(default, bindings, Output.new, [], depth, []).text
|
|
635
|
+
end
|
|
339
636
|
end
|
|
340
637
|
|
|
341
638
|
# A hash parameter passed as a call's last argument reads as the keywords
|
|
342
639
|
# it holds: `validates method, options` is `validates :title, presence: true`.
|
|
343
640
|
def keyword_argument(node, child, bindings, depth)
|
|
344
641
|
return unless node.is_a?(Prism::ArgumentsNode) && child.equal?(node.arguments.last)
|
|
642
|
+
return splatted_keywords(child, bindings, depth) if child.is_a?(Prism::KeywordHashNode)
|
|
345
643
|
|
|
346
644
|
hash_expression(child, bindings, depth)&.pairs
|
|
347
645
|
end
|
|
348
646
|
|
|
647
|
+
# `key: v, **options` with `options` a bound hash, as the pairs it spells out.
|
|
648
|
+
def splatted_keywords(node, bindings, depth)
|
|
649
|
+
parts = node.elements.map do |element|
|
|
650
|
+
next emit(element, bindings, Output.new, [], depth, []).text unless element.is_a?(Prism::AssocSplatNode)
|
|
651
|
+
|
|
652
|
+
hash_expression(element.value, bindings, depth)&.pairs or return nil
|
|
653
|
+
end
|
|
654
|
+
parts.reject(&:empty?).join(", ") if node.elements.any?(Prism::AssocSplatNode)
|
|
655
|
+
end
|
|
656
|
+
|
|
349
657
|
# The bound hash an expression names: the parameter itself, or `merge`,
|
|
350
658
|
# `slice`, `except` or a `reject { |key| key == :x }` on it. Anything else
|
|
351
659
|
# on it is not read, and the expression stays as written.
|
|
352
660
|
def hash_expression(node, bindings, depth)
|
|
353
|
-
if node.is_a?(Prism::LocalVariableReadNode)
|
|
354
|
-
return node.depth == depth ? bindings[node.name]&.then { |b| b if b.value_sources } : nil
|
|
355
|
-
end
|
|
661
|
+
return bound(bindings, node, depth)&.then { |b| b if b.value_sources } if node.is_a?(Prism::LocalVariableReadNode)
|
|
356
662
|
return unless node.is_a?(Prism::CallNode) && node.receiver
|
|
357
663
|
|
|
358
664
|
base = hash_expression(node.receiver, bindings, depth) or return
|
|
@@ -363,7 +669,7 @@ module RailsAiContext
|
|
|
363
669
|
when :merge
|
|
364
670
|
return unless arguments.one? && symbol_keyed?(arguments.first) && node.block.nil?
|
|
365
671
|
|
|
366
|
-
added = arguments.first.elements.to_h { |pair| [ key_of(pair), pair.value.
|
|
672
|
+
added = arguments.first.elements.to_h { |pair| [ key_of(pair), emit(pair.value, bindings, Output.new, [], depth, []).text ] }
|
|
367
673
|
hash_binding(UNKNOWN, sources.merge(added))
|
|
368
674
|
when :slice then keys && hash_binding(UNKNOWN, sources.slice(*keys))
|
|
369
675
|
when :except then keys && hash_binding(UNKNOWN, sources.except(*keys))
|
|
@@ -465,7 +771,7 @@ module RailsAiContext
|
|
|
465
771
|
def evaluate(node, bindings, depth)
|
|
466
772
|
case node
|
|
467
773
|
when Prism::LocalVariableReadNode
|
|
468
|
-
node
|
|
774
|
+
(bound(bindings, node, depth) || unknown).value
|
|
469
775
|
when Prism::ParenthesesNode
|
|
470
776
|
body = node.body&.body
|
|
471
777
|
body&.one? ? evaluate(body.first, bindings, depth) : UNKNOWN
|
|
@@ -502,9 +808,13 @@ module RailsAiContext
|
|
|
502
808
|
when :[] then receiver.is_a?(Hash) ? receiver[arguments.first] : UNKNOWN
|
|
503
809
|
when :key?, :has_key?, :include? then receiver.is_a?(Hash) ? receiver.key?(arguments.first) : UNKNOWN
|
|
504
810
|
when :many?, :any?, :empty?, :size, :length
|
|
505
|
-
return UNKNOWN unless receiver.is_a?(Hash) && arguments.empty? && !node.block
|
|
811
|
+
return UNKNOWN unless (receiver.is_a?(Hash) || receiver.is_a?(Array)) && arguments.empty? && !node.block
|
|
506
812
|
|
|
507
813
|
node.name == :many? ? receiver.size > 1 : receiver.public_send(node.name)
|
|
814
|
+
when :to_s, :to_sym, :singularize, :pluralize
|
|
815
|
+
return UNKNOWN unless (receiver.is_a?(String) || receiver.is_a?(Symbol)) && arguments.empty? && !node.block
|
|
816
|
+
|
|
817
|
+
node.name == :to_sym ? receiver.to_sym : receiver.to_s.public_send(node.name)
|
|
508
818
|
when :fetch
|
|
509
819
|
return UNKNOWN unless receiver.is_a?(Hash)
|
|
510
820
|
|
|
@@ -34,13 +34,23 @@ module RailsAiContext
|
|
|
34
34
|
# File breaks the tie: `sort_by` is not stable, so a name two files
|
|
35
35
|
# declare would otherwise swap places between runs.
|
|
36
36
|
def extract_components
|
|
37
|
-
|
|
37
|
+
dirs = components_dirs
|
|
38
|
+
views = PathResolver.view_dirs(root).map { |dir| File.expand_path(dir) }
|
|
39
|
+
# phlex:install pushes app/views itself under Views: a page view is a Phlex class, not a component.
|
|
40
|
+
namespaced = PathResolver.namespaced_roots(root).map(&:first).reject do |dir|
|
|
41
|
+
views.any? { |view| view == dir || view.start_with?("#{dir}/") }
|
|
42
|
+
end - dirs.map { |dir| File.expand_path(dir) }
|
|
43
|
+
paths = (dirs + namespaced).flat_map { |dir| Dir.glob(File.join(dir, "**/*.rb")) }
|
|
38
44
|
|
|
39
45
|
components = paths.filter_map do |path|
|
|
40
46
|
next if path.end_with?("_preview.rb")
|
|
41
47
|
next if File.basename(path) == "application_component.rb"
|
|
42
48
|
|
|
43
|
-
parse_component(path)
|
|
49
|
+
component = parse_component(path)
|
|
50
|
+
# A push_dir root (Phlex's app/views/components) holds helpers and kits too.
|
|
51
|
+
next if component && component[:type] == :unknown && namespaced.any? { |dir| path.start_with?("#{dir}/") }
|
|
52
|
+
|
|
53
|
+
component
|
|
44
54
|
rescue => e
|
|
45
55
|
{ file: path.sub("#{root}/", ""), error: e.message }
|
|
46
56
|
end.sort_by { |c| [ c[:name] || "", c[:file] || "" ] }
|
|
@@ -53,8 +63,9 @@ module RailsAiContext
|
|
|
53
63
|
def split_bases(components)
|
|
54
64
|
names = components.filter_map { |c| full_names[c[:file]] }.to_set
|
|
55
65
|
inherited = components.filter_map do |c|
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
declared = declared_classes[c[:file]]
|
|
67
|
+
declared&.superclass &&
|
|
68
|
+
SuperclassChain.resolve_in_scope(declared.name, declared.superclass, nesting: declared.nesting) { |name| name if names.include?(name) }
|
|
58
69
|
end.to_set
|
|
59
70
|
# Inherited, always: ApplicationRowComponent is a row, not a base.
|
|
60
71
|
components.partition do |c|
|
|
@@ -63,8 +74,8 @@ module RailsAiContext
|
|
|
63
74
|
end
|
|
64
75
|
end
|
|
65
76
|
|
|
66
|
-
def
|
|
67
|
-
@
|
|
77
|
+
def declared_classes
|
|
78
|
+
@declared_classes ||= {}
|
|
68
79
|
end
|
|
69
80
|
|
|
70
81
|
# A preview named for another namespace than its component is linked by what it
|
|
@@ -107,9 +118,9 @@ module RailsAiContext
|
|
|
107
118
|
relative = path.sub("#{root}/", "")
|
|
108
119
|
declaration = component_declaration(content, path)
|
|
109
120
|
return nil unless declaration
|
|
110
|
-
class_name =
|
|
121
|
+
class_name = declaration.name
|
|
111
122
|
full_names[relative] = declaration.name
|
|
112
|
-
|
|
123
|
+
declared_classes[relative] = declaration
|
|
113
124
|
|
|
114
125
|
structure = extract_structure(content)
|
|
115
126
|
type = detect_component_type(content, declaration)
|
|
@@ -125,7 +136,7 @@ module RailsAiContext
|
|
|
125
136
|
slots: extract_slots(structure, type)
|
|
126
137
|
}
|
|
127
138
|
|
|
128
|
-
preview = find_preview(path, class_name)
|
|
139
|
+
preview = find_preview(path, preview_name(class_name))
|
|
129
140
|
component[:preview] = preview if preview
|
|
130
141
|
|
|
131
142
|
sidecar = find_sidecar_assets(path)
|
|
@@ -142,8 +153,8 @@ module RailsAiContext
|
|
|
142
153
|
declarations.first
|
|
143
154
|
end
|
|
144
155
|
|
|
145
|
-
#
|
|
146
|
-
def
|
|
156
|
+
# A preview is named without the namespace every component shares: "RubyUI::Button" -> "Button".
|
|
157
|
+
def preview_name(full_name)
|
|
147
158
|
parts = full_name.split("::")
|
|
148
159
|
if parts.size > 2 && parts.first == "Components"
|
|
149
160
|
parts[1..].join("::")
|
|
@@ -248,7 +259,7 @@ module RailsAiContext
|
|
|
248
259
|
|
|
249
260
|
def extract_slots(structure, type)
|
|
250
261
|
slots = structure.fetch(:slot_macro, []).map do |entry|
|
|
251
|
-
entry.slice(:name, :type, :renderer).compact
|
|
262
|
+
entry.slice(:name, :type, :renderer, :setters).compact
|
|
252
263
|
end
|
|
253
264
|
|
|
254
265
|
# Phlex slots are plain methods taking a block.
|
|
@@ -330,7 +341,7 @@ module RailsAiContext
|
|
|
330
341
|
end
|
|
331
342
|
|
|
332
343
|
DEFAULT_PREVIEW_DIRS = %w[test/components/previews spec/components/previews app/components/previews].freeze
|
|
333
|
-
PREVIEW_CONFIG_GLOBS = %w[config/application.rb config/environments/*.rb
|
|
344
|
+
PREVIEW_CONFIG_GLOBS = %w[config/application.rb config/environments/*.rb].freeze
|
|
334
345
|
|
|
335
346
|
# Previewed path (without `_preview.rb`) -> the preview file, read once
|
|
336
347
|
# from the default directories and the ones the app configures.
|
|
@@ -343,7 +354,8 @@ module RailsAiContext
|
|
|
343
354
|
end
|
|
344
355
|
|
|
345
356
|
def preview_dirs
|
|
346
|
-
|
|
357
|
+
files = PREVIEW_CONFIG_GLOBS.flat_map { |glob| Dir.glob(File.join(root, glob)).sort } + PathResolver.initializer_paths(root)
|
|
358
|
+
configured = files.flat_map do |file|
|
|
347
359
|
SourceIntrospector.walk(file, { previews: Listeners::PreviewPathsListener })[:previews]
|
|
348
360
|
end
|
|
349
361
|
(DEFAULT_PREVIEW_DIRS + configured).map { |relative| File.join(root, relative) }.uniq.select { |dir| Dir.exist?(dir) }
|