Skip to content

Commit

Permalink
fix(embed): escape dollar symbol of JSON string literal (#21)
Browse files Browse the repository at this point in the history
With the original version, `embed.dart` cannot generate a compilable Dart code if the source JSON has a string literal containing a dollar symbol ($). For example, the following code will be generated from {"dollar": "$"}:

```dart
const _$variable = (dollar: "$");
// the string literal "$" is not valid, which is a beginning of string interpolation.
```

In the revised version, I fixed it to generate the following code from that JSON:

```dart
const _$variable = (dollar: "\$");
// $ is escaped!
```

I also added a test case.
  • Loading branch information
Jumpaku authored Jun 15, 2024
1 parent 894ada9 commit 7bc9b51
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions embed/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.3.1

- fix(embed): escape dollar symbol of JSON string literal (#21 by @Jumpaku)

## 1.3.0

- Update `toml` dependency to 0.15.0 (#17 by @bramp)
Expand Down
2 changes: 1 addition & 1 deletion embed/lib/src/literal/dart_literals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class StringLiteral extends DartLiteral<String> {

@override
String toString() {
final literal = value.replaceAll('"', r'\"');
final literal = value.replaceAll('"', r'\"').replaceAll(r'$', r'\$');
return '"$literal"';
}
}
Expand Down
2 changes: 1 addition & 1 deletion embed/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: embed
description: Code generation for embedding arbitrary file content into Dart code.
version: 1.3.0
version: 1.3.1
repository: https://github.com/fujidaiti/embed.dart.git

environment:
Expand Down
11 changes: 11 additions & 0 deletions embed/test/literal/literal_embedding_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,14 @@ const _$mapPattern = {"a": 0, "b": 0.0, "c": true};
""",
)
Map<String, dynamic>? mapPattern;

@TestEmbedLiteral(
extension: "json",
content: r"""
{ "dollar": "$" }
""",
shouldGenerate: r"""
const _$stringLiteralsDollar = (dollar: "\$");
""",
)
var stringLiteralsDollar;

0 comments on commit 7bc9b51

Please sign in to comment.