Skip to content

Commit 3a605ea

Browse files
authored
feat: enum expressions (#1956)
* feat: enum expressions
1 parent c4973eb commit 3a605ea

File tree

4 files changed

+143
-130
lines changed

4 files changed

+143
-130
lines changed

src/lib/converter/symbols.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,17 @@ function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) {
868868

869869
return type.getProperties().every((prop) => {
870870
const propType = checker.getTypeOfSymbolAtLocation(prop, location);
871-
return propType.isStringLiteral() || propType.isNumberLiteral();
871+
return isValidEnumProperty(propType);
872872
});
873873
}
874874

875+
function isValidEnumProperty(type: ts.Type) {
876+
return hasAnyFlag(
877+
type.flags,
878+
ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike
879+
);
880+
}
881+
875882
function convertVariableAsEnum(
876883
context: Context,
877884
symbol: ts.Symbol,
@@ -899,10 +906,12 @@ function convertVariableAsEnum(
899906
prop,
900907
declaration
901908
);
902-
assert(propType.isStringLiteral() || propType.isNumberLiteral());
903909

904-
reflection.defaultValue = JSON.stringify(propType.value);
905-
reflection.type = new LiteralType(propType.value);
910+
reflection.type = context.converter.convertType(context, propType);
911+
912+
if (propType.isStringLiteral() || propType.isNumberLiteral()) {
913+
reflection.defaultValue = JSON.stringify(propType.value);
914+
}
906915

907916
rc.finalizeDeclarationReflection(reflection, prop, void 0);
908917
}

src/test/behaviorTests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ export const behaviorTests: Record<
8080
ReflectionKind.Enum,
8181
"WithoutReadonlyNumeric"
8282
);
83+
84+
const WithInvalidTypeUnionMember = query(
85+
project,
86+
"WithInvalidTypeUnionMember"
87+
);
88+
equal(
89+
WithInvalidTypeUnionMember.kind,
90+
ReflectionKind.Variable,
91+
"WithInvalidTypeUnionMember"
92+
);
93+
94+
const WithNumericExpression = query(project, "WithNumericExpression");
95+
equal(
96+
WithNumericExpression.kind,
97+
ReflectionKind.Enum,
98+
"WithNumericExpression"
99+
);
83100
},
84101

85102
declareGlobal(project) {

0 commit comments

Comments
 (0)