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
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test_script:
- node --version
- npm --version
# run tests
- npm run lint
- npm run build
- npm test

Expand Down
2 changes: 1 addition & 1 deletion build_lualib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ compile([
"./src/lualib",
"--noHeader",
"true",
...glob.sync("./src/lualib/*.ts"),
...glob.sync("./src/lualib/**/*.ts"),
]);

if (fs.existsSync(bundlePath)) {
Expand Down
77 changes: 70 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"node": ">=8.5.0"
},
"dependencies": {
"source-map": "^0.7.3",
"typescript": "^3.3.1"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const optionDeclarations: {[key: string]: CLIOption<any>} = {
describe: "Disables hoisting.",
type: "boolean",
} as CLIOption<boolean>,
sourceMapTraceback: {
default: false,
describe: "Applies the source map to show source TS files and lines in error tracebacks.",
type: "boolean",
} as CLIOption<boolean>,
};

export const { version } = require("../package.json");
Expand Down
8 changes: 3 additions & 5 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import * as ts from "typescript";
import * as CommandLineParser from "./CommandLineParser";
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
import { LuaTranspiler } from "./LuaTranspiler";
import { LuaTranspiler, TranspileResult } from "./LuaTranspiler";

export function compile(argv: string[]): void {
const parseResult = CommandLineParser.parseCommandLine(argv);
Expand Down Expand Up @@ -167,7 +167,7 @@ export function transpileString(
options: CompilerOptions = defaultCompilerOptions,
ignoreDiagnostics = false,
filePath = "file.ts"
): string {
): TranspileResult {
const program = createStringCompilerProgram(input, options, filePath);

if (!ignoreDiagnostics) {
Expand All @@ -182,7 +182,5 @@ export function transpileString(

const transpiler = new LuaTranspiler(program);

const result = transpiler.transpileSourceFile(program.getSourceFile(filePath));

return result.trim();
return transpiler.transpileSourceFile(program.getSourceFile(filePath));
}
1 change: 1 addition & 0 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface CompilerOptions extends ts.CompilerOptions {
luaTarget?: LuaTarget;
luaLibImport?: LuaLibImportKind;
noHoisting?: boolean;
sourceMapTraceback?: boolean;
}

export enum LuaLibImportKind {
Expand Down
56 changes: 35 additions & 21 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ export type Operator = UnaryOperator | BinaryOperator;

export type SymbolId = number;

// TODO For future sourcemap support?
export interface TextRange {
pos: number;
end: number;
line?: number;
column?: number;
}

export interface Node extends TextRange {
Expand All @@ -110,22 +109,25 @@ export interface Node extends TextRange {
}

export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node, parent?: Node): Node {
let pos = -1;
let end = -1;
if (tsOriginal) {
pos = tsOriginal.pos;
end = tsOriginal.end;
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
return {kind, parent, line: sourcePosition.line, column: sourcePosition.column};
} else {
return {kind, parent};
}
return {kind, parent, pos, end};
}

export function cloneNode<T extends Node>(node: T): T {
return Object.assign({}, node);
}

export function setNodeOriginal<T extends Node>(node: T, tsOriginal: ts.Node): T {
node.pos = tsOriginal.pos;
node.end = tsOriginal.end;
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
node.line = sourcePosition.line;
node.column = sourcePosition.column;
}

return node;
}

Expand All @@ -136,20 +138,32 @@ export function setParent(node: Node | Node[] | undefined, parent: Node): void
if (Array.isArray(node)) {
node.forEach(n => {
n.parent = parent;
if (n.pos === -1 || n.end === -1) {
n.pos = parent.pos;
n.end = parent.end;
}
});
} else {
node.parent = parent;
if (node.pos === -1 || node.end === -1) {
node.pos = parent.pos;
node.end = parent.end;
}
}
}

function getSourcePosition(sourceNode: ts.Node): TextRange | undefined {
if (sourceNode !== undefined && sourceNode.getSourceFile() !== undefined && sourceNode.pos >= 0) {

const { line, character } = ts.getLineAndCharacterOfPosition(
sourceNode.getSourceFile(),
sourceNode.pos + sourceNode.getLeadingTriviaWidth()
);

return { line, column: character };
}
}

export function getOriginalPos(node: Node): TextRange {
while (node.line === undefined && node.parent !== undefined) {
node = node.parent;
}

return { line: node.line, column: node.column };
}

export interface Block extends Node {
kind: SyntaxKind.Block;
statements?: Statement[];
Expand Down Expand Up @@ -812,8 +826,8 @@ export function createIdentifier(
return expression;
}

export function cloneIdentifier(identifier: Identifier): Identifier {
return createIdentifier(identifier.text, undefined, identifier.symbolId);
export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): Identifier {
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId);
}

export function createAnnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier {
Expand Down
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export enum LuaLibFeature {
Set = "Set",
WeakMap = "WeakMap",
WeakSet = "WeakSet",
SourceMapTraceBack = "SourceMapTraceBack",
StringReplace = "StringReplace",
StringSplit = "StringSplit",
StringConcat = "StringConcat",
Expand Down
Loading