@massu/core 1.5.8 → 1.6.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.
- package/dist/adapter.d.ts +76 -0
- package/dist/adapter.js +431 -0
- package/dist/cli.js +818 -356
- package/dist/detect/adapters/.bundle-shasums.json +12 -0
- package/dist/detect/adapters/aspnet.js +577 -0
- package/dist/detect/adapters/go-chi.js +561 -0
- package/dist/detect/adapters/parse-guard.d.ts +69 -0
- package/dist/detect/adapters/parse-guard.js +54 -0
- package/dist/detect/adapters/phoenix.js +556 -0
- package/dist/detect/adapters/query-helpers.d.ts +60 -0
- package/dist/detect/adapters/query-helpers.js +85 -0
- package/dist/detect/adapters/rails.js +567 -0
- package/dist/detect/adapters/spring.js +582 -0
- package/dist/detect/adapters/tree-sitter-loader.d.ts +102 -0
- package/dist/detect/adapters/tree-sitter-loader.js +317 -0
- package/dist/detect/adapters/types.d.ts +151 -0
- package/dist/detect/adapters/types.js +0 -0
- package/dist/hooks/session-start.js +570 -5224
- package/package.json +16 -4
- package/src/adapter.ts +31 -0
- package/src/detect/adapters/aspnet.ts +4 -293
- package/src/detect/adapters/go-chi.ts +4 -261
- package/src/detect/adapters/phoenix.ts +4 -277
- package/src/detect/adapters/rails.ts +4 -279
- package/src/detect/adapters/spring.ts +4 -284
- package/src/security/registry-pubkey.generated.ts +1 -1
|
@@ -1,277 +1,4 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Plan 3c — Phase 7: Phoenix AST adapter.
|
|
6
|
-
*
|
|
7
|
-
* Fourth Phase 7 framework after go-chi + Flask + Rails. First to consume
|
|
8
|
-
* the `elixir` Tree-sitter grammar entry from GRAMMAR_MANIFEST (commit
|
|
9
|
-
* fbb8aa9). Together with the @massu/adapter-phoenix workspace stub from
|
|
10
|
-
* Stage 2 P1-004 (commit ebf2983), completes the per-framework deliverable
|
|
11
|
-
* pattern (4 artifacts):
|
|
12
|
-
* 1. packages/core/templates/phoenix/massu.config.yaml (variant template)
|
|
13
|
-
* 2. packages/core/src/detect/adapters/phoenix.ts (this file — AST adapter)
|
|
14
|
-
* 3. Adversarial fixtures (inline in the test file via mkdirSync+writeFileSync)
|
|
15
|
-
* 4. packages/core/src/__tests__/phoenix.test.ts (golden-output test)
|
|
16
|
-
* + adapter-grammar-strict.test.ts entry (structural gate)
|
|
17
|
-
*
|
|
18
|
-
* Phoenix routes live in `lib/<app>_web/router.ex` using Elixir DSL macros
|
|
19
|
-
* (per Phoenix routing guide: https://hexdocs.pm/phoenix/routing.html).
|
|
20
|
-
* The adapter walks the router DSL invocations directly rather than scanning
|
|
21
|
-
* controller/live directories, mirroring the rails approach against
|
|
22
|
-
* routes.rb.
|
|
23
|
-
*
|
|
24
|
-
* Extracts:
|
|
25
|
-
* - route_method: most-common explicit HTTP verb macro (`get`, `post`,
|
|
26
|
-
* `put`, `patch`, `delete`, `head`, `options`) used at the router scope
|
|
27
|
-
* level with a string-literal path argument. Mirrors python-fastapi/
|
|
28
|
-
* python-flask/go-chi/rails route_method semantics. Excludes the
|
|
29
|
-
* Phoenix-specific `live` and `live_session` macros (those are
|
|
30
|
-
* LiveView-specific and not interchangeable with HTTP verbs), and
|
|
31
|
-
* excludes `resources "/users", UserController` (RESTful sugar — same
|
|
32
|
-
* reasoning as rails resources :users).
|
|
33
|
-
* - scope_prefix_base: first path segment of the first `scope "/api", …
|
|
34
|
-
* do …` block, normalized to a leading-slash path. Mirrors rails
|
|
35
|
-
* api_namespace / python-flask blueprint_url_prefix / go-chi
|
|
36
|
-
* mount_prefix_base. Per Phoenix routing guide §Scoped routes:
|
|
37
|
-
* https://hexdocs.pm/phoenix/routing.html#scoped-routes
|
|
38
|
-
* - router_module: full module name from `defmodule MyAppWeb.Router do`,
|
|
39
|
-
* restricted to modules ending in `Router` (canonical Phoenix naming).
|
|
40
|
-
* Useful for scaffold-router templates that need to know the project's
|
|
41
|
-
* Web module convention.
|
|
42
|
-
*
|
|
43
|
-
* Confidence rules (mirror rails / go-chi):
|
|
44
|
-
* - 'high' if exactly ONE distinct route_method seen (clear convention).
|
|
45
|
-
* - 'low' if multiple distinct route_methods seen (mixed convention).
|
|
46
|
-
* - 'medium' if scope_prefix_base or router_module found but no
|
|
47
|
-
* route_method.
|
|
48
|
-
* - 'none' if no Phoenix DSL signals at all (regex fallback takes over).
|
|
49
|
-
*
|
|
50
|
-
* Tree-sitter-elixir grammar shape (verified via AST probe 2026-05-07):
|
|
51
|
-
* - Macro invocations parse as `(call target: (identifier) (arguments ...))`
|
|
52
|
-
* OR `(call (identifier) (arguments ...))` — both forms accepted; we use
|
|
53
|
-
* the positional shape (no `target:` field) so queries also match the
|
|
54
|
-
* parens-ful invocation `get(...)`.
|
|
55
|
-
* - Module names are `(alias)` (full dotted chain as one node).
|
|
56
|
-
* - String literals are `(string (quoted_content))`; node.text returns
|
|
57
|
-
* the quotes intact.
|
|
58
|
-
* - `do … end` blocks are `(do_block ...)` siblings of `(arguments)`.
|
|
59
|
-
* - Atoms (`:foo`) are `(atom)` — NOT used in any phoenix query (we
|
|
60
|
-
* exclude atom-arg DSL forms by anchoring on `(string)` first arg).
|
|
61
|
-
*
|
|
62
|
-
* Does NOT use regex on file content — only Tree-sitter S-expression queries
|
|
63
|
-
* compiled via query-helpers.ts. Regex would be the regex-fallback path.
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
import { Parser } from 'web-tree-sitter';
|
|
67
|
-
import type { CodebaseAdapter, AdapterResult, DetectionSignals, Provenance, SourceFile } from './types.ts';
|
|
68
|
-
import { runQuery, InvalidQueryError } from './query-helpers.ts';
|
|
69
|
-
import { loadGrammar } from './tree-sitter-loader.ts';
|
|
70
|
-
import { isParsableSource, MAX_AST_FILE_BYTES } from './parse-guard.ts';
|
|
71
|
-
|
|
72
|
-
// ============================================================
|
|
73
|
-
// Tree-sitter S-expression queries (Elixir grammar)
|
|
74
|
-
// ============================================================
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* HTTP method route registration with a STRING-LITERAL first argument:
|
|
78
|
-
* `get "/health", HealthController, :show`,
|
|
79
|
-
* `post "/login", SessionController, :create`.
|
|
80
|
-
*
|
|
81
|
-
* Anchored (`.`) on the first argument so we ONLY match string-literal
|
|
82
|
-
* paths. The `#match?` predicate restricts to canonical HTTP verbs,
|
|
83
|
-
* excluding Phoenix-specific `live` / `live_session` / `forward` /
|
|
84
|
-
* `resources` macros (those have different semantics).
|
|
85
|
-
*
|
|
86
|
-
* Per Phoenix routing guide §HTTP Methods:
|
|
87
|
-
* https://hexdocs.pm/phoenix/routing.html#http-methods
|
|
88
|
-
*/
|
|
89
|
-
const ROUTE_METHOD_QUERY = `
|
|
90
|
-
(call
|
|
91
|
-
(identifier) @method (#match? @method "^(get|post|put|patch|delete|options|head)$")
|
|
92
|
-
(arguments
|
|
93
|
-
.
|
|
94
|
-
(string) @route_path))
|
|
95
|
-
`;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Scope block: `scope "/api", MyAppWeb do … end` or `scope "/admin" do …
|
|
99
|
-
* end`. Captures the FIRST positional argument as a string. The shape
|
|
100
|
-
* `scope MyAppWeb do … end` (alias-only, no path) deliberately doesn't
|
|
101
|
-
* match because the first arg is `(alias)` not `(string)` — verified
|
|
102
|
-
* negative case in AST probe.
|
|
103
|
-
*
|
|
104
|
-
* The presence of a `do_block` is required — `scope "/api", MyAppWeb` (no
|
|
105
|
-
* do/end) wouldn't be a valid Phoenix router scope anyway, but anchoring
|
|
106
|
-
* on the do_block makes the query semantically tighter.
|
|
107
|
-
*/
|
|
108
|
-
const SCOPE_PATH_QUERY = `
|
|
109
|
-
(call
|
|
110
|
-
(identifier) @_method (#eq? @_method "scope")
|
|
111
|
-
(arguments
|
|
112
|
-
.
|
|
113
|
-
(string) @scope_path)
|
|
114
|
-
(do_block))
|
|
115
|
-
`;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Router module definition: `defmodule MyAppWeb.Router do …`. We restrict
|
|
119
|
-
* to module names ending in `Router` to avoid capturing every `defmodule`
|
|
120
|
-
* in the project (Phoenix apps have many modules, only one is THE router).
|
|
121
|
-
*
|
|
122
|
-
* The tree-sitter-elixir grammar represents `MyAppWeb.Router` as a single
|
|
123
|
-
* `(alias)` node — the dotted chain is part of the alias node's text, not
|
|
124
|
-
* a sub-tree of nested aliases. Verified via AST probe 2026-05-07.
|
|
125
|
-
*/
|
|
126
|
-
const ROUTER_MODULE_QUERY = `
|
|
127
|
-
(call
|
|
128
|
-
(identifier) @_method (#eq? @_method "defmodule")
|
|
129
|
-
(arguments
|
|
130
|
-
.
|
|
131
|
-
(alias) @module_name (#match? @module_name "Router$"))
|
|
132
|
-
(do_block))
|
|
133
|
-
`;
|
|
134
|
-
|
|
135
|
-
// ============================================================
|
|
136
|
-
// Adapter
|
|
137
|
-
// ============================================================
|
|
138
|
-
|
|
139
|
-
export const phoenixAdapter: CodebaseAdapter = {
|
|
140
|
-
id: 'phoenix',
|
|
141
|
-
languages: ['elixir'],
|
|
142
|
-
|
|
143
|
-
matches(signals: DetectionSignals): boolean {
|
|
144
|
-
// Cheap signal-only check. No file IO. The canonical Phoenix
|
|
145
|
-
// declaration in mix.exs is `{:phoenix, "~> 1.7.10"}` (per Phoenix
|
|
146
|
-
// install guide: https://hexdocs.pm/phoenix/installation.html). The
|
|
147
|
-
// negative-lookahead `(?!_)` after `:phoenix\b` rejects
|
|
148
|
-
// `:phoenix_live_view` (a sibling dep that Phoenix-LiveView-only
|
|
149
|
-
// projects pull in without Phoenix itself in some Plug-based stacks).
|
|
150
|
-
if (!signals.mixExs) return false;
|
|
151
|
-
return /\{\s*:phoenix\b(?!_)/.test(signals.mixExs);
|
|
152
|
-
},
|
|
153
|
-
|
|
154
|
-
async introspect(files: SourceFile[], _rootDir: string): Promise<AdapterResult> {
|
|
155
|
-
if (files.length === 0) {
|
|
156
|
-
return { conventions: {}, provenance: [], confidence: 'none' };
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let language;
|
|
160
|
-
try {
|
|
161
|
-
language = await loadGrammar('elixir');
|
|
162
|
-
} catch (e) {
|
|
163
|
-
// Grammar unavailable → adapter returns 'none' so regex fallback takes over.
|
|
164
|
-
return { conventions: {}, provenance: [], confidence: 'none' };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const parser = new Parser();
|
|
168
|
-
parser.setLanguage(language);
|
|
169
|
-
|
|
170
|
-
const routeMethods = new Map<string, { line: number; file: string }>();
|
|
171
|
-
const scopePaths = new Map<string, { line: number; file: string }>();
|
|
172
|
-
const routerModules = new Map<string, { line: number; file: string }>();
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
for (const file of files) {
|
|
176
|
-
// Phase 3.5 defense-in-depth size + depth gate at adapter tier.
|
|
177
|
-
const skip = isParsableSource(file.content, file.size);
|
|
178
|
-
if (skip) {
|
|
179
|
-
process.stderr.write(
|
|
180
|
-
`[massu/ast] WARN: phoenix skipping ${file.path}: ${skip.reason} (${skip.detail}). Cap=${MAX_AST_FILE_BYTES}. (Phase 3.5 mitigation)\n`,
|
|
181
|
-
);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
try {
|
|
185
|
-
for (const hit of runQuery(parser, file.content, ROUTE_METHOD_QUERY, 'phoenix-route-method', file.path)) {
|
|
186
|
-
const method = hit.captures.method;
|
|
187
|
-
if (method && !routeMethods.has(method)) {
|
|
188
|
-
routeMethods.set(method, { line: hit.line, file: file.path });
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
for (const hit of runQuery(parser, file.content, SCOPE_PATH_QUERY, 'phoenix-scope-path', file.path)) {
|
|
192
|
-
const raw = hit.captures.scope_path;
|
|
193
|
-
if (!raw) continue;
|
|
194
|
-
const literal = raw.replace(/^["']/, '').replace(/["']$/, '');
|
|
195
|
-
const base = extractPrefixBase(literal);
|
|
196
|
-
if (base && !scopePaths.has(base)) {
|
|
197
|
-
scopePaths.set(base, { line: hit.line, file: file.path });
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
for (const hit of runQuery(parser, file.content, ROUTER_MODULE_QUERY, 'phoenix-router-module', file.path)) {
|
|
201
|
-
const name = hit.captures.module_name;
|
|
202
|
-
if (name && !routerModules.has(name)) {
|
|
203
|
-
routerModules.set(name, { line: hit.line, file: file.path });
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
} catch (e) {
|
|
207
|
-
if (e instanceof InvalidQueryError) {
|
|
208
|
-
// Compile-time failure of OUR query is a developer bug — surface it.
|
|
209
|
-
throw e;
|
|
210
|
-
}
|
|
211
|
-
// Per-file parse error: skip + keep going.
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
} finally {
|
|
216
|
-
try { parser.delete(); } catch { /* ignore */ }
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const conventions: Record<string, unknown> = {};
|
|
220
|
-
const provenance: Provenance[] = [];
|
|
221
|
-
|
|
222
|
-
if (routeMethods.size === 1) {
|
|
223
|
-
const [name, { line, file }] = routeMethods.entries().next().value as [string, { line: number; file: string }];
|
|
224
|
-
conventions.route_method = name;
|
|
225
|
-
provenance.push({ field: 'route_method', sourceFile: file, line, query: 'phoenix-route-method' });
|
|
226
|
-
} else if (routeMethods.size >= 2) {
|
|
227
|
-
// Mixed convention — emit first-seen for visibility.
|
|
228
|
-
const [name, { line, file }] = routeMethods.entries().next().value as [string, { line: number; file: string }];
|
|
229
|
-
conventions.route_method = name;
|
|
230
|
-
provenance.push({ field: 'route_method', sourceFile: file, line, query: 'phoenix-route-method' });
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (scopePaths.size >= 1) {
|
|
234
|
-
const [base, { line, file }] = scopePaths.entries().next().value as [string, { line: number; file: string }];
|
|
235
|
-
conventions.scope_prefix_base = base;
|
|
236
|
-
provenance.push({ field: 'scope_prefix_base', sourceFile: file, line, query: 'phoenix-scope-path' });
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (routerModules.size >= 1) {
|
|
240
|
-
const [name, { line, file }] = routerModules.entries().next().value as [string, { line: number; file: string }];
|
|
241
|
-
conventions.router_module = name;
|
|
242
|
-
provenance.push({ field: 'router_module', sourceFile: file, line, query: 'phoenix-router-module' });
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
let confidence: AdapterResult['confidence'];
|
|
246
|
-
if (Object.keys(conventions).length === 0) {
|
|
247
|
-
confidence = 'none';
|
|
248
|
-
} else if (routeMethods.size === 1) {
|
|
249
|
-
confidence = 'high';
|
|
250
|
-
} else if (routeMethods.size >= 2) {
|
|
251
|
-
confidence = 'low';
|
|
252
|
-
} else {
|
|
253
|
-
confidence = 'medium';
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return { conventions, provenance, confidence };
|
|
257
|
-
},
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
// ============================================================
|
|
261
|
-
// Helpers
|
|
262
|
-
// ============================================================
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Extract the first path segment of a Phoenix scope path. Mirrors
|
|
266
|
-
* python-fastapi/python-flask/go-chi/rails prefix-base extractors.
|
|
267
|
-
* Returns null if input doesn't start with `/`.
|
|
268
|
-
*
|
|
269
|
-
* "/api/v1/users" → "/api"; "/" → null (root scope is uninformative).
|
|
270
|
-
*/
|
|
271
|
-
function extractPrefixBase(prefix: string): string | null {
|
|
272
|
-
if (!prefix.startsWith('/')) return null;
|
|
273
|
-
const stripped = prefix.replace(/^\/+/, '');
|
|
274
|
-
const firstSeg = stripped.split('/')[0];
|
|
275
|
-
if (!firstSeg) return null;
|
|
276
|
-
return '/' + firstSeg;
|
|
277
|
-
}
|
|
1
|
+
// Plan 3c Phase 9b P-A-005: re-export shim. Source-of-truth lives at
|
|
2
|
+
// `packages/adapter-phoenix/src/index.ts` (workspace package). This shim
|
|
3
|
+
// preserves the legacy import path used by codebase-introspector + tests.
|
|
4
|
+
export { phoenixAdapter } from '@massu/adapter-phoenix';
|
|
@@ -1,279 +1,4 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Plan 3c — Phase 7: Rails AST adapter.
|
|
6
|
-
*
|
|
7
|
-
* Second Phase 7 framework after go-chi; first to consume the new `ruby`
|
|
8
|
-
* Tree-sitter grammar entry from GRAMMAR_MANIFEST (commit fbb8aa9). Together
|
|
9
|
-
* with the @massu/adapter-rails workspace stub created in Stage 2 P1-004
|
|
10
|
-
* (commit ebf2983), this completes the per-framework deliverable pattern
|
|
11
|
-
* established by Phase 7 commits 1–3:
|
|
12
|
-
* 1. packages/core/templates/rails/massu.config.yaml (variant template)
|
|
13
|
-
* 2. packages/core/src/detect/adapters/rails.ts (this file — AST adapter)
|
|
14
|
-
* 3. Adversarial fixtures (inline in the test file via mkdirSync+writeFileSync)
|
|
15
|
-
* 4. packages/core/src/__tests__/rails.test.ts (golden-output test)
|
|
16
|
-
*
|
|
17
|
-
* Rails uses a DSL-heavy `config/routes.rb` (per Rails routing guide:
|
|
18
|
-
* https://guides.rubyonrails.org/routing.html). The adapter walks the
|
|
19
|
-
* routes.rb DSL invocations directly rather than scanning controller
|
|
20
|
-
* directories, mirroring the go-chi approach against router.go files.
|
|
21
|
-
*
|
|
22
|
-
* Extracts:
|
|
23
|
-
* - route_method: most-common explicit HTTP verb (`get`, `post`, `put`,
|
|
24
|
-
* `patch`, `delete`, `head`, `options`) used at TOP LEVEL with a
|
|
25
|
-
* string-literal path argument. Mirrors python-fastapi/python-flask/
|
|
26
|
-
* go-chi route_method semantics. Distinct from `resources :users`
|
|
27
|
-
* RESTful blocks — those imply all seven verbs in one declaration and
|
|
28
|
-
* should NOT pin the project to one verb. The string-literal anchor
|
|
29
|
-
* also excludes member/collection block calls like `get :preview`
|
|
30
|
-
* (where the arg is a symbol, not a string).
|
|
31
|
-
* - api_namespace: first segment of the first `namespace :foo do …` or
|
|
32
|
-
* `namespace 'foo' do …` block, normalized to a leading-slash path
|
|
33
|
-
* (mirrors python-fastapi/flask's blueprint_url_prefix and go-chi's
|
|
34
|
-
* mount_prefix_base). Per Rails routing guide §3:
|
|
35
|
-
* https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
|
|
36
|
-
* - root_controller: controller name from `root 'pages#home'` or
|
|
37
|
-
* `root to: 'pages#home'`. The `Foo#bar` syntax canonically denotes
|
|
38
|
-
* `FooController#bar`. Useful for scaffold-router/scaffold-page
|
|
39
|
-
* templates that need to know the project's home route convention.
|
|
40
|
-
*
|
|
41
|
-
* Confidence rules (mirror go-chi):
|
|
42
|
-
* - 'high' if exactly ONE distinct route_method seen (clear convention).
|
|
43
|
-
* - 'low' if multiple distinct route_methods seen (mixed convention).
|
|
44
|
-
* - 'medium' if api_namespace or root_controller found but no route_method.
|
|
45
|
-
* - 'none' if no Rails DSL signals at all (regex fallback takes over).
|
|
46
|
-
*
|
|
47
|
-
* Does NOT use regex on file content — only Tree-sitter S-expression queries
|
|
48
|
-
* compiled via query-helpers.ts. Regex would be the regex-fallback path.
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
import { Parser } from 'web-tree-sitter';
|
|
52
|
-
import type { CodebaseAdapter, AdapterResult, DetectionSignals, Provenance, SourceFile } from './types.ts';
|
|
53
|
-
import { runQuery, InvalidQueryError } from './query-helpers.ts';
|
|
54
|
-
import { loadGrammar } from './tree-sitter-loader.ts';
|
|
55
|
-
import { isParsableSource, MAX_AST_FILE_BYTES } from './parse-guard.ts';
|
|
56
|
-
|
|
57
|
-
// ============================================================
|
|
58
|
-
// Tree-sitter S-expression queries (Ruby grammar)
|
|
59
|
-
// ============================================================
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* HTTP method route registration with a STRING-LITERAL first argument:
|
|
63
|
-
* `get '/health', to: 'health#show'`, `post "/login", to: 'sessions#create'`.
|
|
64
|
-
*
|
|
65
|
-
* Anchored (`.`) on the first argument so that we ONLY match string-literal
|
|
66
|
-
* paths — symbol arguments (`get :preview` inside a member block) are
|
|
67
|
-
* deliberately excluded because those are nested action declarations, NOT
|
|
68
|
-
* top-level routes.
|
|
69
|
-
*
|
|
70
|
-
* tree-sitter-ruby (v0.20.1, pinned by tree-sitter-wasms@0.1.13) emits
|
|
71
|
-
* `(call method: (identifier) arguments: (argument_list ...))` for the
|
|
72
|
-
* parens-less DSL form `get '/x', ...` AND for the parens-ful `get('/x', ...)`
|
|
73
|
-
* form. Verified by AST probe (2026-05-07): there is NO separate `method_call`
|
|
74
|
-
* node in this grammar — historical web sources mentioning `method_call`
|
|
75
|
-
* refer to a different/older grammar fork.
|
|
76
|
-
*/
|
|
77
|
-
const ROUTE_METHOD_QUERY = `
|
|
78
|
-
(call
|
|
79
|
-
method: (identifier) @method (#match? @method "^(get|post|put|patch|delete|options|head)$")
|
|
80
|
-
arguments: (argument_list
|
|
81
|
-
.
|
|
82
|
-
(string) @route_path))
|
|
83
|
-
`;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Namespace block: `namespace :api do ... end` or `namespace 'api' do ... end`.
|
|
87
|
-
* Captures the first symbol-or-string argument so we can normalize to a
|
|
88
|
-
* leading-slash path.
|
|
89
|
-
*
|
|
90
|
-
* Rails accepts both `namespace :api` (symbol — canonical) and the rarer
|
|
91
|
-
* `namespace 'api'` (string). Both forms produce a `namespace` identifier
|
|
92
|
-
* call; the first arg differs only in node type.
|
|
93
|
-
*/
|
|
94
|
-
const NAMESPACE_QUERY = `
|
|
95
|
-
(call
|
|
96
|
-
method: (identifier) @_method (#eq? @_method "namespace")
|
|
97
|
-
arguments: (argument_list
|
|
98
|
-
.
|
|
99
|
-
[
|
|
100
|
-
(simple_symbol) @namespace_symbol
|
|
101
|
-
(string) @namespace_string
|
|
102
|
-
]))
|
|
103
|
-
`;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Root route: `root 'pages#home'`, `root "pages#home"`, or
|
|
107
|
-
* `root to: 'pages#home'`. We capture the string literal in either the
|
|
108
|
-
* positional or `to:` keyword position; the controller is whatever
|
|
109
|
-
* precedes the `#`.
|
|
110
|
-
*
|
|
111
|
-
* Rails routing guide §2.6:
|
|
112
|
-
* https://guides.rubyonrails.org/routing.html#using-root
|
|
113
|
-
*/
|
|
114
|
-
const ROOT_ROUTE_QUERY = `
|
|
115
|
-
(call
|
|
116
|
-
method: (identifier) @_method (#eq? @_method "root")
|
|
117
|
-
arguments: (argument_list
|
|
118
|
-
.
|
|
119
|
-
(string) @root_target))
|
|
120
|
-
|
|
121
|
-
(call
|
|
122
|
-
method: (identifier) @_method (#eq? @_method "root")
|
|
123
|
-
arguments: (argument_list
|
|
124
|
-
(pair
|
|
125
|
-
key: (hash_key_symbol) @_key (#eq? @_key "to")
|
|
126
|
-
value: (string) @root_target)))
|
|
127
|
-
`;
|
|
128
|
-
|
|
129
|
-
// ============================================================
|
|
130
|
-
// Adapter
|
|
131
|
-
// ============================================================
|
|
132
|
-
|
|
133
|
-
export const railsAdapter: CodebaseAdapter = {
|
|
134
|
-
id: 'rails',
|
|
135
|
-
languages: ['ruby'],
|
|
136
|
-
|
|
137
|
-
matches(signals: DetectionSignals): boolean {
|
|
138
|
-
// Cheap signal-only check. No file IO. The canonical Rails declaration
|
|
139
|
-
// is `gem 'rails'` in Gemfile (per Rails install guide:
|
|
140
|
-
// https://guides.rubyonrails.org/getting_started.html). The strict
|
|
141
|
-
// `gem ['"]rails['"]` regex deliberately rejects `gem 'rails-api'`,
|
|
142
|
-
// `gem 'rails_admin'`, etc. — those are Rails-adjacent gems that may
|
|
143
|
-
// appear in non-Rails Ruby projects.
|
|
144
|
-
if (!signals.gemfile) return false;
|
|
145
|
-
return /^\s*gem\s+['"]rails['"]/im.test(signals.gemfile);
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
async introspect(files: SourceFile[], _rootDir: string): Promise<AdapterResult> {
|
|
149
|
-
if (files.length === 0) {
|
|
150
|
-
return { conventions: {}, provenance: [], confidence: 'none' };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let language;
|
|
154
|
-
try {
|
|
155
|
-
language = await loadGrammar('ruby');
|
|
156
|
-
} catch (e) {
|
|
157
|
-
// Grammar unavailable → adapter returns 'none' so regex fallback takes over.
|
|
158
|
-
return { conventions: {}, provenance: [], confidence: 'none' };
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const parser = new Parser();
|
|
162
|
-
parser.setLanguage(language);
|
|
163
|
-
|
|
164
|
-
const routeMethods = new Map<string, { line: number; file: string }>();
|
|
165
|
-
const namespaces = new Map<string, { line: number; file: string }>();
|
|
166
|
-
const rootControllers = new Map<string, { line: number; file: string }>();
|
|
167
|
-
|
|
168
|
-
try {
|
|
169
|
-
for (const file of files) {
|
|
170
|
-
// Phase 3.5 defense-in-depth size + depth gate at adapter tier.
|
|
171
|
-
const skip = isParsableSource(file.content, file.size);
|
|
172
|
-
if (skip) {
|
|
173
|
-
process.stderr.write(
|
|
174
|
-
`[massu/ast] WARN: rails skipping ${file.path}: ${skip.reason} (${skip.detail}). Cap=${MAX_AST_FILE_BYTES}. (Phase 3.5 mitigation)\n`,
|
|
175
|
-
);
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
try {
|
|
179
|
-
for (const hit of runQuery(parser, file.content, ROUTE_METHOD_QUERY, 'rails-route-method', file.path)) {
|
|
180
|
-
const method = hit.captures.method;
|
|
181
|
-
if (method && !routeMethods.has(method)) {
|
|
182
|
-
routeMethods.set(method, { line: hit.line, file: file.path });
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
for (const hit of runQuery(parser, file.content, NAMESPACE_QUERY, 'rails-namespace', file.path)) {
|
|
186
|
-
const symbolRaw = hit.captures.namespace_symbol;
|
|
187
|
-
const stringRaw = hit.captures.namespace_string;
|
|
188
|
-
const name = symbolRaw
|
|
189
|
-
? symbolRaw.replace(/^:/, '')
|
|
190
|
-
: stringRaw
|
|
191
|
-
? stringRaw.replace(/^['"]/, '').replace(/['"]$/, '')
|
|
192
|
-
: null;
|
|
193
|
-
if (!name) continue;
|
|
194
|
-
const path = '/' + name;
|
|
195
|
-
if (!namespaces.has(path)) {
|
|
196
|
-
namespaces.set(path, { line: hit.line, file: file.path });
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
for (const hit of runQuery(parser, file.content, ROOT_ROUTE_QUERY, 'rails-root', file.path)) {
|
|
200
|
-
const raw = hit.captures.root_target;
|
|
201
|
-
if (!raw) continue;
|
|
202
|
-
const literal = raw.replace(/^['"]/, '').replace(/['"]$/, '');
|
|
203
|
-
const controller = extractRootController(literal);
|
|
204
|
-
if (controller && !rootControllers.has(controller)) {
|
|
205
|
-
rootControllers.set(controller, { line: hit.line, file: file.path });
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
} catch (e) {
|
|
209
|
-
if (e instanceof InvalidQueryError) {
|
|
210
|
-
// Compile-time failure of OUR query is a developer bug — surface it.
|
|
211
|
-
throw e;
|
|
212
|
-
}
|
|
213
|
-
// Per-file parse error: skip + keep going.
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
} finally {
|
|
218
|
-
try { parser.delete(); } catch { /* ignore */ }
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const conventions: Record<string, unknown> = {};
|
|
222
|
-
const provenance: Provenance[] = [];
|
|
223
|
-
|
|
224
|
-
if (routeMethods.size === 1) {
|
|
225
|
-
const [name, { line, file }] = routeMethods.entries().next().value as [string, { line: number; file: string }];
|
|
226
|
-
conventions.route_method = name;
|
|
227
|
-
provenance.push({ field: 'route_method', sourceFile: file, line, query: 'rails-route-method' });
|
|
228
|
-
} else if (routeMethods.size >= 2) {
|
|
229
|
-
// Mixed convention — emit first-seen for visibility.
|
|
230
|
-
const [name, { line, file }] = routeMethods.entries().next().value as [string, { line: number; file: string }];
|
|
231
|
-
conventions.route_method = name;
|
|
232
|
-
provenance.push({ field: 'route_method', sourceFile: file, line, query: 'rails-route-method' });
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (namespaces.size >= 1) {
|
|
236
|
-
const [path, { line, file }] = namespaces.entries().next().value as [string, { line: number; file: string }];
|
|
237
|
-
conventions.api_namespace = path;
|
|
238
|
-
provenance.push({ field: 'api_namespace', sourceFile: file, line, query: 'rails-namespace' });
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (rootControllers.size >= 1) {
|
|
242
|
-
const [name, { line, file }] = rootControllers.entries().next().value as [string, { line: number; file: string }];
|
|
243
|
-
conventions.root_controller = name;
|
|
244
|
-
provenance.push({ field: 'root_controller', sourceFile: file, line, query: 'rails-root' });
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
let confidence: AdapterResult['confidence'];
|
|
248
|
-
if (Object.keys(conventions).length === 0) {
|
|
249
|
-
confidence = 'none';
|
|
250
|
-
} else if (routeMethods.size === 1) {
|
|
251
|
-
confidence = 'high';
|
|
252
|
-
} else if (routeMethods.size >= 2) {
|
|
253
|
-
confidence = 'low';
|
|
254
|
-
} else {
|
|
255
|
-
confidence = 'medium';
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return { conventions, provenance, confidence };
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
// ============================================================
|
|
263
|
-
// Helpers
|
|
264
|
-
// ============================================================
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Extract the controller name from a Rails `controller#action` string.
|
|
268
|
-
* `'pages#home'` → `'pages'`; `'admin/dashboard#index'` → `'admin/dashboard'`.
|
|
269
|
-
* Returns null for malformed input (no `#` separator).
|
|
270
|
-
*
|
|
271
|
-
* Per Rails routing guide §2.6: the string before `#` is the controller
|
|
272
|
-
* (with optional namespace prefix), the part after is the action.
|
|
273
|
-
*/
|
|
274
|
-
function extractRootController(target: string): string | null {
|
|
275
|
-
const idx = target.indexOf('#');
|
|
276
|
-
if (idx <= 0) return null;
|
|
277
|
-
const controller = target.slice(0, idx).trim();
|
|
278
|
-
return controller || null;
|
|
279
|
-
}
|
|
1
|
+
// Plan 3c Phase 9b P-A-005: re-export shim. Source-of-truth lives at
|
|
2
|
+
// `packages/adapter-rails/src/index.ts` (workspace package). This shim
|
|
3
|
+
// preserves the legacy import path used by codebase-introspector + tests.
|
|
4
|
+
export { railsAdapter } from '@massu/adapter-rails';
|