Skip to content

Commit ac40ce2

Browse files
committed
Merge remote-tracking branch 'upstream/master' into diagnostics
2 parents 0969123 + 29632f3 commit ac40ce2

68 files changed

Lines changed: 503 additions & 390 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## 0.31.0
4+
5+
- **Breaking:** The old annotation syntax (`/* !varArg */`) **no longer works**, the only currently supported syntax is:
6+
7+
`/** @varArg */`.
8+
9+
- **Breaking:** Fixed some cases where variables were **incorrectly** not labeled `local`. The only variables that are implicitly put in the global context are _top-level variables in non-module files, without any imports or exports in their file_.
10+
11+
- Moved handling of parentheses out of the transformers and unified this logic in the printer. This might result in some more parentheses in the generated code, but also makes it more correct and fixes some related bugs.
12+
13+
- Added support for `array.includes`.
14+
15+
- Fixed a bug breaking global augmentation.
16+
17+
- Fixed hoisting breaking if there were synthetic nodes in the AST (i.e. when a TS transformer modified the AST).
18+
319
## 0.30.0
420

521
- **Breaking:** We dropped support for `var` variables. If you still have any `var` variable declarations, please use `let` or `const` instead.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-to-lua",
3-
"version": "0.30.1",
3+
"version": "0.31.0",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
55
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
66
"license": "MIT",

src/LuaAST.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export enum SyntaxKind {
3636
TableExpression,
3737
UnaryExpression,
3838
BinaryExpression,
39-
ParenthesizedExpression,
4039
CallExpression,
4140
MethodCallExpression,
4241
Identifier,
@@ -669,24 +668,6 @@ export function createBinaryExpression(
669668
return expression;
670669
}
671670

672-
export interface ParenthesizedExpression extends Expression {
673-
kind: SyntaxKind.ParenthesizedExpression;
674-
innerExpression: Expression;
675-
}
676-
677-
export function isParenthesizedExpression(node: Node): node is ParenthesizedExpression {
678-
return node.kind === SyntaxKind.ParenthesizedExpression;
679-
}
680-
681-
export function createParenthesizedExpression(
682-
innerExpression: Expression,
683-
tsOriginal?: ts.Node
684-
): ParenthesizedExpression {
685-
const expression = createNode(SyntaxKind.ParenthesizedExpression, tsOriginal) as ParenthesizedExpression;
686-
expression.innerExpression = innerExpression;
687-
return expression;
688-
}
689-
690671
export interface CallExpression extends Expression {
691672
kind: SyntaxKind.CallExpression;
692673
expression: Expression;

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum LuaLibFeature {
88
ArrayForEach = "ArrayForEach",
99
ArrayFind = "ArrayFind",
1010
ArrayFindIndex = "ArrayFindIndex",
11+
ArrayIncludes = "ArrayIncludes",
1112
ArrayIndexOf = "ArrayIndexOf",
1213
ArrayMap = "ArrayMap",
1314
ArrayPush = "ArrayPush",

src/LuaPrinter.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ function isSimpleExpression(expression: lua.Expression): boolean {
6464
case lua.SyntaxKind.BinaryExpression:
6565
const binaryExpression = expression as lua.BinaryExpression;
6666
return isSimpleExpression(binaryExpression.left) && isSimpleExpression(binaryExpression.right);
67-
68-
case lua.SyntaxKind.ParenthesizedExpression:
69-
return isSimpleExpression((expression as lua.ParenthesizedExpression).innerExpression);
7067
}
7168

7269
return true;
@@ -560,8 +557,6 @@ export class LuaPrinter {
560557
return this.printUnaryExpression(expression as lua.UnaryExpression);
561558
case lua.SyntaxKind.BinaryExpression:
562559
return this.printBinaryExpression(expression as lua.BinaryExpression);
563-
case lua.SyntaxKind.ParenthesizedExpression:
564-
return this.printParenthesizedExpression(expression as lua.ParenthesizedExpression);
565560
case lua.SyntaxKind.CallExpression:
566561
return this.printCallExpression(expression as lua.CallExpression);
567562
case lua.SyntaxKind.MethodCallExpression:
@@ -685,46 +680,40 @@ export class LuaPrinter {
685680
const chunks: SourceChunk[] = [];
686681

687682
chunks.push(this.printOperator(expression.operator));
688-
chunks.push(this.printExpression(expression.operand));
683+
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.operand));
689684

690685
return this.createSourceNode(expression, chunks);
691686
}
692687

693688
public printBinaryExpression(expression: lua.BinaryExpression): SourceNode {
694689
const chunks: SourceChunk[] = [];
695690

696-
chunks.push(this.printExpression(expression.left));
691+
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.left));
697692
chunks.push(" ", this.printOperator(expression.operator), " ");
698-
chunks.push(this.printExpression(expression.right));
693+
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.right));
699694

700695
return this.createSourceNode(expression, chunks);
701696
}
702697

