Skip to content

Commit

Permalink
fix: revert LiteralType from parseLiteralNode
Browse files Browse the repository at this point in the history
  • Loading branch information
miku1958 committed Dec 26, 2023
1 parent 9c458cb commit 3f602d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
27 changes: 15 additions & 12 deletions src/parser/ValueParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import {
isTupleType,
EnumField,
isBasicType,
LiteralType,
UnionType,
OptionalType,
Value,
UnionLiteralType,
} from '../types';
import { isUndefinedOrNull, parseTypeJSDocTags } from './utils';
import { ParserLogger } from '../logger/ParserLogger';
Expand Down Expand Up @@ -245,7 +246,7 @@ export class ValueParser {

let nullable = false;
let valueType: ValueType | undefined;
const literalValues: LiteralType[] = [];
const literalValues: UnionLiteralType[] = [];

node.types.forEach((typeNode) => {
if (isUndefinedOrNull(typeNode)) {
Expand All @@ -254,7 +255,16 @@ export class ValueParser {
}
const literalKind = this.parseLiteralNode(typeNode);
if (literalKind !== null) {
literalValues.push(literalKind);
if (
literalKind.type.kind === ValueTypeKind.basicType &&
(literalKind.type.value === BasicTypeValue.string || literalKind.type.value === BasicTypeValue.number)
) {
literalValues.push({
type: literalKind.type.value,
value: literalKind.value,
});
return;
}
return;
}

Expand Down Expand Up @@ -285,14 +295,7 @@ export class ValueParser {
});

if (literalValues.length > 0) {
const kindSet = new Set(
literalValues.map((obj) => {
if ('value' in obj.type) {
return obj.type.value;
}
return null;
})
);
const kindSet = new Set(literalValues.map((obj) => obj.type));

if (kindSet.size !== 1) {
throw new ValueParserError(
Expand Down Expand Up @@ -686,7 +689,7 @@ export class ValueParser {
};
}

private parseLiteralNode(typeNode: ts.TypeNode): LiteralType | null {
private parseLiteralNode(typeNode: ts.TypeNode): { type: ValueType; value: Value } | null {
if (ts.isLiteralTypeNode(typeNode)) {
if (ts.isNumericLiteral(typeNode.literal)) {
return {
Expand Down
12 changes: 3 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,14 @@ export interface PredefinedType extends BaseValueType {
name: string;
}

/// inferace for parseLiteralNode in src/parser/ValueParser.ts
export interface LiteralType {
type:
| {
kind: ValueTypeKind.basicType;
value: BasicTypeValue.string | BasicTypeValue.number | BasicTypeValue.boolean;
}
| EnumType;
export interface UnionLiteralType {
type: BasicTypeValue.string | BasicTypeValue.number;
value: Value;
}

export interface UnionType extends BaseValueType {
kind: ValueTypeKind.unionType;
members: LiteralType[];
members: UnionLiteralType[];
}

export function isBasicType(valueType: ValueType): valueType is BasicType {
Expand Down
52 changes: 21 additions & 31 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { BasicTypeValue, EnumField, UnionType, ValueTypeKind } from './types';
import { BasicTypeValue, EnumField, UnionType } from './types';

export function capitalize(text: string): string {
if (text.length === 0) {
Expand Down Expand Up @@ -38,41 +38,31 @@ export function uniquePathWithMethodReturnType(ownerName: string, methodName: st
}

export function basicTypeOfUnion(union: UnionType): BasicTypeValue {
const { type } = union.members[0];
if ('value' in type) {
return type.value;
}
return BasicTypeValue.string;
return union.members[0].type;
}

export function membersOfUnion(union: UnionType): EnumField[] {
const result: EnumField[] = [];
union.members.forEach((value) => {
switch (value.type.kind) {
case ValueTypeKind.basicType:
switch (value.type.value) {
case BasicTypeValue.string:
if (typeof value.value === 'string') {
const enumField: EnumField = {
key: value.value,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
case BasicTypeValue.number:
if (typeof value.value === 'number') {
const enumField: EnumField = {
key: `_${value.value}`,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
default:
break;
switch (value.type) {
case BasicTypeValue.string:
if (typeof value.value === 'string') {
const enumField: EnumField = {
key: value.value,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
case BasicTypeValue.number:
if (typeof value.value === 'number') {
const enumField: EnumField = {
key: `_${value.value}`,
value: value.value,
documentation: '',
};
result.push(enumField);
}
break;
default:
Expand Down

0 comments on commit 3f602d0

Please sign in to comment.