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
26 changes: 13 additions & 13 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ export function parseCommandLine(args: string[]): ParsedCommandLine {
const [tstlOptions, tstlDefaults] = getYargOptionsWithoutDefaults(optionDeclarations);

const parsedArgs = yargs
.usage(
"Syntax: tstl [options] [files...]\n\n" +
"In addition to the options listed below you can also pass options" +
"for the typescript compiler (For a list of options use tsc -h).\n" +
"Some tsc options might have no effect.")
.example("tstl path/to/file.ts [...]", "Compile files")
.example("tstl -p path/to/tsconfig.json", "Compile project")
.wrap(yargs.terminalWidth())
.options(tstlOptions)
.fail((msg, err) => {
throw new CLIError(msg);
})
.parse(args);
.usage(
"Syntax: tstl [options] [files...]\n\n" +
"In addition to the options listed below you can also pass options" +
"for the typescript compiler (For a list of options use tsc -h).\n" +
"Some tsc options might have no effect.")
.example("tstl path/to/file.ts [...]", "Compile files")
.example("tstl -p path/to/tsconfig.json", "Compile project")
.wrap(yargs.terminalWidth())
.options(tstlOptions)
.fail((msg, err) => {
throw new CLIError(msg);
})
.parse(args);

let commandLine = ts.parseCommandLine(args);

Expand Down
30 changes: 23 additions & 7 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export enum SyntaxKind {
OrOperator,
NotOperator, // Unary
// Bitwise
// Not sure we need those since we always used the lib functions bit.bor, bit.band ... irrc
BitwiseAndOperator,
BitwiseOrOperator,
BitwiseExclusiveOrOperator,
Expand Down Expand Up @@ -201,7 +200,9 @@ export function createVariableDeclarationStatement(
left: Identifier | Identifier[],
right?: Expression | Expression[],
parent?: Node,
tsOriginal?: ts.Node): VariableDeclarationStatement {
tsOriginal?: ts.Node
): VariableDeclarationStatement
{
const statement = createNode(
SyntaxKind.VariableDeclarationStatement,
parent,
Expand Down Expand Up @@ -237,7 +238,9 @@ export function createAssignmentStatement(
left: IdentifierOrTableIndexExpression | IdentifierOrTableIndexExpression[],
right: Expression | Expression[],
parent?: Node,
tsOriginal?: ts.Node): AssignmentStatement {
tsOriginal?: ts.Node
): AssignmentStatement
{
const statement = createNode(SyntaxKind.AssignmentStatement, parent, tsOriginal) as AssignmentStatement;
setParent(left, statement);
if (Array.isArray(left)) {
Expand Down Expand Up @@ -287,7 +290,7 @@ export interface IterationStatement extends Statement {
body: Block;
}

export function isIterationStatement(node: Node): node is WhileStatement {
export function isIterationStatement(node: Node): node is IterationStatement {
return node.kind === SyntaxKind.WhileStatement || node.kind === SyntaxKind.RepeatStatement
|| node.kind === SyntaxKind.ForStatement || node.kind === SyntaxKind.ForInStatement;
}
Expand Down Expand Up @@ -360,7 +363,9 @@ export function createForStatement(
limitExpression: Expression,
stepExpression?: Expression,
parent?: Node,
tsOriginal?: ts.Node): ForStatement {
tsOriginal?: ts.Node
): ForStatement
{
const statement = createNode(SyntaxKind.ForStatement, parent, tsOriginal) as ForStatement;
setParent(body, statement);
statement.body = body;
Expand All @@ -386,7 +391,13 @@ export function isForInStatement(node: Node): node is ForInStatement {
}

export function createForInStatement(
body: Block, names: Identifier[], expressions: Expression[], parent?: Node, tsOriginal?: ts.Node): ForInStatement {
body: Block,
names: Identifier[],
expressions: Expression[],
parent?: Node,
tsOriginal?: ts.Node
): ForInStatement
{
const statement = createNode(SyntaxKind.ForInStatement, parent, tsOriginal) as ForInStatement;
setParent(body, statement);
statement.body = body;
Expand Down Expand Up @@ -804,7 +815,12 @@ export function isTableIndexExpression(node: Node): node is TableIndexExpression
}

export function createTableIndexExpression(
table: Expression, index: Expression, parent?: Node, tsOriginal?: ts.Node): TableIndexExpression {
table: Expression,
index: Expression,
parent?: Node,
tsOriginal?: ts.Node
): TableIndexExpression
{
const expression = createNode(SyntaxKind.TableIndexExpression, parent, tsOriginal) as TableIndexExpression;
setParent(table, expression);
expression.table = table;
Expand Down
33 changes: 5 additions & 28 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ export class LuaTransformer {
public transformClassDeclaration(
statement: ts.ClassLikeDeclaration,
nameOverride?: tstl.Identifier
): tstl.Statement[] {
): tstl.Statement[]
{
let className = statement.name ? this.transformIdentifier(statement.name) : nameOverride;
if (!className) {
throw TSTLErrors.MissingClassName(statement);
Expand Down Expand Up @@ -2946,15 +2947,15 @@ export class LuaTransformer {
}

public transformStringLiteral(literal: ts.StringLiteralLike): tstl.StringLiteral {
const text = this.escapeString(literal.text);
const text = tsHelper.escapeString(literal.text);
return tstl.createStringLiteral(text);
}

public transformTemplateExpression(expression: ts.TemplateExpression): tstl.BinaryExpression {
const parts: tstl.Expression[] = [tstl.createStringLiteral(this.escapeString(expression.head.text))];
const parts: tstl.Expression[] = [tstl.createStringLiteral(tsHelper.escapeString(expression.head.text))];
expression.templateSpans.forEach(span => {
const expr = this.transformExpression(span.expression);
const text = tstl.createStringLiteral(this.escapeString(span.literal.text));
const text = tstl.createStringLiteral(tsHelper.escapeString(span.literal.text));

// tostring(expr).."text"
parts.push(tstl.createBinaryExpression(
Expand Down Expand Up @@ -3038,30 +3039,6 @@ export class LuaTransformer {
tstl.createStringLiteral(identifier.text));
}

public escapeString(text: string): string {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
const escapeSequences: Array<[RegExp, string]> = [
[/[\\]/g, "\\\\"],
[/[\']/g, "\\\'"],
[/[\`]/g, "\\\`"],
[/[\"]/g, "\\\""],
[/[\n]/g, "\\n"],
[/[\r]/g, "\\r"],
[/[\v]/g, "\\v"],
[/[\t]/g, "\\t"],
[/[\b]/g, "\\b"],
[/[\f]/g, "\\f"],
[/[\0]/g, "\\0"],
];

if (text.length > 0) {
for (const [regex, replacement] of escapeSequences) {
text = text.replace(regex, replacement);
}
}
return text;
}

public transformLuaLibFunction(func: LuaLibFeature, ...params: tstl.Expression[]): tstl.CallExpression {
this.importLuaLibFeature(func);
const functionIdentifier = tstl.createIdentifier(`__TS__${func}`);
Expand Down