Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- `Function.length` is supported now

- Fixed `string.replace` incorrectly escaping some `replaceValue` characters (`().+-*?[^$`)

## 0.34.0

- Added new `"luaTarget"` option value - `"universal"`. Choosing this target makes TypeScriptToLua generate code compatible with all supported Lua targets.
Expand Down
2 changes: 1 addition & 1 deletion src/lualib/StringReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function __TS__StringReplace(
[searchValue] = string.gsub(searchValue, "[%%%(%)%.%+%-%*%?%[%^%$]", "%%%1");

if (typeof replaceValue === "string") {
[replaceValue] = string.gsub(replaceValue, "[%%%(%)%.%+%-%*%?%[%^%$]", "%%%1");
[replaceValue] = string.gsub(replaceValue, "%%", "%%%%");
const [result] = string.gsub(source, searchValue, replaceValue, 1);
return result;
} else {
Expand Down
6 changes: 4 additions & 2 deletions test/unit/builtins/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ test.each([
{ inp: "hello test", searchValue: "test", replaceValue: "" },
{ inp: "hello test", searchValue: "test", replaceValue: "world" },
{ inp: "hello test", searchValue: "test", replaceValue: "%world" },
{ inp: "hello test", searchValue: "test", replaceValue: "." },
{ inp: "hello %test", searchValue: "test", replaceValue: "world" },
{ inp: "hello %test", searchValue: "%test", replaceValue: "world" },
{ inp: "hello test", searchValue: "test", replaceValue: (): string => "a" },
{ inp: "hello test", searchValue: "test", replaceValue: (): string => "%a" },
{ inp: "hello test.", searchValue: ".", replaceValue: "$" },
{ inp: "hello test", searchValue: "test", replaceValue: () => "a" },
{ inp: "hello test", searchValue: "test", replaceValue: () => "%a" },
{ inp: "aaa", searchValue: "a", replaceValue: "b" },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this? I don't think there is another test that replaces multiple references in the same string.

])("string.replace (%p)", ({ inp, searchValue, replaceValue }) => {
util.testExpression`"${inp}".replace(${util.formatCode(searchValue, replaceValue)})`.expectToMatchJsResult();
Expand Down