Skip to content

Commit 9ca89e5

Browse files
committed
Process a union or intersection type of all strings as a string
1 parent c0bf63d commit 9ca89e5

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/LuaTransformer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,14 @@ export class LuaTransformer {
38953895
}
38963896

38973897
switch (ownerType.flags) {
3898+
case ts.TypeFlags.Union:
3899+
const unionOwnerType = ownerType as ts.UnionOrIntersectionType;
3900+
if (
3901+
!tsHelper.isUnionAllOfType(unionOwnerType, ts.TypeFlags.String) &&
3902+
!tsHelper.isUnionAllOfType(unionOwnerType, ts.TypeFlags.StringLiteral)
3903+
) {
3904+
break;
3905+
}
38983906
case ts.TypeFlags.String:
38993907
case ts.TypeFlags.StringLiteral:
39003908
return this.transformStringCallExpression(node);

src/TSHelper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ export function isArrayType(type: ts.Type, checker: ts.TypeChecker, program: ts.
176176
return forTypeOrAnySupertype(type, checker, t => isExplicitArrayType(t, checker, program));
177177
}
178178

179+
export function isUnionAllOfType(ownerType: ts.UnionOrIntersectionType, type: ts.TypeFlags): boolean {
180+
return !ownerType.types.some(subType => {
181+
const flags = subType.flags;
182+
183+
return flags !== type;
184+
});
185+
}
186+
179187
export function isLuaIteratorType(node: ts.Node, checker: ts.TypeChecker): boolean {
180188
const type = checker.getTypeAtLocation(node);
181189
return getCustomDecorators(type, checker).has(DecoratorKind.LuaIterator);

test/unit/string.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ test.each(["hello test"])("string.toUpperCase (%p)", inp => {
233233
expect(result).toBe(inp.toUpperCase());
234234
});
235235

236+
test.each(["hello", "test", "hello test"])("string.toUpperCase (union) (%p)", inp => {
237+
const result = util.transpileAndExecute(`
238+
const _union: string = "${inp}";
239+
if (_union === 'hello' || _union === 'test' || _union === 'hello test') {
240+
return _union.toUpperCase();
241+
}
242+
`);
243+
244+
expect(result).toBe(inp.toUpperCase());
245+
});
246+
236247
test.each([
237248
{ inp: "hello test", separator: "" },
238249
{ inp: "hello test", separator: " " },

0 commit comments

Comments
 (0)