Skip to content

Commit 5ba6436

Browse files
committed
Merge branch 'master' into feature/expression-list-formatting
2 parents 9d9ffee + d9a996f commit 5ba6436

27 files changed

Lines changed: 1084 additions & 928 deletions

src/LuaAST.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ export function createMethodCallExpression(
830830
export interface Identifier extends Expression {
831831
kind: SyntaxKind.Identifier;
832832
text: string;
833+
originalName?: string;
833834
symbolId?: SymbolId;
834835
}
835836

@@ -841,16 +842,18 @@ export function createIdentifier(
841842
text: string | ts.__String,
842843
tsOriginal?: ts.Node,
843844
symbolId?: SymbolId,
845+
originalName?: string,
844846
parent?: Node
845847
): Identifier {
846848
const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier;
847849
expression.text = text as string;
848850
expression.symbolId = symbolId;
851+
expression.originalName = originalName;
849852
return expression;
850853
}
851854

852855
export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): Identifier {
853-
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId);
856+
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId, identifier.originalName);
854857
}
855858

856859
export function createAnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier {

src/LuaPrinter.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as path from "path";
2-
3-
import { Mapping, SourceNode, SourceMapGenerator } from "source-map";
4-
5-
import * as tstl from "./LuaAST";
2+
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
63
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
7-
import { LuaLib, LuaLibFeature } from "./LuaLib";
8-
import { TSHelper as tsHelper } from "./TSHelper";
4+
import * as tstl from "./LuaAST";
95
import { luaKeywords } from "./LuaKeywords";
6+
import { LuaLib, LuaLibFeature } from "./LuaLib";
7+
import * as tsHelper from "./TSHelper";
108

119
type SourceChunk = string | SourceNode;
1210

@@ -157,12 +155,12 @@ export class LuaPrinter {
157155
return this.concatNodes(this.currentIndent, input);
158156
}
159157

160-
protected createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[]): SourceNode {
158+
protected createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[], name?: string): SourceNode {
161159
const originalPos = tstl.getOriginalPos(node);
162160

163161
return originalPos !== undefined && originalPos.line !== undefined && originalPos.column !== undefined
164-
? new SourceNode(originalPos.line + 1, originalPos.column, this.sourceFile, chunks)
165-
: new SourceNode(null, null, this.sourceFile, chunks); // tslint:disable-line:no-null-keyword
162+
? new SourceNode(originalPos.line + 1, originalPos.column, this.sourceFile, chunks, name)
163+
: new SourceNode(null, null, this.sourceFile, chunks, name); // tslint:disable-line:no-null-keyword
166164
}
167165

168166
protected concatNodes(...chunks: SourceChunk[]): SourceNode {
@@ -274,7 +272,7 @@ export class LuaPrinter {
274272
}
275273
}
276274

277-
return this.concatNodes(...chunks);
275+
return this.createSourceNode(statement, chunks);
278276
}
279277

280278
public printVariableAssignmentStatement(statement: tstl.AssignmentStatement): SourceNode {
@@ -516,13 +514,13 @@ export class LuaPrinter {
516514
...this.joinChunks(", ", returnStatement.expressions.map(e => this.printExpression(e))),
517515
];
518516
chunks.push(this.createSourceNode(returnStatement, returnNode));
519-
chunks.push(" end");
517+
chunks.push(this.createSourceNode(expression, " end"));
520518
} else {
521519
chunks.push("\n");
522520
this.pushIndent();
523521
chunks.push(this.printBlock(expression.body));
524522
this.popIndent();
525-
chunks.push(this.indent("end"));
523+
chunks.push(this.indent(this.createSourceNode(expression, "end")));
526524
}
527525

528526
return this.createSourceNode(expression, chunks);
@@ -541,7 +539,7 @@ export class LuaPrinter {
541539
this.pushIndent();
542540
chunks.push(this.printBlock(expression.body));
543541
this.popIndent();
544-
chunks.push(this.indent("end"));
542+
chunks.push(this.indent(this.createSourceNode(statement, "end")));
545543

546544
return this.createSourceNode(expression, chunks);
547545
}
@@ -655,7 +653,11 @@ export class LuaPrinter {
655653
}
656654

657655
public printIdentifier(expression: tstl.Identifier): SourceNode {
658-
return this.createSourceNode(expression, expression.text);
656+
return this.createSourceNode(
657+
expression,
658+
expression.text,
659+
expression.originalName !== expression.text ? expression.originalName : undefined
660+
);
659661
}
660662

661663
public printTableIndexExpression(expression: tstl.TableIndexExpression): SourceNode {
@@ -784,12 +786,15 @@ export class LuaPrinter {
784786
}
785787
if (
786788
currentMapping.generated.line === generatedLine &&
787-
currentMapping.generated.column === generatedColumn
789+
currentMapping.generated.column === generatedColumn &&
790+
currentMapping.name === sourceNode.name
788791
) {
789792
return false;
790793
}
791794
return (
792-
currentMapping.original.line !== sourceNode.line || currentMapping.original.column !== sourceNode.column
795+
currentMapping.original.line !== sourceNode.line ||
796+
currentMapping.original.column !== sourceNode.column ||
797+
currentMapping.name !== sourceNode.name
793798
);
794799
};
795800

@@ -799,6 +804,7 @@ export class LuaPrinter {
799804
source: sourceNode.source,
800805
original: { line: sourceNode.line, column: sourceNode.column },
801806
generated: { line: generatedLine, column: generatedColumn },
807+
name: sourceNode.name,
802808
};
803809
map.addMapping(currentMapping);
804810
}

0 commit comments

Comments
 (0)