Skip to content

Commit ffcca57

Browse files
authored
Fix exception trying to push single element to optional chain array (#1257)
1 parent 288ab8c commit ffcca57

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/transformation/utils/typescript/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export function findFirstNodeAbove<T extends ts.Node>(node: ts.Node, callback: (
2525
}
2626

2727
export function findFirstNonOuterParent(node: ts.Node): ts.Node {
28-
let current = node.parent;
28+
let current = ts.getOriginalNode(node).parent;
2929
while (ts.isOuterExpression(current)) {
30-
current = current.parent;
30+
current = ts.getOriginalNode(current).parent;
3131
}
3232
return current;
3333
}

test/unit/builtins/array.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@ test("array.push(...)", () => {
497497
`.expectToMatchJsResult();
498498
});
499499

500+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1256
501+
test.each(["[1, 2, 3]", "undefined"])("array push on optional array", value => {
502+
util.testFunction`
503+
const arr = ${value} as number[] | undefined;
504+
arr?.push(4);
505+
return arr
506+
`.expectToMatchJsResult();
507+
});
508+
500509
test.each([
501510
{ array: [1, 2, 3], expected: [3, 2] },
502511
{ array: [1, 2, 3, null], expected: [3, 2] },

0 commit comments

Comments
 (0)