Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@ export function createTableIndexExpression(
}

export type AssignmentLeftHandSideExpression = Identifier | TableIndexExpression;
export function isAssignmentLeftHandSideExpression(node: Node): node is AssignmentLeftHandSideExpression {
return isIdentifier(node) || isTableIndexExpression(node);
}

export type FunctionDefinition = (VariableDeclarationStatement | AssignmentStatement) & {
right: [FunctionExpression];
Expand Down
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export enum LuaLibFeature {
ObjectEntries = "ObjectEntries",
ObjectFromEntries = "ObjectFromEntries",
ObjectKeys = "ObjectKeys",
ObjectRest = "ObjectRest",
ObjectValues = "ObjectValues",
Set = "Set",
WeakMap = "WeakMap",
Expand Down
297 changes: 166 additions & 131 deletions src/LuaTransformer.ts

Large diffs are not rendered by default.

44 changes: 13 additions & 31 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export function isAssignmentPattern(node: ts.Node): node is ts.AssignmentPattern
return ts.isObjectLiteralExpression(node) || ts.isArrayLiteralExpression(node);
}

export function isDestructuringAssignment(node: ts.Node): node is ts.DestructuringAssignment {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript has this one too, any reason we're not using that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about that? I couldn't find any functions that have a is ts.DestructuringAssignment type guard

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the block with @internal annotation, so can't be used directly

return (
ts.isBinaryExpression(node) &&
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
isAssignmentPattern(node.left)
);
}

export function getExportable(exportSpecifiers: ts.NamedExports, resolver: EmitResolver): ts.ExportSpecifier[] {
return exportSpecifiers.elements.filter(exportSpecifier => resolver.isValueAliasDeclaration(exportSpecifier));
}
Expand Down Expand Up @@ -400,7 +408,7 @@ export function getCustomNodeDirectives(node: ts.Node): Map<DecoratorKind, Decor
const directivesMap = new Map<DecoratorKind, Decorator>();

ts.getJSDocTags(node).forEach(tag => {
const tagName = tag.tagName.escapedText as string;
const tagName = tag.tagName.text;
if (Decorator.isValid(tagName)) {
const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []);
directivesMap.set(dec.kind, dec);
Expand Down Expand Up @@ -928,34 +936,6 @@ export function moduleHasEmittedBody(
return false;
}

export function isValidFlattenableDestructuringAssignmentLeftHandSide(
node: ts.DestructuringAssignment,
checker: ts.TypeChecker,
program: ts.Program
): boolean {
if (ts.isArrayLiteralExpression(node.left)) {
if (node.left.elements.length > 0) {
return !node.left.elements.some(element => {
switch (element.kind) {
case ts.SyntaxKind.Identifier:
case ts.SyntaxKind.PropertyAccessExpression:
if (isArrayLength(element, checker, program)) {
return true;
}
case ts.SyntaxKind.ElementAccessExpression:
// Can be on the left hand side of a Lua assignment statement
return false;
default:
// Cannot be
return true;
}
});
}
}

return false;
}

export function isArrayLength(
expression: ts.Expression,
checker: ts.TypeChecker,
Expand All @@ -971,8 +951,10 @@ export function isArrayLength(
}

const name = ts.isPropertyAccessExpression(expression)
? (expression.name.escapedText as string)
: ts.isStringLiteral(expression.argumentExpression) && expression.argumentExpression.text;
? expression.name.text
: ts.isStringLiteral(expression.argumentExpression)
? expression.argumentExpression.text
: undefined;

return name === "length";
}
Expand Down
3 changes: 0 additions & 3 deletions src/TSTLErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const getLuaTargetName = (version: LuaTarget) => (version === LuaTarget.LuaJIT ?
export const CouldNotCast = (castName: string) =>
new Error(`Failed to cast all elements to expected type using ${castName}.`);

export const ForbiddenEllipsisDestruction = (node: ts.Node) =>
new TranspileError(`Ellipsis destruction is not allowed.`, node);

export const ForbiddenForIn = (node: ts.Node) =>
new TranspileError(`Iterating over arrays with 'for ... in' is not allowed.`, node);

Expand Down
14 changes: 14 additions & 0 deletions src/lualib/ObjectRest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function __TS__ObjectRest<K extends keyof any, V>(
this: void,
target: Record<K, V>,
usedProperties: Partial<Record<K, true>>
): Partial<Record<K, V>> {
const result: Partial<Record<K, V>> = {};
for (const property in target) {
if (!usedProperties[property]) {
result[property] = target[property];
}
}

return result;
}
10 changes: 8 additions & 2 deletions test/unit/destructuring.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from "../util";

const allBindings = "x, y, z";
const allBindings = "x, y, z, rest";
const testCases = [
{ binding: "{ x }", value: { x: true } },
{ binding: "{ x, y }", value: { x: false, y: true } },
Expand All @@ -9,12 +9,17 @@ const testCases = [
{ binding: "{ x, y = true }", value: { x: false, y: false } },
{ binding: "{ x = true }", value: {} },
{ binding: "{ x, y = true }", value: { x: false } },
{ binding: "{ ...rest }", value: {} },
{ binding: "{ x, ...rest }", value: { x: "x" } },
{ binding: "{ x, ...rest }", value: { x: "x", y: "y", z: "z" } },

{ binding: "[]", value: [] },
{ binding: "[x, y]", value: ["x", "y"] },
{ binding: "[x, , y]", value: ["x", "", "y"] },
{ binding: "[x = true]", value: [false] },
{ binding: "[[x, y]]", value: [["x", "y"]] },
{ binding: "[x, ...rest]", value: ["x"] },
{ binding: "[x, ...rest]", value: ["x", "y", "z"] },

{ binding: "{ y: [z = true] }", value: { y: [false] } },
{ binding: "{ x: [x, y] }", value: { x: ["x", "y"] } },
Expand Down Expand Up @@ -47,7 +52,8 @@ test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => {
});

// TODO: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/695
test.each(testCases.filter(x => x.binding !== "[x, , y]"))(
// TODO: https://github.com/microsoft/TypeScript/issues/32656
test.each(testCases.filter(x => x.binding !== "[x, , y]" && x.binding !== "{ x, ...rest }"))(
"in exported variable declaration (%p)",
({ binding, value }) => {
util.testModule`
Expand Down