seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
71 stars 9 forks source link

`remove_interpolated_string` can be simplified and made to match Luau behavior more precisely #204

Closed sircfenner closed 2 months ago

sircfenner commented 2 months ago

Currently, the following expression:

`hello {name}!`

is translated as:

string.format("hello %s!", tostring(name))

This can be simplified to:

string.format("hello %*!", name)

This also more closely matches how Luau compiles it and means we don't need to track if tostring has been redefined somewhere.

In fact, the most accurate translation is:

("hello %*!"):format(name)

This would also mean we don't need to track if string was redefined.

This may have a minor performance drawback as currently Luau doesn't optimize this method-call style for string functions.

jeparlefrancais commented 2 months ago

There is a strategy parameter to the existing rule which lets you pick between both translations

When set to string, it will use the %s formatter and add the call to convert the value. You can get it to use %* when it is set to tostring.

It is mentioned there: https://darklua.com/docs/rules/remove_interpolated_string/#parameters