diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 9f430f290..6f6d43773 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -51,32 +51,32 @@ export interface DiagnosticsProducingTypeChecker extends ts.TypeChecker { } export class LuaTransformer { - private isStrict: boolean; - private luaTarget: LuaTarget; + protected isStrict: boolean; + protected luaTarget: LuaTarget; - private checker: DiagnosticsProducingTypeChecker; + protected checker: DiagnosticsProducingTypeChecker; protected options: CompilerOptions; // Resolver is lazy-initialized in transformSourceFile to avoid type-checking all files - private resolver!: EmitResolver; + protected resolver!: EmitResolver; - private isModule = false; - private currentSourceFile?: ts.SourceFile; + protected isModule = false; + protected currentSourceFile?: ts.SourceFile; - private currentNamespace: ts.ModuleDeclaration | undefined; - private classStack: ts.ClassLikeDeclaration[] = []; + protected currentNamespace: ts.ModuleDeclaration | undefined; + protected classStack: ts.ClassLikeDeclaration[] = []; - private scopeStack: Scope[] = []; - private genVarCounter = 0; + protected scopeStack: Scope[] = []; + protected genVarCounter = 0; - private luaLibFeatureSet = new Set(); + protected luaLibFeatureSet = new Set(); - private symbolInfo = new Map(); - private symbolIds = new Map(); + protected symbolInfo = new Map(); + protected symbolIds = new Map(); - private genSymbolIdCounter = 0; + protected genSymbolIdCounter = 0; - private readonly typeValidationCache: Map> = new Map>(); + protected readonly typeValidationCache: Map> = new Map>(); public constructor(protected program: ts.Program) { this.checker = (program as any).getDiagnosticsProducingTypeChecker(); @@ -91,7 +91,7 @@ export class LuaTransformer { this.setupState(); } - private setupState(): void { + protected setupState(): void { this.genVarCounter = 0; this.currentSourceFile = undefined; this.isModule = false; @@ -103,7 +103,6 @@ export class LuaTransformer { this.genSymbolIdCounter = 1; } - // TODO make all other methods private??? public transformSourceFile(node: ts.SourceFile): [tstl.Block, Set] { this.setupState(); @@ -213,7 +212,7 @@ export class LuaTransformer { } /** Converts an array of ts.Statements into an array of tstl.Statements */ - private transformStatements(statements: readonly ts.Statement[]): tstl.Statement[] { + protected transformStatements(statements: readonly ts.Statement[]): tstl.Statement[] { const tstlStatements: tstl.Statement[] = []; (statements as ts.Statement[]).forEach(statement => { tstlStatements.push(...this.statementVisitResultToArray(this.transformStatement(statement))); @@ -445,7 +444,7 @@ export class LuaTransformer { } } - private createModuleRequire(moduleSpecifier: ts.StringLiteral, resolveModule = true): tstl.CallExpression { + protected createModuleRequire(moduleSpecifier: ts.StringLiteral, resolveModule = true): tstl.CallExpression { const modulePathString = resolveModule ? this.getImportPath(moduleSpecifier.text.replace(new RegExp('"', "g"), ""), moduleSpecifier) : moduleSpecifier.text; @@ -453,7 +452,7 @@ export class LuaTransformer { return tstl.createCallExpression(tstl.createIdentifier("require"), [modulePath], moduleSpecifier); } - private validateClassElement(element: ts.ClassElement): void { + 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") { throw TSTLErrors.ForbiddenStaticClassPropertyName(element, element.name.text); @@ -497,7 +496,7 @@ export class LuaTransformer { return this.createModuleRequire(externalModuleReference.expression as ts.StringLiteral); } - private transformEntityName(entityName: ts.EntityName): ExpressionVisitResult { + protected transformEntityName(entityName: ts.EntityName): ExpressionVisitResult { return ts.isQualifiedName(entityName) ? this.transformQualifiedName(entityName) : this.transformIdentifierExpression(entityName); @@ -757,7 +756,7 @@ export class LuaTransformer { return result; } - private createClassCreationMethods( + protected createClassCreationMethods( statement: ts.ClassLikeDeclarationBase, className: tstl.Identifier, localClassName: tstl.Identifier, @@ -1089,7 +1088,7 @@ export class LuaTransformer { return result; } - private transformClassInstanceFields( + protected transformClassInstanceFields( classDeclaration: ts.ClassLikeDeclaration, instanceFields: ts.PropertyDeclaration[] ): tstl.Statement[] { @@ -1132,14 +1131,14 @@ export class LuaTransformer { return statements; } - private createConstructorName(className: tstl.Identifier): tstl.TableIndexExpression { + protected createConstructorName(className: tstl.Identifier): tstl.TableIndexExpression { return tstl.createTableIndexExpression( tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("prototype")), tstl.createStringLiteral("____constructor") ); } - private transformConstructorDeclaration( + protected transformConstructorDeclaration( statement: ts.ConstructorDeclaration, className: tstl.Identifier, instanceFields: ts.PropertyDeclaration[], @@ -1344,7 +1343,7 @@ export class LuaTransformer { ); } - private transformParameters( + protected transformParameters( parameters: ts.NodeArray, context?: tstl.Identifier ): [tstl.Identifier[], tstl.DotsLiteral | undefined, tstl.Identifier | undefined] { @@ -1384,7 +1383,7 @@ export class LuaTransformer { return [paramNames, dotsLiteral, restParamName]; } - private transformFunctionBody( + protected transformFunctionBody( parameters: ts.NodeArray, body: ts.Block, spreadIdentifier?: tstl.Identifier @@ -1437,7 +1436,7 @@ export class LuaTransformer { return [headerStatements.concat(bodyStatements), scope]; } - private transformParameterDefaultValueDeclaration( + protected transformParameterDefaultValueDeclaration( parameterName: tstl.Identifier, value?: ts.Expression, tsOriginal?: ts.Node @@ -1701,7 +1700,7 @@ export class LuaTransformer { }); } - private transformGeneratorFunction( + protected transformGeneratorFunction( parameters: ts.NodeArray, body: ts.Block, spreadIdentifier?: tstl.Identifier @@ -2184,7 +2183,7 @@ export class LuaTransformer { return tstl.createDoStatement(result, statement); } - private transformForOfInitializer(initializer: ts.ForInitializer, expression: tstl.Expression): tstl.Statement { + protected transformForOfInitializer(initializer: ts.ForInitializer, expression: tstl.Expression): tstl.Statement { if (ts.isVariableDeclarationList(initializer)) { // Declaration of new variable const variableDeclarations = this.transformVariableDeclaration(initializer.declarations[0]); @@ -2240,13 +2239,13 @@ export class LuaTransformer { return baseResult; } - private transformBlockOrStatement(statement: ts.Statement): tstl.Statement[] { + protected transformBlockOrStatement(statement: ts.Statement): tstl.Statement[] { return ts.isBlock(statement) ? this.transformStatements(statement.statements) : this.statementVisitResultToArray(this.transformStatement(statement)); } - private transformForOfArrayStatement(statement: ts.ForOfStatement, block: tstl.Block): StatementVisitResult { + protected transformForOfArrayStatement(statement: ts.ForOfStatement, block: tstl.Block): StatementVisitResult { let valueVariable: tstl.Identifier; if (ts.isVariableDeclarationList(statement.initializer)) { // Declaration of new variable @@ -2275,7 +2274,10 @@ export class LuaTransformer { ); } - private transformForOfLuaIteratorStatement(statement: ts.ForOfStatement, block: tstl.Block): StatementVisitResult { + protected transformForOfLuaIteratorStatement( + statement: ts.ForOfStatement, + block: tstl.Block + ): StatementVisitResult { const luaIterator = this.transformExpression(statement.expression); const type = this.checker.getTypeAtLocation(statement.expression); const tupleReturn = tsHelper.getCustomDecorators(type, this.checker).has(DecoratorKind.TupleReturn); @@ -2344,7 +2346,7 @@ export class LuaTransformer { } } - private transformForOfIteratorStatement(statement: ts.ForOfStatement, block: tstl.Block): StatementVisitResult { + protected transformForOfIteratorStatement(statement: ts.ForOfStatement, block: tstl.Block): StatementVisitResult { const iterable = this.transformExpression(statement.expression); if ( ts.isVariableDeclarationList(statement.initializer) && @@ -2735,7 +2737,7 @@ export class LuaTransformer { } } - private transformAssignment(lhs: ts.Expression, right?: tstl.Expression): tstl.Statement { + protected transformAssignment(lhs: ts.Expression, right?: tstl.Expression): tstl.Statement { return tstl.createAssignmentStatement( this.transformExpression(lhs) as tstl.AssignmentLeftHandSideExpression, right, @@ -2743,7 +2745,7 @@ export class LuaTransformer { ); } - private transformAssignmentStatement(expression: ts.BinaryExpression): StatementVisitResult { + protected transformAssignmentStatement(expression: ts.BinaryExpression): StatementVisitResult { // Validate assignment const rightType = this.checker.getTypeAtLocation(expression.right); const leftType = this.checker.getTypeAtLocation(expression.left); @@ -2788,7 +2790,7 @@ export class LuaTransformer { } } - private transformAssignmentExpression( + protected transformAssignmentExpression( expression: ts.BinaryExpression ): tstl.CallExpression | tstl.MethodCallExpression { // Validate assignment @@ -2882,7 +2884,7 @@ export class LuaTransformer { } } - private transformCompoundAssignmentExpression( + protected transformCompoundAssignmentExpression( expression: ts.Expression, lhs: ts.Expression, rhs: ts.Expression, @@ -3051,7 +3053,7 @@ export class LuaTransformer { ); } - private transformCompoundAssignmentStatement( + protected transformCompoundAssignmentStatement( node: ts.Node, lhs: ts.Expression, rhs: ts.Expression, @@ -3092,7 +3094,7 @@ export class LuaTransformer { } } - private transformUnaryBitLibOperation( + protected transformUnaryBitLibOperation( node: ts.Node, expression: tstl.Expression, operator: tstl.UnaryBitwiseOperator, @@ -3113,7 +3115,7 @@ export class LuaTransformer { ); } - private transformUnaryBitOperation( + protected transformUnaryBitOperation( node: ts.Node, expression: tstl.Expression, operator: tstl.UnaryBitwiseOperator @@ -3133,7 +3135,7 @@ export class LuaTransformer { } } - private transformBinaryBitLibOperation( + protected transformBinaryBitLibOperation( node: ts.Node, left: tstl.Expression, right: tstl.Expression, @@ -3170,7 +3172,7 @@ export class LuaTransformer { ); } - private transformBinaryBitOperation( + protected transformBinaryBitOperation( node: ts.Node, left: tstl.Expression, right: tstl.Expression, @@ -3192,7 +3194,7 @@ export class LuaTransformer { } } - private transformProtectedConditionalExpression(expression: ts.ConditionalExpression): tstl.CallExpression { + protected transformProtectedConditionalExpression(expression: ts.ConditionalExpression): tstl.CallExpression { const condition = this.transformExpression(expression.condition); const val1 = this.transformExpression(expression.whenTrue); const val2 = this.transformExpression(expression.whenFalse); @@ -3526,7 +3528,7 @@ export class LuaTransformer { return wrapResult ? this.wrapInTable(callExpression) : callExpression; } - private transformGlobalFunctionCall(node: ts.CallExpression): tstl.Expression | undefined { + protected transformGlobalFunctionCall(node: ts.CallExpression): tstl.Expression | undefined { const signature = this.checker.getResolvedSignature(node); const parameters = this.transformArguments(node.arguments, signature); @@ -3733,7 +3735,7 @@ export class LuaTransformer { } } - private transformArguments( + protected transformArguments( params: ts.NodeArray | ts.Expression[], sig?: ts.Signature, context?: T @@ -3806,7 +3808,7 @@ export class LuaTransformer { } // Transpile a Math._ property - private transformMathExpression(identifier: ts.Identifier): tstl.Expression { + protected transformMathExpression(identifier: ts.Identifier): tstl.Expression { const name = identifier.escapedText as string; switch (name) { case "PI": @@ -3829,7 +3831,7 @@ export class LuaTransformer { } // Transpile a Math._ property - private transformMathCallExpression(node: ts.CallExpression): tstl.Expression { + protected transformMathCallExpression(node: ts.CallExpression): tstl.Expression { const expression = node.expression as ts.PropertyAccessExpression; const signature = this.checker.getResolvedSignature(node); const params = this.transformArguments(node.arguments, signature); @@ -3902,7 +3904,7 @@ export class LuaTransformer { } // Transpile access of string properties, only supported properties are allowed - private transformStringProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression { + protected transformStringProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression { switch (node.name.escapedText) { case "length": const expression = this.transformExpression(node.expression); @@ -3913,7 +3915,7 @@ export class LuaTransformer { } // Transpile access of array properties, only supported properties are allowed - private transformArrayProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression | undefined { + protected transformArrayProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression | undefined { switch (node.name.escapedText) { case "length": const expression = this.transformExpression(node.expression); @@ -3923,7 +3925,7 @@ export class LuaTransformer { } } - private transformLuaTableProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression { + protected transformLuaTableProperty(node: ts.PropertyAccessExpression): tstl.UnaryExpression { switch (node.name.escapedText) { case "length": const propertyAccessExpression = this.transformExpression(node.expression); @@ -3960,7 +3962,7 @@ export class LuaTransformer { } } - private transformConstEnumValue( + protected transformConstEnumValue( enumType: ts.EnumType, memberName: string, tsOriginal: ts.Node @@ -4002,7 +4004,7 @@ export class LuaTransformer { throw TSTLErrors.CouldNotFindEnumMember(enumDeclaration, memberName, tsOriginal); } - private transformStringCallExpression(node: ts.CallExpression): tstl.Expression { + protected transformStringCallExpression(node: ts.CallExpression): tstl.Expression { const expression = node.expression as ts.PropertyAccessExpression; const signature = this.checker.getResolvedSignature(node); const params = this.transformArguments(node.arguments, signature); @@ -4136,7 +4138,7 @@ export class LuaTransformer { } } - private createStringCall( + protected createStringCall( methodName: string, tsOriginal: ts.Node, ...params: tstl.Expression[] @@ -4150,7 +4152,7 @@ export class LuaTransformer { } // Transpile a String._ property - private transformStringExpression(identifier: ts.Identifier): ExpressionVisitResult { + protected transformStringExpression(identifier: ts.Identifier): ExpressionVisitResult { const identifierString = identifier.escapedText as string; switch (identifierString) { @@ -4169,7 +4171,7 @@ export class LuaTransformer { } // Transpile an Object._ property - private transformObjectCallExpression(expression: ts.CallExpression): ExpressionVisitResult { + protected transformObjectCallExpression(expression: ts.CallExpression): ExpressionVisitResult { const method = expression.expression as ts.PropertyAccessExpression; const parameters = this.transformArguments(expression.arguments); const methodName = method.name.escapedText; @@ -4190,7 +4192,7 @@ export class LuaTransformer { } } - private transformConsoleCallExpression(expression: ts.CallExpression): ExpressionVisitResult { + protected transformConsoleCallExpression(expression: ts.CallExpression): ExpressionVisitResult { const method = expression.expression as ts.PropertyAccessExpression; const methodName = method.name.escapedText; const signature = this.checker.getResolvedSignature(expression); @@ -4261,12 +4263,12 @@ export class LuaTransformer { } } - private isStringFormatTemplate(expression: ts.Expression): boolean { + protected isStringFormatTemplate(expression: ts.Expression): boolean { return ts.isStringLiteral(expression) && expression.text.match(/\%/g) !== null; } // Transpile a Symbol._ property - private transformSymbolCallExpression(expression: ts.CallExpression): tstl.CallExpression { + protected transformSymbolCallExpression(expression: ts.CallExpression): tstl.CallExpression { const method = expression.expression as ts.PropertyAccessExpression; const signature = this.checker.getResolvedSignature(expression); const parameters = this.transformArguments(expression.arguments, signature); @@ -4285,7 +4287,7 @@ export class LuaTransformer { } // Transpile a Number._ property - private transformNumberCallExpression(expression: ts.CallExpression): tstl.CallExpression { + protected transformNumberCallExpression(expression: ts.CallExpression): tstl.CallExpression { const method = expression.expression as ts.PropertyAccessExpression; const parameters = this.transformArguments(expression.arguments); const methodName = method.name.escapedText; @@ -4300,7 +4302,7 @@ export class LuaTransformer { } } - private validateLuaTableCall( + protected validateLuaTableCall( expression: ts.CallExpression & { expression: ts.PropertyAccessExpression }, isWithinExpressionStatement: boolean ): void { @@ -4329,7 +4331,7 @@ export class LuaTransformer { } } - private transformLuaTableExpressionStatement( + protected transformLuaTableExpressionStatement( node: ts.ExpressionStatement & { expression: ts.CallExpression } & { expression: { expression: ts.PropertyAccessExpression }; } @@ -4358,7 +4360,7 @@ export class LuaTransformer { } } - private transformLuaTableCallExpression( + protected transformLuaTableCallExpression( expression: ts.CallExpression & { expression: ts.PropertyAccessExpression } ): tstl.Expression { const method = expression.expression; @@ -4376,7 +4378,7 @@ export class LuaTransformer { } } - private transformArrayCallExpression(node: ts.CallExpression): tstl.CallExpression { + protected transformArrayCallExpression(node: ts.CallExpression): tstl.CallExpression { const expression = node.expression as ts.PropertyAccessExpression; const signature = this.checker.getResolvedSignature(node); const params = this.transformArguments(node.arguments, signature); @@ -4439,7 +4441,7 @@ export class LuaTransformer { } } - private transformFunctionCallExpression(node: ts.CallExpression): tstl.CallExpression { + 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) { @@ -4593,7 +4595,7 @@ export class LuaTransformer { return tstl.createIdentifier(text, identifier, symbolId); } - private transformIdentifierExpression(expression: ts.Identifier): tstl.Expression { + protected transformIdentifierExpression(expression: ts.Identifier): tstl.Expression { const identifier = this.transformIdentifier(expression); const exportScope = this.getIdentifierExportScope(identifier); @@ -5199,7 +5201,7 @@ export class LuaTransformer { this.genVarCounter++; } - private shouldHoist(symbolId: tstl.SymbolId, scope: Scope): boolean { + protected shouldHoist(symbolId: tstl.SymbolId, scope: Scope): boolean { const symbolInfo = this.symbolInfo.get(symbolId); if (!symbolInfo) { return false; @@ -5401,7 +5403,7 @@ export class LuaTransformer { } } - private createConstructorDecorationStatement( + protected createConstructorDecorationStatement( declaration: ts.ClassLikeDeclaration ): tstl.AssignmentStatement | undefined { const className =