Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enum expressions #1956

Merged
merged 11 commits into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/lib/converter/symbols.ts
Expand Up @@ -868,10 +868,25 @@ 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 isNumberLike(type: ts.Type) {
return hasAnyFlag(type.flags, ts.TypeFlags.NumberLike);
}

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

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

reflection.defaultValue = JSON.stringify(propType.value);
reflection.type = new LiteralType(propType.value);
if (propType.isStringLiteral() || propType.isNumberLiteral()) {
// The trivial case is a string or number literal.
const enumMemberValue = propType.value;
reflection.defaultValue = JSON.stringify(enumMemberValue);
reflection.type = new LiteralType(enumMemberValue);
} else {
// If this is not a string or number literal, this it is probably an expression, like
// "1 << 1". We cannot safely evaluate the code to find out the value, so revert to
// printing "number" or "string", similar to what TypeScript itself does.
if (isNumberLike(propType)) {
reflection.type = new IntrinsicType("number");
} else if (isStringLike(propType)) {
reflection.type = new IntrinsicType("string");
} else {
reflection.type = new IntrinsicType("unknown");
}
}
Zamiell marked this conversation as resolved.
Show resolved Hide resolved

rc.finalizeDeclarationReflection(reflection, prop, void 0);
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/behaviorTests.ts
Expand Up @@ -80,6 +80,20 @@ 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