Skip to content

Commit 10b8682

Browse files
fix(web): clicking a logged request route no longer opens it in the editor (#15838)
Co-authored-by: Julius Marminge <[email protected]>
1 parent d407f54 commit 10b8682

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎apps/web/src/terminal-links.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,37 @@ describe("extractTerminalLinks", () => {
171171
{ kind: "path", text, start, end: start + text.length },
172172
]);
173173
});
174+
175+
it.each([
176+
" GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
177+
"GET /api?+debug 200",
178+
'Started GET "/posts?page=2" for ::1',
179+
"proxying api/trpc/post.list?batch=1 to the backend",
180+
])("does not turn a logged request route into an editor link: %s", (line) => {
181+
expect(extractTerminalLinks(line)).toEqual([]);
182+
});
183+
184+
it("keeps file and URL links before and after a logged request route", () => {
185+
const before = "src/server.ts:12";
186+
const after = "/app/bin/server:3:7";
187+
const url = "https://example.com/api/users?id=42";
188+
const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
189+
const afterStart = line.indexOf(after);
190+
const urlStart = line.indexOf(url);
191+
192+
expect(extractTerminalLinks(line)).toEqual([
193+
{ kind: "path", text: before, start: 0, end: before.length },
194+
{ kind: "path", text: after, start: afterStart, end: afterStart + after.length },
195+
{ kind: "url", text: url, start: urlStart, end: urlStart + url.length },
196+
]);
197+
});
198+
199+
it("keeps a question mark inside a filesystem path", () => {
200+
const path = "/tmp/report?draft";
201+
expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
202+
{ kind: "path", text: path, start: 6, end: 6 + path.length },
203+
]);
204+
});
174205
});
175206

176207
describe("collectWrappedTerminalLinkLine", () => {

‎apps/web/src/terminal-links.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const QUOTED_FILE_PATH_PATTERN =
3434
const TRAILING_PUNCTUATION_PATTERN = /[.,;!?]+$/;
3535
// Paths also drop a trailing colon: compilers end `file:line:col:` with one.
3636
const TRAILING_PATH_PUNCTUATION_PATTERN = /[.,;:!?]+$/;
37+
// A question mark is legal in filenames; only exclude query-bearing request targets.
38+
const REQUEST_ROUTE_PATTERN =
39+
/(?:^|[\s"'`])(?:GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|CONNECT|TRACE|proxying)\s+["'`]?(?:\/|[A-Za-z0-9_-]+\/)[^\s?]*\?\S*/g;
3740

3841
function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
3942
let output = value.replace(
@@ -99,13 +102,21 @@ function collectMatches(
99102
}
100103

101104
export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
105+
const requestRoutes = Array.from(line.matchAll(REQUEST_ROUTE_PATTERN), (match) => ({
106+
start: match.index,
107+
end: match.index + match[0].length,
108+
}));
102109
const urlMatches = collectMatches(line, "url", URL_PATTERN, []);
103110
const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, urlMatches);
104111
const pathMatches = collectMatches(line, "path", FILE_PATH_PATTERN, [
105112
...urlMatches,
106113
...quotedMatches,
107114
]);
108-
return [...urlMatches, ...quotedMatches, ...pathMatches].toSorted((a, b) => a.start - b.start);
115+
return [...urlMatches, ...quotedMatches, ...pathMatches]
116+
.filter(
117+
(match) => match.kind === "url" || !requestRoutes.some((route) => overlaps(match, route)),
118+
)
119+
.toSorted((a, b) => a.start - b.start);
109120
}
110121

111122
export function isTerminalUrl(value: string): boolean {

0 commit comments

Comments
 (0)