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
29 changes: 29 additions & 0 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3515,6 +3515,35 @@ export class LuaTransformer {
const firstParamPlusOne = this.expressionPlusOne(params[0]);
return this.createStringCall("byte", node, caller, firstParamPlusOne);
}
case "byte":
case "char":
case "dump":
case "find":
case "format":
case "gmatch":
case "gsub":
case "len":
case "lower":
case "match":
case "pack":
case "packsize":
case "rep":
case "reverse":
case "sub":
case "unpack":
case "upper":
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow upper to be redefined? TS already has toUpperCase() that should translate to this? Same for several others of these.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd like to be able to disable the es libs altogether and just use lua's in some circumstances.

// Allow lua's string instance methods
let stringVariable = this.transformExpression(expression.expression);
if (ts.isStringLiteral(expression.expression)) {
// "foo":method() needs to be ("foo"):method()
stringVariable = tstl.createParenthesizedExpression(stringVariable);
}
return tstl.createMethodCallExpression(
stringVariable,
this.transformIdentifier(expression.name),
params,
node
);
default:
throw TSTLErrors.UnsupportedProperty("string", expressionName, node);
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ export class StringTests
}).toThrowError(TranspileError, "Unsupported property on string: testThisIsNoMember");
}

@Test("Suported lua string function")
public stringSuportedLuaFunction(): void {
Expect(util.transpileAndExecute(
`return "test".upper()`,
undefined,
undefined,
`interface String { upper(): string; }`
)
).toBe("TEST");
}

@TestCase([])
@TestCase([65])
@TestCase([65, 66])
Expand Down