seaofvoices / darklua

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

A rule for removing duplicated numeric field in table #210

Open jiwonz opened 1 week ago

jiwonz commented 1 week ago

Luau and Lua have different behavior between numeric field in table for example:

Luau:

{ 1, [1] = "A" } -- { "A" }
{ [1] = "A", 1 } -- { 1 }

Lua:

{ 1, [1] = "A" } -- { 1 }
{ [1] = "A", 1 } -- { 1 }

I think Luau checks the orders of them while Lua always think array item is first than indexing. For easy understand, Luau is behaving

local a = { 1, [1] = "A" }

as

local a = { 1 }
a[1] = "A"

So, my idea is

local a = { 1, [1] = "A" }
local b = { [1] = "A", 1 }

into

local a = { "A" }
local b = { 1 }

I'm not sure what to name the rule, but it might help when transpiling Luau to Lua.