Skip to content

Commit 503cc68

Browse files
committed
Merge remote-tracking branch 'upstream/master' into new-emit-pipeline
2 parents de5ec24 + 576c7e5 commit 503cc68

30 files changed

+1569
-649
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ node_js:
33
- stable
44

55
script:
6-
- npm run lint
76
- npm run build
87
- npm test -- --coverage
98
after_success: npx codecov

appveyor.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
environment:
33
nodejs_version: "8"
44

5+
# Do not build feature branch with open Pull Requests
6+
skip_branch_with_pr: true
7+
58
# Cache dependencies
69
cache:
710
- node_modules
@@ -19,7 +22,6 @@ test_script:
1922
- node --version
2023
- npm --version
2124
# run tests
22-
- npm run lint
2325
- npm run build
2426
- npm test
2527

build_lualib.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,4 @@ if (fs.existsSync(bundlePath)) {
2525
fs.unlinkSync(bundlePath);
2626
}
2727

28-
fs.writeFileSync(
29-
bundlePath,
30-
LuaLib.loadFeatures(Object.keys(tstl.LuaLibFeature).map(lib => tstl.LuaLibFeature[lib])),
31-
);
28+
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature)));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"scripts": {
2121
"build": "tsc -p tsconfig.json && npm run build-lualib",
2222
"build-lualib": "ts-node ./build_lualib.ts",
23-
"pretest": "ts-node --transpile-only ./build_lualib.ts",
23+
"pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts",
2424
"test": "jest",
2525
"lint": "npm run lint:tslint && npm run lint:prettier",
2626
"lint:prettier": "prettier --check **/*.{js,ts,yml,json} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",

src/CommandLineParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function updateParsedCommandLine(
163163
const hasTwoDashes = args[i].startsWith("--");
164164
const parameterValue = args[i].substr(hasTwoDashes ? 2 : 1);
165165
let argumentName = optionDeclarations[parameterValue] && parameterValue;
166-
if (!hasTwoDashes && !argumentName) {
166+
if (!hasTwoDashes && argumentName === undefined) {
167167
for (const key in optionDeclarations) {
168168
if ((optionDeclarations[key].aliases || []).includes(parameterValue)) {
169169
argumentName = key;
@@ -172,7 +172,7 @@ function updateParsedCommandLine(
172172
}
173173
}
174174

175-
if (argumentName) {
175+
if (argumentName !== undefined) {
176176
const argumentResult = getArgumentValue(argumentName, i, args);
177177
if (argumentResult.isValid === true) {
178178
parsedCommandLine.options[argumentName] = argumentResult.result;

src/Decorator.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class Decorator {
33
return this.getDecoratorKind(decoratorKindString) !== undefined;
44
}
55

6-
public static getDecoratorKind(decoratorKindString: string): DecoratorKind {
6+
public static getDecoratorKind(decoratorKindString: string): DecoratorKind | undefined {
77
switch (decoratorKindString.toLowerCase()) {
88
case "extension":
99
return DecoratorKind.Extension;
@@ -21,8 +21,6 @@ export class Decorator {
2121
return DecoratorKind.Phantom;
2222
case "tuplereturn":
2323
return DecoratorKind.TupleReturn;
24-
case "noclassor":
25-
return DecoratorKind.NoClassOr;
2624
case "luaiterator":
2725
return DecoratorKind.LuaIterator;
2826
case "noself":
@@ -38,7 +36,12 @@ export class Decorator {
3836
public args: string[];
3937

4038
constructor(name: string, args: string[]) {
41-
this.kind = Decorator.getDecoratorKind(name);
39+
const kind = Decorator.getDecoratorKind(name);
40+
if (kind === undefined) {
41+
throw new Error(`Failed to parse decorator '${name}'`);
42+
}
43+
44+
this.kind = kind;
4245
this.args = args;
4346
}
4447
}
@@ -52,7 +55,6 @@ export enum DecoratorKind {
5255
PureAbstract = "PureAbstract",
5356
Phantom = "Phantom",
5457
TupleReturn = "TupleReturn",
55-
NoClassOr = "NoClassOr",
5658
LuaIterator = "LuaIterator",
5759
NoSelf = "NoSelf",
5860
NoSelfInFile = "NoSelfInFile",

src/LuaAST.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export interface Node extends TextRange {
109109
}
110110

111111
export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node, parent?: Node): Node {
112+
if (tsOriginal === undefined) {
113+
return {kind, parent};
114+
}
115+
112116
const sourcePosition = getSourcePosition(tsOriginal);
113117
if (sourcePosition) {
114118
return {kind, parent, line: sourcePosition.line, column: sourcePosition.column};
@@ -128,7 +132,11 @@ export function setNodePosition<T extends Node>(node: T, position: TextRange): T
128132
return node;
129133
}
130134

131-
export function setNodeOriginal<T extends Node>(node: T, tsOriginal: ts.Node): T {
135+
export function setNodeOriginal<T extends Node>(node: T | undefined, tsOriginal: ts.Node): T | undefined {
136+
if (node === undefined) {
137+
return undefined;
138+
}
139+
132140
const sourcePosition = getSourcePosition(tsOriginal);
133141
if (sourcePosition) {
134142
setNodePosition(node, sourcePosition);
@@ -172,14 +180,14 @@ export function getOriginalPos(node: Node): TextRange {
172180

173181
export interface Block extends Node {
174182
kind: SyntaxKind.Block;
175-
statements?: Statement[];
183+
statements: Statement[];
176184
}
177185

178186
export function isBlock(node: Node): node is Block {
179187
return node.kind === SyntaxKind.Block;
180188
}
181189

182-
export function createBlock(statements?: Statement[], tsOriginal?: ts.Node, parent?: Node): Block {
190+
export function createBlock(statements: Statement[], tsOriginal?: ts.Node, parent?: Node): Block {
183191
const block = createNode(SyntaxKind.Block, tsOriginal, parent) as Block;
184192
setParent(statements, block);
185193
block.statements = statements;
@@ -192,14 +200,14 @@ export interface Statement extends Node {
192200

193201
export interface DoStatement extends Statement {
194202
kind: SyntaxKind.DoStatement;
195-
statements?: Statement[];
203+
statements: Statement[];
196204
}
197205

198206
export function isDoStatement(node: Node): node is DoStatement {
199207
return node.kind === SyntaxKind.DoStatement;
200208
}
201209

202-
export function createDoStatement(statements?: Statement[], tsOriginal?: ts.Node, parent?: Node): DoStatement {
210+
export function createDoStatement(statements: Statement[], tsOriginal?: ts.Node, parent?: Node): DoStatement {
203211
const statement = createNode(SyntaxKind.DoStatement, tsOriginal, parent) as DoStatement;
204212
setParent(statements, statement);
205213
statement.statements = statements;
@@ -257,7 +265,7 @@ export function isAssignmentStatement(node: Node): node is AssignmentStatement {
257265

258266
export function createAssignmentStatement(
259267
left: IdentifierOrTableIndexExpression | IdentifierOrTableIndexExpression[],
260-
right: Expression | Expression[],
268+
right?: Expression | Expression[],
261269
tsOriginal?: ts.Node,
262270
parent?: Node
263271
): AssignmentStatement
@@ -273,7 +281,7 @@ export function createAssignmentStatement(
273281
if (Array.isArray(right)) {
274282
statement.right = right;
275283
} else {
276-
statement.right = [right];
284+
statement.right = right ? [right] : [];
277285
}
278286
return statement;
279287
}
@@ -878,18 +886,19 @@ export function isFunctionDefinition(statement: VariableDeclarationStatement | A
878886
: statement is FunctionDefinition
879887
{
880888
return statement.left.length === 1
881-
&& statement.right
889+
&& statement.right !== undefined
882890
&& statement.right.length === 1
883891
&& isFunctionExpression(statement.right[0]);
884892
}
885893

886894
export type InlineFunctionExpression = FunctionExpression & {
887-
body: { statements: [ReturnStatement]; };
895+
body: { statements: [ReturnStatement & { expressions: Expression[] }]; };
888896
};
889897

890898
export function isInlineFunctionExpression(expression: FunctionExpression) : expression is InlineFunctionExpression {
891-
return expression.body.statements
899+
return expression.body.statements !== undefined
892900
&& expression.body.statements.length === 1
893901
&& isReturnStatement(expression.body.statements[0])
902+
&& (expression.body.statements[0] as ReturnStatement).expressions !== undefined
894903
&& (expression.flags & FunctionExpressionFlags.Inline) !== 0;
895904
}

src/LuaLib.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export enum LuaLibFeature {
1919
ArraySplice = "ArraySplice",
2020
ArrayFlat = "ArrayFlat",
2121
ArrayFlatMap = "ArrayFlatMap",
22+
ArraySetLength = "ArraySetLength",
2223
ClassIndex = "ClassIndex",
2324
ClassNewIndex = "ClassNewIndex",
2425
FunctionApply = "FunctionApply",
@@ -66,8 +67,9 @@ export class LuaLib {
6667
function load(feature: LuaLibFeature): void {
6768
if (!loadedFeatures.has(feature)) {
6869
loadedFeatures.add(feature);
69-
if (luaLibDependencies[feature]) {
70-
luaLibDependencies[feature].forEach(load);
70+
const dependencies = luaLibDependencies[feature];
71+
if (dependencies) {
72+
dependencies.forEach(load);
7173
}
7274
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
7375
result += fs.readFileSync(featureFile).toString() + "\n";

src/LuaPrinter.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ export class LuaPrinter {
4343
private options: CompilerOptions;
4444
private currentIndent: string;
4545

46-
private sourceFile: string;
46+
private sourceFile = "";
4747

4848
public constructor(options: CompilerOptions) {
4949
this.options = options;
5050
this.currentIndent = "";
5151
}
5252

53-
public print(block: tstl.Block, luaLibFeatures?: Set<LuaLibFeature>, sourceFile?: string): [string, string] {
53+
public print(block: tstl.Block, luaLibFeatures?: Set<LuaLibFeature>, sourceFile = ""): [string, string] {
5454
// Add traceback lualib if sourcemap traceback option is enabled
5555
if (this.options.sourceMapTraceback) {
5656
if (luaLibFeatures === undefined) {
@@ -83,7 +83,7 @@ export class LuaPrinter {
8383
const map = sourceMap.toString();
8484
const base64Map = Buffer.from(map).toString('base64');
8585

86-
return `//# sourceMappingURL=data:application/json;base64,${base64Map}\n`;
86+
return `--# sourceMappingURL=data:application/json;base64,${base64Map}\n`;
8787
}
8888

8989
private printStackTraceOverride(rootNode: SourceNode): string {
@@ -113,7 +113,7 @@ export class LuaPrinter {
113113
private printImplementation(
114114
block: tstl.Block,
115115
luaLibFeatures?: Set<LuaLibFeature>,
116-
sourceFile?: string): SourceNode {
116+
sourceFile = ""): SourceNode {
117117

118118
let header = "";
119119

@@ -165,13 +165,15 @@ export class LuaPrinter {
165165
private createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[]): SourceNode {
166166
const originalPos = tstl.getOriginalPos(node);
167167

168-
return originalPos !== undefined
168+
return originalPos !== undefined && originalPos.line !== undefined && originalPos.column !== undefined
169169
? new SourceNode(originalPos.line + 1, originalPos.column, this.sourceFile, chunks)
170-
: new SourceNode(undefined, undefined, this.sourceFile, chunks);
170+
// tslint:disable-next-line:no-null-keyword
171+
: new SourceNode(null, null, this.sourceFile, chunks);
171172
}
172173

173174
private concatNodes(...chunks: SourceChunk[]): SourceNode {
174-
return new SourceNode(undefined, undefined, this.sourceFile, chunks);
175+
// tslint:disable-next-line:no-null-keyword
176+
return new SourceNode(null, null, this.sourceFile, chunks);
175177
}
176178

177179
private printBlock(block: tstl.Block): SourceNode {
@@ -619,19 +621,26 @@ export class LuaPrinter {
619621

620622
private printCallExpression(expression: tstl.CallExpression): SourceNode {
621623
const chunks = [];
622-
const parameterChunks = this.joinChunks(", ", expression.params.map(e => this.printExpression(e)));
623624

624-
chunks.push(this.printExpression(expression.expression), "(", ...parameterChunks, ")");
625+
const parameterChunks = expression.params !== undefined
626+
? expression.params.map(e => this.printExpression(e))
627+
: [];
628+
629+
chunks.push(this.printExpression(expression.expression), "(", ...this.joinChunks(", ", parameterChunks), ")");
625630

626631
return this.concatNodes(...chunks);
627632
}
628633

629634
private printMethodCallExpression(expression: tstl.MethodCallExpression): SourceNode {
630635
const prefix = this.printExpression(expression.prefixExpression);
631-
const parameterChunks = this.joinChunks(", ", expression.params.map(e => this.printExpression(e)));
636+
637+
const parameterChunks = expression.params !== undefined
638+
? expression.params.map(e => this.printExpression(e))
639+
: [];
640+
632641
const name = this.printIdentifier(expression.name);
633642

634-
return this.concatNodes(prefix, ":", name, "(", ...parameterChunks, ")");
643+
return this.concatNodes(prefix, ":", name, "(", ...this.joinChunks(", ", parameterChunks), ")");
635644
}
636645

637646
private printIdentifier(expression: tstl.Identifier): SourceNode {

0 commit comments

Comments
 (0)