Skip to content

Commit

Permalink
union parsing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Astroner committed Jan 13, 2023
1 parent a4879ad commit 6288a06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
19 changes: 10 additions & 9 deletions src/parser/parseTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const parseTypeNode = (
parseTypeNode(
first.type,
checker,
checker.getTypeFromTypeNode(first.type),
checker.getTypeAtLocation(first.type),
env
),
map((parsed) => ({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
Expand Down
36 changes: 14 additions & 22 deletions src/utils/defaultPredicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ 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) => (
node: PropertySignature
): boolean => {
return pipe(
getComments(node),
map((lines) => {
Option.map((lines) => {
let isIgnored = false;
for (const line of lines) {
if (line.search(reg) !== -1) {
Expand All @@ -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)
);
};

0 comments on commit 6288a06

Please sign in to comment.