TypeScript
fix(web): clicking a logged request route no longer opens it in the editor (#15838)
Lars Nieuwenhuis10b86829e8f2
diff --git a/apps/web/src/terminal-links.test.ts b/apps/web/src/terminal-links.test.tsindex a148931e2db..a4e12cd1226 100644--- a/apps/web/src/terminal-links.test.ts+++ b/apps/web/src/terminal-links.test.ts@@ -171,6 +171,37 @@ describe("extractTerminalLinks", () => {{ kind: "path", text, start, end: start + text.length },]);});++ it.each([+ " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",+ "GET /api?+debug 200",+ 'Started GET "/posts?page=2" for ::1',+ "proxying api/trpc/post.list?batch=1 to the backend",+ ])("does not turn a logged request route into an editor link: %s", (line) => {+ expect(extractTerminalLinks(line)).toEqual([]);+ });++ it("keeps file and URL links before and after a logged request route", () => {+ const before = "src/server.ts:12";+ const after = "/app/bin/server:3:7";+ const url = "https://example.com/api/users?id=42";+ const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;+ const afterStart = line.indexOf(after);+ const urlStart = line.indexOf(url);++ expect(extractTerminalLinks(line)).toEqual([+ { kind: "path", text: before, start: 0, end: before.length },+ { kind: "path", text: after, start: afterStart, end: afterStart + after.length },+ { kind: "url", text: url, start: urlStart, end: urlStart + url.length },+ ]);+ });++ it("keeps a question mark inside a filesystem path", () => {+ const path = "/tmp/report?draft";+ expect(extractTerminalLinks(`wrote ${path}`)).toEqual([+ { kind: "path", text: path, start: 6, end: 6 + path.length },+ ]);+ });});describe("collectWrappedTerminalLinkLine", () => {diff --git a/apps/web/src/terminal-links.ts b/apps/web/src/terminal-links.tsindex 628d7ab2131..dd46062ac77 100644--- a/apps/web/src/terminal-links.ts+++ b/apps/web/src/terminal-links.ts@@ -34,6 +34,9 @@ const QUOTED_FILE_PATH_PATTERN =const TRAILING_PUNCTUATION_PATTERN = /[.,;!?]+$/;// Paths also drop a trailing colon: compilers end `file:line:col:` with one.const TRAILING_PATH_PUNCTUATION_PATTERN = /[.,;:!?]+$/;+// A question mark is legal in filenames; only exclude query-bearing request targets.+const REQUEST_ROUTE_PATTERN =+ /(?:^|[\s"'`])(?:GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|CONNECT|TRACE|proxying)\s+["'`]?(?:\/|[A-Za-z0-9_-]+\/)[^\s?]*\?\S*/g;function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {let output = value.replace(@@ -99,13 +102,21 @@ function collectMatches(}export function extractTerminalLinks(line: string): TerminalLinkMatch[] {+ const requestRoutes = Array.from(line.matchAll(REQUEST_ROUTE_PATTERN), (match) => ({+ start: match.index,+ end: match.index + match[0].length,+ }));const urlMatches = collectMatches(line, "url", URL_PATTERN, []);const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, urlMatches);const pathMatches = collectMatches(line, "path", FILE_PATH_PATTERN, [...urlMatches,...quotedMatches,]);- return [...urlMatches, ...quotedMatches, ...pathMatches].toSorted((a, b) => a.start - b.start);+ return [...urlMatches, ...quotedMatches, ...pathMatches]+ .filter(+ (match) => match.kind === "url" || !requestRoutes.some((route) => overlaps(match, route)),+ )+ .toSorted((a, b) => a.start - b.start);}export function isTerminalUrl(value: string): boolean {