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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "concurrent"
|
|
3
4
|
require "prism"
|
|
4
5
|
|
|
5
6
|
module RailsAiContext
|
|
@@ -19,6 +20,7 @@ module RailsAiContext
|
|
|
19
20
|
# like a handler is claimed here and then validated. Predicate and bang
|
|
20
21
|
# methods cannot be event names, so they stay the listener's own.
|
|
21
22
|
HANDLER_PATTERN = /\Aon_[a-z0-9_]+\z/
|
|
23
|
+
CLASS_EVENTS = Concurrent::Map.new
|
|
22
24
|
|
|
23
25
|
class << self
|
|
24
26
|
# A dispatcher with every listener wired. Validation runs for all of
|
|
@@ -32,8 +34,16 @@ module RailsAiContext
|
|
|
32
34
|
end
|
|
33
35
|
end
|
|
34
36
|
|
|
35
|
-
# The listener's handlers, sorted for a stable registration order.
|
|
37
|
+
# The listener's handlers, sorted for a stable registration order. Every walk
|
|
38
|
+
# builds fresh listeners, so the reflection is kept per class unless the
|
|
39
|
+
# instance has methods of its own.
|
|
36
40
|
def events_for(listener)
|
|
41
|
+
return handler_events(listener) unless listener.singleton_methods.empty?
|
|
42
|
+
|
|
43
|
+
CLASS_EVENTS.compute_if_absent(listener.class) { handler_events(listener) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def handler_events(listener)
|
|
37
47
|
events = listener.public_methods.grep(HANDLER_PATTERN).sort
|
|
38
48
|
|
|
39
49
|
unknown = events.reject { |event| known_event?(event) }
|
|
@@ -4,7 +4,8 @@ module RailsAiContext
|
|
|
4
4
|
module Introspectors
|
|
5
5
|
module Listeners
|
|
6
6
|
# Detects association macro calls via Prism AST:
|
|
7
|
-
# belongs_to, has_many, has_one, has_and_belongs_to_many
|
|
7
|
+
# belongs_to, has_many, has_one, has_and_belongs_to_many, and the
|
|
8
|
+
# belongs_to delegated_type declares
|
|
8
9
|
class AssociationsListener < BaseListener
|
|
9
10
|
include WithOptionsScope
|
|
10
11
|
|
|
@@ -12,22 +13,34 @@ module RailsAiContext
|
|
|
12
13
|
belongs_to has_many has_one has_and_belongs_to_many
|
|
13
14
|
].to_set.freeze
|
|
14
15
|
|
|
16
|
+
# acts_as_tenant calls `belongs_to tenant, scope, **valid_options`, the tenant defaulting to :account.
|
|
17
|
+
TENANT_OPTIONS = %i[foreign_key class_name inverse_of optional primary_key counter_cache polymorphic touch].freeze
|
|
18
|
+
|
|
15
19
|
def on_call_node_enter(node)
|
|
16
|
-
return
|
|
20
|
+
return record_tenant(node) if node.name == :acts_as_tenant && in_scope?(node)
|
|
21
|
+
|
|
22
|
+
delegated = node.name == :delegated_type
|
|
23
|
+
return unless delegated || ASSOCIATION_METHODS.include?(node.name)
|
|
17
24
|
return unless in_scope?(node)
|
|
18
25
|
|
|
19
26
|
name, literal = first_name_and_literal(node)
|
|
27
|
+
options = scope_options(receiver_name(node)).merge(extract_keyword_sources(node))
|
|
28
|
+
# delegated_type passes its options to `belongs_to role, polymorphic: true`.
|
|
29
|
+
options = options.except(:types).merge(polymorphic: true) if delegated
|
|
20
30
|
# The booted tier reads the macro off `assoc.macro.to_s`, so a
|
|
21
31
|
# static record spells it the same way or no consumer can compare
|
|
22
32
|
# the two.
|
|
23
33
|
@results << {
|
|
24
|
-
type: node.name.to_s,
|
|
34
|
+
type: delegated ? "belongs_to" : node.name.to_s,
|
|
25
35
|
name: name,
|
|
26
36
|
computed_name: (true unless literal),
|
|
27
37
|
computed_foreign_key: (true if computed_key?(node)),
|
|
28
38
|
# Sources, not literals: `class_name: Organisation.name` names a
|
|
29
39
|
# class, and the marker names nothing.
|
|
30
|
-
options:
|
|
40
|
+
options: options,
|
|
41
|
+
delegated_types: (delegated_types(node) if delegated),
|
|
42
|
+
delegated_types_source: (delegated_types_source(node) if delegated),
|
|
43
|
+
extension_methods: extension_methods(node),
|
|
31
44
|
location: node.location.start_line,
|
|
32
45
|
confidence: confidence_for(node)
|
|
33
46
|
}.compact
|
|
@@ -35,6 +48,40 @@ module RailsAiContext
|
|
|
35
48
|
|
|
36
49
|
private
|
|
37
50
|
|
|
51
|
+
def record_tenant(node)
|
|
52
|
+
first = node.arguments&.arguments&.first
|
|
53
|
+
first = nil if first.is_a?(Prism::KeywordHashNode)
|
|
54
|
+
name, literal = first ? first_name_and_literal(node) : [ :account, true ]
|
|
55
|
+
@results << {
|
|
56
|
+
type: "belongs_to",
|
|
57
|
+
name: name,
|
|
58
|
+
computed_name: (true unless literal),
|
|
59
|
+
options: scope_options(receiver_name(node)).merge(extract_keyword_sources(node).slice(*TENANT_OPTIONS)),
|
|
60
|
+
location: node.location.start_line,
|
|
61
|
+
confidence: confidence_for(node)
|
|
62
|
+
}.compact
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def delegated_types(node)
|
|
66
|
+
types = extract_keyword_nodes(node)[:types]
|
|
67
|
+
types && literal_strings(types).presence
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# `types: Entryable::TYPES` names the list only at run time.
|
|
71
|
+
def delegated_types_source(node)
|
|
72
|
+
types = extract_keyword_nodes(node)[:types]
|
|
73
|
+
NodeSource.text(types) if types && literal_strings(types).blank?
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# `has_many :sessions do def active ... end end` adds `user.sessions.active`.
|
|
77
|
+
def extension_methods(node)
|
|
78
|
+
body = node.block.is_a?(Prism::BlockNode) ? node.block.body : nil
|
|
79
|
+
return nil unless body.is_a?(Prism::StatementsNode)
|
|
80
|
+
|
|
81
|
+
names = body.body.grep(Prism::DefNode).map { |d| d.name.to_s }
|
|
82
|
+
names.presence
|
|
83
|
+
end
|
|
84
|
+
|
|
38
85
|
# `foreign_key: AUTHOR_KEY` names a column only at run time.
|
|
39
86
|
def computed_key?(node)
|
|
40
87
|
key = extract_keyword_nodes(node)[:foreign_key]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Introspectors
|
|
5
|
+
module Listeners
|
|
6
|
+
# The lib subdirectories `config.autoload_lib(ignore: %w[assets tasks])`
|
|
7
|
+
# keeps Zeitwerk out of, as root-relative paths. Literal names only.
|
|
8
|
+
class AutoloadIgnoreListener < BaseListener
|
|
9
|
+
AUTOLOAD_LIB = %i[autoload_lib autoload_lib_once].to_set.freeze
|
|
10
|
+
|
|
11
|
+
def on_call_node_enter(node)
|
|
12
|
+
return unless AUTOLOAD_LIB.include?(node.name) && node.receiver
|
|
13
|
+
|
|
14
|
+
ignore = extract_keyword_nodes(node)[:ignore]
|
|
15
|
+
collect(ignore) if ignore
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def collect(node)
|
|
21
|
+
case node
|
|
22
|
+
when Prism::ArrayNode then node.elements.each { |element| collect(element) }
|
|
23
|
+
when Prism::StringNode, Prism::SymbolNode
|
|
24
|
+
name = node.unescaped.to_s.strip.delete_prefix("/")
|
|
25
|
+
@results << File.join("lib", name) unless name.empty? || name.split("/").include?("..")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -7,15 +7,16 @@ module RailsAiContext
|
|
|
7
7
|
#
|
|
8
8
|
# config.autoload_paths << "lib_static" → "lib_static"
|
|
9
9
|
# config.eager_load_paths += %W(#{config.root}/x) → "x"
|
|
10
|
-
# config.autoload_lib(ignore: %w[tasks]) → "lib"
|
|
10
|
+
# config.autoload_lib(ignore: %w[tasks]) → "lib", and autoload_lib_once
|
|
11
11
|
#
|
|
12
12
|
# Literal paths only; a root built at run time stays unread.
|
|
13
13
|
class AutoloadPathsListener < BaseListener
|
|
14
|
+
include LiteralPaths
|
|
15
|
+
|
|
14
16
|
PATH_SETTINGS = %i[autoload_paths eager_load_paths autoload_once_paths].to_set.freeze
|
|
15
|
-
APPENDING = %i[<< push append concat].to_set.freeze
|
|
16
17
|
|
|
17
18
|
def on_call_node_enter(node)
|
|
18
|
-
return @results << "lib" if node.name
|
|
19
|
+
return @results << "lib" if AutoloadIgnoreListener::AUTOLOAD_LIB.include?(node.name) && node.receiver
|
|
19
20
|
return collect_added_path(node) if node.name == :add && paths_receiver?(node.receiver)
|
|
20
21
|
return unless APPENDING.include?(node.name) && path_setting?(node.receiver)
|
|
21
22
|
|
|
@@ -31,10 +32,6 @@ module RailsAiContext
|
|
|
31
32
|
|
|
32
33
|
private
|
|
33
34
|
|
|
34
|
-
def paths_receiver?(node)
|
|
35
|
-
node.is_a?(Prism::CallNode) && node.name == :paths
|
|
36
|
-
end
|
|
37
|
-
|
|
38
35
|
# `config.paths.add "lib/base", eager_load: true` is railties' own way
|
|
39
36
|
# of adding a root; the same call without those options adds no code.
|
|
40
37
|
def collect_added_path(node)
|
|
@@ -56,46 +53,6 @@ module RailsAiContext
|
|
|
56
53
|
def path_setting?(node)
|
|
57
54
|
node.is_a?(Prism::CallNode) && PATH_SETTINGS.include?(node.name)
|
|
58
55
|
end
|
|
59
|
-
|
|
60
|
-
def collect_paths(node)
|
|
61
|
-
case node
|
|
62
|
-
when Prism::ArrayNode then node.elements.each { |element| collect_paths(element) }
|
|
63
|
-
when Prism::StringNode then push_path(node.unescaped)
|
|
64
|
-
when Prism::InterpolatedStringNode then collect_interpolated_path(node)
|
|
65
|
-
when Prism::CallNode then collect_call_path(node)
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# `"#{config.root}/lib_static"`, and only that: an interpolation of
|
|
70
|
-
# some gem's root names a path outside the app.
|
|
71
|
-
def collect_interpolated_path(node)
|
|
72
|
-
return unless app_root?(node.parts.first)
|
|
73
|
-
|
|
74
|
-
push_path(node.parts.filter_map { |part| part.content if part.is_a?(Prism::StringNode) }.join)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# `Rails.root.join("lib_static")` and its `.to_s`, whose arguments are
|
|
78
|
-
# the path relative to the app root.
|
|
79
|
-
def collect_call_path(node)
|
|
80
|
-
return collect_paths(node.receiver) unless node.name == :join
|
|
81
|
-
return unless app_root?(node.receiver)
|
|
82
|
-
|
|
83
|
-
segments = Array(node.arguments&.arguments).filter_map { |argument| argument.unescaped if argument.is_a?(Prism::StringNode) }
|
|
84
|
-
push_path(File.join(*segments)) if segments.any?
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
APP_ROOT = /\A(?:(?:Rails|config)\.)?root\z/
|
|
88
|
-
|
|
89
|
-
def app_root?(node)
|
|
90
|
-
node = node.statements if node.is_a?(Prism::EmbeddedStatementsNode)
|
|
91
|
-
node = node.body.first if node.is_a?(Prism::StatementsNode)
|
|
92
|
-
node.is_a?(Prism::CallNode) && APP_ROOT.match?(node.slice.strip)
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def push_path(value)
|
|
96
|
-
cleaned = value.to_s.strip.delete_prefix("/")
|
|
97
|
-
@results << cleaned unless cleaned.empty?
|
|
98
|
-
end
|
|
99
56
|
end
|
|
100
57
|
end
|
|
101
58
|
end
|
|
@@ -21,22 +21,26 @@ module RailsAiContext
|
|
|
21
21
|
private
|
|
22
22
|
|
|
23
23
|
# A node's source with its comments removed, folded onto one line.
|
|
24
|
-
|
|
24
|
+
# `upto` ends the text at that offset (a call without its block).
|
|
25
|
+
def one_line_source(node, upto: nil)
|
|
25
26
|
start = node.location.start_offset
|
|
26
|
-
finish = node.location.end_offset
|
|
27
|
+
finish = upto || node.location.end_offset
|
|
27
28
|
cuts = Array(@comments).filter_map do |comment|
|
|
28
29
|
location = comment.location
|
|
29
30
|
[ location.start_offset, location.end_offset, "" ] if location.start_offset >= start && location.end_offset <= finish
|
|
30
31
|
end
|
|
31
|
-
cuts.concat(multiline_literals(node))
|
|
32
|
+
cuts.concat(multiline_literals(node).select { |_, to, _| to <= finish })
|
|
32
33
|
|
|
33
34
|
# The node's text runs on past its end when it opens a heredoc, and
|
|
34
35
|
# every cut sits inside the node, so the offsets still line up.
|
|
35
|
-
text
|
|
36
|
+
# Offsets count bytes, so the cuts work on the bytes and the text gets its encoding back after.
|
|
37
|
+
source = NodeSource.text(node)
|
|
38
|
+
text = source.b
|
|
39
|
+
text = text.byteslice(0, finish - start) if upto
|
|
36
40
|
cuts.sort_by(&:first).reverse_each do |from, to, replacement|
|
|
37
|
-
text[(from - start)...(to - start)] = replacement
|
|
41
|
+
text[(from - start)...(to - start)] = replacement.b
|
|
38
42
|
end
|
|
39
|
-
fold_newlines(text)
|
|
43
|
+
fold_newlines(text.force_encoding(source.encoding))
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
# A plain multi-line string literal (a SQL fragment) is respelt on one line as `inspect`
|
|
@@ -63,8 +67,15 @@ module RailsAiContext
|
|
|
63
67
|
# line to fold it onto.
|
|
64
68
|
return text.strip if tokens.any? { |token| token.type == :HEREDOC_START }
|
|
65
69
|
|
|
70
|
+
encoding = text.encoding
|
|
66
71
|
kinds = newline_kinds(tokens)
|
|
67
|
-
|
|
72
|
+
# Token offsets count bytes, so the fold walks the bytes; everything it compares is ASCII.
|
|
73
|
+
# Outside a heredoc a backslash ending a line only continues it, so it folds as a continuation.
|
|
74
|
+
text = text.b.gsub("\\\n") do
|
|
75
|
+
kinds[Regexp.last_match.begin(0) + 1] = :continuation
|
|
76
|
+
" \n"
|
|
77
|
+
end
|
|
78
|
+
folded = "".b
|
|
68
79
|
index = 0
|
|
69
80
|
while index < text.length
|
|
70
81
|
unless text[index] == "\n" && kinds.key?(index)
|
|
@@ -88,7 +99,7 @@ module RailsAiContext
|
|
|
88
99
|
end
|
|
89
100
|
folded << joiner
|
|
90
101
|
end
|
|
91
|
-
folded.strip.sub(/;\z/, "").strip
|
|
102
|
+
folded.strip.sub(/;\z/, "").strip.force_encoding(encoding)
|
|
92
103
|
end
|
|
93
104
|
|
|
94
105
|
# No statement begins with one of these, so a newline in front of one
|
|
@@ -194,9 +205,18 @@ module RailsAiContext
|
|
|
194
205
|
end
|
|
195
206
|
|
|
196
207
|
def proc_default?(node)
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
208
|
+
proc_argument?(default_node(node))
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# `-> {}`, `lambda {}`, `proc {}` or `Proc.new {}`.
|
|
212
|
+
def proc_argument?(arg)
|
|
213
|
+
return true if arg.is_a?(Prism::LambdaNode)
|
|
214
|
+
return false unless arg.is_a?(Prism::CallNode) && arg.block.is_a?(Prism::BlockNode)
|
|
215
|
+
|
|
216
|
+
receiver = arg.receiver
|
|
217
|
+
return %i[lambda proc].include?(arg.name) if receiver.nil?
|
|
218
|
+
|
|
219
|
+
arg.name == :new && (receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)) && constant_path_string(receiver) == "Proc"
|
|
200
220
|
end
|
|
201
221
|
|
|
202
222
|
def default_node(node)
|
|
@@ -207,6 +227,26 @@ module RailsAiContext
|
|
|
207
227
|
assoc&.value
|
|
208
228
|
end
|
|
209
229
|
|
|
230
|
+
# The source of each positional argument, or array element, that is no literal,
|
|
231
|
+
# since `arguments` gives a local variable's source and a string's value alike.
|
|
232
|
+
def computed_arguments(node)
|
|
233
|
+
positional_elements(node).select { |arg| extract_value(arg) == RailsAiContext::Confidence::INFERRED }.map { |arg| one_line_source(arg) }
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Argument as `arguments` gives it => the constant it is, or builds with `.new`.
|
|
237
|
+
def constant_arguments(node)
|
|
238
|
+
positional_elements(node).each_with_object({}) do |arg, found|
|
|
239
|
+
target = arg.is_a?(Prism::CallNode) && arg.name == :new ? arg.receiver : arg
|
|
240
|
+
next unless target.is_a?(Prism::ConstantReadNode) || target.is_a?(Prism::ConstantPathNode)
|
|
241
|
+
|
|
242
|
+
found[value_or_source(arg)] = constant_path_string(target)
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def positional_elements(node)
|
|
247
|
+
(node.arguments&.arguments || []).flat_map { |arg| arg.is_a?(Prism::ArrayNode) ? arg.elements : [ arg ] }
|
|
248
|
+
end
|
|
249
|
+
|
|
210
250
|
def value_or_source(node)
|
|
211
251
|
extract_value(node, source: true)
|
|
212
252
|
end
|
|
@@ -248,6 +288,7 @@ module RailsAiContext
|
|
|
248
288
|
when Prism::ArrayNode then node.elements.map { |e| extract_value(e, source: source) }
|
|
249
289
|
when Prism::HashNode then hash_node_to_hash(node, source: source)
|
|
250
290
|
when Prism::KeywordHashNode then hash_node_to_hash(node, source: source)
|
|
291
|
+
when Prism::CallNode then module_name(node)
|
|
251
292
|
else RailsAiContext::Confidence::INFERRED
|
|
252
293
|
end
|
|
253
294
|
|
|
@@ -256,6 +297,16 @@ module RailsAiContext
|
|
|
256
297
|
one_line_source(node)
|
|
257
298
|
end
|
|
258
299
|
|
|
300
|
+
# `Cron::CleanupJob.name` is the constant's own name; `APP_SETTINGS.name` holds a value, so it stays unknown.
|
|
301
|
+
def module_name(node)
|
|
302
|
+
receiver = node.receiver
|
|
303
|
+
constant = receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
|
|
304
|
+
return RailsAiContext::Confidence::INFERRED unless constant && node.name == :name && node.arguments.nil? && node.block.nil?
|
|
305
|
+
|
|
306
|
+
name = constant_path_string(receiver)
|
|
307
|
+
name.split("::").last.match?(/\A[A-Z0-9_]+\z/) ? RailsAiContext::Confidence::INFERRED : name
|
|
308
|
+
end
|
|
309
|
+
|
|
259
310
|
# `"a" "b"` and its line-continued form parse as one interpolated node
|
|
260
311
|
# with only literal parts. Anything with a real `#{}` stays inferred.
|
|
261
312
|
def adjacent_literals(node)
|
|
@@ -314,6 +365,21 @@ module RailsAiContext
|
|
|
314
365
|
node.slice.delete_prefix("::")
|
|
315
366
|
end
|
|
316
367
|
|
|
368
|
+
# `File` or `::File`, and a bare `__dir__`, as a path anchored at the walked file is written.
|
|
369
|
+
def file_constant?(node)
|
|
370
|
+
(node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)) && constant_path_string(node) == "File"
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def dir_call?(node)
|
|
374
|
+
node.is_a?(Prism::CallNode) && node.name == :__dir__ && node.receiver.nil? && node.arguments.nil?
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# A class or module's full name: `::A` is top level, `A` nests under the open `outer`.
|
|
378
|
+
def nested_name(constant_path, outer)
|
|
379
|
+
path = constant_path.slice
|
|
380
|
+
path.start_with?("::") || outer.nil? ? path.delete_prefix("::") : "#{outer}::#{path}"
|
|
381
|
+
end
|
|
382
|
+
|
|
317
383
|
def hash_node_to_hash(node, source: false)
|
|
318
384
|
elements = node.respond_to?(:elements) ? node.elements : []
|
|
319
385
|
elements.each_with_object({}) do |assoc, h|
|
|
@@ -322,9 +388,9 @@ module RailsAiContext
|
|
|
322
388
|
end
|
|
323
389
|
end
|
|
324
390
|
|
|
325
|
-
def confidence_for(node)
|
|
326
|
-
|
|
327
|
-
|
|
391
|
+
def confidence_for(node) = self.class.confidence_for(node)
|
|
392
|
+
|
|
393
|
+
def self.confidence_for(node) = RailsAiContext::Confidence.for_node(node)
|
|
328
394
|
end
|
|
329
395
|
end
|
|
330
396
|
end
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
module Introspectors
|
|
5
|
+
module Listeners
|
|
6
|
+
# The `if`/`unless`/`case` branches open around the node being visited, as
|
|
7
|
+
# text (`if Rails.env.development?`). A declaration under one runs only when
|
|
8
|
+
# it holds, which source cannot tell, so the record carries the condition.
|
|
9
|
+
# A listener that sets `@statements` counts only the branches registered there.
|
|
10
|
+
module BranchConditions
|
|
11
|
+
def on_if_node_enter(node)
|
|
12
|
+
enter_condition(node, "if", node.subsequent)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def on_unless_node_enter(node)
|
|
16
|
+
enter_condition(node, "unless", node.else_clause)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# `Rails.env.local? && get(...)` draws only when the left side holds.
|
|
20
|
+
def on_and_node_enter(node)
|
|
21
|
+
enter_guard(node, "if")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def on_or_node_enter(node)
|
|
25
|
+
enter_guard(node, "unless")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def on_and_node_leave(node)
|
|
29
|
+
on_if_node_leave(node)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def on_or_node_leave(node)
|
|
33
|
+
on_if_node_leave(node)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def on_else_node_enter(node)
|
|
37
|
+
current = branch_conditions.last
|
|
38
|
+
current[:negated] = true if current && current[:other].equal?(node)
|
|
39
|
+
on_when_node_enter(node)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def on_else_node_leave(node)
|
|
43
|
+
on_when_node_leave(node)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def on_case_node_enter(node)
|
|
47
|
+
return unless branch_statement?(node)
|
|
48
|
+
|
|
49
|
+
node.conditions.each do |branch|
|
|
50
|
+
register_statements(branch.statements)
|
|
51
|
+
case_branches[branch] = -> { case_text(node, branch.conditions, "is", "if") }
|
|
52
|
+
end
|
|
53
|
+
return unless node.else_clause
|
|
54
|
+
|
|
55
|
+
register_statements(node.else_clause.statements)
|
|
56
|
+
case_branches[node.else_clause] = -> { case_text(node, node.conditions.flat_map(&:conditions), "is none of", "unless") }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def on_when_node_enter(node)
|
|
60
|
+
text = case_branches[node]
|
|
61
|
+
branch_conditions << { node: node, text: text } if text
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def on_when_node_leave(node)
|
|
65
|
+
branch_conditions.pop if branch_conditions.last && branch_conditions.last[:node].equal?(node)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def on_if_node_leave(node)
|
|
69
|
+
branch_conditions.pop if branch_conditions.last && branch_conditions.last[:node].equal?(node)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def on_unless_node_leave(node)
|
|
73
|
+
on_if_node_leave(node)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def branch_conditions
|
|
79
|
+
@branch_conditions ||= []
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def case_branches
|
|
83
|
+
@case_branches ||= {}.compare_by_identity
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def branch_statement?(node)
|
|
87
|
+
@statements.nil? || @statements.key?(node)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def register_statements(statements)
|
|
91
|
+
return unless @statements
|
|
92
|
+
|
|
93
|
+
Array(statements&.body).each { |statement| @statements[statement] = true } if statements.is_a?(Prism::StatementsNode)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# ponytail: the guard covers both operands; a route on the left of `&&` would be mislabelled.
|
|
97
|
+
def enter_guard(node, keyword)
|
|
98
|
+
return unless branch_statement?(node)
|
|
99
|
+
|
|
100
|
+
@statements[node.right] = true if @statements
|
|
101
|
+
branch_conditions << { node: node, text: -> { "#{keyword} #{squish(node.left)}" } }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def enter_condition(node, keyword, other)
|
|
105
|
+
return unless branch_statement?(node)
|
|
106
|
+
|
|
107
|
+
register_statements(node.statements)
|
|
108
|
+
@statements[other] = true if @statements && other.is_a?(Prism::IfNode)
|
|
109
|
+
register_statements(other.statements) if other.is_a?(Prism::ElseNode)
|
|
110
|
+
# An elsif runs only when the condition before it failed.
|
|
111
|
+
outer = branch_conditions.last
|
|
112
|
+
outer[:negated] = true if outer && outer[:other].equal?(node)
|
|
113
|
+
branch_conditions << { node: node, other: other, keyword: keyword }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def current_condition
|
|
117
|
+
branch_conditions.map { |c| c[:text]&.call || condition_text(c) }.join(" and ").then { |text| text unless text.empty? }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# [chain offset, arm, arms] when every open branch is an arm of one if/unless chain that ends
|
|
121
|
+
# in else, so copies on every arm add up to no condition at all.
|
|
122
|
+
def chain_arm
|
|
123
|
+
stack = branch_conditions
|
|
124
|
+
return nil if stack.empty? || stack.any? { |c| c[:text] }
|
|
125
|
+
return nil unless stack.each_cons(2).all? { |outer, inner| outer[:other].equal?(inner[:node]) }
|
|
126
|
+
|
|
127
|
+
arms = 1
|
|
128
|
+
other = stack.first[:other]
|
|
129
|
+
while other.is_a?(Prism::IfNode)
|
|
130
|
+
arms += 1
|
|
131
|
+
other = other.subsequent
|
|
132
|
+
end
|
|
133
|
+
return nil unless other.is_a?(Prism::ElseNode)
|
|
134
|
+
|
|
135
|
+
[ stack.first[:node].location.start_offset, stack.size - (stack.last[:negated] ? 0 : 1), arms + 1 ]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Built only when a record asks, since most branches a walk passes hold nothing it records.
|
|
139
|
+
def condition_text(branch)
|
|
140
|
+
keyword = branch[:keyword]
|
|
141
|
+
keyword = keyword == "if" ? "unless" : "if" if branch[:negated]
|
|
142
|
+
"#{keyword} #{squish(branch[:node].predicate)}"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def case_text(node, conditions, relation, keyword)
|
|
146
|
+
texts = conditions.map { |c| squish(c) }
|
|
147
|
+
node.predicate ? "when #{squish(node.predicate)} #{relation} #{texts.join(', ')}" : "#{keyword} #{texts.join(" or ")}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def squish(node)
|
|
151
|
+
node.slice.gsub(/\s+/, " ")
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|