Skip to content

Commit

Permalink
feat: enum expressions (#1956)
Browse files Browse the repository at this point in the history
* feat: enum expressions
  • Loading branch information
Zamiell committed Jun 25, 2022
1 parent c4973eb commit 3a605ea
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 130 deletions.
17 changes: 13 additions & 4 deletions src/lib/converter/symbols.ts
Expand Up @@ -868,10 +868,17 @@ function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) {

return type.getProperties().every((prop) => {
const propType = checker.getTypeOfSymbolAtLocation(prop, location);
return propType.isStringLiteral() || propType.isNumberLiteral();
return isValidEnumProperty(propType);
});
}

function isValidEnumProperty(type: ts.Type) {
return hasAnyFlag(
type.flags,
ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike
);
}

function convertVariableAsEnum(
context: Context,
symbol: ts.Symbol,
Expand Down Expand Up @@ -899,10 +906,12 @@ function convertVariableAsEnum(
prop,
declaration
);
assert(propType.isStringLiteral() || propType.isNumberLiteral());

reflection.defaultValue = JSON.stringify(propType.value);
reflection.type = new LiteralType(propType.value);
reflection.type = context.converter.convertType(context, propType);

if (propType.isStringLiteral() || propType.isNumberLiteral()) {
reflection.defaultValue = JSON.stringify(propType.value);
}

rc.finalizeDeclarationReflection(reflection, prop, void 0);
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/behaviorTests.ts
Expand Up @@ -80,6 +80,23 @@ export const behaviorTests: Record<
ReflectionKind.Enum,
"WithoutReadonlyNumeric"
);

const WithInvalidTypeUnionMember = query(
project,
"WithInvalidTypeUnionMember"
);
equal(
WithInvalidTypeUnionMember.kind,
ReflectionKind.Variable,
"WithInvalidTypeUnionMember"
);

const WithNumericExpression = query(project, "WithNumericExpression");
equal(
WithNumericExpression.kind,
ReflectionKind.Enum,
"WithNumericExpression"
);
},

declareGlobal(project) {
Expand Down

0 comments on commit 3a605ea

Please sign in to comment.