From 6288a064b48ed57861ce9ebb2d85422ae189e546 Mon Sep 17 00:00:00 2001 From: Evgeny Nikiforov Date: Fri, 13 Jan 2023 03:23:33 +0300 Subject: [PATCH] union parsing fix --- src/parser/parseTypeNode.ts | 19 +++++++++--------- src/utils/defaultPredicates.ts | 36 +++++++++++++--------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/parser/parseTypeNode.ts b/src/parser/parseTypeNode.ts index 3e696a4..89d60f6 100644 --- a/src/parser/parseTypeNode.ts +++ b/src/parser/parseTypeNode.ts @@ -227,7 +227,7 @@ export const parseTypeNode = ( parseTypeNode( first.type, checker, - checker.getTypeFromTypeNode(first.type), + checker.getTypeAtLocation(first.type), env ), map((parsed) => ({ @@ -323,11 +323,6 @@ export const parseTypeNode = ( ); } - // if its a shape and a type declaration - if (ts.isIdentifier(node.typeName)) { - // console.log(node.typeName.text, node.typeName["symbol"].declarations.length) - } - // Unfortunately, in non-shape cases we only have ts.Type. return pipe( env, @@ -420,12 +415,18 @@ export const parseTypeNode = ( ); } - if (ts.isUnionTypeNode(node) && type.isUnion()) { + // You need to refactor this shit based on type.isUnion() and type, coz somehow typePart has type of Reference, but typePart's type is Union xDxDxD + if (ts.isUnionTypeNode(node)) { return pipe( config, sequenceReaderEither( - node.types.map((typePart, i) => - parseTypeNode(typePart, checker, type.types[i], env) + node.types.map((typePart) => + parseTypeNode( + typePart, + checker, + checker.getTypeAtLocation(typePart), + env + ) ) ), mapEither((types) => new UnionType(types)), diff --git a/src/utils/defaultPredicates.ts b/src/utils/defaultPredicates.ts index 13b1095..ed2f4d8 100644 --- a/src/utils/defaultPredicates.ts +++ b/src/utils/defaultPredicates.ts @@ -5,10 +5,12 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { constFalse, pipe } from "fp-ts/lib/function"; -import { getOrElse, map } from "fp-ts/lib/Option"; import { PropertySignature, InterfaceDeclaration } from "typescript"; +import * as array from "fp-ts/lib/Array"; +import { constFalse, pipe, flow } from "fp-ts/lib/function"; +import * as Option from "fp-ts/lib/Option"; + import { getComments } from "./getComments"; export const defaultFieldPredicate = (reg: RegExp) => ( @@ -16,7 +18,7 @@ export const defaultFieldPredicate = (reg: RegExp) => ( ): boolean => { return pipe( getComments(node), - map((lines) => { + Option.map((lines) => { let isIgnored = false; for (const line of lines) { if (line.search(reg) !== -1) { @@ -27,26 +29,16 @@ export const defaultFieldPredicate = (reg: RegExp) => ( return isIgnored; }), - getOrElse(constFalse) + Option.getOrElse(constFalse) ); }; -export const defaultInterfacePredicate = (reg: RegExp) => ( - node: InterfaceDeclaration -): boolean => { - return pipe( - getComments(node), - map((lines) => { - let toExport = false; - for (const line of lines) { - if (line.search(reg) !== -1) { - toExport = true; - break; - } - } - - return toExport; - }), - getOrElse(constFalse) +export const defaultInterfacePredicate = ( + reg: RegExp +): ((node: InterfaceDeclaration) => boolean) => + flow( + getComments, + Option.chain(array.last), + Option.map((line) => line.search(reg) !== -1), + Option.getOrElse(constFalse) ); -};