Skip to content

Commit d870f00

Browse files
authored
fixed transforming undefined as a property name (#474)
1 parent 0bbae3c commit d870f00

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/LuaTransformer.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3978,29 +3978,34 @@ export class LuaTransformer {
39783978
const value = Number(propertyName.text);
39793979
return tstl.createNumericLiteral(value, propertyName);
39803980
} else {
3981-
return tstl.createStringLiteral(this.transformIdentifier(propertyName).text);
3981+
return tstl.createStringLiteral(this.getIdentifierText(propertyName));
39823982
}
39833983
}
39843984

3985-
public transformIdentifier(expression: ts.Identifier): tstl.Identifier {
3986-
if (expression.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
3987-
return tstl.createIdentifier("nil"); // TODO this is a hack that allows use to keep Identifier
3988-
// as return time as changing that would break a lot of stuff.
3989-
// But this should be changed to retun tstl.createNilLiteral()
3990-
// at some point.
3991-
}
3992-
3993-
let escapedText = expression.escapedText as string;
3985+
public getIdentifierText(identifier: ts.Identifier): string {
3986+
let escapedText = identifier.escapedText as string;
39943987
const underScoreCharCode = "_".charCodeAt(0);
39953988
if (escapedText.length >= 3 && escapedText.charCodeAt(0) === underScoreCharCode &&
39963989
escapedText.charCodeAt(1) === underScoreCharCode && escapedText.charCodeAt(2) === underScoreCharCode) {
39973990
escapedText = escapedText.substr(1);
39983991
}
39993992

40003993
if (this.luaKeywords.has(escapedText)) {
4001-
throw TSTLErrors.KeywordIdentifier(expression);
3994+
throw TSTLErrors.KeywordIdentifier(identifier);
3995+
}
3996+
3997+
return escapedText;
3998+
}
3999+
4000+
public transformIdentifier(expression: ts.Identifier): tstl.Identifier {
4001+
if (expression.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
4002+
return tstl.createIdentifier("nil"); // TODO this is a hack that allows use to keep Identifier
4003+
// as return time as changing that would break a lot of stuff.
4004+
// But this should be changed to retun tstl.createNilLiteral()
4005+
// at some point.
40024006
}
40034007

4008+
const escapedText = this.getIdentifierText(expression);
40044009
const symbolId = this.getIdentifierSymbolId(expression);
40054010
return tstl.createIdentifier(escapedText, expression, symbolId);
40064011
}

test/unit/objectLiteral.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ export class ObjectLiteralTests {
2323
const result = util.transpileAndExecute(`const x = ${input}; const o = {x}; return o.x;`);
2424
Expect(result).toBe(expected);
2525
}
26+
27+
@Test("undefined as object key")
28+
public undefinedAsObjectKey(): void {
29+
const code =
30+
`const foo = {undefined: "foo"};
31+
return foo.undefined;`;
32+
Expect(util.transpileAndExecute(code)).toBe("foo");
33+
}
2634
}

0 commit comments

Comments
 (0)