Skip to content

Commit 6c7c336

Browse files
authored
optimizing varargs on functions passed as parameters (#1042)
1 parent b847804 commit 6c7c336

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/transformation/utils/scope.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export function popScope(context: TransformationContext): Scope {
9393
}
9494

9595
function isDeclaredInScope(symbol: ts.Symbol, scopeNode: ts.Node) {
96-
return symbol?.declarations?.some(d => findFirstNodeAbove(d, (n): n is ts.Node => n === scopeNode));
96+
return symbol?.declarations?.some(
97+
d => findFirstNodeAbove(d, (n): n is ts.Node => n === scopeNode) && !ts.isParameter(d.parent)
98+
);
9799
}
98100

99101
// Checks for references to local functions which haven't been defined yet,

test/unit/__snapshots__/spread.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ end
6666
return ____exports"
6767
`;
6868

69+
exports[`vararg spread optimization curry 1`] = `
70+
"local ____exports = {}
71+
function ____exports.__main(self)
72+
local function test(self, fn, ...)
73+
return fn(nil, ...)
74+
end
75+
return test(
76+
nil,
77+
function(____, arg) return arg end,
78+
\\"foobar\\"
79+
)
80+
end
81+
return ____exports"
82+
`;
83+
6984
exports[`vararg spread optimization finally clause 1`] = `
7085
"local ____exports = {}
7186
function ____exports.__main(self)

test/unit/spread.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ describe("vararg spread optimization", () => {
230230
.expectLuaToMatchSnapshot()
231231
.expectToEqual("b");
232232
});
233+
234+
test("curry", () => {
235+
util.testFunction`
236+
function test<A extends any[]>(fn: (...args: A) => void, ...args: A) {
237+
return fn(...args);
238+
}
239+
return test((arg: string) => arg, "foobar");
240+
`
241+
.expectLuaToMatchSnapshot()
242+
.expectToMatchJsResult();
243+
});
233244
});
234245

235246
describe("vararg spread de-optimization", () => {

0 commit comments

Comments
 (0)