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
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ The real power of this transpiler is usage together with good declarations for t
- [Defold Game Engine Scripting](https://github.com/dasannikov/DefoldTypeScript/blob/master/defold.d.ts)
- [LÖVE 2D Game Development](https://github.com/hazzard993/love-typescript-definitions)

## Building & Tests

`npm run build` to build the project.

`npm run test` to run tests.

`npm run test-threaded` runs test in parallel, faster but less detailed output.

`npm run coverage` or `npm run coverage-html` to generate a coverage report.

## Sublime Text integration

This compiler works great in combination with the [Sublime Text Typescript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin) (available through the package manager as `TypeScript`).
Expand Down
8 changes: 2 additions & 6 deletions build_lualib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import * as tstl from "./src";
import { LuaLib } from "./src/LuaLib";
import { loadLuaLibFeatures } from "./src/LuaLib";

const configFileName = path.resolve(__dirname, "src/lualib/tsconfig.json");
const { emitResult, diagnostics } = tstl.transpileProject(configFileName);
Expand All @@ -16,8 +16,4 @@ if (fs.existsSync(bundlePath)) {
fs.unlinkSync(bundlePath);
}

const emitHost = {
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
writeFile: fs.writeFileSync,
};
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature), emitHost));
fs.writeFileSync(bundlePath, loadLuaLibFeatures(Object.values(tstl.LuaLibFeature), ts.sys));
39 changes: 4 additions & 35 deletions src/Decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,20 @@ export class Decorator {
}

public static getDecoratorKind(decoratorKindString: string): DecoratorKind | undefined {
switch (decoratorKindString.toLowerCase()) {
case "extension":
return DecoratorKind.Extension;
case "metaextension":
return DecoratorKind.MetaExtension;
case "customconstructor":
return DecoratorKind.CustomConstructor;
case "compilemembersonly":
return DecoratorKind.CompileMembersOnly;
case "noresolution":
return DecoratorKind.NoResolution;
case "pureabstract":
return DecoratorKind.PureAbstract;
case "phantom":
return DecoratorKind.Phantom;
case "tuplereturn":
return DecoratorKind.TupleReturn;
case "luaiterator":
return DecoratorKind.LuaIterator;
case "luatable":
return DecoratorKind.LuaTable;
case "noself":
return DecoratorKind.NoSelf;
case "noselfinfile":
return DecoratorKind.NoSelfInFile;
case "vararg":
return DecoratorKind.Vararg;
case "forrange":
return DecoratorKind.ForRange;
}

return undefined;
return Object.values(DecoratorKind).find(
decoratorKind => decoratorKind.toLowerCase() === decoratorKindString.toLowerCase()
);
}

public kind: DecoratorKind;
public args: string[];

constructor(name: string, args: string[]) {
constructor(name: string, public args: string[]) {
const kind = Decorator.getDecoratorKind(name);
if (kind === undefined) {
throw new Error(`Failed to parse decorator '${name}'`);
}

this.kind = kind;
this.args = args;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ export function isStringLiteral(node: Node): node is StringLiteral {
return node.kind === SyntaxKind.StringLiteral;
}

export function createStringLiteral(value: string | ts.__String, tsOriginal?: ts.Node, parent?: Node): StringLiteral {
export function createStringLiteral(value: string, tsOriginal?: ts.Node, parent?: Node): StringLiteral {
const expression = createNode(SyntaxKind.StringLiteral, tsOriginal, parent) as StringLiteral;
expression.value = value as string;
return expression;
Expand Down Expand Up @@ -687,15 +687,15 @@ export function createTableFieldExpression(

export interface TableExpression extends Expression {
kind: SyntaxKind.TableExpression;
fields?: TableFieldExpression[];
fields: TableFieldExpression[];
}

export function isTableExpression(node: Node): node is TableExpression {
return node.kind === SyntaxKind.TableExpression;
}

export function createTableExpression(
fields?: TableFieldExpression[],
fields: TableFieldExpression[] = [],
tsOriginal?: ts.Node,
parent?: Node
): TableExpression {
Expand Down Expand Up @@ -839,14 +839,14 @@ export function isIdentifier(node: Node): node is Identifier {
}

export function createIdentifier(
text: string | ts.__String,
text: string,
tsOriginal?: ts.Node,
symbolId?: SymbolId,
originalName?: string,
parent?: Node
): Identifier {
const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier;
expression.text = text as string;
expression.text = text;
expression.symbolId = symbolId;
expression.originalName = originalName;
return expression;
Expand Down
44 changes: 22 additions & 22 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
SymbolRegistry: [LuaLibFeature.Symbol],
};

export class LuaLib {
public static loadFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
let result = "";
export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
let result = "";

const loadedFeatures = new Set<LuaLibFeature>();
const loadedFeatures = new Set<LuaLibFeature>();

function load(feature: LuaLibFeature): void {
if (!loadedFeatures.has(feature)) {
loadedFeatures.add(feature);
const dependencies = luaLibDependencies[feature];
if (dependencies) {
dependencies.forEach(load);
}
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
const luaLibFeature = emitHost.readFile(featureFile);
if (luaLibFeature !== undefined) {
result += luaLibFeature.toString() + "\n";
} else {
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
}
}
function load(feature: LuaLibFeature): void {
if (loadedFeatures.has(feature)) return;
loadedFeatures.add(feature);

const dependencies = luaLibDependencies[feature];
if (dependencies) {
dependencies.forEach(load);
}

for (const feature of features) {
load(feature);
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
const luaLibFeature = emitHost.readFile(featureFile);
if (luaLibFeature !== undefined) {
result += luaLibFeature + "\n";
} else {
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
}
return result;
}

for (const feature of features) {
load(feature);
}

return result;
}
18 changes: 4 additions & 14 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import * as tstl from "./LuaAST";
import { luaKeywords } from "./LuaKeywords";
import { LuaLib, LuaLibFeature } from "./LuaLib";
import * as tsHelper from "./TSHelper";
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
import { EmitHost } from "./Transpile";
import * as tsHelper from "./TSHelper";

type SourceChunk = string | SourceNode;

Expand Down Expand Up @@ -132,7 +132,7 @@ export class LuaPrinter {
// Inline lualib features
else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
header += "-- Lua Library inline imports\n";
header += LuaLib.loadFeatures(luaLibFeatures, this.emitHost);
header += loadLuaLibFeatures(luaLibFeatures, this.emitHost);
}
}

Expand Down Expand Up @@ -571,17 +571,7 @@ export class LuaPrinter {
}

public printTableExpression(expression: tstl.TableExpression): SourceNode {
const chunks: SourceChunk[] = [];

chunks.push("{");

if (expression.fields) {
chunks.push(...this.printExpressionList(expression.fields));
}

chunks.push("}");

return this.createSourceNode(expression, chunks);
return this.createSourceNode(expression, ["{", ...this.printExpressionList(expression.fields), "}"]);
}

public printUnaryExpression(expression: tstl.UnaryExpression): SourceNode {
Expand Down
Loading