Skip to content

Commit 42c920a

Browse files
committed
PR feedback
1 parent f9bc20a commit 42c920a

File tree

5 files changed

+63
-44
lines changed

5 files changed

+63
-44
lines changed

src/transformation/builtins/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import { transformGlobalCall } from "./global";
2121
import { transformMathCall, transformMathProperty } from "./math";
2222
import { transformNumberConstructorCall, transformNumberPrototypeCall } from "./number";
2323
import { transformObjectConstructorCall, transformObjectPrototypeCall } from "./object";
24-
import { transformStringConstructorCall, transformStringProperty, transformStringPrototypeCall } from "./string";
24+
import {
25+
transformStringConstructorCall,
26+
transformStringProperty,
27+
transformStringPrototypeCall,
28+
isLuaStringMethod,
29+
} from "./string";
2530
import { transformSymbolConstructorCall } from "./symbol";
2631

2732
export function transformBuiltinPropertyAccessExpression(
@@ -92,22 +97,25 @@ export function transformBuiltinCallExpression(
9297
}
9398
}
9499

95-
if (isStringType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
100+
if (
101+
isStringType(context, ownerType) &&
102+
(hasStandardLibrarySignature(context, node) || isLuaStringMethod(node.expression.name.text))
103+
) {
96104
return transformStringPrototypeCall(context, node);
97105
}
98106

99107
if (isNumberType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
100108
return transformNumberPrototypeCall(context, node);
101109
}
102110

103-
if (isArrayType(context, ownerType)) {
111+
if (isArrayType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
104112
const result = transformArrayPrototypeCall(context, node);
105113
if (result) {
106114
return result;
107115
}
108116
}
109117

110-
if (isFunctionType(context, ownerType)) {
118+
if (isFunctionType(context, ownerType) && hasStandardLibrarySignature(context, node)) {
111119
return transformFunctionPrototypeCall(context, node);
112120
}
113121

src/transformation/builtins/string.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
77
import { PropertyCallExpression, transformArguments } from "../visitors/call";
88
import { transformIdentifier } from "../visitors/identifier";
99

10+
const luaStringMethodNames = new Set([
11+
"byte",
12+
"char",
13+
"dump",
14+
"find",
15+
"format",
16+
"gmatch",
17+
"gsub",
18+
"len",
19+
"lower",
20+
"match",
21+
"pack",
22+
"packsize",
23+
"rep",
24+
"reverse",
25+
"sub",
26+
"unpack",
27+
"upper",
28+
]);
29+
1030
function createStringCall(methodName: string, tsOriginal: ts.Node, ...params: lua.Expression[]): lua.CallExpression {
1131
const stringIdentifier = lua.createIdentifier("string");
1232
return lua.createCallExpression(
@@ -124,40 +144,29 @@ export function transformStringPrototypeCall(
124144
return transformLuaLibFunction(context, LuaLibFeature.StringPadStart, node, caller, ...params);
125145
case "padEnd":
126146
return transformLuaLibFunction(context, LuaLibFeature.StringPadEnd, node, caller, ...params);
147+
}
127148

128-
case "byte":
129-
case "char":
130-
case "dump":
131-
case "find":
132-
case "format":
133-
case "gmatch":
134-
case "gsub":
135-
case "len":
136-
case "lower":
137-
case "match":
138-
case "pack":
139-
case "packsize":
140-
case "rep":
141-
case "reverse":
142-
case "sub":
143-
case "unpack":
144-
case "upper":
145-
// Allow lua's string instance methods
146-
let stringVariable = context.transformExpression(expression.expression);
147-
if (ts.isStringLiteralLike(expression.expression)) {
148-
// "foo":method() needs to be ("foo"):method()
149-
stringVariable = lua.createParenthesizedExpression(stringVariable);
150-
}
149+
// Allow lua's string instance methods
150+
if (isLuaStringMethod(expressionName)) {
151+
let stringVariable = context.transformExpression(expression.expression);
152+
if (ts.isStringLiteralLike(expression.expression)) {
153+
// "foo":method() needs to be ("foo"):method()
154+
stringVariable = lua.createParenthesizedExpression(stringVariable);
155+
}
151156

152-
return lua.createMethodCallExpression(
153-
stringVariable,
154-
transformIdentifier(context, expression.name),
155-
params,
156-
node
157-
);
158-
default:
159-
throw UnsupportedProperty("string", expressionName, node);
157+
return lua.createMethodCallExpression(
158+
stringVariable,
159+
transformIdentifier(context, expression.name),
160+
params,
161+
node
162+
);
160163
}
164+
165+
throw UnsupportedProperty("string", expressionName, node);
166+
}
167+
168+
export function isLuaStringMethod(method: string): boolean {
169+
return luaStringMethodNames.has(method);
161170
}
162171

163172
export function transformStringConstructorCall(

test/unit/builtins/loading.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,4 @@ describe("Unknown builtin property", () => {
4444
.disableSemanticCheck()
4545
.expectToHaveDiagnosticOfError(UnsupportedProperty("Math", "unknownProperty", util.nodeStub));
4646
});
47-
48-
test("function call", () => {
49-
util.testExpression`[].unknownFunction()`
50-
.disableSemanticCheck()
51-
.expectToHaveDiagnosticOfError(UnsupportedProperty("array", "unknownFunction", util.nodeStub));
52-
});
5347
});

test/unit/builtins/numbers.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ test("number intersected method", () => {
7272
util.testFunction`
7373
type Vector = number & { normalize(): Vector };
7474
return ({ normalize: () => 3 } as Vector).normalize();
75-
`.expectToMatchJsResult();
75+
`.expectToMatchJsResult();
7676
});

test/unit/builtins/string.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import * as util from "../../util";
22

33
test("Supported lua string function", () => {
4-
util.testExpression`"test".toUpperCase()`.expectToEqual("TEST");
4+
const tsHeader = `
5+
declare global {
6+
interface String {
7+
upper(): string;
8+
}
9+
}
10+
`;
11+
12+
util.testExpression`"test".upper()`.setTsHeader(tsHeader).expectToEqual("TEST");
513
});
614

715
test.each([[], [65], [65, 66], [65, 66, 67]])("String.fromCharCode (%p)", (...args) => {
@@ -277,5 +285,5 @@ test("string intersected method", () => {
277285
util.testFunction`
278286
type Vector = string & { abc(): Vector };
279287
return ({ abc: () => "a" } as Vector).abc();
280-
`.expectToMatchJsResult();
288+
`.expectToMatchJsResult();
281289
});

0 commit comments

Comments
 (0)