seaofvoices / darklua

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

Add rule to remove interpolated strings #156

Closed jeparlefrancais closed 9 months ago

jeparlefrancais commented 9 months ago

Closes #92

This new rule replaces interpolated strings with string.format calls. The rule has a strategy property that defines how it converts the interpolated strings.

The default strategy "string" uses the %s string specifier in string.format calls and wrap each interpolated value in a tostring call.

The other available strategy is tostring, which uses the %* string specifier in string.format. This strategy does not need to wrap interpolated values.

Example:

local _ = `{object}`
-- generates:
local _ = tostring(object)
local _ = `Value = {object}`
-- generates:
local _ = string.format("Value = %s", tostring(object))
-- or generates with the `tostring` strategy:
local _ = string.format("Value = %*", object)

Todo:

github-actions[bot] commented 9 months ago

Coverage after merging remove-interpolated-str-rule into main will be

87.08%

Coverage Report for Changed Files
FileStmtsBranchesFuncsLinesUncovered Lines
src
   parser.rs99.15%100%73.33%99.38%37, 43, 507–513, 55–59, 66
src/nodes
   arguments.rs86.81%100%85.71%87.14%130–131, 153–157, 159, 189, 57–59, 87–89
src/nodes/expressions
   interpolated_string.rs81.09%100%79.49%81.55%136–139, 164–167, 198–201, 203, 231–233, 306–312, 356–361, 386–389, 48–50, 76–79
src/process
   scope_visitor.rs81.70%100%76.92%82.65%132, 164–167, 180–190, 195, 219–221, 227–229, 249–251, 259–261
src/rules
   mod.rs89.03%100%77.78%90.49%105–107, 111, 121–123, 141–143, 145, 176–178, 264, 305–306, 341–343, 406–408, 66
   remove_interpolated_string.rs90.59%100%79.17%92.13%13, 211–218