Skip to content

refactor: replace fragile StackTrace RegExp parsing with package:stack_trace #52

Description

@RandalSchwartz

[authored by Randal, assisted by Gemini]

Problem

In packages/kaisel_core/lib/src/kaisel_router.dart, the helper function kaiselOriginFrames captures and parses the stringified representation of a StackTrace using a regular expression (_frameLocation) to resolve file paths, lines, and columns for DevTools support:

https://github.com/Mastersam07/kaisel/blob/dev/packages/kaisel_core/lib/src/kaisel_router.dart#L38-L58

Relying on manual RegExp parsing of StackTrace.toString() is highly fragile:

  • Stack trace string formats are not guaranteed stable by the Dart SDK.
  • The format differs significantly depending on the platform/compiler (Dart VM vs. JS vs. WebAssembly).
  • Any updates to the compiler output or SDK changes can silently break DevTools frame location resolution.

Proposed Solution

Migrate stack trace processing to the official, Dart-team-maintained package:stack_trace which is built precisely to parse, normalize, and format stack traces reliably across VM and web platforms.

Proposed Implementation:

  1. Add stack_trace as a dependency in packages/kaisel_core/pubspec.yaml.
  2. Refactor kaiselOriginFrames to construct a Trace and filter/map its frames:
import 'package:stack_trace/stack_trace.dart';

List<KaiselOriginFrame> kaiselOriginFrames(StackTrace? trace, {int limit = 5}) {
  if (trace == null) return const [];
  
  final frames = <KaiselOriginFrame>[];
  final parsedTrace = Trace.from(trace);

  for (final frame in parsedTrace.frames) {
    final uriStr = frame.uri.toString();
    
    // Filter out framework and SDK frames
    if (uriStr.startsWith('dart:') ||
        uriStr.startsWith('package:kaisel/') ||
        uriStr.startsWith('package:kaisel_core/') ||
        uriStr.startsWith('package:flutter/')) {
      continue;
    }

    frames.add(
      KaiselOriginFrame(
        display: frame.toString(),
        uri: uriStr,
        line: frame.line,
        column: frame.column,
      ),
    );

    if (frames.length >= limit) break;
  }
  return frames;
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions