Skip to content

Issues with non-lua operators on properties of objects #221

Description

@tomblind

Operators such as '+=', '++', etc... can be broken easily right now.

Example 1:

declare function foo(): number;
declare const a: number[];
a[foo()]++;

=>

a[(foo())+1] = (a[(foo())+1]+1);

Here foo() will be called multiple times, producing possibly different values each time or having other undesired side-effects.

Example 2:

declare function bar(): number[];
let f = bar()[0]++;

=>

local f = (function()
    local __originalValuebar()[(0)+1]0 = bar()[(0)+1];
    bar()[(0)+1] = (bar()[(0)+1]+1);
    return __originalValuebar()[(0)+1]0
end
)();

This one won't even compile in lua.

These kinds of issues could be solved by detecting if the value being operated on is a property and caching the object and index parts before performing the operation. This could be wrapped in specialized polyfill functions like the ternary operator is:

function __TS__PropertyPostInc(o, i)
    local original = o[i]
    o[i] = o[i] + 1
    return original
end

--Example 1
__TS__PropertyPostInc(a, foo())

--Example 2
local f = __TS__PropertyPostInc(bar(), 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions