From 648ff8ec7f2ed7985fea894b10aea03dd3ff31eb Mon Sep 17 00:00:00 2001 From: Jumpaku Date: Fri, 14 Jun 2024 22:10:04 +0900 Subject: [PATCH] fix(embed): escape dollar symbol of JSON string literal --- embed/lib/src/literal/dart_literals.dart | 2 +- .../literal/literal_embedding_generator_test.dart | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/embed/lib/src/literal/dart_literals.dart b/embed/lib/src/literal/dart_literals.dart index 4610e13..636943b 100644 --- a/embed/lib/src/literal/dart_literals.dart +++ b/embed/lib/src/literal/dart_literals.dart @@ -72,7 +72,7 @@ class StringLiteral extends DartLiteral { @override String toString() { - final literal = value.replaceAll('"', r'\"'); + final literal = value.replaceAll('"', r'\"').replaceAll(r'$', r'\$'); return '"$literal"'; } } diff --git a/embed/test/literal/literal_embedding_generator_test.dart b/embed/test/literal/literal_embedding_generator_test.dart index 2534850..1f8eeed 100644 --- a/embed/test/literal/literal_embedding_generator_test.dart +++ b/embed/test/literal/literal_embedding_generator_test.dart @@ -243,3 +243,14 @@ const _$mapPattern = {"a": 0, "b": 0.0, "c": true}; """, ) Map? mapPattern; + +@TestEmbedLiteral( + extension: "json", + content: r""" +{ "dollar": "$" } +""", + shouldGenerate: r""" +const _$stringLiteralsDollar = (dollar: "\$"); +""", +) +var stringLiteralsDollar;