Skip to content

Commit 464bba1

Browse files
committed
Remove TS prefix from 4-underscore-prefixed identifiers
1 parent 6d78e35 commit 464bba1

2 files changed

Lines changed: 62 additions & 66 deletions

File tree

src/LuaTransformer.ts

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ export class LuaTransformer {
887887
tstl.createStringLiteral("__index")
888888
);
889889
if (tsHelper.hasGetAccessorInClassOrAncestor(statement, false, this.checker)) {
890-
// localClassName.prototype.__index = __TS_Index(localClassName.prototype)
890+
// localClassName.prototype.__index = __TS__Index(localClassName.prototype)
891891
const assignClassPrototypeIndex = tstl.createAssignmentStatement(
892892
classPrototypeIndex,
893893
this.transformLuaLibFunction(LuaLibFeature.Index, undefined, createClassPrototype()),
@@ -919,7 +919,7 @@ export class LuaTransformer {
919919
}
920920

921921
if (tsHelper.hasSetAccessorInClassOrAncestor(statement, false, this.checker)) {
922-
// localClassName.prototype.__newindex = __TS_NewIndex(localClassName.prototype)
922+
// localClassName.prototype.__newindex = __TS__NewIndex(localClassName.prototype)
923923
const classPrototypeNewIndex = tstl.createTableIndexExpression(
924924
createClassPrototype(),
925925
tstl.createStringLiteral("__newindex")
@@ -1388,11 +1388,11 @@ export class LuaTransformer {
13881388
continue;
13891389
}
13901390

1391-
// Binding patterns become ____TS_bindingPattern0, ____TS_bindingPattern1, etc as function parameters
1391+
// Binding patterns become ____bindingPattern0, ____bindingPattern1, etc as function parameters
13921392
// See transformFunctionBody for how these values are destructured
13931393
const paramName =
13941394
ts.isObjectBindingPattern(param.name) || ts.isArrayBindingPattern(param.name)
1395-
? tstl.createIdentifier(`____TS_bindingPattern${identifierIndex++}`)
1395+
? tstl.createIdentifier(`____bindingPattern${identifierIndex++}`)
13961396
: this.transformIdentifier(param.name as ts.Identifier);
13971397

13981398
// This parameter is a spread parameter (...param)
@@ -1441,7 +1441,7 @@ export class LuaTransformer {
14411441
let bindPatternIndex = 0;
14421442
for (const declaration of parameters) {
14431443
if (ts.isObjectBindingPattern(declaration.name) || ts.isArrayBindingPattern(declaration.name)) {
1444-
const identifier = tstl.createIdentifier(`____TS_bindingPattern${bindPatternIndex++}`);
1444+
const identifier = tstl.createIdentifier(`____bindingPattern${bindPatternIndex++}`);
14451445
if (declaration.initializer !== undefined) {
14461446
// Default binding parameter
14471447
headerStatements.push(
@@ -2336,14 +2336,14 @@ export class LuaTransformer {
23362336
// Declaration of new variable
23372337
const variables = statement.initializer.declarations[0].name;
23382338
if (ts.isArrayBindingPattern(variables) || ts.isObjectBindingPattern(variables)) {
2339-
valueVariable = tstl.createIdentifier("____TS_values");
2339+
valueVariable = tstl.createIdentifier("____values");
23402340
block.statements.unshift(this.transformForOfInitializer(statement.initializer, valueVariable));
23412341
} else {
23422342
valueVariable = this.transformIdentifier(variables);
23432343
}
23442344
} else {
23452345
// Assignment to existing variable
2346-
valueVariable = tstl.createIdentifier("____TS_value");
2346+
valueVariable = tstl.createIdentifier("____value");
23472347
block.statements.unshift(this.transformForOfInitializer(statement.initializer, valueVariable));
23482348
}
23492349

@@ -2387,12 +2387,10 @@ export class LuaTransformer {
23872387
}
23882388
} else {
23892389
// Variables NOT declared in for loop - catch iterator values in temps and assign
2390-
// for ____TS_value0 in ${iterable} do
2391-
// ${initializer} = ____TS_value0
2390+
// for ____value0 in ${iterable} do
2391+
// ${initializer} = ____value0
23922392
if (ts.isArrayLiteralExpression(statement.initializer)) {
2393-
const tmps = statement.initializer.elements.map((_, i) =>
2394-
tstl.createIdentifier(`____TS_value${i}`)
2395-
);
2393+
const tmps = statement.initializer.elements.map((_, i) => tstl.createIdentifier(`____value${i}`));
23962394
const assign = tstl.createAssignmentStatement(
23972395
statement.initializer.elements.map(
23982396
e => this.transformExpression(e) as tstl.AssignmentLeftHandSideExpression
@@ -2421,9 +2419,9 @@ export class LuaTransformer {
24212419
);
24222420
} else {
24232421
// Destructuring or variable NOT declared in for loop
2424-
// for ____TS_value in ${iterator} do
2425-
// local ${initializer} = unpack(____TS_value)
2426-
const valueVariable = tstl.createIdentifier("____TS_value");
2422+
// for ____value in ${iterator} do
2423+
// local ${initializer} = unpack(____value)
2424+
const valueVariable = tstl.createIdentifier("____value");
24272425
const initializer = this.transformForOfInitializer(statement.initializer, valueVariable);
24282426
block.statements.splice(0, 0, initializer);
24292427
return tstl.createForInStatement(block, [valueVariable], [luaIterator]);
@@ -2446,9 +2444,9 @@ export class LuaTransformer {
24462444
);
24472445
} else {
24482446
// Destructuring or variable NOT declared in for loop
2449-
// for ____TS_value in __TS__iterator(${iterator}) do
2450-
// local ${initializer} = ____TS_value
2451-
const valueVariable = tstl.createIdentifier("____TS_value");
2447+
// for ____value in __TS__iterator(${iterator}) do
2448+
// local ${initializer} = ____value
2449+
const valueVariable = tstl.createIdentifier("____value");
24522450
const initializer = this.transformForOfInitializer(statement.initializer, valueVariable);
24532451
block.statements.splice(0, 0, initializer);
24542452
return tstl.createForInStatement(
@@ -2556,7 +2554,7 @@ export class LuaTransformer {
25562554
if (scope === undefined) {
25572555
throw TSTLErrors.UndefinedScope();
25582556
}
2559-
const switchName = `____TS_switch${scope.id}`;
2557+
const switchName = `____switch${scope.id}`;
25602558

25612559
const expression = this.transformExpression(statement.expression);
25622560
const switchVariable = tstl.createIdentifier(switchName);
@@ -2612,7 +2610,7 @@ export class LuaTransformer {
26122610
}
26132611

26142612
if (breakableScope.type === ScopeType.Switch) {
2615-
return tstl.createGotoStatement(`____TS_switch${breakableScope.id}_end`);
2613+
return tstl.createGotoStatement(`____switch${breakableScope.id}_end`);
26162614
} else {
26172615
return tstl.createBreakStatement(breakStatement);
26182616
}
@@ -2628,8 +2626,8 @@ export class LuaTransformer {
26282626
public transformTryStatement(statement: ts.TryStatement): StatementVisitResult {
26292627
const [tryBlock, tryScope] = this.transformScopeBlock(statement.tryBlock, ScopeType.Try);
26302628

2631-
const tryResultIdentfier = tstl.createIdentifier("____TS_try");
2632-
const returnValueIdentifier = tstl.createIdentifier("____TS_returnValue");
2629+
const tryResultIdentifier = tstl.createIdentifier("____try");
2630+
const returnValueIdentifier = tstl.createIdentifier("____returnValue");
26332631

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

@@ -2643,18 +2641,18 @@ export class LuaTransformer {
26432641
// try with catch
26442642
let [catchBlock, catchScope] = this.transformScopeBlock(statement.catchClause.block, ScopeType.Catch);
26452643
if (statement.catchClause.variableDeclaration) {
2646-
// Replace ____TS_returned with catch variable
2644+
// Replace ____returned with catch variable
26472645
returnedIdentifier = this.transformIdentifier(statement.catchClause.variableDeclaration
26482646
.name as ts.Identifier);
26492647
} else if (tryScope.functionReturned || catchScope.functionReturned) {
2650-
returnedIdentifier = tstl.createIdentifier("____TS_returned");
2648+
returnedIdentifier = tstl.createIdentifier("____returned");
26512649
}
26522650

2653-
const tryReturnIdentifiers = [tryResultIdentfier]; // ____TS_try
2651+
const tryReturnIdentifiers = [tryResultIdentifier]; // ____try
26542652
if (returnedIdentifier) {
2655-
tryReturnIdentifiers.push(returnedIdentifier); // ____TS_returned or catch variable
2653+
tryReturnIdentifiers.push(returnedIdentifier); // ____returned or catch variable
26562654
if (tryScope.functionReturned || catchScope.functionReturned) {
2657-
tryReturnIdentifiers.push(returnValueIdentifier); // ____TS_returnValue
2655+
tryReturnIdentifiers.push(returnValueIdentifier); // ____returnValue
26582656
returnCondition = tstl.cloneIdentifier(returnedIdentifier);
26592657
}
26602658
}
@@ -2672,19 +2670,19 @@ export class LuaTransformer {
26722670
catchBlock = tstl.createBlock([catchAssign]);
26732671
}
26742672
const notTryCondition = tstl.createUnaryExpression(
2675-
tstl.createParenthesizedExpression(tryResultIdentfier),
2673+
tstl.createParenthesizedExpression(tryResultIdentifier),
26762674
tstl.SyntaxKind.NotOperator
26772675
);
26782676
result.push(tstl.createIfStatement(notTryCondition, catchBlock));
26792677
} else if (tryScope.functionReturned) {
26802678
// try with return, but no catch
2681-
returnedIdentifier = tstl.createIdentifier("____TS_returned");
2682-
const returnedVariables = [tryResultIdentfier, returnedIdentifier, returnValueIdentifier];
2679+
returnedIdentifier = tstl.createIdentifier("____returned");
2680+
const returnedVariables = [tryResultIdentifier, returnedIdentifier, returnValueIdentifier];
26832681
result.push(tstl.createVariableDeclarationStatement(returnedVariables, tryCall));
26842682

2685-
// change return condition from '____TS_returned' to '____TS_try and ____TS_returned'
2683+
// change return condition from '____returned' to '____try and ____returned'
26862684
returnCondition = tstl.createBinaryExpression(
2687-
tstl.cloneIdentifier(tryResultIdentfier),
2685+
tstl.cloneIdentifier(tryResultIdentifier),
26882686
returnedIdentifier,
26892687
tstl.SyntaxKind.AndOperator
26902688
);
@@ -2699,9 +2697,9 @@ export class LuaTransformer {
26992697

27002698
if (returnCondition && returnedIdentifier) {
27012699
// With catch clause:
2702-
// if ____TS_returned then return ____TS_returnValue end
2700+
// if ____returned then return ____returnValue end
27032701
// No catch clause:
2704-
// if ____TS_try and ____TS_returned then return ____TS_returnValue end
2702+
// if ____try and ____returned then return ____returnValue end
27052703
const returnValues: tstl.Expression[] = [];
27062704
const parentTryCatch = this.findScope(ScopeType.Function | ScopeType.Try | ScopeType.Catch);
27072705
if (parentTryCatch && parentTryCatch.type !== ScopeType.Function) {
@@ -3083,7 +3081,7 @@ export class LuaTransformer {
30833081
} else {
30843082
right = [this.createUnpackCall(this.transformExpression(expression.right), expression.right)];
30853083
}
3086-
const tmps = left.map((_, i) => tstl.createIdentifier(`____TS_tmp${i}`));
3084+
const tmps = left.map((_, i) => tstl.createIdentifier(`____tmp${i}`));
30873085
const statements: tstl.Statement[] = [
30883086
tstl.createVariableDeclarationStatement(tmps, right),
30893087
tstl.createAssignmentStatement(left as tstl.AssignmentLeftHandSideExpression[], tmps),
@@ -3153,28 +3151,28 @@ export class LuaTransformer {
31533151
);
31543152
if (hasEffects && objExpression && indexExpression) {
31553153
// Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
3156-
// local __TS_obj, __TS_index = ${objExpression}, ${indexExpression};
3157-
const obj = tstl.createIdentifier("____TS_obj");
3158-
const index = tstl.createIdentifier("____TS_index");
3154+
// local __obj, __index = ${objExpression}, ${indexExpression};
3155+
const obj = tstl.createIdentifier("____obj");
3156+
const index = tstl.createIdentifier("____index");
31593157
const objAndIndexDeclaration = tstl.createVariableDeclarationStatement(
31603158
[obj, index],
31613159
[this.transformExpression(objExpression), this.transformExpression(indexExpression)]
31623160
);
31633161
const accessExpression = tstl.createTableIndexExpression(obj, index);
31643162

3165-
const tmp = tstl.createIdentifier("____TS_tmp");
3163+
const tmp = tstl.createIdentifier("____tmp");
31663164
right = tstl.createParenthesizedExpression(right);
31673165
let tmpDeclaration: tstl.VariableDeclarationStatement;
31683166
let assignStatement: tstl.AssignmentStatement;
31693167
if (isPostfix) {
3170-
// local ____TS_tmp = ____TS_obj[____TS_index];
3171-
// ____TS_obj[____TS_index] = ____TS_tmp ${replacementOperator} ${right};
3168+
// local ____tmp = ____obj[____index];
3169+
// ____obj[____index] = ____tmp ${replacementOperator} ${right};
31723170
tmpDeclaration = tstl.createVariableDeclarationStatement(tmp, accessExpression);
31733171
const operatorExpression = this.transformBinaryOperation(tmp, right, replacementOperator, expression);
31743172
assignStatement = tstl.createAssignmentStatement(accessExpression, operatorExpression);
31753173
} else {
3176-
// local ____TS_tmp = ____TS_obj[____TS_index] ${replacementOperator} ${right};
3177-
// ____TS_obj[____TS_index] = ____TS_tmp;
3174+
// local ____tmp = ____obj[____index] ${replacementOperator} ${right};
3175+
// ____obj[____index] = ____tmp;
31783176
const operatorExpression = this.transformBinaryOperation(
31793177
accessExpression,
31803178
right,
@@ -3184,18 +3182,18 @@ export class LuaTransformer {
31843182
tmpDeclaration = tstl.createVariableDeclarationStatement(tmp, operatorExpression);
31853183
assignStatement = tstl.createAssignmentStatement(accessExpression, tmp);
31863184
}
3187-
// return ____TS_tmp
3185+
// return ____tmp
31883186
return this.createImmediatelyInvokedFunctionExpression(
31893187
[objAndIndexDeclaration, tmpDeclaration, assignStatement],
31903188
tmp,
31913189
expression
31923190
);
31933191
} else if (isPostfix) {
31943192
// Postfix expressions need to cache original value in temp
3195-
// local ____TS_tmp = ${left};
3196-
// ${left} = ____TS_tmp ${replacementOperator} ${right};
3197-
// return ____TS_tmp
3198-
const tmpIdentifier = tstl.createIdentifier("____TS_tmp");
3193+
// local ____tmp = ${left};
3194+
// ${left} = ____tmp ${replacementOperator} ${right};
3195+
// return ____tmp
3196+
const tmpIdentifier = tstl.createIdentifier("____tmp");
31993197
const tmpDeclaration = tstl.createVariableDeclarationStatement(tmpIdentifier, left);
32003198
const operatorExpression = this.transformBinaryOperation(
32013199
tmpIdentifier,
@@ -3211,10 +3209,10 @@ export class LuaTransformer {
32113209
);
32123210
} else if (ts.isPropertyAccessExpression(lhs) || ts.isElementAccessExpression(lhs)) {
32133211
// Simple property/element access expressions need to cache in temp to avoid double-evaluation
3214-
// local ____TS_tmp = ${left} ${replacementOperator} ${right};
3215-
// ${left} = ____TS_tmp;
3216-
// return ____TS_tmp
3217-
const tmpIdentifier = tstl.createIdentifier("____TS_tmp");
3212+
// local ____tmp = ${left} ${replacementOperator} ${right};
3213+
// ${left} = ____tmp;
3214+
// return ____tmp
3215+
const tmpIdentifier = tstl.createIdentifier("____tmp");
32183216
const operatorExpression = this.transformBinaryOperation(left, right, replacementOperator, expression);
32193217
const tmpDeclaration = tstl.createVariableDeclarationStatement(tmpIdentifier, operatorExpression);
32203218
const assignStatement = this.transformAssignment(lhs, tmpIdentifier);
@@ -3321,10 +3319,10 @@ export class LuaTransformer {
33213319
);
33223320
if (hasEffects && objExpression && indexExpression) {
33233321
// Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
3324-
// local __TS_obj, __TS_index = ${objExpression}, ${indexExpression};
3325-
// ____TS_obj[____TS_index] = ____TS_obj[____TS_index] ${replacementOperator} ${right};
3326-
const obj = tstl.createIdentifier("____TS_obj");
3327-
const index = tstl.createIdentifier("____TS_index");
3322+
// local __obj, __index = ${objExpression}, ${indexExpression};
3323+
// ____obj[____index] = ____obj[____index] ${replacementOperator} ${right};
3324+
const obj = tstl.createIdentifier("____obj");
3325+
const index = tstl.createIdentifier("____index");
33283326
const objAndIndexDeclaration = tstl.createVariableDeclarationStatement(
33293327
[obj, index],
33303328
[this.transformExpression(objExpression), this.transformExpression(indexExpression)]
@@ -3991,14 +3989,14 @@ export class LuaTransformer {
39913989
const context = this.transformExpression(left.expression);
39923990
if (tsHelper.isExpressionWithEvaluationEffect(left.expression)) {
39933991
// Inject context parameter
3994-
transformedArguments.unshift(tstl.createIdentifier("____TS_self"));
3992+
transformedArguments.unshift(tstl.createIdentifier("____self"));
39953993

39963994
// Cache left-side if it has effects
3997-
//(function() local ____TS_self = context; return ____TS_self[argument](parameters); end)()
3995+
//(function() local ____self = context; return ____self[argument](parameters); end)()
39983996
const argument = ts.isElementAccessExpression(left)
39993997
? this.transformElementAccessArgument(left)
40003998
: tstl.createStringLiteral(left.name.text);
4001-
const selfIdentifier = tstl.createIdentifier("____TS_self");
3999+
const selfIdentifier = tstl.createIdentifier("____self");
40024000
const selfAssignment = tstl.createVariableDeclarationStatement(selfIdentifier, context);
40034001
const index = tstl.createTableIndexExpression(selfIdentifier, argument);
40044002
const callExpression = tstl.createCallExpression(index, transformedArguments);
@@ -4894,9 +4892,7 @@ export class LuaTransformer {
48944892
}
48954893
}
48964894

4897-
const text = this.hasUnsafeIdentifierName(identifier)
4898-
? this.createSafeName(identifier.text)
4899-
: identifier.text;
4895+
const text = this.hasUnsafeIdentifierName(identifier) ? this.createSafeName(identifier.text) : identifier.text;
49004896

49014897
const symbolId = this.getIdentifierSymbolId(identifier);
49024898
return tstl.createIdentifier(text, identifier, symbolId, identifier.text);

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,25 +562,25 @@ f = function(____, x) return ({x = x}) end"
562562

563563
exports[`Transformation (tryCatch) 1`] = `
564564
"do
565-
local ____TS_try, er = pcall(
565+
local ____try, er = pcall(
566566
function()
567567
local a = 42
568568
end
569569
)
570-
if not ____TS_try then
570+
if not ____try then
571571
local b = \\"fail\\"
572572
end
573573
end"
574574
`;
575575

576576
exports[`Transformation (tryCatchFinally) 1`] = `
577577
"do
578-
local ____TS_try, er = pcall(
578+
local ____try, er = pcall(
579579
function()
580580
local a = 42
581581
end
582582
)
583-
if not ____TS_try then
583+
if not ____try then
584584
local b = \\"fail\\"
585585
end
586586
do

0 commit comments

Comments
 (0)