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
16 changes: 8 additions & 8 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ const defaultCompilerOptions: CompilerOptions = {
};

export function createStringCompilerProgram(
input: string, options: CompilerOptions = defaultCompilerOptions): ts.Program {

input: string, options: CompilerOptions = defaultCompilerOptions, filePath = "file.ts"): ts.Program {
const compilerHost = {
directoryExists: () => true,
fileExists: (fileName): boolean => true,
Expand All @@ -100,8 +99,8 @@ export function createStringCompilerProgram(
getDirectories: () => [],
getNewLine: () => "\n",

getSourceFile: (filename: string, languageVersion) => {
if (filename === "file.ts") {
getSourceFile: (filename: string) => {
if (filename === filePath) {
return ts.createSourceFile(filename, input, ts.ScriptTarget.Latest, false);
}
if (filename.indexOf(".d.ts") !== -1) {
Expand All @@ -126,15 +125,16 @@ export function createStringCompilerProgram(
// Don't write output
writeFile: (name, text, writeByteOrderMark) => undefined,
};
return ts.createProgram(["file.ts"], options, compilerHost);
return ts.createProgram([filePath], options, compilerHost);
}

export function transpileString(
str: string,
options: CompilerOptions = defaultCompilerOptions,
ignoreDiagnostics = false
ignoreDiagnostics = false,
filePath = "file.ts"
): string {
const program = createStringCompilerProgram(str, options);
const program = createStringCompilerProgram(str, options, filePath);

if (!ignoreDiagnostics) {
const diagnostics = ts.getPreEmitDiagnostics(program);
Expand All @@ -148,7 +148,7 @@ export function transpileString(

const transpiler = new LuaTranspiler(program);

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

return result.trim();
}
48 changes: 31 additions & 17 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,41 @@ export class LuaTransformer {
// TODO make all other methods private???
public transformSourceFile(node: ts.SourceFile): [tstl.Block, Set<LuaLibFeature>] {
this.setupState();
this.pushScope(ScopeType.File, node);

this.currentSourceFile = node;
this.isModule = tsHelper.isFileModule(node);

const statements = this.performHoisting(this.transformStatements(node.statements));
this.popScope();
let statements: tstl.Statement[] = [];
if (node.flags & ts.NodeFlags.JsonFile) {
this.isModule = false;

if (this.isModule) {
statements.unshift(
tstl.createVariableDeclarationStatement(
tstl.createIdentifier("exports"),
tstl.createBinaryExpression(
const statement = node.statements[0];
if (!statement || !ts.isExpressionStatement(statement)) {
throw TSTLErrors.InvalidJsonFileContent(node);
}

statements.push(tstl.createReturnStatement([this.transformExpression(statement.expression)]));
} else {
this.pushScope(ScopeType.File, node);

this.isModule = tsHelper.isFileModule(node);
statements = this.performHoisting(this.transformStatements(node.statements));

this.popScope();

if (this.isModule) {
statements.unshift(
tstl.createVariableDeclarationStatement(
tstl.createIdentifier("exports"),
tstl.createTableExpression(),
tstl.SyntaxKind.OrOperator
)));
statements.push(
tstl.createReturnStatement(
[tstl.createIdentifier("exports")]
));
tstl.createBinaryExpression(
tstl.createIdentifier("exports"),
tstl.createTableExpression(),
tstl.SyntaxKind.OrOperator
)));
statements.push(
tstl.createReturnStatement(
[tstl.createIdentifier("exports")]
));
}
}

return [tstl.createBlock(statements, node), this.luaLibFeatureSet];
Expand Down Expand Up @@ -3644,7 +3658,7 @@ export class LuaTransformer {
}

private pathToLuaRequirePath(filePath: string): string {
return filePath.replace(new RegExp("\\\\|\/", "g"), ".");
return filePath.replace(new RegExp("\\\\|\/", "g"), ".").replace(/\.json$/, '');
}

private shouldExportIdentifier(identifier: tstl.Identifier | tstl.Identifier[]): boolean {
Expand Down
2 changes: 2 additions & 0 deletions src/TSTLErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,6 @@ export class TSTLErrors {
node
);
}

public static InvalidJsonFileContent = (node: ts.Node) => new TranspileError("Invalid JSON file content", node);
}
2 changes: 1 addition & 1 deletion test/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function transpileString(str: string, options?: CompilerOptions, ignoreDi
}
}

function executeLua(luaStr: string, withLib = true): any {
export function executeLua(luaStr: string, withLib = true): any {
if (withLib) {
luaStr = minimalTestLib + luaStr;
}
Expand Down
27 changes: 27 additions & 0 deletions test/unit/json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Expect, Test, TestCase } from "alsatian";
import { transpileString } from "../../src/Compiler";
import { TranspileError } from "../../src/TranspileError";
import * as util from "../src/util";

export class JsonTests {
@Test("JSON")
@TestCase("0")
@TestCase('""')
@TestCase("[]")
@TestCase('[1, "2", []]')
@TestCase('{ "a": "b" }')
@TestCase('{ "a": { "b": "c" } }')
public json(json: string): void {
const lua = transpileString(json, { resolveJsonModule: true, noHeader: true }, false, "file.json")
.replace(/^return (.+);$/s, "return JSONStringify($1);");

const result = util.executeLua(lua);
Expect(JSON.parse(result)).toEqual(JSON.parse(json));
}

@Test("Empty JSON")
public emptyJson(): void {
Expect(() => transpileString("", { resolveJsonModule: true, noHeader: true }, false, "file.json"))
.toThrowError(TranspileError, "Invalid JSON file content");
}
}