lolleko / gmod-typescript

Tool to generate typescript definitions for garrysmod.
MIT License
21 stars 7 forks source link

Replacing generated JS functions with builtin gmod functions #16

Open mattkrins opened 11 months ago

mattkrins commented 11 months ago

Gmod comes with a number of builtin libraries such as the string library. For example:

// JavaScript version
print("haystack".endsWith("needle"))
// Gmod version
print(string.EndsWith("haystack","needle"))

Both of these functions will accomplish the same goal, however if using the JS version, the transpiler will need to add

local function __TS__StringEndsWith(self, searchString, endPosition)
    if endPosition == nil or endPosition > #self then
        endPosition = #self
    end
    return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString
end

to the transpiled output. This will result in a lot of redundant code. Is there a way we can make TypeScriptToLua use the built-in gmod libraries? I'd be happy to write the logic to do so, just not sure if it is possible or where to start.

lolleko commented 11 months ago

Can't you just use the second version? string.EndsWith

mattkrins commented 11 months ago

I suppose, but it means having to look at docs to use basic function prototypes people will generally already know in JS. It would be nice to have a TS compiler factory (or some such) to modify/override the functions being generated.