Skip to content

Commit 0969123

Browse files
committed
Merge remote-tracking branch 'upstream/master' into diagnostics
2 parents e6883bf + 13817df commit 0969123

24 files changed

Lines changed: 420 additions & 438 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
os: [ubuntu-latest, windows-latest]
2727

2828
steps:
29-
- uses: actions/checkout@v1
29+
- uses: actions/checkout@v2
3030
- name: Use Node.js 12.13.1
3131
uses: actions/setup-node@v1
3232
with:

.github/workflows/release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13-
- uses: actions/checkout@v1
14-
- uses: actions/setup-node@v1
13+
- uses: actions/checkout@v2
14+
- name: Use Node.js 12.13.1
15+
uses: actions/setup-node@v1
1516
with:
17+
node-version: 12.13.1
1618
registry-url: "https://registry.npmjs.org"
1719
- run: npm ci
1820
- run: npm run build

CHANGELOG.md

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

3+
## 0.30.0
4+
5+
- **Breaking:** We dropped support for `var` variables. If you still have any `var` variable declarations, please use `let` or `const` instead.
6+
- **Breaking:** We now depend on Node.js >= 12.13.0
7+
- Added support for string `trimLeft`, `trimRight`, `trimStart` and `trimEnd`.
8+
- Added support for `console.error`, `console.warn` and `console.info` , they will all be transpiled to Lua's `print`.
9+
- Avoided exporting anonymous identifiers.
10+
- Fixed an issue when assigning to an already-exported variable.
11+
- Math.atan2 will now be transpiled to the correct Lua atan2 (or atan for 5.3) method.
12+
- Fixed various destructuring issues.
13+
- Fixed incorrect error for intersection types containing built-ins (like `number` or `string`)
14+
- Modules containing `import` or `export` will now always be recognized as module to match TypeScript's logic.
15+
- Fixed `true` not being recognized as lua keyword.
16+
- Fixed inaccuracies in switch case variable scoping.
17+
- Fixed various problems with variables being global instead of local.
18+
19+
### Internal:
20+
21+
- Refactored transformation pipeline from one big LuaTransformer class to many small modules.
22+
- Moved class construction methods from transformer to LuaLib.
23+
- Upgraded dependencies.
24+
325
## 0.29.0
426

