Skip to content

Commit e99ba54

Browse files
srawlinscopybara-github
authored andcommitted
Properly generate code for parameter default value Strings.
Fixes #756 In order to properly allow single quotes and dollar signs in parameter default values, we must not print as raw, and we must escape dollar signs ourselves, before passing to code_builder. PiperOrigin-RevId: 646140324
1 parent 2302814 commit e99ba54

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

lib/src/builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,11 @@ class _MockClassInfo {
18221822
} else if (constant.isInt) {
18231823
return literalNum(constant.intValue);
18241824
} else if (constant.isString) {
1825-
return literalString(constant.stringValue, raw: true);
1825+
// code_builder writes all strings with single quotes.
1826+
// Raw single quoted strings may not contain single quotes,
1827+
// so escape dollar signs and use a non-raw string instead.
1828+
final stringValue = constant.stringValue.replaceAll('\$', '\\\$');
1829+
return literalString(stringValue);
18261830
} else if (constant.isList) {
18271831
return literalConstList([
18281832
for (final element in constant.listValue)

test/builder/auto_mocks_test.dart

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,46 @@ void main() {
281281
await expectSingleNonNullableOutput(
282282
dedent(r'''
283283
class Foo {
284-
void m([String a = 'Hello', String b = 'Hello ' r"World"]) {}
284+
void m([String a = 'Hello', String b = "World"]) {}
285285
}
286286
'''),
287287
_containsAllOf(dedent2('''
288288
void m([
289-
String? a = r'Hello',
290-
String? b = r'Hello World',
289+
String? a = 'Hello',
290+
String? b = 'World',
291+
]) =>
292+
''')),
293+
);
294+
});
295+
296+
test('matches string literal parameter default values with quote characters',
297+
() async {
298+
await expectSingleNonNullableOutput(
299+
dedent(r'''
300+
class Foo {
301+
void m([String a = 'Hel"lo', String b = "Wor'ld"]) {}
302+
}
303+
'''),
304+
_containsAllOf(dedent2('''
305+
void m([
306+
String? a = 'Hel"lo',
307+
String? b = 'Wor\\'ld',
308+
]) =>
309+
''')),
310+
);
311+
});
312+
313+
test('matches raw string literal parameter default values', () async {
314+
await expectSingleNonNullableOutput(
315+
dedent(r'''
316+
class Foo {
317+
void m([String a = r'$Hello', String b = r"$World"]) {}
318+
}
319+
'''),
320+
_containsAllOf(dedent2('''
321+
void m([
322+
String? a = '\\\$Hello',
323+
String? b = '\\\$World',
291324
]) =>
292325
''')),
293326
);
@@ -337,8 +370,8 @@ void main() {
337370
_containsAllOf(dedent2('''
338371
void m(
339372
[Map<int, String>? a = const {
340-
1: r'a',
341-
2: r'b',
373+
1: 'a',
374+
2: 'b',
342375
}]) =>
343376
''')),
344377
);
@@ -354,8 +387,8 @@ void main() {
354387
_containsAllOf(dedent2('''
355388
void m(
356389
[Map<int, String>? a = const {
357-
1: r'a',
358-
2: r'b',
390+
1: 'a',
391+
2: 'b',
359392
}]) =>
360393
''')),
361394
);

0 commit comments

Comments
 (0)