Skip to content

Commit

Permalink
fix: interpolation bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Sep 16, 2022
1 parent 90ece50 commit 9ea5baf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion slang/lib/builder/model/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ _ParamWithArg _parseParamWithArg({
required String input,
required CaseStyle? paramCase,
}) {
final match = RegexUtils.paramWithArg.firstMatch(input)!;
final match = RegexUtils.paramWithArg.firstMatch(input);
if (match == null) {
throw 'Rich text parameters must follow the syntax: "param(default text)"\nGot instead: "$input"\n';
}
return _ParamWithArg(match.group(1)!.toCase(paramCase), match.group(3));
}

Expand Down
5 changes: 3 additions & 2 deletions slang/lib/builder/utils/string_interpolation_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ String _replaceBetween({
buffer.write(curr.substring(0, startIndex));
}

int endIndex = curr.indexOf(endCharacter);
int endIndex =
curr.indexOf(endCharacter, startIndex + startCharacterLength);
if (endIndex == -1) {
buffer.write(curr);
buffer.write(curr.substring(startIndex));
break;
}

Expand Down
25 changes: 25 additions & 0 deletions slang/test/unit/utils/string_interpolation_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ void main() {
expect(input.braces(), 'Hello X');
});

test('start with closing bracket', () {
final input = '} {a}';
expect(input.braces(), '} X');
});

test('ends with opening bracket', () {
final input = '{a} {';
expect(input.braces(), 'X {');
});

test('has double braces instead single braces', () {
final input = 'Hello {{a}} {{b}}';
expect(input.braces(), 'Hello X} X}');
});

test('ignore \\{ only', () {
final input = 'Hello \\{ World!';
expect(input.braces(), 'Hello { World!');
Expand Down Expand Up @@ -69,6 +84,16 @@ void main() {
expect(input.doubleBraces(), 'Hello X');
});

test('start with closing bracket', () {
final input = '}} {{a}}';
expect(input.doubleBraces(), '}} X');
});

test('ends with opening bracket', () {
final input = '{{a}} {{';
expect(input.doubleBraces(), 'X {{');
});

test('ignore \\{{ only', () {
final input = 'Hello \\{{ World!';
expect(input.doubleBraces(), 'Hello {{ World!');
Expand Down

0 comments on commit 9ea5baf

Please sign in to comment.