Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A rule for removing duplicated numeric field in table #210

Open
jiwonz opened this issue Sep 4, 2024 · 2 comments
Open

A rule for removing duplicated numeric field in table #210

jiwonz opened this issue Sep 4, 2024 · 2 comments

Comments

@jiwonz
Copy link
Contributor

jiwonz commented Sep 4, 2024

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.

@jiwonz jiwonz changed the title A rule for removing overlapped numeric field in table A rule for removing duplicated numeric field in table Sep 5, 2024
@ewd3v
Copy link

ewd3v commented Nov 7, 2024

This Lua behavior isn't exclusive to numeric keys, this applies to any table key.

print({ ["a"] = 1, ["a"] = 2 }) --> { ["a"] = 2 }

-- Another example showing it applies to all keys (no matter the type).
local userdata = newproxy()
print({ [userdata] = 1, [userdata] = 2 }) --> { [Userdata(...)] = 2 }

Perhaps it could be called "remove_duplicate_table_keys"?

@jiwonz
Copy link
Contributor Author

jiwonz commented Nov 12, 2024

This Lua behavior isn't exclusive to numeric keys, this applies to any table key.

print({ ["a"] = 1, ["a"] = 2 }) --> { ["a"] = 2 }

-- Another example showing it applies to all keys (no matter the type).
local userdata = newproxy()
print({ [userdata] = 1, [userdata] = 2 }) --> { [Userdata(...)] = 2 }

Perhaps it could be called "remove_duplicate_table_keys"?

You are correct. So I named it remove_redeclared_keys in my fork implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants