Skip to content

Commit b0e6e8e

Browse files
committed
fix: $wip usage within brackets
1 parent 93dfd00 commit b0e6e8e

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

slang/lib/src/runner/wip.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,36 @@ class WipInvocationCollection {
199199
final invocationsList = <WipInvocationMatch>[];
200200

201201
for (final match in regex.allMatches(sourceSanitized)) {
202+
String original = match.group(0)!;
202203
final path = match.group(1)!;
203-
final value = match.group(2)!;
204+
String value = match.group(2)!;
205+
206+
if (value.contains(')')) {
207+
// The regex is greedy and might capture too many closing parentheses.
208+
// We need to find the matching closing parenthesis for the opening one.
209+
final openParenIndex = original.indexOf('(');
210+
if (openParenIndex != -1) {
211+
int depth = 0;
212+
213+
for (int i = openParenIndex; i < original.length; i++) {
214+
if (original[i] == '(') {
215+
depth++;
216+
} else if (original[i] == ')') {
217+
depth--;
218+
if (depth == 0) {
219+
// Found the matching closing paren!
220+
original = original.substring(0, i + 1);
221+
value = original.substring(openParenIndex + 1, i);
222+
break;
223+
}
224+
}
225+
}
226+
}
227+
}
204228

205229
final invocation = WipInvocationMatch.parse(
206230
interpolation: interpolation,
207-
original: match.group(0)!,
231+
original: original,
208232
path: path,
209233
value: value,
210234
);

slang/test/unit/runner/wip_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ t.$wip.multilineMethod('test');
4242
expect(result.list[0].parameterMap, {});
4343
});
4444

45+
test('Should find invocation within brackets', () {
46+
final result = f(r"f(t.$wip.a('b'));");
47+
48+
expect(result.map, {
49+
'a': 'b',
50+
});
51+
expect(result.list.length, 1);
52+
expect(
53+
result.list[0].original,
54+
r"""t.$wip.a('b')""",
55+
);
56+
expect(result.list[0].path, 'a');
57+
expect(result.list[0].sanitizedValue, 'b');
58+
expect(result.list[0].parameterMap, {});
59+
});
60+
61+
test('Should find invocation within nested brackets', () {
62+
final result = f(r"""
63+
f(
64+
g(t.$wip.a('b')),
65+
);
66+
""");
67+
68+
expect(result.map, {
69+
'a': 'b',
70+
});
71+
expect(result.list.length, 1);
72+
expect(
73+
result.list[0].original,
74+
r"""t.$wip.a('b')""",
75+
);
76+
expect(result.list[0].path, 'a');
77+
expect(result.list[0].sanitizedValue, 'b');
78+
expect(result.list[0].parameterMap, {});
79+
});
80+
4581
test('Should find invocation with interpolation', () {
4682
final result = f(r'''
4783
final greeting = t.$wip.welcome.message('Hello, $name!');

0 commit comments

Comments
 (0)