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

♻️ Use bigint for long types in mcdoc #1487

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/json/src/checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ function inferType(node: JsonNode): Exclude<mcdoc.McdocType, mcdoc.UnionType> {
case 'json:number':
return {
kind: 'literal',
value: { kind: node.value.type, value: Number(node.value.value) },
value: {
kind: node.value.type,
value: node.value.value,
} as (mcdoc.LiteralNumericValue | mcdoc.LiteralLongNumberValue),
}
case 'json:null':
return { kind: 'any' } // null is always invalid?
Expand Down
38 changes: 23 additions & 15 deletions packages/mcdoc/src/binder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
ListTypeNode,
LiteralNode,
LiteralTypeNode,
LongRangeNode,
NumericTypeNode,
PathNode,
PrimitiveArrayTypeNode,
Expand Down Expand Up @@ -870,7 +871,7 @@ function convertEnumField(node: EnumFieldNode, ctx: McdocBinderContext): EnumTyp
}
}

function convertEnumValue(node: EnumValueNode, ctx: McdocBinderContext): string | number {
function convertEnumValue(node: EnumValueNode, ctx: McdocBinderContext): string | number | bigint {
if (TypedNumberNode.is(node)) {
const { value } = TypedNumberNode.destruct(node)
return value.value
Expand Down Expand Up @@ -964,23 +965,30 @@ function convertList(node: ListTypeNode, ctx: McdocBinderContext): McdocType {
return wrapType(node, {
kind: 'list',
item: convertType(item, ctx),
lengthRange: convertRange(lengthRange, ctx),
lengthRange: convertRange(lengthRange),
}, ctx)
}

function convertRange(node: FloatRangeNode | IntRangeNode, ctx: McdocBinderContext): NumericRange
function convertRange(node: FloatRangeNode | IntRangeNode): NumericRange<number>
function convertRange(node: LongRangeNode): NumericRange<bigint>
function convertRange(
node: FloatRangeNode | IntRangeNode | undefined,
ctx: McdocBinderContext,
): NumericRange | undefined
): NumericRange<number> | undefined
function convertRange(node: LongRangeNode | undefined): NumericRange<bigint> | undefined
function convertRange(
node: FloatRangeNode | IntRangeNode | undefined,
ctx: McdocBinderContext,
): NumericRange | undefined {
node: FloatRangeNode | IntRangeNode | LongRangeNode | undefined,
): NumericRange<number> | NumericRange<bigint> | undefined
function convertRange(
node: FloatRangeNode | IntRangeNode | LongRangeNode | undefined,
): NumericRange | NumericRange<bigint> | undefined {
if (!node) {
return undefined
}

if (LongRangeNode.is(node)) {
const { kind, min, max } = LongRangeNode.destruct(node)
return { kind, min: min?.value, max: max?.value }
}
const { kind, min, max } = FloatRangeNode.is(node)
? FloatRangeNode.destruct(node)
: IntRangeNode.destruct(node)
Expand All @@ -1001,7 +1009,7 @@ function convertLiteralValue(node: LiteralTypeValueNode, ctx: McdocBinderContext
kind: convertLiteralNumberSuffix(suffix, ctx)
?? (value.type === 'integer' ? 'int' : 'double'),
value: value.value,
}
} as LiteralValue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds a couple of these unsafe casts, if you find a way to get rid of them that would be nice

Copy link
Contributor Author

@NeunEinser NeunEinser Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some places, you can do

return kind === 'long'
  ? { kind, value }
  : { kind, value }

to satisfy the type checker, but it's also a bit weird ¯\_(ツ)_/¯

} else {
return { kind: 'string', value: node.value }
}
Expand Down Expand Up @@ -1032,22 +1040,22 @@ function convertNumericType(node: NumericTypeNode, ctx: McdocBinderContext): Mcd
const { numericKind, valueRange } = NumericTypeNode.destruct(node)
return wrapType(node, {
kind: numericKind.value as NumericTypeKind,
valueRange: convertRange(valueRange, ctx),
}, ctx)
valueRange: convertRange(valueRange),
} as McdocType, ctx)
}

function convertPrimitiveArray(node: PrimitiveArrayTypeNode, ctx: McdocBinderContext): McdocType {
const { arrayKind, lengthRange, valueRange } = PrimitiveArrayTypeNode.destruct(node)
return wrapType(node, {
kind: `${arrayKind.value as PrimitiveArrayValueKind}_array`,
lengthRange: convertRange(lengthRange, ctx),
valueRange: convertRange(valueRange, ctx),
}, ctx)
lengthRange: convertRange(lengthRange),
valueRange: convertRange(valueRange),
} as McdocType, ctx)
}

function convertString(node: StringTypeNode, ctx: McdocBinderContext): McdocType {
const { lengthRange } = StringTypeNode.destruct(node)
return wrapType(node, { kind: 'string', lengthRange: convertRange(lengthRange, ctx) }, ctx)
return wrapType(node, { kind: 'string', lengthRange: convertRange(lengthRange) }, ctx)
}

function convertReference(node: ReferenceTypeNode, ctx: McdocBinderContext): McdocType {
Expand Down
Loading
Loading