Skip to content

Commit d9a996f

Browse files
ark120202Perryvw
authored andcommitted
Make utils regular functions instead of utility classes (#640)
1 parent d5f644c commit d9a996f

24 files changed

+866
-884
lines changed

src/LuaPrinter.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as path from "path";
2-
3-
import { Mapping, SourceNode, SourceMapGenerator } from "source-map";
4-
5-
import * as tstl from "./LuaAST";
2+
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
63
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
7-
import { LuaLib, LuaLibFeature } from "./LuaLib";
8-
import { TSHelper as tsHelper } from "./TSHelper";
4+
import * as tstl from "./LuaAST";
95
import { luaKeywords } from "./LuaKeywords";
6+
import { LuaLib, LuaLibFeature } from "./LuaLib";
7+
import * as tsHelper from "./TSHelper";
108

119
type SourceChunk = string | SourceNode;
1210

src/LuaTransformer.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { CompilerOptions, LuaTarget } from "./CompilerOptions";
44
import { Decorator, DecoratorKind } from "./Decorator";
55
import * as tstl from "./LuaAST";
66
import { LuaLibFeature } from "./LuaLib";
7-
import { ContextType, TSHelper as tsHelper } from "./TSHelper";
8-
import { TSTLErrors } from "./TSTLErrors";
7+
import * as tsHelper from "./TSHelper";
8+
import * as TSTLErrors from "./TSTLErrors";
99
import { luaKeywords, luaBuiltins } from "./LuaKeywords";
1010

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

466466
protected validateClassElement(element: ts.ClassElement): void {
467467
if (element.name && (ts.isStringLiteral(element.name) || ts.isIdentifier(element.name))) {
468-
if (tsHelper.isStatic(element) && element.name.text === "new") {
468+
if (tsHelper.isStaticNode(element) && element.name.text === "new") {
469469
throw TSTLErrors.ForbiddenStaticClassPropertyName(element, element.name.text);
470470
}
471471
}
@@ -579,16 +579,16 @@ export class LuaTransformer {
579579
}
580580

581581
// LuaTable classes must be ambient
582-
if (decorators.has(DecoratorKind.LuaTable) && !tsHelper.isAmbient(statement)) {
582+
if (decorators.has(DecoratorKind.LuaTable) && !tsHelper.isAmbientNode(statement)) {
583583
throw TSTLErrors.ForbiddenLuaTableNonDeclaration(statement);
584584
}
585585

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

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

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

@@ -808,7 +808,7 @@ export class LuaTransformer {
808808
);
809809

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

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

868868
// localClassName.prototype.____getters = {}
869-
if (statement.members.some(m => ts.isGetAccessor(m) && !tsHelper.isStatic(m))) {
869+
if (statement.members.some(m => ts.isGetAccessor(m) && !tsHelper.isStaticNode(m))) {
870870
const classPrototypeGetters = tstl.createTableIndexExpression(
871871
createClassPrototype(),
872872
tstl.createStringLiteral("____getters")
@@ -901,7 +901,7 @@ export class LuaTransformer {
901901
result.push(assignClassPrototypeIndex);
902902
}
903903

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

1276-
const methodTable = tsHelper.isStatic(getAccessor)
1276+
const methodTable = tsHelper.isStaticNode(getAccessor)
12771277
? tstl.cloneIdentifier(className)
12781278
: tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("prototype"));
12791279

@@ -1306,7 +1306,7 @@ export class LuaTransformer {
13061306
tstl.FunctionExpressionFlags.Declaration
13071307
);
13081308

1309-
const methodTable = tsHelper.isStatic(setAccessor)
1309+
const methodTable = tsHelper.isStaticNode(setAccessor)
13101310
? tstl.cloneIdentifier(className)
13111311
: tstl.createTableIndexExpression(tstl.cloneIdentifier(className), tstl.createStringLiteral("prototype"));
13121312

@@ -1335,7 +1335,7 @@ export class LuaTransformer {
13351335

13361336
const type = this.checker.getTypeAtLocation(node);
13371337
const context =
1338-
tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void
1338+
tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void
13391339
? this.createSelfIdentifier()
13401340
: undefined;
13411341
const [paramNames, dots, restParamName] = this.transformParameters(node.parameters, context);
@@ -1351,7 +1351,7 @@ export class LuaTransformer {
13511351
);
13521352

13531353
const methodTable =
1354-
tsHelper.isStatic(node) || noPrototype
1354+
tsHelper.isStaticNode(node) || noPrototype
13551355
? tstl.cloneIdentifier(className)
13561356
: tstl.createTableIndexExpression(
13571357
tstl.cloneIdentifier(className),
@@ -1847,7 +1847,7 @@ export class LuaTransformer {
18471847

18481848
const type = this.checker.getTypeAtLocation(functionDeclaration);
18491849
const context =
1850-
tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void
1850+
tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void
18511851
? this.createSelfIdentifier()
18521852
: undefined;
18531853
const [params, dotsLiteral, restParamName] = this.transformParameters(functionDeclaration.parameters, context);
@@ -3481,7 +3481,7 @@ export class LuaTransformer {
34813481
const type = this.checker.getTypeAtLocation(node);
34823482

34833483
let context: tstl.Identifier | undefined;
3484-
if (tsHelper.getFunctionContextType(type, this.checker) !== ContextType.Void) {
3484+
if (tsHelper.getFunctionContextType(type, this.checker) !== tsHelper.ContextType.Void) {
34853485
if (ts.isArrowFunction(node)) {
34863486
// dummy context for arrow functions with parameters
34873487
if (node.parameters.length > 0) {
@@ -3661,7 +3661,7 @@ export class LuaTransformer {
36613661
const signatureDeclaration = signature && signature.getDeclaration();
36623662
if (
36633663
signatureDeclaration &&
3664-
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) === ContextType.Void
3664+
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) === tsHelper.ContextType.Void
36653665
) {
36663666
parameters = this.transformArguments(expression.arguments, signature);
36673667
} else {
@@ -3805,7 +3805,7 @@ export class LuaTransformer {
38053805
const signatureDeclaration = signature && signature.getDeclaration();
38063806
if (
38073807
!signatureDeclaration ||
3808-
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== ContextType.Void
3808+
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== tsHelper.ContextType.Void
38093809
) {
38103810
if (
38113811
luaKeywords.has(node.expression.name.text) ||
@@ -3845,7 +3845,7 @@ export class LuaTransformer {
38453845
const signatureDeclaration = signature && signature.getDeclaration();
38463846
if (
38473847
!signatureDeclaration ||
3848-
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== ContextType.Void
3848+
tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== tsHelper.ContextType.Void
38493849
) {
38503850
// Pass left-side as context
38513851

@@ -4557,7 +4557,7 @@ export class LuaTransformer {
45574557
protected transformFunctionCallExpression(node: ts.CallExpression): tstl.CallExpression {
45584558
const expression = node.expression as ts.PropertyAccessExpression;
45594559
const callerType = this.checker.getTypeAtLocation(expression.expression);
4560-
if (tsHelper.getFunctionContextType(callerType, this.checker) === ContextType.Void) {
4560+
if (tsHelper.getFunctionContextType(callerType, this.checker) === tsHelper.ContextType.Void) {
45614561
throw TSTLErrors.UnsupportedSelfFunctionConversion(node);
45624562
}
45634563
const signature = this.checker.getResolvedSignature(node);
@@ -5075,10 +5075,14 @@ export class LuaTransformer {
50755075
const fromContext = tsHelper.getFunctionContextType(fromType, this.checker);
50765076
const toContext = tsHelper.getFunctionContextType(toType, this.checker);
50775077

5078-
if (fromContext === ContextType.Mixed || toContext === ContextType.Mixed) {
5078+
if (fromContext === tsHelper.ContextType.Mixed || toContext === tsHelper.ContextType.Mixed) {
50795079
throw TSTLErrors.UnsupportedOverloadAssignment(node, toName);
5080-
} else if (fromContext !== toContext && fromContext !== ContextType.None && toContext !== ContextType.None) {
5081-
if (toContext === ContextType.Void) {
5080+
} else if (
5081+
fromContext !== toContext &&
5082+
fromContext !== tsHelper.ContextType.None &&
5083+
toContext !== tsHelper.ContextType.None
5084+
) {
5085+
if (toContext === tsHelper.ContextType.Void) {
50825086
throw TSTLErrors.UnsupportedNoSelfFunctionConversion(node, toName);
50835087
} else {
50845088
throw TSTLErrors.UnsupportedSelfFunctionConversion(node, toName);
@@ -5239,7 +5243,7 @@ export class LuaTransformer {
52395243
protected hasUnsafeSymbolName(symbol: ts.Symbol, tsOriginal?: ts.Identifier): boolean {
52405244
const isLuaKeyword = luaKeywords.has(symbol.name);
52415245
const isInvalidIdentifier = !tsHelper.isValidLuaIdentifier(symbol.name);
5242-
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbient(d));
5246+
const isAmbient = symbol.declarations.some(d => tsHelper.isAmbientNode(d));
52435247
if ((isLuaKeyword || isInvalidIdentifier) && isAmbient) {
52445248
// Catch ambient declarations of identifiers with bad names
52455249
throw TSTLErrors.InvalidAmbientIdentifierName(tsOriginal || ts.createIdentifier(symbol.name));
@@ -5545,7 +5549,7 @@ export class LuaTransformer {
55455549
const expression = decorator.expression;
55465550
const type = this.checker.getTypeAtLocation(expression);
55475551
const context = tsHelper.getFunctionContextType(type, this.checker);
5548-
if (context === ContextType.Void) {
5552+
if (context === tsHelper.ContextType.Void) {
55495553
throw TSTLErrors.InvalidDecoratorContext(decorator);
55505554
}
55515555
return this.transformExpression(expression);

0 commit comments

Comments
 (0)