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
|
@@ -10,40 +10,81 @@ module RailsAiContext
|
|
|
10
10
|
# records so "does this route exist and which controller serves it" can
|
|
11
11
|
# be answered without booting the app. Constructs whose routes depend on
|
|
12
12
|
# runtime state (devise_for, draw, computed names) are recorded as
|
|
13
|
-
# :dynamic markers rather than guessed at. Leading-slash paths
|
|
14
|
-
#
|
|
15
|
-
# constraints are ignored.
|
|
13
|
+
# :dynamic markers rather than guessed at. Leading-slash paths anchor
|
|
14
|
+
# at the accumulated prefix.
|
|
16
15
|
class RoutesDslListener < BaseListener
|
|
17
|
-
|
|
16
|
+
include BranchConditions
|
|
17
|
+
|
|
18
|
+
VERB_METHODS = %i[get post put patch delete options].freeze
|
|
18
19
|
PLURAL_ACTIONS = %i[index create new edit show update destroy].freeze
|
|
19
20
|
# Rails' drawing order: the first route asking for a name gets it.
|
|
20
21
|
SINGULAR_ACTIONS = %i[new edit show update destroy create].freeze
|
|
21
22
|
RESTFUL_ACTIONS = %w[index show new create edit update destroy].freeze
|
|
22
|
-
DYNAMIC_MACROS = %i[devise_for draw
|
|
23
|
+
DYNAMIC_MACROS = %i[devise_for draw].freeze
|
|
24
|
+
# `direct` and `resolve` define URL helpers and draw no route.
|
|
25
|
+
URL_HELPER_MACROS = %i[direct resolve].freeze
|
|
26
|
+
# Calls that draw routes; an unknown block that makes none of them configures a gem macro.
|
|
27
|
+
ROUTE_METHODS = (VERB_METHODS + %i[match namespace scope resources resource member collection new shallow
|
|
28
|
+
concern concerns with_options controller mount root draw devise_for]).freeze
|
|
29
|
+
# Statement-level calls that draw nothing into the table.
|
|
30
|
+
NON_ROUTING = %i[require require_relative puts p pp print warn raise default_url_options extend include].freeze
|
|
23
31
|
|
|
24
|
-
# `scope` is the frames a `draw` of this file sits in, as its record carries them.
|
|
25
|
-
# `route_set` answers { prefix:, name_prefix: } for an app class that draws routes.
|
|
26
|
-
# `names` is the set of route names taken so far, shared by every file of one table.
|
|
27
32
|
# ponytail: an engine's namespace is its first segment; read isolate_namespace if one differs.
|
|
28
33
|
def self.engine_namespace(engine)
|
|
29
34
|
engine.split("::").first.underscore
|
|
30
35
|
end
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
# `scope` is the frames a `draw` of this file sits in, as its record carries them.
|
|
38
|
+
# `route_set` answers { prefix:, name_prefix: } for an app class that draws routes.
|
|
39
|
+
# `names` is the set of route names taken so far, shared by every file of one table.
|
|
40
|
+
# `multi_path`: the app's Rails draws every path of `get "/a", "/b"`; 8.1 raises instead.
|
|
41
|
+
def initialize(scope: [], route_set: nil, names: Set.new, multi_path: false)
|
|
33
42
|
super()
|
|
43
|
+
@multi_path = multi_path
|
|
34
44
|
@stack = scope.map { |frame| frame.merge(node: nil) }
|
|
35
45
|
@route_set = route_set
|
|
36
46
|
@concern_blocks = {}
|
|
37
47
|
@replaying = []
|
|
38
48
|
# Rails leaves a route unnamed when another route already took its name.
|
|
39
49
|
@taken_names = names
|
|
50
|
+
# The calls that stand as statements of a route body, as opposed to
|
|
51
|
+
# arguments or lambda bodies: only those can draw a route.
|
|
52
|
+
@statements = {}.compare_by_identity
|
|
53
|
+
@global_path_names = {}
|
|
54
|
+
@methods = {}
|
|
55
|
+
@routing_calls = 0
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def on_program_node_enter(node)
|
|
59
|
+
register_statements(node.statements)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Ruby runs a method's body where it is called, so it is replayed there.
|
|
63
|
+
def on_def_node_enter(node)
|
|
64
|
+
@methods[node.name] = node if @statements.key?(node) && node.receiver.nil?
|
|
65
|
+
push_frame(node, suppress: true)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def on_def_node_leave(node)
|
|
69
|
+
@stack.pop if @stack.last && @stack.last[:node].equal?(node)
|
|
40
70
|
end
|
|
41
71
|
|
|
42
72
|
def on_call_node_enter(node)
|
|
73
|
+
statement = @statements.key?(node)
|
|
74
|
+
register_statements(node.block.body) if statement && route_body?(node)
|
|
43
75
|
return enter_engine_draw(node) if engine_draw?(node)
|
|
44
76
|
return enter_route_set(node) if route_set_draw?(node)
|
|
77
|
+
# `ActiveAdmin.routes(self)` hands the mapper to code this walk cannot see.
|
|
78
|
+
return emit_dynamic(node) if node.receiver && statement && node.arguments&.arguments&.any?(Prism::SelfNode)
|
|
79
|
+
if after_initialize?(node)
|
|
80
|
+
register_table_changes(node.block.body)
|
|
81
|
+
return push_frame(node, late: true)
|
|
82
|
+
end
|
|
83
|
+
return enter_late_table_change(node) if late_table_change?(node, statement)
|
|
84
|
+
return push_frame(node, prepend: true) if node.receiver && node.name == :prepend && statement && route_body?(node)
|
|
45
85
|
return unless node.receiver.nil?
|
|
46
86
|
|
|
87
|
+
@routing_calls += 1 if ROUTE_METHODS.include?(node.name)
|
|
47
88
|
case node.name
|
|
48
89
|
when :namespace then enter_namespace(node)
|
|
49
90
|
when :scope then enter_scope(node)
|
|
@@ -51,21 +92,119 @@ module RailsAiContext
|
|
|
51
92
|
when :resource then handle_resources(node, singular: true)
|
|
52
93
|
when :member then enter_member_collection(node, :member)
|
|
53
94
|
when :collection then enter_member_collection(node, :collection)
|
|
95
|
+
when :new then enter_member_collection(node, :new)
|
|
96
|
+
when :shallow then push_frame(node, shallow: true) if node.block
|
|
54
97
|
when :concern then define_concern(node)
|
|
55
98
|
when :concerns then apply_concerns(node)
|
|
56
99
|
when :with_options then enter_with_options(node)
|
|
100
|
+
when :controller then enter_controller(node)
|
|
101
|
+
when :resources_path_names then @global_path_names.merge!(path_names_option(path_names: own_options(node)))
|
|
102
|
+
when *URL_HELPER_MACROS then push_frame(node, suppress: true) if node.block
|
|
103
|
+
when :mount then emit_dynamic(node) unless mounted_app?(node)
|
|
57
104
|
when :root then emit_root(node)
|
|
58
105
|
when *VERB_METHODS, :match then emit_verb_route(node) unless rack_app_target?(node)
|
|
59
106
|
when *DYNAMIC_MACROS then emit_dynamic(node) unless rack_app_target?(node)
|
|
107
|
+
else
|
|
108
|
+
if statement && node.block
|
|
109
|
+
# An empty block (a commented-out `constraints do`) configures nothing.
|
|
110
|
+
push_frame(node, unknown_block: @routing_calls, pending: []) if node.block.body
|
|
111
|
+
elsif statement && !NON_ROUTING.include?(node.name)
|
|
112
|
+
call_method_or_count(node)
|
|
113
|
+
end
|
|
60
114
|
end
|
|
61
115
|
end
|
|
62
116
|
|
|
117
|
+
# An unknown block that draws no route is a gem macro (`use_doorkeeper do`), counted once.
|
|
63
118
|
def on_call_node_leave(node)
|
|
64
|
-
|
|
119
|
+
return unless @stack.last && @stack.last[:node].equal?(node)
|
|
120
|
+
|
|
121
|
+
frame = @stack.pop
|
|
122
|
+
return unless frame[:unknown_block]
|
|
123
|
+
return frame[:pending].each { |call| emit_dynamic(call) } if frame[:unknown_block] != @routing_calls
|
|
124
|
+
|
|
125
|
+
outer = open_unknown_block
|
|
126
|
+
outer ? outer[:pending] << node : emit_dynamic(node)
|
|
65
127
|
end
|
|
66
128
|
|
|
67
129
|
private
|
|
68
130
|
|
|
131
|
+
# A receiver's block (`Sidekiq::Web.use ... do`) is plain Ruby; only a draw holds routes.
|
|
132
|
+
def route_body?(node)
|
|
133
|
+
node.block.is_a?(Prism::BlockNode) && (node.receiver.nil? || %i[draw append prepend].include?(node.name))
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def call_method_or_count(node)
|
|
137
|
+
definition = @methods[node.name]
|
|
138
|
+
key = "def #{node.name}"
|
|
139
|
+
# A gem macro's configuration until its block turns out to draw routes.
|
|
140
|
+
outer = open_unknown_block if definition.nil?
|
|
141
|
+
return (outer[:pending] << node unless suppressed?) if outer
|
|
142
|
+
return emit_dynamic(node) if definition.nil? || node.arguments || @replaying.include?(key) || takes_arguments?(definition)
|
|
143
|
+
return if suppressed?
|
|
144
|
+
|
|
145
|
+
@replaying.push(key)
|
|
146
|
+
begin
|
|
147
|
+
register_statements(definition.body)
|
|
148
|
+
replay_dispatcher.dispatch(definition.body) if definition.body
|
|
149
|
+
ensure
|
|
150
|
+
@replaying.pop
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Rails runs after_initialize hooks before it draws the routes (7.0 to 8.1).
|
|
155
|
+
def after_initialize?(node)
|
|
156
|
+
return false unless node.block.is_a?(Prism::BlockNode)
|
|
157
|
+
|
|
158
|
+
hook = node.arguments&.arguments&.first
|
|
159
|
+
node.name == :after_initialize || (node.name == :on_load && hook.is_a?(Prism::SymbolNode) && hook.unescaped == "after_initialize")
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def register_table_changes(body)
|
|
163
|
+
return unless body.is_a?(Prism::StatementsNode)
|
|
164
|
+
|
|
165
|
+
body.body.each { |statement| @statements[statement] = true if late_table_change?(statement, false) }
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# A `routes.prepend` inside any other hook may register after the draw, which
|
|
169
|
+
# Rails evaluates only on the next reload.
|
|
170
|
+
def late_table_change?(node, statement)
|
|
171
|
+
!statement && node.is_a?(Prism::CallNode) && %i[append prepend].include?(node.name) && node.block &&
|
|
172
|
+
node.receiver.is_a?(Prism::CallNode) && node.receiver.name == :routes
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def enter_late_table_change(node)
|
|
176
|
+
emit_dynamic(node)
|
|
177
|
+
push_frame(node, suppress: true)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def open_unknown_block
|
|
181
|
+
@stack.reverse.find { |f| f[:unknown_block] }
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def takes_arguments?(definition)
|
|
185
|
+
params = definition.parameters
|
|
186
|
+
!params.nil? && (params.requireds.any? || params.posts.any? || params.keywords.any? { |k| k.is_a?(Prism::RequiredKeywordParameterNode) })
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# `controller :pages do` is `scope(controller: :pages)`.
|
|
190
|
+
def enter_controller(node)
|
|
191
|
+
return unless node.block
|
|
192
|
+
|
|
193
|
+
name = literal_first_arg(node)
|
|
194
|
+
return push_frame(node, controller: name.to_s) if name
|
|
195
|
+
|
|
196
|
+
emit_dynamic(node)
|
|
197
|
+
push_frame(node, suppress: true)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# A mount MountListener names. A lambda or a variable is an app no walk
|
|
201
|
+
# can name, and the booted tier counts a lambda as a dynamic construct.
|
|
202
|
+
def mounted_app?(node)
|
|
203
|
+
first = node.arguments&.arguments&.first
|
|
204
|
+
first = first.elements.first&.key if first.is_a?(Prism::KeywordHashNode) || first.is_a?(Prism::HashNode)
|
|
205
|
+
!first.nil? && !app_name(first).nil?
|
|
206
|
+
end
|
|
207
|
+
|
|
69
208
|
# `Spree::Core::Engine.routes.draw` adds to the engine's table, controllers under its
|
|
70
209
|
# namespace (spree/admin/orders); the app's own `X::Application.routes.draw` is no engine.
|
|
71
210
|
def engine_draw?(node)
|
|
@@ -133,7 +272,131 @@ module RailsAiContext
|
|
|
133
272
|
end
|
|
134
273
|
|
|
135
274
|
def push_frame(node, **attrs)
|
|
136
|
-
|
|
275
|
+
frame = { node: node }
|
|
276
|
+
constraints = node_constraints(node) if node.is_a?(Prism::CallNode)
|
|
277
|
+
frame[:route_constraints] = constraints if constraints
|
|
278
|
+
@stack << frame.merge(attrs)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Mapper's URL options, which a hash constraint turns into route defaults.
|
|
282
|
+
URL_OPTIONS = %w[protocol subdomain domain host port].freeze
|
|
283
|
+
ROUTE_LEVEL = (VERB_METHODS + %i[match root]).freeze
|
|
284
|
+
# Calls whose options Rails passes on to the mapping, where one it does not know becomes a default.
|
|
285
|
+
OPTION_CARRIERS = (ROUTE_LEVEL + %i[scope namespace resources resource with_options]).freeze
|
|
286
|
+
MAPPER_OPTIONS = %w[as via to controller action on defaults constraints anchor format path internal shallow_path
|
|
287
|
+
shallow_prefix module path_names shallow blocks options only except param concerns].freeze
|
|
288
|
+
|
|
289
|
+
# `bin/rails routes`' defaults then segment constraints; scope options win, as Mapping merges them.
|
|
290
|
+
def route_constraints(node, path)
|
|
291
|
+
route_level = ROUTE_LEVEL.include?(node.name)
|
|
292
|
+
scopes = @stack.reject { |f| f[:node].equal?(node) }.filter_map { |f| f[:route_constraints] }
|
|
293
|
+
own = node_constraints(node) || {}
|
|
294
|
+
scopes << own unless route_level || own.empty?
|
|
295
|
+
own = {} unless route_level
|
|
296
|
+
defaults = (own[:url] || {}).merge(scopes.map { |c| c[:url].merge(c[:defaults]) }.reduce({}, :merge)).merge(own[:defaults] || {})
|
|
297
|
+
defaults = defaults.merge(scopes.map { |c| c[:options] }.reduce({}, :merge)).merge(own[:options] || {})
|
|
298
|
+
params = path.scan(/[:*](\w+)/).flatten << "format"
|
|
299
|
+
segments = scopes.map { |c| c[:segment] }.reduce({}, :merge).merge(own[:segment] || {}).select { |key, value| value && params.include?(key) }
|
|
300
|
+
all = defaults.merge(segments)
|
|
301
|
+
"{#{all.map { |key, value| "#{key}: #{value}" }.join(', ')}}" if all.any?
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Literal constraints and defaults a call carries, rendered as Ruby inspects them.
|
|
305
|
+
def node_constraints(node)
|
|
306
|
+
hash = hash_arg(node)
|
|
307
|
+
return unless hash
|
|
308
|
+
|
|
309
|
+
# `scoped` is what Rails keeps in the scope's constraints, which nested resources read.
|
|
310
|
+
found = { url: {}, defaults: {}, segment: {}, scoped: {}, options: {} }
|
|
311
|
+
if node.name == :constraints && node.receiver.nil?
|
|
312
|
+
read_constraint_hash(hash, found)
|
|
313
|
+
elsif node.name == :defaults && node.receiver.nil?
|
|
314
|
+
each_literal(hash) { |k, v| found[:defaults][k] = constraint_value(v) }
|
|
315
|
+
else
|
|
316
|
+
hash.elements.each do |assoc|
|
|
317
|
+
key = assoc_key(assoc)
|
|
318
|
+
next unless key
|
|
319
|
+
|
|
320
|
+
case key
|
|
321
|
+
when "constraints" then read_constraint_hash(assoc.value, found)
|
|
322
|
+
when "defaults" then each_literal(assoc.value) { |k, v| found[:defaults][k] = constraint_value(v) }
|
|
323
|
+
when "format" then read_format(assoc.value, found) if OPTION_CARRIERS.include?(node.name)
|
|
324
|
+
else
|
|
325
|
+
unless assoc.value.is_a?(Prism::RegularExpressionNode)
|
|
326
|
+
found[:options][key] = constraint_value(assoc.value) if OPTION_CARRIERS.include?(node.name) && !MAPPER_OPTIONS.include?(key)
|
|
327
|
+
next
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
found[:segment][key] = constraint_value(assoc.value)
|
|
331
|
+
# A resource moves its regexp options into its constraints.
|
|
332
|
+
found[:scoped][key] = assoc.value if %i[resources resource].include?(node.name)
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
found if found.values.any?(&:any?)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def read_constraint_hash(hash, found)
|
|
340
|
+
each_literal(hash) do |key, value|
|
|
341
|
+
if URL_OPTIONS.include?(key)
|
|
342
|
+
found[:url][key] = constraint_value(value) if value.is_a?(Prism::StringNode) || value.is_a?(Prism::IntegerNode)
|
|
343
|
+
else
|
|
344
|
+
found[:segment][key] = constraint_value(value)
|
|
345
|
+
found[:scoped][key] = value
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
# Mapping#normalize_format: `true` requires a format, a string also defaults it, `false` clears a scope's.
|
|
351
|
+
def read_format(value, found)
|
|
352
|
+
case value
|
|
353
|
+
when Prism::TrueNode then found[:segment]["format"] = "/.+/"
|
|
354
|
+
when Prism::FalseNode then found[:segment]["format"] = nil
|
|
355
|
+
when Prism::RegularExpressionNode then found[:segment]["format"] = constraint_value(value)
|
|
356
|
+
when Prism::StringNode
|
|
357
|
+
found[:defaults]["format"] = constraint_value(value)
|
|
358
|
+
found[:segment]["format"] = Regexp.new(value.unescaped).inspect
|
|
359
|
+
end
|
|
360
|
+
rescue RegexpError
|
|
361
|
+
nil
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# The innermost `format:` a route or its scopes give; `true` makes the segment required.
|
|
365
|
+
def required_format?(node)
|
|
366
|
+
carriers = [ node, *@stack.reverse_each.map { |f| f[:node] } ].select do |n|
|
|
367
|
+
n.is_a?(Prism::CallNode) && n.receiver.nil? && OPTION_CARRIERS.include?(n.name)
|
|
368
|
+
end
|
|
369
|
+
carrier = carriers.find { |n| own_options(n).key?(:format) }
|
|
370
|
+
carrier && own_options(carrier)[:format] == true
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def hash_arg(node)
|
|
374
|
+
(node.arguments&.arguments || []).find { |a| a.is_a?(Prism::KeywordHashNode) || a.is_a?(Prism::HashNode) }
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def each_literal(hash)
|
|
378
|
+
return unless hash.is_a?(Prism::KeywordHashNode) || hash.is_a?(Prism::HashNode)
|
|
379
|
+
|
|
380
|
+
hash.elements.each do |assoc|
|
|
381
|
+
key = assoc_key(assoc)
|
|
382
|
+
yield key, assoc.value if key
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def assoc_key(assoc)
|
|
387
|
+
assoc.key.unescaped if assoc.is_a?(Prism::AssocNode) && assoc.key.is_a?(Prism::SymbolNode)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def constraint_value(node)
|
|
391
|
+
case node
|
|
392
|
+
when Prism::StringNode, Prism::SymbolNode then (node.is_a?(Prism::SymbolNode) ? node.unescaped.to_sym : node.unescaped).inspect
|
|
393
|
+
when Prism::RegularExpressionNode
|
|
394
|
+
flags = (node.ignore_case? ? Regexp::IGNORECASE : 0) | (node.extended? ? Regexp::EXTENDED : 0) | (node.multi_line? ? Regexp::MULTILINE : 0)
|
|
395
|
+
Regexp.new(node.unescaped, flags).inspect
|
|
396
|
+
else node.slice
|
|
397
|
+
end
|
|
398
|
+
rescue RegexpError
|
|
399
|
+
node.slice
|
|
137
400
|
end
|
|
138
401
|
|
|
139
402
|
def suppressed?
|
|
@@ -182,6 +445,7 @@ module RailsAiContext
|
|
|
182
445
|
|
|
183
446
|
@replaying.push(key)
|
|
184
447
|
begin
|
|
448
|
+
register_statements(block.body)
|
|
185
449
|
replay_dispatcher.dispatch(block)
|
|
186
450
|
ensure
|
|
187
451
|
@replaying.pop
|
|
@@ -230,10 +494,14 @@ module RailsAiContext
|
|
|
230
494
|
return unless node.block
|
|
231
495
|
|
|
232
496
|
opts = route_options(node)
|
|
497
|
+
path = (opts[:path] || name).to_s
|
|
498
|
+
as = (opts[:as] || name).to_s
|
|
233
499
|
push_frame(node,
|
|
234
|
-
prefix: join_path(current_prefix,
|
|
500
|
+
prefix: join_path(current_prefix, path),
|
|
235
501
|
mod: (opts[:module] || name).to_s,
|
|
236
|
-
name_prefix:
|
|
502
|
+
name_prefix: as,
|
|
503
|
+
shallow_path: join_path(current_shallow_path, (literal_option(opts[:shallow_path]) || path).to_s),
|
|
504
|
+
shallow_prefix: join_names(current_shallow_prefix, (literal_option(opts[:shallow_prefix]) || as).to_s))
|
|
237
505
|
end
|
|
238
506
|
|
|
239
507
|
def enter_scope(node)
|
|
@@ -242,12 +510,18 @@ module RailsAiContext
|
|
|
242
510
|
opts = route_options(node)
|
|
243
511
|
first = literal_first_arg(node)
|
|
244
512
|
path = (first || opts[:path])&.to_s
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
513
|
+
shallow_path = (literal_option(opts[:shallow_path]) || path)&.to_s
|
|
514
|
+
shallow_prefix = (literal_option(opts[:shallow_prefix]) || literal_option(opts[:as]))&.to_s
|
|
515
|
+
frame = { prefix: path ? join_path(current_prefix, path) : current_prefix,
|
|
516
|
+
mod: opts[:module]&.to_s,
|
|
517
|
+
name_prefix: opts[:as]&.to_s,
|
|
518
|
+
controller: opts[:controller]&.to_s,
|
|
519
|
+
via: opts[:via],
|
|
520
|
+
path_names: path_names_option(opts) }
|
|
521
|
+
frame[:shallow_path] = join_path(current_shallow_path, shallow_path) if shallow_path
|
|
522
|
+
frame[:shallow_prefix] = join_names(current_shallow_prefix, shallow_prefix) if shallow_prefix
|
|
523
|
+
frame[:shallow] = opts[:shallow] == true if opts.key?(:shallow)
|
|
524
|
+
push_frame(node, **frame)
|
|
251
525
|
end
|
|
252
526
|
|
|
253
527
|
def handle_resources(node, singular:)
|
|
@@ -256,7 +530,7 @@ module RailsAiContext
|
|
|
256
530
|
return emit_dynamic(node) if current_route_set
|
|
257
531
|
|
|
258
532
|
names = extract_symbol_args(node)
|
|
259
|
-
if names.empty?
|
|
533
|
+
if names.empty? || opaque_options?(node)
|
|
260
534
|
emit_dynamic(node)
|
|
261
535
|
# Same reasoning as the namespace case: a block we can't attach
|
|
262
536
|
# resource info to still opens a nested scope, so its children
|
|
@@ -289,49 +563,144 @@ module RailsAiContext
|
|
|
289
563
|
|
|
290
564
|
# Routes nested under this resource inherit its singular route key as a
|
|
291
565
|
# name prefix (resources :posts { resources :comments } -> the comments
|
|
292
|
-
# index route is named "post_comments", not "comments").
|
|
566
|
+
# index route is named "post_comments", not "comments"). A shallow
|
|
567
|
+
# resource nests its children under the shallow path and prefix alone.
|
|
293
568
|
def push_resource_frame(node, name, opts, singular:)
|
|
294
|
-
|
|
295
|
-
|
|
569
|
+
layout = resource_layout(name, opts, singular: singular)
|
|
570
|
+
frame = {
|
|
571
|
+
prefix: layout[:nested_prefix],
|
|
572
|
+
mod: opts[:module]&.to_s,
|
|
573
|
+
name_prefix: layout[:shallow] ? join_names(current_shallow_prefix, layout[:key]) : layout[:key],
|
|
574
|
+
name_root: layout[:shallow],
|
|
575
|
+
path_names: path_names_option(opts),
|
|
576
|
+
resource: {
|
|
577
|
+
name: name,
|
|
578
|
+
singular: singular,
|
|
579
|
+
controller: resource_controller(name, opts, singular: singular),
|
|
580
|
+
base: layout[:base],
|
|
581
|
+
member_path: layout[:member_path],
|
|
582
|
+
new_path: layout[:new_path],
|
|
583
|
+
singular_route_name: layout[:member_name],
|
|
584
|
+
new_route_name: layout[:singular_name],
|
|
585
|
+
plural_route_name: layout[:plural_name]
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
frame[:shallow] = opts[:shallow] == true if opts.key?(:shallow)
|
|
589
|
+
push_frame(node, **frame)
|
|
590
|
+
nest_param_constraint(layout, opts)
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
# Rails' nested_options: a regexp constraint on the resource's param
|
|
594
|
+
# also constrains the param its nested routes name it by.
|
|
595
|
+
def nest_param_constraint(layout, opts)
|
|
296
596
|
param = resource_param(opts)
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
base: base,
|
|
306
|
-
member_path: member_path(base, singular, param),
|
|
307
|
-
singular_route_name: route_name_for(key),
|
|
308
|
-
plural_route_name: route_name_for(collection_route_key(name, opts, singular: singular))
|
|
309
|
-
})
|
|
597
|
+
value = @stack.filter_map { |f| f[:route_constraints]&.dig(:scoped) }.reduce({}, :merge)[param]
|
|
598
|
+
return unless value.is_a?(Prism::RegularExpressionNode)
|
|
599
|
+
|
|
600
|
+
key = "#{layout[:key]}_#{param}"
|
|
601
|
+
frame = @stack.last
|
|
602
|
+
own = frame[:route_constraints] || { url: {}, defaults: {}, segment: {}, scoped: {}, options: {} }
|
|
603
|
+
frame[:route_constraints] = own.merge(segment: own[:segment].merge(key => constraint_value(value)),
|
|
604
|
+
scoped: own[:scoped].merge(key => value))
|
|
310
605
|
end
|
|
311
606
|
|
|
312
607
|
def emit_resource_routes(node, name, opts, singular:)
|
|
313
|
-
|
|
608
|
+
layout = resource_layout(name, opts, singular: singular)
|
|
609
|
+
base = layout[:base]
|
|
610
|
+
member = layout[:member_path]
|
|
611
|
+
member_name = layout[:member_name]
|
|
314
612
|
controller = resource_controller(name, opts, singular: singular)
|
|
315
613
|
actions = requested_actions(singular ? SINGULAR_ACTIONS : PLURAL_ACTIONS, opts)
|
|
316
|
-
param = resource_param(opts)
|
|
317
|
-
plural_name = route_name_for(collection_route_key(name, opts, singular: singular))
|
|
318
|
-
singular_name = route_name_for(singular_route_key(name, opts, singular: singular))
|
|
319
614
|
|
|
320
615
|
actions.each do |action|
|
|
321
616
|
case action
|
|
322
|
-
when :index then emit(node, "GET", base, controller, "index", plural_name)
|
|
323
|
-
when :create then emit(node, "POST", base, controller, "create", singular ? singular_name : plural_name)
|
|
324
|
-
when :new then emit(node, "GET",
|
|
325
|
-
when :edit then emit(node, "GET", edit_path
|
|
326
|
-
when :show then emit(node, "GET",
|
|
617
|
+
when :index then emit(node, "GET", base, controller, "index", layout[:plural_name])
|
|
618
|
+
when :create then emit(node, "POST", base, controller, "create", singular ? layout[:singular_name] : layout[:plural_name])
|
|
619
|
+
when :new then emit(node, "GET", layout[:new_path], controller, "new", "new_#{layout[:singular_name]}")
|
|
620
|
+
when :edit then emit(node, "GET", layout[:edit_path], controller, "edit", "edit_#{member_name}")
|
|
621
|
+
when :show then emit(node, "GET", member, controller, "show", member_name)
|
|
327
622
|
when :update
|
|
328
|
-
emit(node, "PATCH",
|
|
329
|
-
emit(node, "PUT",
|
|
330
|
-
when :destroy then emit(node, "DELETE",
|
|
623
|
+
emit(node, "PATCH", member, controller, "update", member_name)
|
|
624
|
+
emit(node, "PUT", member, controller, "update", member_name)
|
|
625
|
+
when :destroy then emit(node, "DELETE", member, controller, "destroy", member_name)
|
|
331
626
|
end
|
|
332
627
|
end
|
|
333
628
|
end
|
|
334
629
|
|
|
630
|
+
# Shallow member routes drop the parents' paths and names; a singleton is never shallow.
|
|
631
|
+
def resource_layout(name, opts, singular:)
|
|
632
|
+
path = (opts[:path] || name).to_s
|
|
633
|
+
base = join_path(current_prefix, path)
|
|
634
|
+
key = singular_route_key(name, opts, singular: singular)
|
|
635
|
+
param = resource_param(opts)
|
|
636
|
+
path_names = current_path_names.merge(path_names_option(opts))
|
|
637
|
+
shallow = !singular && (opts.key?(:shallow) ? opts[:shallow] == true : current_shallow)
|
|
638
|
+
shallow_base = join_path(current_shallow_path, path)
|
|
639
|
+
member = singular ? base : "#{shallow ? shallow_base : base}/:#{param}"
|
|
640
|
+
{
|
|
641
|
+
base: base,
|
|
642
|
+
key: key,
|
|
643
|
+
shallow: shallow,
|
|
644
|
+
member_path: member,
|
|
645
|
+
edit_path: join_path(member, (path_names[:edit] || "edit").to_s),
|
|
646
|
+
new_path: join_path(base, (path_names[:new] || "new").to_s),
|
|
647
|
+
nested_prefix: singular ? base : "#{shallow ? shallow_base : base}/:#{key}_#{param}",
|
|
648
|
+
singular_name: route_name_for(key),
|
|
649
|
+
member_name: shallow ? join_names(current_shallow_prefix, key) : route_name_for(key),
|
|
650
|
+
plural_name: route_name_for(collection_route_key(name, opts, singular: singular))
|
|
651
|
+
}
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
def current_shallow
|
|
655
|
+
@stack.reverse.find { |f| f.key?(:shallow) }&.dig(:shallow) == true
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
def current_shallow_path
|
|
659
|
+
@stack.reverse.find { |f| f[:shallow_path] }&.dig(:shallow_path) || "/"
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
def current_shallow_prefix
|
|
663
|
+
@stack.reverse.find { |f| f[:shallow_prefix] }&.dig(:shallow_prefix)
|
|
664
|
+
end
|
|
665
|
+
|
|
666
|
+
def current_path_names
|
|
667
|
+
@stack.filter_map { |f| f[:path_names] }.reduce(@global_path_names, :merge)
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
def path_names_option(opts)
|
|
671
|
+
names = opts[:path_names]
|
|
672
|
+
names.is_a?(Hash) ? names.transform_keys(&:to_sym) : {}
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
# A value the source spells out; an expression reads as INFERRED and adds nothing.
|
|
676
|
+
def literal_option(value)
|
|
677
|
+
value if (value.is_a?(String) || value.is_a?(Symbol)) && value != RailsAiContext::Confidence::INFERRED
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
def join_names(*parts)
|
|
681
|
+
joined = parts.compact.map(&:to_s).reject(&:empty?).join("_")
|
|
682
|
+
joined unless joined.empty?
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
# Options the source does not spell out: `resources :x, opts`, `**splat`, a computed only:/except:.
|
|
686
|
+
def opaque_options?(node)
|
|
687
|
+
(node.arguments&.arguments || []).any? do |arg|
|
|
688
|
+
case arg
|
|
689
|
+
when Prism::SymbolNode, Prism::StringNode then false
|
|
690
|
+
when Prism::KeywordHashNode, Prism::HashNode
|
|
691
|
+
arg.elements.any? { |assoc| !assoc.is_a?(Prism::AssocNode) || opaque_action_list?(assoc) }
|
|
692
|
+
else true
|
|
693
|
+
end
|
|
694
|
+
end
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
def opaque_action_list?(assoc)
|
|
698
|
+
return false unless %i[only except].include?(extract_key(assoc.key))
|
|
699
|
+
|
|
700
|
+
values = assoc.value.is_a?(Prism::ArrayNode) ? assoc.value.elements : [ assoc.value ]
|
|
701
|
+
!values.all? { |v| v.is_a?(Prism::SymbolNode) || v.is_a?(Prism::StringNode) }
|
|
702
|
+
end
|
|
703
|
+
|
|
335
704
|
# `as:` renames the route helpers and the nested param, and leaves the
|
|
336
705
|
# path and the controller on the resource's own name.
|
|
337
706
|
def route_key(name, opts)
|
|
@@ -360,26 +729,39 @@ module RailsAiContext
|
|
|
360
729
|
(opts[:param] || "id").to_s
|
|
361
730
|
end
|
|
362
731
|
|
|
363
|
-
def member_path(base, singular, param)
|
|
364
|
-
singular ? base : "#{base}/:#{param}"
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
def edit_path(base, singular, param)
|
|
368
|
-
singular ? "#{base}/edit" : "#{base}/:#{param}/edit"
|
|
369
|
-
end
|
|
370
|
-
|
|
371
732
|
def emit_verb_route(node)
|
|
372
733
|
return if suppressed?
|
|
373
734
|
|
|
374
735
|
opts = route_options(node)
|
|
375
736
|
via = opts.key?(:via) ? opts[:via] : @stack.reverse.find { |f| f[:via] }&.dig(:via)
|
|
376
737
|
verb = node.name == :match ? match_verb(via) : node.name.to_s.upcase
|
|
377
|
-
return emit_dynamic(node)
|
|
738
|
+
return emit_dynamic(node) if verb.nil? || opaque_options?(node)
|
|
378
739
|
|
|
379
|
-
segment = literal_first_arg(node)&.to_s
|
|
380
740
|
rocket_key = opts.keys.find { |k| k.is_a?(String) }
|
|
381
|
-
|
|
382
|
-
|
|
741
|
+
return emit_dynamic(node) if !@multi_path && (node.arguments&.arguments || []).count { |a| !a.is_a?(Prism::KeywordHashNode) && !a.is_a?(Prism::HashNode) } > 1
|
|
742
|
+
|
|
743
|
+
paths = literal_paths(node)
|
|
744
|
+
paths = [ [ rocket_key, false ] ] if paths.empty? && rocket_key
|
|
745
|
+
return emit_dynamic(node) if paths.empty?
|
|
746
|
+
|
|
747
|
+
paths.each { |segment, action| emit_verb_path(node, verb, segment, action, opts, rocket_key) }
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
# Rails 7.x and 8.0 draw every path of `get "/a", "/b"`, strings before symbols.
|
|
751
|
+
# @return [Array<[String, Boolean]>] each path, and whether it names an action
|
|
752
|
+
def literal_paths(node)
|
|
753
|
+
args = (node.arguments&.arguments || []).select { |a| a.is_a?(Prism::StringNode) || a.is_a?(Prism::SymbolNode) }
|
|
754
|
+
args.sort_by { |a| a.is_a?(Prism::StringNode) ? 0 : 1 }.map { |a| [ a.unescaped, a.is_a?(Prism::SymbolNode) ] }
|
|
755
|
+
end
|
|
756
|
+
|
|
757
|
+
def emit_verb_path(node, verb, segment, action_given, opts, rocket_key)
|
|
758
|
+
# Rails' match reads `path => :action` as the action and `path => "controller"` as the controller.
|
|
759
|
+
rocket = rocket_key && opts[rocket_key]
|
|
760
|
+
if rocket.is_a?(Symbol) || (rocket.is_a?(String) && !rocket.include?("#"))
|
|
761
|
+
opts = opts.except(rocket_key)
|
|
762
|
+
opts[rocket.is_a?(Symbol) ? :action : :controller] ||= rocket
|
|
763
|
+
rocket_key = nil
|
|
764
|
+
end
|
|
383
765
|
|
|
384
766
|
target_given = opts.key?(:to) || !rocket_key.nil?
|
|
385
767
|
target = opts[:to] || (rocket_key && opts[rocket_key])
|
|
@@ -396,7 +778,9 @@ module RailsAiContext
|
|
|
396
778
|
return emit_dynamic(node) unless controller && action
|
|
397
779
|
|
|
398
780
|
name = route_set ? as && verb_route_name(as, segment, opts[:on]) : verb_route_name(opts[:as], segment, opts[:on])
|
|
399
|
-
|
|
781
|
+
# Rails maps an action given as a symbol through path_names; a string is the path.
|
|
782
|
+
path = action_given ? (current_path_names[segment.to_sym] || segment).to_s : segment
|
|
783
|
+
emit(node, verb, verb_route_path(path, opts[:on]), controller, action, name)
|
|
400
784
|
end
|
|
401
785
|
|
|
402
786
|
# Rails draws one route answering every verb in `via:` ("GET|POST"), and
|
|
@@ -418,7 +802,7 @@ module RailsAiContext
|
|
|
418
802
|
resource = current_resource
|
|
419
803
|
return unless resource
|
|
420
804
|
|
|
421
|
-
prefix =
|
|
805
|
+
prefix = { member: resource[:member_path], new: resource[:new_path] }.fetch(kind, resource[:base])
|
|
422
806
|
push_frame(node, prefix: prefix, kind: kind)
|
|
423
807
|
end
|
|
424
808
|
|
|
@@ -434,7 +818,7 @@ module RailsAiContext
|
|
|
434
818
|
def verb_route_path(segment, on_option)
|
|
435
819
|
resource = current_resource
|
|
436
820
|
if on_option && resource
|
|
437
|
-
base =
|
|
821
|
+
base = { member: resource[:member_path], new: resource[:new_path] }.fetch(on_option.to_sym, resource[:base])
|
|
438
822
|
return join_path(base, segment)
|
|
439
823
|
end
|
|
440
824
|
|
|
@@ -484,6 +868,7 @@ module RailsAiContext
|
|
|
484
868
|
end
|
|
485
869
|
|
|
486
870
|
def emit(node, verb, path, controller, action, name)
|
|
871
|
+
path = "#{path}.:format" if required_format?(node)
|
|
487
872
|
record = {
|
|
488
873
|
type: :route,
|
|
489
874
|
verb: verb,
|
|
@@ -497,6 +882,12 @@ module RailsAiContext
|
|
|
497
882
|
# An engine's table keeps its own names.
|
|
498
883
|
record[:name] = name if name && !name.empty? && @taken_names.add?([ engine, name ])
|
|
499
884
|
record[:engine] = engine if engine
|
|
885
|
+
condition = current_condition
|
|
886
|
+
record[:condition] = condition if condition
|
|
887
|
+
record[:prepend] = true if @stack.any? { |f| f[:prepend] }
|
|
888
|
+
record[:late] = true if @stack.any? { |f| f[:late] }
|
|
889
|
+
constraints = route_constraints(node, path)
|
|
890
|
+
record[:constraints] = constraints if constraints
|
|
500
891
|
params = path.scan(/:(\w+)/).flatten
|
|
501
892
|
record[:params] = params if params.any?
|
|
502
893
|
record[:restful] = RESTFUL_ACTIONS.include?(record[:action])
|
|
@@ -587,7 +978,11 @@ module RailsAiContext
|
|
|
587
978
|
end
|
|
588
979
|
|
|
589
980
|
def current_name_prefix
|
|
590
|
-
parts =
|
|
981
|
+
parts = []
|
|
982
|
+
@stack.reverse_each do |frame|
|
|
983
|
+
parts.unshift(frame[:name_prefix]) if frame[:name_prefix]
|
|
984
|
+
break if frame[:name_root]
|
|
985
|
+
end
|
|
591
986
|
parts.empty? ? nil : parts.join("_")
|
|
592
987
|
end
|
|
593
988
|
|
|
@@ -608,7 +1003,8 @@ module RailsAiContext
|
|
|
608
1003
|
kind = member_collection_kind(on_option)
|
|
609
1004
|
resource = current_resource
|
|
610
1005
|
if kind && resource
|
|
611
|
-
resource_key =
|
|
1006
|
+
resource_key = { member: resource[:singular_route_name], new: "new_#{resource[:new_route_name]}" }
|
|
1007
|
+
.fetch(kind, resource[:plural_route_name])
|
|
612
1008
|
base = as_option ? as_option.to_s : plain_segment_name(segment)
|
|
613
1009
|
return nil unless base && resource_key && !resource_key.empty?
|
|
614
1010
|
|