Comparing version 0.9.0 to 0.9.1
{ | ||
"name": "martok", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "", | ||
@@ -13,3 +13,4 @@ "main": "dist/src/index.js", | ||
"types": "ts-node src/index.ts tests/comparisons/single/types.d.ts -o ./schema/martok --package net.sarazan.martok --datePattern standard", | ||
"hateful": "ts-node src/index.ts tests/comparisons/single/tagged.d.ts -o ./schema/martok --package net.sarazan.martok --datePattern standard", | ||
"tagged": "ts-node src/index.ts tests/comparisons/single/tagged.d.ts -o ./schema/martok --package net.sarazan.martok --datePattern standard", | ||
"literal": "ts-node src/index.ts tests/comparisons/single/stringLiteral.d.ts -o ./schema/martok --package net.sarazan.martok --datePattern standard", | ||
"lint": "eslint . --ext .ts" | ||
@@ -16,0 +17,0 @@ }, |
@@ -19,3 +19,7 @@ import { Martok } from "../Martok"; | ||
import Klass = kotlin.Klass; | ||
import { getMembers, getMemberType } from "../../typescript/MemberHelpers"; | ||
import { | ||
getMembers, | ||
getMemberType, | ||
MemberTypeOptions, | ||
} from "../../typescript/MemberHelpers"; | ||
import { title } from "../NameGenerators"; | ||
@@ -36,3 +40,3 @@ import ConstructorParameter = kotlin.ConstructorParameter; | ||
extendSealed?: Klass; | ||
}; | ||
} & MemberTypeOptions; | ||
@@ -77,4 +81,9 @@ export class KlassGenerator { | ||
const alias = this.generateTypeAlias(node); | ||
if (alias) return alias; | ||
if (isTypeAliasDeclaration(node)) { | ||
if (this.enums.canGenerate(node.type)) { | ||
return this.enums.generate(name, node.type); | ||
} | ||
const alias = this.generateTypeAlias(node); | ||
if (alias) return alias; | ||
} | ||
@@ -131,3 +140,5 @@ const members = getMembers(node, this.checker); | ||
const name = node.name!.getText(); | ||
let type = getMemberType(this.checker, node); | ||
let type = getMemberType(this.checker, node, { | ||
followReferences: options?.followReferences ?? false, | ||
}); | ||
if (type === InternalSymbolName.Type) { | ||
@@ -134,0 +145,0 @@ type = title(name); |
@@ -10,3 +10,5 @@ import { Martok } from "../Martok"; | ||
isTypeLiteralNode, | ||
isTypeReferenceNode, | ||
isUnionTypeNode, | ||
PropertySignature, | ||
TypeElement, | ||
@@ -59,3 +61,3 @@ TypeNode, | ||
value, | ||
{ abstract: true } | ||
{ abstract: true, followReferences: true } | ||
); | ||
@@ -82,12 +84,7 @@ }) | ||
outer: for (const prop1 of type1.members) { | ||
if (!prop1.name) continue; | ||
if (!isPropertySignature(prop1)) continue; | ||
const propType = prop1.type; | ||
if (!propType) continue; | ||
if (!isLiteralTypeNode(propType)) continue; | ||
if (!isLiteralExpression(propType.literal)) continue; | ||
const name = prop1.name?.getText(); | ||
if (!name) continue; | ||
const k = this.getTagValue(prop1); | ||
// We've found a candidate for our tag discriminator. | ||
const name = prop1.name.getText(); | ||
const k = propType.literal.text; | ||
if (!k) continue; | ||
const result: TagMappings = { | ||
@@ -104,11 +101,7 @@ name, | ||
const prop2 = type2.members.find( | ||
(value) => value.name?.getText() === prop1.name.getText() | ||
(value) => value.name?.getText() === name | ||
); | ||
if (!prop2?.name) continue outer; | ||
if (!isPropertySignature(prop2)) continue outer; | ||
const propType2 = prop2.type; | ||
if (!propType2) continue outer; | ||
if (!isLiteralTypeNode(propType2)) continue outer; | ||
if (!isLiteralExpression(propType2.literal)) continue outer; | ||
const k2 = propType2.literal.text; | ||
const k2 = this.getTagValue(prop2); | ||
if (!k2) continue outer; | ||
result.mappings[k2] = type2; | ||
@@ -121,2 +114,16 @@ } | ||
private getTagValue(prop: TypeElement): string | undefined { | ||
if (!isPropertySignature(prop)) return undefined; | ||
const propType = prop.type; | ||
if (!propType) return undefined; | ||
if (isTypeReferenceNode(propType)) { | ||
const ref = this.checker.getTypeFromTypeNode(propType); | ||
if (!ref.isStringLiteral()) return undefined; | ||
return ref.value; | ||
} | ||
if (!isLiteralTypeNode(propType)) return undefined; | ||
if (!isLiteralExpression(propType.literal)) return undefined; | ||
return propType.literal.text; | ||
} | ||
private generateSerializerAndFriends( | ||
@@ -123,0 +130,0 @@ name: string, |
@@ -10,3 +10,3 @@ import { | ||
} from "typescript"; | ||
import { pascalToSnake } from "../martok/NameGenerators"; | ||
import { pascalToSnake, title } from "../martok/NameGenerators"; | ||
@@ -60,7 +60,11 @@ export function getEnumName( | ||
function getValName(name: string): string { | ||
let result = pascalToSnake(name).toUpperCase(); | ||
// TODO this should all probably be considered more thoroughly. | ||
let result = name; | ||
// let result = title(name); | ||
// let result = pascalToSnake(name).toUpperCase(); | ||
if (!isNaN(parseFloat(result))) { | ||
result = `_${result.replace(".", "_")}`; | ||
} | ||
result = result.replace(/\s/g, "_"); | ||
return result; | ||
} |
@@ -24,8 +24,17 @@ import { | ||
import { dedupeUnion } from "./UnionHelpers"; | ||
import _ from "lodash"; | ||
const QUESTION_TOKEN = factory.createToken(SyntaxKind.QuestionToken); | ||
export type MemberTypeOptions = { | ||
/** | ||
* @default true | ||
*/ | ||
followReferences?: boolean; | ||
}; | ||
export function getMemberType( | ||
checker: TypeChecker, | ||
type: TypeNode | TypeElement | ||
type: TypeNode | TypeElement, | ||
options?: MemberTypeOptions | ||
): string { | ||
@@ -37,6 +46,8 @@ if (isTypeElement(type)) { | ||
const intrinsic = getIntrinsicType(checker, type); | ||
if (intrinsic) return intrinsic; | ||
if (options?.followReferences === false && isTypeReferenceNode(type)) { | ||
const ref = type.typeName.getText(); | ||
if (ref) return ref; | ||
} | ||
const literal = getLiteralType(checker, type); | ||
const literal = getLiteralLikeType(checker, type); | ||
if (literal) return literal; | ||
@@ -72,2 +83,25 @@ | ||
export function getReferencedLiteralType( | ||
checker: TypeChecker, | ||
type: TypeNode | ||
): string | undefined { | ||
if (!isTypeReferenceNode(type)) return undefined; | ||
const ttype = checker.getTypeFromTypeNode(type); | ||
if (!ttype) return undefined; | ||
if (ttype.isStringLiteral()) return "String"; | ||
if (ttype.isNumberLiteral()) return "Double"; | ||
return undefined; | ||
} | ||
export function getLiteralLikeType( | ||
checker: TypeChecker, | ||
type: TypeNode | ||
): string | undefined { | ||
return ( | ||
getIntrinsicType(checker, type) ?? | ||
getLiteralType(checker, type) ?? | ||
getReferencedLiteralType(checker, type) | ||
); | ||
} | ||
export function getIntrinsicType( | ||
@@ -134,6 +168,8 @@ checker: TypeChecker, | ||
const symbol = ref.aliasSymbol ?? ref.getSymbol(); | ||
const decl = symbol!.declarations![0]; | ||
return getMembers(decl, checker, isTaggedUnion); | ||
if (symbol) { | ||
const decl = symbol!.declarations![0]; | ||
return getMembers(decl, checker, isTaggedUnion); | ||
} | ||
} | ||
return []; | ||
} |
@@ -0,1 +1,5 @@ | ||
export type Type1 = "type 1"; | ||
export type Type2 = "type 2"; | ||
export type Types = Type1 | Type2; | ||
export type Tagged = { | ||
@@ -6,7 +10,7 @@ id: string; | ||
| { | ||
type: "type1"; | ||
type: Type1; | ||
state: State1; | ||
} | ||
| { | ||
type: "type2"; | ||
type: Type2; | ||
state: State2; | ||
@@ -13,0 +17,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
113486
101
2450