Skip to content

Commit 77635fe

Browse files
Copilotna-trium-144
andcommitted
Extract magic strings and regexes to named constants for better maintainability
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent ecff1be commit 77635fe

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

app/terminal/wandbox/cpp.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export async function cppRunFiles(
8787
filenames: string[],
8888
onOutput: (output: ReplOutput) => void
8989
): Promise<string> {
90+
// Constants for stack trace processing
91+
const WANDBOX_PATH = "/home/wandbox";
92+
9093
// Track state for processing stack traces
9194
let inStackTrace = false;
9295
let foundSignal = false;
@@ -125,10 +128,10 @@ export async function cppRunFiles(
125128
// Process stack trace lines
126129
if (inStackTrace && ndjsonType === "StdErr") {
127130
// Filter to show only user source code
128-
if (output.message.includes("/home/wandbox")) {
131+
if (output.message.includes(WANDBOX_PATH)) {
129132
onOutput({
130133
type: "trace",
131-
message: output.message.replace("/home/wandbox/", ""),
134+
message: output.message.replace(WANDBOX_PATH + "/", ""),
132135
});
133136
}
134137
return;

app/terminal/wandbox/rust.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export async function rustRunFiles(
3333
filenames: string[],
3434
onOutput: (output: ReplOutput) => void
3535
): Promise<string> {
36+
// Regular expressions for parsing stack traces
37+
const STACK_FRAME_PATTERN = /^\s*\d+:/;
38+
const LOCATION_PATTERN = /^\s*at .\//;
39+
const SYSTEM_CODE_PATTERN = /^\s*at .\/prog.rs/;
40+
3641
// Track state for processing panic traces
3742
let inPanicHook = false;
3843
let foundBacktraceHeader = false;
@@ -79,12 +84,12 @@ export async function rustRunFiles(
7984
if (foundBacktraceHeader) {
8085
// Process stack trace lines
8186
// Look for pattern: " N: ..." followed by " at ./file.rs:line"
82-
if (/^\s*\d+:/.test(output.message)) {
87+
if (STACK_FRAME_PATTERN.test(output.message)) {
8388
traceLines.push(output.message);
84-
} else if (/^\s*at .\//.test(output.message)) {
89+
} else if (LOCATION_PATTERN.test(output.message)) {
8590
if (traceLines.length > 0) {
8691
// Check if this is user code (not prog.rs)
87-
if (!/^\s*at .\/prog.rs/.test(output.message)) {
92+
if (!SYSTEM_CODE_PATTERN.test(output.message)) {
8893
onOutput({
8994
type: "trace",
9095
message: traceLines[traceLines.length - 1].replace("prog::", ""),

0 commit comments

Comments
 (0)