Skip to content

Commit

Permalink
Fixed linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daanboer committed May 28, 2022
1 parent b186860 commit 1658e10
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/NodeParser/ConditionalTypeNodeParser.ts
Expand Up @@ -25,7 +25,7 @@ export class ConditionalTypeNodeParser implements SubNodeParser {
}

// Narrow down check type for both condition branches
let inferMap = new Map();
const inferMap = new Map();
const trueCheckType = narrowType(checkType, (type) => isAssignableTo(extendsType, type, inferMap));
const falseCheckType = narrowType(checkType, (type) => !isAssignableTo(extendsType, type));

Expand Down
32 changes: 16 additions & 16 deletions src/TypeFormatter/TupleTypeFormatter.ts
Expand Up @@ -9,6 +9,21 @@ import { TypeFormatter } from "../TypeFormatter";
import { notUndefined } from "../Utils/notUndefined";
import { uniqueArray } from "../Utils/uniqueArray";

function uniformRestType(type: RestType, check_type: BaseType): boolean {
const inner = type.getType();
return (
(inner instanceof ArrayType && inner.getItem().getId() === check_type.getId()) ||
(inner instanceof TupleType &&
inner.getTypes().every((tuple_type) => {
if (tuple_type instanceof RestType) {
return uniformRestType(tuple_type, check_type);
} else {
return tuple_type?.getId() === check_type.getId();
}
}))
);
}

export class TupleTypeFormatter implements SubTypeFormatter {
public constructor(protected childTypeFormatter: TypeFormatter) {}

Expand All @@ -25,21 +40,6 @@ export class TupleTypeFormatter implements SubTypeFormatter {
const restType = subTypes.find((t): t is RestType => t instanceof RestType);
const firstItemType = requiredElements.length > 0 ? requiredElements[0] : optionalElements[0]?.getType();

function uniformRestType(type: RestType): boolean {
const inner = type.getType();
return (
(inner instanceof ArrayType && inner.getItem().getId() === firstItemType.getId()) ||
(inner instanceof TupleType &&
inner.getTypes().every((tuple_type) => {
if (tuple_type instanceof RestType) {
return uniformRestType(tuple_type);
} else {
return tuple_type?.getId() === firstItemType.getId();
}
}))
);
}

// Check whether the tuple is of any of the following forms:
// [A, A, A]
// [A, A, A?]
Expand All @@ -49,7 +49,7 @@ export class TupleTypeFormatter implements SubTypeFormatter {
firstItemType &&
requiredElements.every((item) => item.getId() === firstItemType.getId()) &&
optionalElements.every((item) => item.getType().getId() === firstItemType.getId()) &&
(!restType || uniformRestType(restType));
(!restType || uniformRestType(restType, firstItemType));

// If so, generate a simple array with minItems (and possibly maxItems) instead.
if (isUniformArray) {
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/isAssignableTo.ts
Expand Up @@ -108,8 +108,8 @@ export function isAssignableTo(

// Infer type can become anything
if (target instanceof InferType) {
let infer = inferMap.get(target.getName());
const key = target.getName();
const infer = inferMap.get(key);

if (infer === undefined) {
inferMap.set(key, source);
Expand Down Expand Up @@ -262,7 +262,7 @@ export function isAssignableTo(
if (i == numTarget - 1) {
if (numTarget <= numSource + 1) {
if (targetMember instanceof RestType) {
let remaining: Array<BaseType | undefined> = [];
const remaining: Array<BaseType | undefined> = [];
for (let j = i; j < numSource; j++) {
remaining.push(sourceMembers[j]);
}
Expand Down
5 changes: 1 addition & 4 deletions test/unit/isAssignableTo.test.ts
Expand Up @@ -319,10 +319,7 @@ describe("isAssignableTo", () => {
)
).toBe(true);
expect(
isAssignableTo(
new TupleType([new StringType(), new InferType("T")]),
new TupleType([new StringType()])
)
isAssignableTo(new TupleType([new StringType(), new InferType("T")]), new TupleType([new StringType()]))
).toBe(false);
expect(
isAssignableTo(
Expand Down

0 comments on commit 1658e10

Please sign in to comment.