utility-library / leap

Pre-processor of a "modernized" and extended version of lua that pre-processes in pure executable lua. Mainly intended for FiveM
MIT License
20 stars 4 forks source link

(request) Object destructuring #4

Open JaRoLoz opened 1 year ago

JaRoLoz commented 1 year ago

Hi, it would be nice if this syntax could be implemented:

function someFunction({ prop1, prop2 })
    print(prop1, prop2)
end
XenoS-ITA commented 1 year ago

You can already do an object destruction but not in the parameters, example:

function someFunction(obj)
    prop1, prop2 = ...obj
    print(prop1, prop2)
end

However, we could also add the version you mentioned, have you seen similar syntax yet? (like a documentation of some languages or something) @MrFreex what do you think?

JaRoLoz commented 1 year ago

I've been searching for any documentation on how to implement it, but haven't found anything. What i've came up with is just replacing the destructured object in the parameters with a dummy parameter name, such as __parameter, and then replacing the used propperties of the object inside the function with __parameter.property. Example:

# before preprocessing
function myFunction({ prop1, prop2 })
    print(prop1, prop2)
end
# after preprocessing
function myFunction(__parameter)
    print(__parameter.prop1, __parameter,prop2)
end
XenoS-ITA commented 1 year ago

that method could be quite a challenge since leap works on regex, it does not use AST or parser, so it is quite complicated to do it with regex.