703-
private canStripParenthesis(expression: lua.Expression): boolean {
704-
return (
705-
lua.isParenthesizedExpression(expression) ||
706-
lua.isTableIndexExpression(expression) ||
707-
lua.isCallExpression(expression) ||
708-
lua.isMethodCallExpression(expression) ||
709-
lua.isIdentifier(expression) ||
710-
lua.isNilLiteral(expression) ||
711-
lua.isNumericLiteral(expression) ||
712-
lua.isBooleanLiteral(expression)
713-
);
698+
private printExpressionInParenthesesIfNeeded(expression: lua.Expression): SourceNode {
699+
return this.needsParenthesis(expression)
700+
? this.createSourceNode(expression, ["(", this.printExpression(expression), ")"])
701+
: this.printExpression(expression);
714702
}
715703

716-
public printParenthesizedExpression(expression: lua.ParenthesizedExpression): SourceNode {
717-
const innerExpression = this.printExpression(expression.innerExpression);
718-
if (this.canStripParenthesis(expression.innerExpression)) {
719-
return this.createSourceNode(expression, innerExpression);
720-
}
721-
return this.createSourceNode(expression, ["(", innerExpression, ")"]);
704+
private needsParenthesis(expression: lua.Expression): boolean {
705+
return (
706+
lua.isBinaryExpression(expression) ||
707+
lua.isFunctionExpression(expression) ||
708+
lua.isTableExpression(expression) ||
709+
(lua.isUnaryExpression(expression) && expression.operator === lua.SyntaxKind.NotOperator)
710+
);
722711
}
723712

724713
public printCallExpression(expression: lua.CallExpression): SourceNode {
725714
const chunks = [];
726715

727-
chunks.push(this.printExpression(expression.expression), "(");
716+
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.expression), "(");
728717

729718
if (expression.params) {
730719
chunks.push(...this.printExpressionList(expression.params));
@@ -738,13 +727,14 @@ export class LuaPrinter {
738727
public printMethodCallExpression(expression: lua.MethodCallExpression): SourceNode {
739728
const chunks = [];
740729

741-
const prefix = lua.isStringLiteral(expression.prefixExpression)
742-
? this.printExpression(lua.createParenthesizedExpression(expression.prefixExpression))
743-
: this.printExpression(expression.prefixExpression);
730+
const prefix =
731+
this.needsParenthesis(expression.prefixExpression) || lua.isStringLiteral(expression.prefixExpression)
732+
? ["(", this.printExpression(expression.prefixExpression), ")"]
733+
: [this.printExpression(expression.prefixExpression)];
744734

745735
const name = this.printIdentifier(expression.name);
746736

747-
chunks.push(prefix, ":", name, "(");
737+
chunks.push(...prefix, ":", name, "(");
748738

749739
if (expression.params) {
750740
chunks.push(...this.printExpressionList(expression.params));
@@ -766,7 +756,7 @@ export class LuaPrinter {
766756
public printTableIndexExpression(expression: lua.TableIndexExpression): SourceNode {
767757
const chunks: SourceChunk[] = [];
768758

769-
chunks.push(this.printExpression(expression.table));
759+
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.table));
770760
if (lua.isStringLiteral(expression.index) && isValidLuaIdentifier(expression.index.value)) {
771761
chunks.push(".", this.createSourceNode(expression.index, expression.index.value));
772762
} else {

src/lualib/ArrayIncludes.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.includes
2+
function __TS__ArrayIncludes<T>(this: T[], searchElement: T, fromIndex = 0): boolean {
3+
const len = this.length;
4+
let k = fromIndex;
5+
6+
if (fromIndex < 0) {
7+
k = len + fromIndex;
8+
}
9+
10+
if (k < 0) {
11+
k = 0;
12+
}
13+
14+
for (const i of forRange(k, len)) {
15+
if (this[i] === searchElement) {
16+
return true;
17+
}
18+
}
19+
20+
return false;
21+
}

src/transformation/builtins/array.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export function transformArrayPrototypeCall(
4040
return transformLuaLibFunction(context, LuaLibFeature.ArrayFind, node, caller, ...params);
4141
case "findIndex":
4242
return transformLuaLibFunction(context, LuaLibFeature.ArrayFindIndex, node, caller, ...params);
43+
case "includes":
44+
return transformLuaLibFunction(context, LuaLibFeature.ArrayIncludes, node, caller, ...params);
4345
case "indexOf":
4446
return transformLuaLibFunction(context, LuaLibFeature.ArrayIndexOf, node, caller, ...params);
4547
case "map":
@@ -87,10 +89,7 @@ export function transformArrayProperty(
8789
): lua.UnaryExpression | undefined {
8890
switch (node.name.text) {
8991
case "length":
90-
let expression = context.transformExpression(node.expression);
91-
if (lua.isTableExpression(expression)) {
92-
expression = lua.createParenthesizedExpression(expression);
93-
}
92+
const expression = context.transformExpression(node.expression);
9493
return lua.createUnaryExpression(expression, lua.SyntaxKind.LengthOperator, node);
9594
default:
9695
return undefined;

src/transformation/builtins/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ export function transformBuiltinIdentifierExpression(
118118
): lua.Expression | undefined {
119119
switch (node.text) {
120120
case "NaN":
121-
return lua.createParenthesizedExpression(
122-
lua.createBinaryExpression(
123-
lua.createNumericLiteral(0),
124-
lua.createNumericLiteral(0),
125-
lua.SyntaxKind.DivisionOperator,
126-
node
127-
)
121+
return lua.createBinaryExpression(
122+
lua.createNumericLiteral(0),
123+
lua.createNumericLiteral(0),
124+
lua.SyntaxKind.DivisionOperator,
125+
node
128126
);
129127

130128
case "Infinity":

src/transformation/builtins/math.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ export function transformMathCall(
5656
const log1 = lua.createTableIndexExpression(math, lua.createStringLiteral("log"));
5757
const logCall1 = lua.createCallExpression(log1, params);
5858
const e = lua.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
59-
const div = lua.createBinaryExpression(logCall1, e, lua.SyntaxKind.DivisionOperator);
60-
return lua.createParenthesizedExpression(div, node);
59+
return lua.createBinaryExpression(logCall1, e, lua.SyntaxKind.DivisionOperator, node);
6160
}
6261

6362
// math.log(1 + x)

0 commit comments

Comments
 (0)