527
- Added bundling support using options `luaBundle` and `luaBundleEntry` (so **not** TS's outFile). This will bundle all output files into one single bundle file, with _luaBundleEntry_ as entry point. For more information on these options see https://github.com/TypeScriptToLua/TypeScriptToLua/wiki#tstl-specific-options

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.29.1",
3+
"version": "0.30.1",
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/LuaPrinter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,9 @@ export class LuaPrinter {
738738
public printMethodCallExpression(expression: lua.MethodCallExpression): SourceNode {
739739
const chunks = [];
740740

741-
const prefix = this.printExpression(expression.prefixExpression);
741+
const prefix = lua.isStringLiteral(expression.prefixExpression)
742+
? this.printExpression(lua.createParenthesizedExpression(expression.prefixExpression))
743+
: this.printExpression(expression.prefixExpression);
742744

743745
const name = this.printIdentifier(expression.name);
744746

src/transformation/builtins/array.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { unsupportedProperty } from "../utils/diagnostics";
55
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
6-
import { isExplicitArrayType } from "../utils/typescript";
76
import { PropertyCallExpression, transformArguments } from "../visitors/call";
87

98
export function transformArrayPrototypeCall(
109
context: TransformationContext,
1110
node: PropertyCallExpression
1211
): lua.CallExpression | undefined {
1312
const expression = node.expression;
14-
const ownerType = context.checker.getTypeAtLocation(expression.expression);
1513
const signature = context.checker.getResolvedSignature(node);
1614
const params = transformArguments(context, node.arguments, signature);
1715
const caller = context.transformExpression(expression.expression);
@@ -79,9 +77,7 @@ export function transformArrayPrototypeCall(
7977
case "flatMap":
8078
return transformLuaLibFunction(context, LuaLibFeature.ArrayFlatMap, node, caller, ...params);
8179
default:
82-
if (isExplicitArrayType(context, ownerType)) {
83-
context.diagnostics.push(unsupportedProperty(node, "array", expressionName));
84-
}
80+
context.diagnostics.push(unsupportedProperty(node, "array", expressionName));
8581
}
8682
}
8783

src/transformation/builtins/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import { assume } from "../../utils";
44
import { TransformationContext } from "../context";
55
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
66
import { getIdentifierSymbolId } from "../utils/symbols";
7-
import { isArrayType, isFunctionType, isNumberType, isStandardLibraryType, isStringType } from "../utils/typescript";
7+
import {
8+
hasStandardLibrarySignature,
9+
isArrayType,
10+
isFunctionType,
11+
isNumberType,
12+
isStandardLibraryType,
13+
isStringType,
14+
} from "../utils/typescript";
815
import { PropertyCallExpression } from "../visitors/call";
916
import { checkForLuaLibType } from "../visitors/class/new";
1017
import { transformArrayProperty, transformArrayPrototypeCall } from "./array";
@@ -83,22 +90,19 @@ export function transformBuiltinCallExpression(
8390
}
8491
}
8592

86-
if (isStringType(context, ownerType)) {
93+
if (isStringType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
8794
return transformStringPrototypeCall(context, node);
8895
}
8996

90-
if (isNumberType(context, ownerType)) {
97+
if (isNumberType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
9198
return transformNumberPrototypeCall(context, node);
9299
}
93100

94-
if (isArrayType(context, ownerType)) {
95-
const result = transformArrayPrototypeCall(context, node);
96-
if (result) {
97-
return result;
98-
}
101+
if (isArrayType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
102+
return transformArrayPrototypeCall(context, node);
99103
}
100104

101-
if (isFunctionType(context, ownerType)) {
105+
if (isFunctionType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
102106
return transformFunctionPrototypeCall(context, node);
103107
}
104108

src/transformation/builtins/string.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { unsupportedProperty } from "../utils/diagnostics";
55
import { createExpressionPlusOne } from "../utils/lua-ast";
66
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
77
import { PropertyCallExpression, transformArguments } from "../visitors/call";
8-
import { transformIdentifier } from "../visitors/identifier";
98

109
function createStringCall(methodName: string, tsOriginal: ts.Node, ...params: lua.Expression[]): lua.CallExpression {
1110
const stringIdentifier = lua.createIdentifier("string");
@@ -124,37 +123,6 @@ export function transformStringPrototypeCall(
124123
return transformLuaLibFunction(context, LuaLibFeature.StringPadStart, node, caller, ...params);
125124
case "padEnd":
126125
return transformLuaLibFunction(context, LuaLibFeature.StringPadEnd, node, caller, ...params);
127-
128-
case "byte":
129-
case "char":
130-
case "dump":
131-
case "find":
132-
case "format":
133-
case "gmatch":
134-
case "gsub":
135-
case "len":
136-
case "lower":
137-
case "match":
138-
case "pack":
139-
case "packsize":
140-
case "rep":
141-
case "reverse":
142-
case "sub":
143-
case "unpack":
144-
case "upper":
145-
// Allow lua's string instance methods
146-
let stringVariable = context.transformExpression(expression.expression);
147-
if (ts.isStringLiteralLike(expression.expression)) {
148-
// "foo":method() needs to be ("foo"):method()
149-
stringVariable = lua.createParenthesizedExpression(stringVariable);
150-
}
151-
152-
return lua.createMethodCallExpression(
153-
stringVariable,
154-
transformIdentifier(context, expression.name),
155-
params,
156-
node
157-
);
158126
default:
159127
context.diagnostics.push(unsupportedProperty(node, "string", expressionName));
160128
}

src/transformation/utils/typescript/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ export function isFirstDeclaration(context: TransformationContext, node: ts.Vari
4343
return firstDeclaration === node;
4444
}
4545

46+
function isStandardLibraryDeclaration(context: TransformationContext, declaration: ts.Declaration): boolean {
47+
const sourceFile = declaration.getSourceFile();
48+
if (!sourceFile) {
49+
return false;
50+
}
51+
52+
return context.program.isSourceFileDefaultLibrary(sourceFile);
53+
}
54+
4655
export function isStandardLibraryType(
4756
context: TransformationContext,
4857
type: ts.Type,
@@ -59,12 +68,16 @@ export function isStandardLibraryType(
5968
return true;
6069
}
6170

62-
const sourceFile = declaration.getSourceFile();
63-
if (!sourceFile) {
64-
return false;
65-
}
71+
return isStandardLibraryDeclaration(context, declaration);
72+
}
6673

67-
return context.program.isSourceFileDefaultLibrary(sourceFile);
74+
export function hasStandardLibrarySignature(
75+
context: TransformationContext,
76+
callExpression: ts.CallExpression
77+
): boolean {
78+
const signature = context.checker.getResolvedSignature(callExpression);
79+
80+
return signature && signature.declaration ? isStandardLibraryDeclaration(context, signature.declaration) : false;
6881
}
6982

7083
export function inferAssignedType(context: TransformationContext, expression: ts.Expression): ts.Type {

0 commit comments

Comments
 (0)