Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/frameworks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,19 @@ describe('vaporResolver.extract', () => {
expect(references[0].referenceName).toBe('listUsers');
});

it('does not backtrack exponentially on a call that never reaches use: (#1544)', () => {
// `app.get(a, b, c, ... )` with no `use:` label: the route regex must fail
// fast. The previous pattern had `\s*` inside the repeated group, which
// overlapped `[^,()]+`, so each extra argument roughly quadrupled the work
// and a generated file could hang indexing for minutes.
const args = Array.from({ length: 40 }, (_, i) => `arg${i}, `).join('');
const src = `app.get(${args}x)\n`;
const started = Date.now();
const { nodes } = vaporResolver.extract!('routes.swift', src);
expect(Date.now() - started).toBeLessThan(1000);
expect(nodes).toEqual([]);
});

it('extracts grouped RouteCollection routes with the group prefix and no path arg', () => {
const src = `
func boot(routes: RoutesBuilder) throws {
Expand Down
9 changes: 8 additions & 1 deletion src/resolution/frameworks/swift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,14 @@ export const vaporResolver: FrameworkResolver = {
// (`BlogUser.parameter`, `:id`, a path constant) so accept any comma-separated
// args before `use:` — the label keeps only the string parts. `use:`
// discriminates a real route from Environment.get("X")/req.parameters.get("X").
const routeRegex = /\b(\w+)\.(get|post|put|patch|delete|head|options)\s*\(\s*((?:[^,()]+,\s*)*)use:\s*([A-Za-z_][\w.]*)/g;
// The `\s*` that used to sit inside the repeated group overlapped
// `[^,()]+` (which also matches whitespace), so every space after a comma
// could be consumed by either branch. On a call whose argument list never
// reaches `use:` that ambiguity backtracks exponentially — ~4x per extra
// argument, seconds by 28 — hanging index/sync/MCP on generated files.
// Keeping the group purely `<run>,` and matching the gap before `use:`
// once, outside the loop, leaves exactly one way to split the input.
const routeRegex = /\b(\w+)\.(get|post|put|patch|delete|head|options)\s*\(\s*((?:[^,()]+,)*)\s*use:\s*([A-Za-z_][\w.]*)/g;
let match: RegExpExecArray | null;
while ((match = routeRegex.exec(safe)) !== null) {
const [, receiver, method, segsStr, handlerExpr] = match;
Expand Down