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: 4 additions & 6 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as path from "path";

import { Mapping, SourceNode, SourceMapGenerator } from "source-map";

import * as tstl from "./LuaAST";
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import { LuaLib, LuaLibFeature } from "./LuaLib";
import { TSHelper as tsHelper } from "./TSHelper";
import * as tstl from "./LuaAST";
import { luaKeywords } from "./LuaKeywords";
import { LuaLib, LuaLibFeature } from "./LuaLib";
import * as tsHelper from "./TSHelper";

type SourceChunk = string | SourceNode;

Expand Down
54 changes: 29 additions & 25 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CompilerOptions, LuaTarget } from "./CompilerOptions";
import { Decorator, DecoratorKind } from "./Decorator";
import * as tstl from "./LuaAST";
import { LuaLibFeature } from "./LuaLib";
import { ContextType, TSHelper as tsHelper } from "./TSHelper";
import { TSTLErrors } from "./TSTLErrors";
import * as tsHelper from "./TSHelper";
import * as TSTLErrors from "./TSTLErrors";
import { luaKeywords, luaBuiltins } from "./LuaKeywords";

export type StatementVisitResult = tstl.Statement | tstl.Statement[] | undefined;
Expand Down Expand Up @@ -465,7 +465,7 @@ export class LuaTransformer {

protected validateClassElement(element: ts.ClassElement): void {
if (element.name && (ts.isStringLiteral(element.name) || ts.isIdentifier(element.name))) {
if (tsHelper.isStatic(element) && element.name.text === "new") {
if (tsHelper.isStaticNode(element) && element.name.text === "new") {
throw TSTLErrors.ForbiddenStaticClassPropertyName(element, element.name.text);
}
}
Expand Down Expand Up @@ -579,16 +579,16 @@ export class LuaTransformer {
}

// LuaTable classes must be ambient
if (decorators.has(DecoratorKind.LuaTable) && !tsHelper.isAmbient(statement)) {
if (decorators.has(DecoratorKind.LuaTable) && !tsHelper.isAmbientNode(statement)) {
throw TSTLErrors.ForbiddenLuaTableNonDeclaration(statement);
}

// Get all properties with value
const properties = statement.members.filter(ts.isPropertyDeclaration).filter(member => member.initializer);

// Divide properties into static and non-static
const staticFields = properties.filter(tsHelper.isStatic);
const instanceFields = properties.filter(prop => !tsHelper.isStatic(prop));
const staticFields = properties.filter(tsHelper.isStaticNode);
const instanceFields = properties.filter(prop => !tsHelper.isStaticNode(prop));

const result: tstl.Statement[] = [];

Expand Down Expand Up @@ -808,7 +808,7 @@ export class LuaTransformer {
);

// localClassName.____getters = {}
if (statement.members.some(m => ts.isGetAccessor(m) && tsHelper.isStatic(m))) {
if (statement.members.some(m => ts.isGetAccessor(m) && tsHelper.isStaticNode(m))) {
const classGetters = tstl.createTableIndexExpression(
tstl.cloneIdentifier(localClassName),
tstl.createStringLiteral("____getters")
Expand Down Expand Up @@ -836,7 +836,7 @@ export class LuaTransformer {
result.push(assignClassIndex);

// localClassName.____setters = {}
if (statement.members.some(m => ts.isSetAccessor(m) && tsHelper.isStatic(m))) {
if (statement.members.some(m => ts.isSetAccessor(m) && tsHelper.isStaticNode(m))) {
const classSetters = tstl.createTableIndexExpression(
tstl.cloneIdentifier(localClassName),
tstl.createStringLiteral("____setters")
Expand Down Expand Up @@ -866,7 +866,7 @@ export class LuaTransformer {
result.push(assignClassPrototype);

// localClassName.prototype.____getters = {}
if (statement.members.some(m => ts.isGetAccessor(m) && !tsHelper.isStatic(m))) {
if (statement.members.some(m => ts.isGetAccessor(m) && !tsHelper.isStaticNode(m))) {
const classPrototypeGetters = tstl.createTableIndexExpression(
createClassPrototype(),
tstl.createStringLiteral("____getters")
Expand Down Expand Up @@ -901,7 +901,7 @@ export class LuaTransformer {
result.push(assignClassPrototypeIndex);
}

if (statement.members.some(m => ts.isSetAccessor(m) && !tsHelper.isStatic(m))) {
if (statement.members.some(m => ts.isSetAccessor(m) && !tsHelper.isStaticNode(m))) {
// localClassName.prototype.____setters = {}
const classPrototypeSetters = tstl.createTableIndexExpression(
createClassPrototype(),
Expand Down Expand Up @@ -1273,7 +1273,7 @@ export class LuaTransformer {
tstl.FunctionExpressionFlags.Declaration
);

const methodTable = tsHelper.isStatic(getAccessor)
const methodTable = tsHelper.isStaticNode(getAccessor)
? tstl.cloneIdentifier(className)
: tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("prototype"));

Expand Down Expand Up @@ -1306,7 +1306,7 @@ export class LuaTransformer {
tstl.FunctionExpressionFlags.Declaration
);

const methodTable = tsHelper.isStatic(setAccessor)
const methodTable = tsHelper.isStaticNode(setAccessor)
? tstl.cloneIdentifier(className)
: tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("prototype"));

Expand Down Expand Up @@ -1335,7 +1335,7 @@ export class LuaTransformer {

const type = this.checker.getTypeAtLocation(node);
const context =
tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void
tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void
? this.createSelfIdentifier()
: undefined;
const [paramNames, dots, restParamName] = this.transformParameters(node.parameters, context);
Expand All @@ -1351,7 +1351,7 @@ export class LuaTransformer {
);

const methodTable =
tsHelper.isStatic(node) || noPrototype
tsHelper.isStaticNode(node) || noPrototype
? tstl.cloneIdentifier(className)
: tstl.createTableIndexExpression(
tstl.cloneIdentifier(className),
Expand Down Expand Up @@ -1847,7 +1847,7 @@ export class LuaTransformer {

const type = this.checker.getTypeAtLocation(functionDeclaration);
const context =
tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void
tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void
? this.createSelfIdentifier()
: undefined;
const [params, dotsLiteral, restParamName] = this.transformParameters(functionDeclaration.parameters, context);
Expand Down Expand Up @@ -3481,7 +3481,7 @@ export class LuaTransformer {
const type = this.checker.getTypeAtLocation(node);

let context: tstl.Identifier | undefined;
if (tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void) {
if (tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void) {
if (ts.isArrowFunction(node)) {
// dummy context for arrow functions with parameters
if (node.parameters.length > 0) {
Expand Down Expand Up @@ -3661,7 +3661,7 @@ export class LuaTransformer {
const signatureDeclaration = signature && signature.getDeclaration();
if (
signatureDeclaration &&
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) === ContextType.Void
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) === tsHelper.ContextType.Void
) {
parameters = this.transformArguments(expression.arguments, signature);
} else {
Expand Down Expand Up @@ -3805,7 +3805,7 @@ export class LuaTransformer {
const signatureDeclaration = signature && signature.getDeclaration();
if (
!signatureDeclaration ||
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== ContextType.Void
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== tsHelper.ContextType.Void
) {
if (
luaKeywords.has(node.expression.name.text) ||
Expand Down Expand Up @@ -3845,7 +3845,7 @@ export class LuaTransformer {
const signatureDeclaration = signature && signature.getDeclaration();
if (
!signatureDeclaration ||
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== ContextType.Void
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== tsHelper.ContextType.Void
) {
// Pass left-side as context

Expand Down Expand Up @@ -4557,7 +4557,7 @@ export class LuaTransformer {
protected transformFunctionCallExpression(node: ts.CallExpression): tstl.CallExpression {
const expression = node.expression as ts.PropertyAccessExpression;
const callerType = this.checker.getTypeAtLocation(expression.expression);
if (tsHelper.getFunctionContextType(callerType, this.checker) === ContextType.Void) {
if (tsHelper.getFunctionContextType(callerType, this.checker) === tsHelper.ContextType.Void) {
throw TSTLErrors.UnsupportedSelfFunctionConversion(node);
}
const signature = this.checker.getResolvedSignature(node);
Expand Down Expand Up @@ -5075,10 +5075,14 @@ export class LuaTransformer {
const fromContext = tsHelper.getFunctionContextType(fromType, this.checker);
const toContext = tsHelper.getFunctionContextType(toType, this.checker);

if (fromContext === ContextType.Mixed || toContext === ContextType.Mixed) {
if (fromContext === tsHelper.ContextType.Mixed || toContext === tsHelper.ContextType.Mixed) {
throw TSTLErrors.UnsupportedOverloadAssignment(node, toName);
} else if (fromContext !== toContext && fromContext !== ContextType.None && toContext !== ContextType.None) {
if (toContext === ContextType.Void) {
} else if (
fromContext !== toContext &&
fromContext !== tsHelper.ContextType.None &&
toContext !== tsHelper.ContextType.None
) {
if (toContext === tsHelper.ContextType.Void) {
throw TSTLErrors.UnsupportedNoSelfFunctionConversion(node, toName);
} else {
throw TSTLErrors.UnsupportedSelfFunctionConversion(node, toName);
Expand Down Expand Up @@ -5239,7 +5243,7 @@ export class LuaTransformer {
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
const isLuaKeyword = luaKeywords.has(symbol.name);
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbient(d));
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbientNode(d));
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
// Catch ambient declarations of identifiers with bad names
throw TSTLErrors.InvalidAmbientIdentifierName(tsOriginal || ts.createIdentifier(symbol.name));
Expand Down Expand Up @@ -5545,7 +5549,7 @@ export class LuaTransformer {
const expression = decorator.expression;
const type = this.checker.getTypeAtLocation(expression);
const context = tsHelper.getFunctionContextType(type, this.checker);
if (context === ContextType.Void) {
if (context === tsHelper.ContextType.Void) {
throw TSTLErrors.InvalidDecoratorContext(decorator);
}
return this.transformExpression(expression);
Expand Down
Loading