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
|
@@ -7,6 +7,7 @@ module RailsAiContext
|
|
|
7
7
|
# before_validation, after_save, after_commit, etc.
|
|
8
8
|
class CallbacksListener < BaseListener
|
|
9
9
|
include WithOptionsScope
|
|
10
|
+
include OwnerScope
|
|
10
11
|
|
|
11
12
|
CALLBACK_METHODS = %i[
|
|
12
13
|
before_validation after_validation
|
|
@@ -21,43 +22,103 @@ module RailsAiContext
|
|
|
21
22
|
].to_set.freeze
|
|
22
23
|
|
|
23
24
|
INLINE_BLOCK = "[inline_block]"
|
|
24
|
-
|
|
25
|
+
|
|
26
|
+
# Macros that call args.extract_options!, so a trailing braced hash is options, not a filter.
|
|
27
|
+
EXTRACTS_OPTIONS = %i[
|
|
28
|
+
before_validation after_validation after_commit after_rollback
|
|
29
|
+
after_create_commit after_update_commit after_destroy_commit after_save_commit
|
|
30
|
+
].to_set.freeze
|
|
25
31
|
|
|
26
32
|
def on_call_node_enter(node)
|
|
33
|
+
return record_skip(node) if node.name == :skip_callback && in_scope?(node)
|
|
27
34
|
return unless CALLBACK_METHODS.include?(node.name) && in_scope?(node)
|
|
28
35
|
|
|
29
36
|
# Sources, not literals: a lambda condition reads as the line the
|
|
30
37
|
# file holds rather than as a marker.
|
|
38
|
+
hash = options_hash(node)
|
|
39
|
+
options = scope_options(receiver_name(node)).merge(extract_keyword_sources(node)).merge(hash_sources(hash))
|
|
40
|
+
emit(node, resolve_callback_types(node.name, options), callback_targets(node, hash), options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
ON_EVENT = "after_commit_on_"
|
|
44
|
+
|
|
45
|
+
# Every commit spelling lands in the one after-commit kind of one chain.
|
|
46
|
+
def self.chain_key(type)
|
|
47
|
+
type.to_s.match?(/\Aafter_(\w+_)?commit/) ? "after_commit" : type.to_s
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.transaction?(type)
|
|
51
|
+
type.to_s.match?(/\Aafter_(\w+_)?(commit|rollback)/)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.after?(type)
|
|
55
|
+
type.to_s.start_with?("after_")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# `after_commit on: :create` resolves to a type that names its event.
|
|
59
|
+
def self.names_event?(type)
|
|
60
|
+
type.to_s.start_with?(ON_EVENT)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The event such a type names, `"create"`, or nil.
|
|
64
|
+
def self.event(type)
|
|
65
|
+
type.to_s.delete_prefix(ON_EVENT) if names_event?(type)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
KINDS = %w[before after around].freeze
|
|
71
|
+
|
|
72
|
+
# Rails' skip_callback; the kind defaults to :before, as normalize_callback_params does.
|
|
73
|
+
def record_skip(node)
|
|
74
|
+
positional = (node.arguments&.arguments || []).reject { |a| a.is_a?(Prism::KeywordHashNode) }
|
|
75
|
+
event, *rest = positional.map { |a| literal_string(a) || one_line_source(a) }
|
|
76
|
+
return unless event && literal_string(positional.first)
|
|
77
|
+
|
|
78
|
+
kind = KINDS.include?(rest.first) ? rest.shift : "before"
|
|
31
79
|
options = scope_options(receiver_name(node)).merge(extract_keyword_sources(node))
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
rest.each do |method_name|
|
|
81
|
+
@results << {
|
|
82
|
+
name: "skip_callback",
|
|
83
|
+
type: "#{kind}_#{event}",
|
|
84
|
+
method: method_name,
|
|
85
|
+
skip: true,
|
|
86
|
+
options: options,
|
|
87
|
+
owner: @owner_stack.dup,
|
|
88
|
+
location: node.location.start_line,
|
|
89
|
+
confidence: confidence_for(node)
|
|
90
|
+
}
|
|
91
|
+
end
|
|
92
|
+
end
|
|
34
93
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
94
|
+
# Each filter Rails registers, in its order: the block first, then every positional
|
|
95
|
+
# argument. An object (`Normalizer.new`) is kept as written; a lambda names nothing.
|
|
96
|
+
def callback_targets(node, options_hash)
|
|
97
|
+
positional = (node.arguments&.arguments || []).reject { |a| a.is_a?(Prism::KeywordHashNode) || a.equal?(options_hash) }
|
|
98
|
+
targets = node.block ? [ [ INLINE_BLOCK, RailsAiContext::Confidence::INFERRED ] ] : []
|
|
99
|
+
targets + positional.map do |arg|
|
|
100
|
+
if (name = literal_string(arg)) then [ name, confidence_for(node) ]
|
|
101
|
+
elsif proc_argument?(arg) then [ INLINE_BLOCK, RailsAiContext::Confidence::INFERRED ]
|
|
102
|
+
else [ one_line_source(arg), confidence_for(node) ]
|
|
103
|
+
end
|
|
39
104
|
end
|
|
40
105
|
end
|
|
41
106
|
|
|
42
|
-
|
|
107
|
+
def options_hash(node)
|
|
108
|
+
last = node.arguments&.arguments&.last
|
|
109
|
+
last if last.is_a?(Prism::HashNode) && EXTRACTS_OPTIONS.include?(node.name)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def hash_sources(hash)
|
|
113
|
+
return {} unless hash
|
|
43
114
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def emit_without_symbol_args(node, callback_types, options)
|
|
47
|
-
positional = extract_arg_values(node).map(&:to_s)
|
|
48
|
-
targets = positional.grep(NAME_SHAPED)
|
|
49
|
-
|
|
50
|
-
if targets.any?
|
|
51
|
-
emit(node, callback_types, targets, options, confidence_for(node))
|
|
52
|
-
elsif node.block || positional.any?
|
|
53
|
-
# Keyword options are not a target: `after_commit on: :create`
|
|
54
|
-
# declares no block, so there is none to report.
|
|
55
|
-
emit(node, callback_types, [ INLINE_BLOCK ], options, RailsAiContext::Confidence::INFERRED)
|
|
115
|
+
hash.elements.each_with_object({}) do |assoc, h|
|
|
116
|
+
h[extract_key(assoc.key)] = value_or_source(assoc.value) if assoc.is_a?(Prism::AssocNode)
|
|
56
117
|
end
|
|
57
118
|
end
|
|
58
119
|
|
|
59
|
-
def emit(node, callback_types,
|
|
60
|
-
|
|
120
|
+
def emit(node, callback_types, targets, options)
|
|
121
|
+
targets.each do |method_name, confidence|
|
|
61
122
|
callback_types.each do |callback_type|
|
|
62
123
|
@results << {
|
|
63
124
|
# The declared macro, so a renderer can print what the file
|
|
@@ -66,6 +127,7 @@ module RailsAiContext
|
|
|
66
127
|
type: callback_type,
|
|
67
128
|
method: method_name,
|
|
68
129
|
options: options,
|
|
130
|
+
owner: @owner_stack.dup,
|
|
69
131
|
location: node.location.start_line,
|
|
70
132
|
confidence: confidence
|
|
71
133
|
}
|
|
@@ -77,7 +139,7 @@ module RailsAiContext
|
|
|
77
139
|
# declaration for several events stays one callback, its on: kept.
|
|
78
140
|
def resolve_callback_types(name, options)
|
|
79
141
|
events = Array(options[:on])
|
|
80
|
-
name == :after_commit && events.one? ? [ "
|
|
142
|
+
name == :after_commit && events.one? ? [ "#{ON_EVENT}#{events.first}" ] : [ name.to_s ]
|
|
81
143
|
end
|
|
82
144
|
end
|
|
83
145
|
end
|
|
@@ -6,20 +6,49 @@ module RailsAiContext
|
|
|
6
6
|
# Class definitions with their superclass, in source order.
|
|
7
7
|
# `class Admin::User < ApplicationRecord` →
|
|
8
8
|
# { name: "Admin::User", superclass: "ApplicationRecord" }
|
|
9
|
+
#
|
|
10
|
+
# `nesting` is Module.nesting where the superclass is read, innermost
|
|
11
|
+
# first, as DeclaredConstant::Declaration carries it: empty at the top
|
|
12
|
+
# level, for a compact `class Admin::User`, and for a `< ::Base`.
|
|
9
13
|
class ClassDefinitionListener < BaseListener
|
|
14
|
+
def initialize
|
|
15
|
+
super
|
|
16
|
+
@nestings = [ [] ]
|
|
17
|
+
end
|
|
18
|
+
|
|
10
19
|
def on_class_node_enter(node)
|
|
11
20
|
name = constant_path_string(node.constant_path)
|
|
12
|
-
|
|
21
|
+
unless name.empty?
|
|
22
|
+
rooted = node.superclass&.slice&.start_with?("::")
|
|
23
|
+
@results << {
|
|
24
|
+
name: name,
|
|
25
|
+
superclass: superclass_name(node.superclass),
|
|
26
|
+
location: node.location.start_line,
|
|
27
|
+
nesting: rooted ? [] : @nestings.last
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
enter_scope(node)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def on_class_node_leave(_node)
|
|
34
|
+
@nestings.pop
|
|
35
|
+
end
|
|
13
36
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
37
|
+
def on_module_node_enter(node)
|
|
38
|
+
enter_scope(node)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def on_module_node_leave(_node)
|
|
42
|
+
@nestings.pop
|
|
19
43
|
end
|
|
20
44
|
|
|
21
45
|
private
|
|
22
46
|
|
|
47
|
+
def enter_scope(node)
|
|
48
|
+
outer = @nestings.last
|
|
49
|
+
@nestings.push([ nested_name(node.constant_path, outer.first) ] + outer)
|
|
50
|
+
end
|
|
51
|
+
|
|
23
52
|
# nil for an anonymous or computed superclass (`< Struct.new(:a)`).
|
|
24
53
|
def superclass_name(node)
|
|
25
54
|
return nil unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "active_support/core_ext/string/inflections"
|
|
4
|
+
|
|
3
5
|
module RailsAiContext
|
|
4
6
|
module Introspectors
|
|
5
7
|
module Listeners
|
|
@@ -69,17 +71,43 @@ module RailsAiContext
|
|
|
69
71
|
name = extract_symbol_args(node).first
|
|
70
72
|
return unless name
|
|
71
73
|
|
|
72
|
-
renderer = extract_arg_values(node).drop(1).map(&:to_s)
|
|
73
74
|
entry = {
|
|
74
75
|
kind: :slot_macro,
|
|
75
76
|
name: name.to_s,
|
|
76
77
|
type: SLOT_MACROS.fetch(node.name),
|
|
77
78
|
location: node.location.start_line
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
+
types = polymorphic_types(node)
|
|
81
|
+
if types
|
|
82
|
+
entry[:setters] = polymorphic_setters(name.to_s, entry[:type], types)
|
|
83
|
+
else
|
|
84
|
+
renderer = extract_arg_values(node).drop(1).map(&:to_s)
|
|
85
|
+
entry[:renderer] = renderer.join(", ") if renderer.any?
|
|
86
|
+
end
|
|
80
87
|
@results << entry
|
|
81
88
|
end
|
|
82
89
|
|
|
90
|
+
def polymorphic_types(node)
|
|
91
|
+
hash = node.arguments&.arguments&.find { |a| a.is_a?(Prism::KeywordHashNode) || a.is_a?(Prism::HashNode) }
|
|
92
|
+
types = hash&.elements&.find { |e| e.is_a?(Prism::AssocNode) && e.key.is_a?(Prism::SymbolNode) && e.key.unescaped == "types" }
|
|
93
|
+
types.value if types&.value.is_a?(Prism::HashNode)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Slotable#__vc_register_polymorphic_slot: `as:` names the setter, else slot and type joined.
|
|
97
|
+
def polymorphic_setters(name, type, types)
|
|
98
|
+
base = type == :many ? name.singularize : name
|
|
99
|
+
types.elements.filter_map do |assoc|
|
|
100
|
+
next unless assoc.is_a?(Prism::AssocNode) && assoc.key.is_a?(Prism::SymbolNode)
|
|
101
|
+
|
|
102
|
+
as = assoc.value.is_a?(Prism::HashNode) && assoc.value.elements.find { |e| e.is_a?(Prism::AssocNode) && e.key.is_a?(Prism::SymbolNode) && e.key.unescaped == "as" }
|
|
103
|
+
as && literal_name(as.value) || "#{base}_#{assoc.key.unescaped}"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def literal_name(node)
|
|
108
|
+
node.unescaped if node.is_a?(Prism::SymbolNode) || node.is_a?(Prism::StringNode)
|
|
109
|
+
end
|
|
110
|
+
|
|
83
111
|
# `SIZES[@size]` ties a prop to the constant table that enumerates it.
|
|
84
112
|
def record_constant_index(node)
|
|
85
113
|
return unless node.receiver.is_a?(Prism::ConstantReadNode)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Introspectors
|
|
5
|
+
module Listeners
|
|
6
|
+
# GenericMacroListener that also records the `if`/`unless`/`case` branches a macro sits under.
|
|
7
|
+
# Apart, so the many walks that never read a condition do not pay for tracking branches.
|
|
8
|
+
class ConditionalMacroListener < GenericMacroListener
|
|
9
|
+
include BranchConditions
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def macro_condition
|
|
14
|
+
current_condition
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
3
5
|
module RailsAiContext
|
|
4
6
|
module Introspectors
|
|
5
7
|
module Listeners
|
|
@@ -7,20 +9,68 @@ module RailsAiContext
|
|
|
7
9
|
#
|
|
8
10
|
# config.timeout_in = 30.minutes → path [:timeout_in], assignment
|
|
9
11
|
# config.action_mailer.delivery = :smtp → path [:action_mailer, :delivery], assignment
|
|
10
|
-
# config.jwt do |jwt| ... end → path [:jwt], not an assignment
|
|
12
|
+
# config.jwt do |jwt| ... end → path [:jwt], not an assignment, write :block
|
|
13
|
+
# config.hosts << "x" → path [:hosts, :<<], write :call
|
|
14
|
+
# config.headers["X"] = "y" → path [:headers, :[]=], write :call
|
|
15
|
+
# config.filter_parameters += [:pin] → path [:filter_parameters], write :operator
|
|
16
|
+
# config.paths["app/views"] << "x" → path [:paths, :<<], write :call
|
|
11
17
|
#
|
|
12
18
|
# The chain is matched from a root receiver name, so
|
|
13
19
|
# `Rails.application.config.assets.paths = x` reports [:assets, :paths].
|
|
20
|
+
# A block root binds the block's first param: `on_load(:active_record)` (self
|
|
21
|
+
# too, unless the hook yields) or `Apartment.configure` for `Apartment.configure do |c|`.
|
|
22
|
+
# The param can be named, `_1`, or `it`.
|
|
14
23
|
class ConfigAssignmentListener < BaseListener
|
|
24
|
+
include LiteralPaths
|
|
25
|
+
|
|
15
26
|
DEFAULT_ROOTS = %w[config].freeze
|
|
27
|
+
SETTER = /\A[A-Za-z_]\w*=\z/
|
|
28
|
+
# A predicate or comparison with arguments reads a setting rather than changing it.
|
|
29
|
+
MUTATOR = /\A(?:<<|\[\]=|[A-Za-z_]\w*!?)\z/
|
|
30
|
+
EVALS = %i[class_eval module_eval class_exec module_exec instance_eval instance_exec].freeze
|
|
16
31
|
|
|
17
32
|
def initialize(*roots)
|
|
18
33
|
super()
|
|
19
34
|
names = roots.flatten.map(&:to_s)
|
|
20
35
|
@roots = (names.empty? ? DEFAULT_ROOTS : names).to_set
|
|
21
36
|
@conditions = []
|
|
37
|
+
@statements = Set.new.compare_by_identity
|
|
38
|
+
@chain_heads = {}.compare_by_identity
|
|
39
|
+
@def_params = []
|
|
40
|
+
# The root `self` and each block-bound local stand for here.
|
|
41
|
+
@scopes = [ { self: nil, locals: {} } ]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A call with arguments changes a setting only when it runs as a statement;
|
|
45
|
+
# `x = config.root.join("tmp")` reads one.
|
|
46
|
+
def on_statements_node_enter(node)
|
|
47
|
+
@statements.merge(node.body)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def on_rescue_modifier_node_enter(node)
|
|
51
|
+
@statements << node.expression if @statements.include?(node)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Inside `def initialize(config)` the local is that argument, not the app's config.
|
|
55
|
+
def on_def_node_enter(node)
|
|
56
|
+
params = node.parameters
|
|
57
|
+
names = params ? params.child_nodes.flatten.compact.flat_map { |p| p.respond_to?(:name) ? [ p.name ] : [] } : []
|
|
58
|
+
@def_params.push(names)
|
|
59
|
+
@scopes.push(self: nil, locals: {})
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def on_def_node_leave(_node)
|
|
63
|
+
@def_params.pop
|
|
64
|
+
@scopes.pop
|
|
22
65
|
end
|
|
23
66
|
|
|
67
|
+
def on_class_node_enter(_node) = @scopes.push(self: nil, locals: {})
|
|
68
|
+
def on_class_node_leave(_node) = @scopes.pop
|
|
69
|
+
def on_module_node_enter(_node) = @scopes.push(self: nil, locals: {})
|
|
70
|
+
def on_module_node_leave(_node) = @scopes.pop
|
|
71
|
+
def on_singleton_class_node_enter(_node) = @scopes.push(self: nil, locals: {})
|
|
72
|
+
def on_singleton_class_node_leave(_node) = @scopes.pop
|
|
73
|
+
|
|
24
74
|
# The branch an assignment sits in. Rails' own generated
|
|
25
75
|
# `config/environments/development.rb` assigns `perform_caching` in
|
|
26
76
|
# both halves of one `if`, and reporting the first as the value made
|
|
@@ -50,20 +100,96 @@ module RailsAiContext
|
|
|
50
100
|
end
|
|
51
101
|
|
|
52
102
|
def on_call_node_enter(node)
|
|
103
|
+
@scopes.push(block_scope(node)) if node.block.is_a?(Prism::BlockNode)
|
|
53
104
|
return if node.receiver.nil?
|
|
54
105
|
|
|
55
|
-
if node.name.to_s.
|
|
106
|
+
if node.name.to_s.match?(SETTER)
|
|
56
107
|
record_assignment(node)
|
|
108
|
+
elsif node.arguments
|
|
109
|
+
return unless node.name.to_s.match?(MUTATOR) && (@statements.include?(node) || @chain_heads.key?(node))
|
|
110
|
+
|
|
111
|
+
# `config.hosts << "a" << "b"` writes through the inner call, which records the whole chain.
|
|
112
|
+
receiver = node.receiver
|
|
113
|
+
@chain_heads[receiver] = @chain_heads.fetch(node, node) if receiver.is_a?(Prism::CallNode) && receiver.arguments && receiver.name.to_s.match?(MUTATOR)
|
|
114
|
+
record_write(node.receiver, node.name, :call, node)
|
|
57
115
|
else
|
|
58
116
|
record_reference(node)
|
|
59
117
|
end
|
|
60
118
|
end
|
|
61
119
|
|
|
120
|
+
def on_call_node_leave(node)
|
|
121
|
+
@scopes.pop if node.block.is_a?(Prism::BlockNode)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def on_call_operator_write_node_enter(node)
|
|
125
|
+
record_write(node.receiver, node.read_name, :operator, node)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def on_call_or_write_node_enter(node)
|
|
129
|
+
record_write(node.receiver, node.read_name, :operator, node)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def on_call_and_write_node_enter(node)
|
|
133
|
+
record_write(node.receiver, node.read_name, :operator, node)
|
|
134
|
+
end
|
|
135
|
+
|
|
62
136
|
private
|
|
63
137
|
|
|
138
|
+
# A block sees the outer locals bar its own params; class_eval and the like run it on another self.
|
|
139
|
+
def block_scope(node)
|
|
140
|
+
outer = @scopes.last
|
|
141
|
+
params = block_params(node.block)
|
|
142
|
+
own_self = EVALS.include?(node.name) && node.receiver ? receiver_root(node.receiver, outer) : outer[:self]
|
|
143
|
+
scope = { self: own_self, locals: outer[:locals].except(*params.compact) }
|
|
144
|
+
root = block_root(node)
|
|
145
|
+
return scope unless root
|
|
146
|
+
|
|
147
|
+
scope[:locals][params.first] = root if params.first
|
|
148
|
+
scope[:self] = root if root.start_with?("on_load(") && !extract_keyword_nodes(node).key?(:yield)
|
|
149
|
+
scope
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The root an eval's receiver stands for: the hook's self, a bound block param, or a root constant.
|
|
153
|
+
def receiver_root(receiver, outer)
|
|
154
|
+
case receiver
|
|
155
|
+
when Prism::SelfNode then outer[:self]
|
|
156
|
+
when Prism::LocalVariableReadNode then outer[:locals][receiver.name]
|
|
157
|
+
when Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
158
|
+
name = constant_path_string(receiver)
|
|
159
|
+
name if @roots.include?(name)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# `ActiveSupport.on_load(:x)` names `on_load(:x)`; `Apartment.configure` names itself when it is a root.
|
|
164
|
+
def block_root(node)
|
|
165
|
+
receiver = node.receiver
|
|
166
|
+
return unless receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
|
|
167
|
+
|
|
168
|
+
owner = constant_path_string(receiver)
|
|
169
|
+
if node.name == :on_load && owner == "ActiveSupport"
|
|
170
|
+
hook = node.arguments&.arguments&.first
|
|
171
|
+
return "on_load(:#{hook.unescaped})" if hook.is_a?(Prism::SymbolNode)
|
|
172
|
+
end
|
|
173
|
+
name = "#{owner}.#{node.name}"
|
|
174
|
+
name if @roots.include?(name)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def block_params(block)
|
|
178
|
+
params = block.parameters
|
|
179
|
+
return [ :_1 ] if params.is_a?(Prism::NumberedParametersNode)
|
|
180
|
+
return [ :it ] if params.is_a?(Prism::ItParametersNode)
|
|
181
|
+
|
|
182
|
+
params = params.parameters if params.is_a?(Prism::BlockParametersNode)
|
|
183
|
+
return [] unless params.is_a?(Prism::ParametersNode)
|
|
184
|
+
|
|
185
|
+
# A destructured `|(a, b)|` names nothing, so it binds no root.
|
|
186
|
+
params.requireds.map { |param| param.name if param.respond_to?(:name) }
|
|
187
|
+
end
|
|
188
|
+
|
|
64
189
|
def record_assignment(node)
|
|
65
190
|
prefix = chain_path(node.receiver)
|
|
66
191
|
return unless prefix
|
|
192
|
+
return record_write(node.receiver, node.name, :call, node) if prefix.include?(:[])
|
|
67
193
|
|
|
68
194
|
value_node = node.arguments&.arguments&.first
|
|
69
195
|
setting = node.name.to_s.delete_suffix("=").to_sym
|
|
@@ -83,7 +209,7 @@ module RailsAiContext
|
|
|
83
209
|
path, value: value, source: value_node && NodeSource.text(value_node)
|
|
84
210
|
)
|
|
85
211
|
|
|
86
|
-
|
|
212
|
+
entry = {
|
|
87
213
|
path: path,
|
|
88
214
|
assignment: true,
|
|
89
215
|
value: redacted[:value],
|
|
@@ -91,6 +217,58 @@ module RailsAiContext
|
|
|
91
217
|
condition: @conditions.compact.last,
|
|
92
218
|
location: node.location.start_line
|
|
93
219
|
}
|
|
220
|
+
entry[:literal] = true if LITERAL_NODES.any? { |kind| value_node.is_a?(kind) } && value != RailsAiContext::Confidence::INFERRED
|
|
221
|
+
# Each element as written, the constant it names or builds, and which are no literal; kept out of a redacted value.
|
|
222
|
+
if value_node && redacted[:source] == NodeSource.text(value_node)
|
|
223
|
+
entry.merge!(arguments: extract_arg_values(node), computed: computed_arguments(node), constants: constant_arguments(node))
|
|
224
|
+
end
|
|
225
|
+
# Read off the node rather than the source, which is redacted under a secret-shaped key.
|
|
226
|
+
if (call = config_for(value_node))
|
|
227
|
+
entry[:config_for] = call
|
|
228
|
+
end
|
|
229
|
+
@results << entry
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
LITERAL_NODES = [ Prism::StringNode, Prism::SymbolNode, Prism::InterpolatedStringNode ].freeze
|
|
233
|
+
|
|
234
|
+
# One value pulled out of the result still names the file: `config_for(:redis)[:url]`.
|
|
235
|
+
CONFIG_FOR_READERS = %i[[] fetch dig].freeze
|
|
236
|
+
|
|
237
|
+
# `config_for(:name, env: "production")`: its arguments as written, the YAML file it reads, nil for a path
|
|
238
|
+
# that is not a literal, and its env:, :expression when that is not a literal.
|
|
239
|
+
def config_for(value)
|
|
240
|
+
node = value
|
|
241
|
+
node = node.receiver while node.is_a?(Prism::CallNode) && CONFIG_FOR_READERS.include?(node.name) && node.receiver
|
|
242
|
+
return nil unless node.is_a?(Prism::CallNode) && node.name == :config_for
|
|
243
|
+
return nil unless node.receiver.nil? || rails_call?(node.receiver, "Rails.application")
|
|
244
|
+
|
|
245
|
+
argument = node.arguments&.arguments&.first
|
|
246
|
+
env = extract_keyword_nodes(node)[:env]
|
|
247
|
+
env = nil if env && rails_call?(env, "Rails.env")
|
|
248
|
+
{ arguments: node.arguments && RailsAiContext::Redaction.call(NodeSource.text(node.arguments)), file: config_for_file(argument),
|
|
249
|
+
env: env && (literal_string(env) || :expression), read: chained_read(value, node) }.compact
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# What the chain reads off the result, `.fetch(:api)`: the value holds that one entry, not the file.
|
|
253
|
+
def chained_read(value, call)
|
|
254
|
+
return nil if value.equal?(call)
|
|
255
|
+
|
|
256
|
+
text = value.location.slice.byteslice((call.location.end_offset - value.location.start_offset)..)
|
|
257
|
+
RailsAiContext::Redaction.call(text)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Rails reads `config/<name>.yml` for a name, and the Pathname itself for `Rails.root.join(...)` or `config.root.join(...)`.
|
|
261
|
+
def config_for_file(node)
|
|
262
|
+
return nil if node.nil? || node.is_a?(Prism::KeywordHashNode)
|
|
263
|
+
|
|
264
|
+
name = literal_string(node)
|
|
265
|
+
path = name ? "config/#{name}.yml" : (segments = app_root_join(node)) && File.join(*segments)
|
|
266
|
+
# Rails reads `config/../x.yml` as `x.yml`, inside the app; only a path still climbing leaves it.
|
|
267
|
+
Pathname.new(path).cleanpath.to_s if path
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def rails_call?(node, text)
|
|
271
|
+
node.is_a?(Prism::CallNode) && NodeSource.text(node).delete_prefix("::") == text
|
|
94
272
|
end
|
|
95
273
|
|
|
96
274
|
def condition_text(node)
|
|
@@ -102,18 +280,63 @@ module RailsAiContext
|
|
|
102
280
|
# A bare `config.jwt` reference, with or without a block. Enough to tell
|
|
103
281
|
# that a section of the initializer exists at all.
|
|
104
282
|
def record_reference(node)
|
|
105
|
-
return unless node.arguments.nil?
|
|
106
|
-
|
|
107
283
|
prefix = chain_path(node.receiver)
|
|
108
|
-
return unless prefix
|
|
284
|
+
return unless prefix && !prefix.include?(:[])
|
|
109
285
|
|
|
110
|
-
|
|
286
|
+
entry = {
|
|
111
287
|
path: prefix + [ node.name ],
|
|
112
288
|
assignment: false,
|
|
113
289
|
value: nil,
|
|
114
290
|
source: nil,
|
|
115
291
|
location: node.location.start_line
|
|
116
292
|
}
|
|
293
|
+
entry[:write] = :block if node.block
|
|
294
|
+
@results << entry
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# A setting changed without `=`: a call with arguments or an operator write.
|
|
298
|
+
def record_write(receiver, name, kind, node)
|
|
299
|
+
return if receiver.nil?
|
|
300
|
+
|
|
301
|
+
prefix = chain_path(receiver)
|
|
302
|
+
return unless prefix
|
|
303
|
+
|
|
304
|
+
# An element of a setting (`config.paths["app/views"]`) is changed through it.
|
|
305
|
+
setting = prefix.take_while { |part| part != :[] }
|
|
306
|
+
return if setting.empty? && prefix.include?(:[])
|
|
307
|
+
|
|
308
|
+
path = setting + [ name ]
|
|
309
|
+
head = @chain_heads[node]
|
|
310
|
+
args = kind == :call && !head ? node.arguments&.arguments : nil
|
|
311
|
+
# A one-argument call (`<<`, `merge!`) carries what it adds; `[]=` and `store` carry a key,
|
|
312
|
+
# which names the secret: `smtp_settings[:password] = ...`.
|
|
313
|
+
key = literal_string(args.first) if args&.size == 2
|
|
314
|
+
value = extract_value(args.last) if args&.size == 1 || key
|
|
315
|
+
# An operator write's value only decides the redaction; the record keeps none.
|
|
316
|
+
judged_value = kind == :operator ? extract_value(node.value) : value
|
|
317
|
+
# Every name the write goes through can mark it secret: the setting, each index key, the written name.
|
|
318
|
+
names = [ key, *index_keys(receiver) ].compact.map { |k| setting + [ k.to_sym ] }
|
|
319
|
+
names += [ setting, setting + [ name.to_s.delete_suffix("=").to_sym ] ]
|
|
320
|
+
judged = names.find { |n| RailsAiContext::Redaction.secret_name?(n) } || path
|
|
321
|
+
redacted = RailsAiContext::Redaction.redact_assignment(judged, value: judged_value, source: NodeSource.text(head || node))
|
|
322
|
+
@results << {
|
|
323
|
+
path: path,
|
|
324
|
+
assignment: false,
|
|
325
|
+
write: kind,
|
|
326
|
+
value: (redacted[:value] unless key || kind == :operator),
|
|
327
|
+
source: redacted[:source],
|
|
328
|
+
location: node.location.start_line
|
|
329
|
+
}
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# The literal keys of every `[]` read in the chain, the elements a write goes through.
|
|
333
|
+
def index_keys(node)
|
|
334
|
+
keys = []
|
|
335
|
+
while node.is_a?(Prism::CallNode)
|
|
336
|
+
keys << literal_string(node.arguments&.arguments&.first) if node.name == :[]
|
|
337
|
+
node = node.receiver
|
|
338
|
+
end
|
|
339
|
+
keys.compact
|
|
117
340
|
end
|
|
118
341
|
|
|
119
342
|
# Returns the path segments after the root, or nil when the chain is not
|
|
@@ -125,25 +348,47 @@ module RailsAiContext
|
|
|
125
348
|
while current
|
|
126
349
|
case current
|
|
127
350
|
when Prism::CallNode
|
|
128
|
-
return nil unless current.
|
|
351
|
+
return nil unless current.block.nil? && (current.arguments.nil? || current.name == :[])
|
|
129
352
|
parts.unshift(current.name)
|
|
130
353
|
current = current.receiver
|
|
131
354
|
when Prism::LocalVariableReadNode
|
|
132
|
-
|
|
355
|
+
bound = @scopes.last[:locals][current.name]
|
|
356
|
+
return nil if !bound && @def_params.last&.include?(current.name)
|
|
357
|
+
|
|
358
|
+
parts.unshift(bound ? bound.to_sym : current.name)
|
|
359
|
+
current = nil
|
|
360
|
+
when Prism::ItLocalVariableReadNode
|
|
361
|
+
bound = @scopes.last[:locals][:it]
|
|
362
|
+
return nil unless bound
|
|
363
|
+
|
|
364
|
+
parts.unshift(bound.to_sym)
|
|
133
365
|
current = nil
|
|
134
366
|
when Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
135
367
|
parts.unshift(constant_path_string(current).to_sym)
|
|
136
368
|
current = nil
|
|
369
|
+
when Prism::SelfNode
|
|
370
|
+
return nil unless @scopes.last[:self]
|
|
371
|
+
|
|
372
|
+
parts.unshift(@scopes.last[:self].to_sym)
|
|
373
|
+
current = nil
|
|
137
374
|
else
|
|
138
375
|
return nil
|
|
139
376
|
end
|
|
140
377
|
end
|
|
141
378
|
|
|
142
379
|
root_index = parts.rindex { |part| @roots.include?(part.to_s) }
|
|
143
|
-
return nil unless root_index
|
|
380
|
+
return nil unless root_index && app_owned?(parts.first(root_index))
|
|
144
381
|
|
|
145
382
|
parts[(root_index + 1)..] || []
|
|
146
383
|
end
|
|
384
|
+
|
|
385
|
+
# What the root hangs off: nothing, a local (`app`), `Rails.application` or
|
|
386
|
+
# the app's Application class. `OmniAuth.config` is another library's config.
|
|
387
|
+
def app_owned?(prefix)
|
|
388
|
+
first = prefix.first.to_s
|
|
389
|
+
prefix.empty? || !first.match?(/\A[A-Z]/) || prefix.first(2) == %i[Rails application] ||
|
|
390
|
+
first == "Application" || first.end_with?("::Application")
|
|
391
|
+
end
|
|
147
392
|
end
|
|
148
393
|
end
|
|
149
394
|
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Introspectors
|
|
5
|
+
module Listeners
|
|
6
|
+
# References to constants by their last name (`MessageVerifier`, `ActiveSupport::MessageVerifier`).
|
|
7
|
+
# A constant that only qualifies a nested one (`MessageVerifier::InvalidSignature`) is no reference to it.
|
|
8
|
+
class ConstantReferenceListener < BaseListener
|
|
9
|
+
def initialize(names: [])
|
|
10
|
+
super()
|
|
11
|
+
@names = names.map(&:to_sym).to_set
|
|
12
|
+
@qualifiers = {}.compare_by_identity
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def on_constant_path_node_enter(node)
|
|
16
|
+
@qualifiers[node.parent] = true if node.parent
|
|
17
|
+
record(node)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def on_constant_read_node_enter(node)
|
|
21
|
+
record(node)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def record(node)
|
|
27
|
+
return if @qualifiers.delete(node) || !@names.include?(node.name)
|
|
28
|
+
|
|
29
|
+
@results << { name: node.name.to_s, source: node.slice, line: node.location.start_line }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|