Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RegExp #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 148 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"prettier": "^2.1.1",
"rimraf": "^3.0.2",
"rollup": "^2.26.9",
"ts-morph": "^14.0.0",
"ts-node": "^9.0.0",
"typescript": "^4.3.5"
},
Expand Down
5 changes: 5 additions & 0 deletions src/is-assignable/is-assignable-to-simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,11 @@ function isAssignableToSimpleTypeInternal(typeA: SimpleType, typeB: SimpleType,
case "DATE": {
return typeB.kind === "DATE";
}

// [typeA] (compare)
case "REGEXP": {
return typeB.kind === "REGEXP";
}
}

// If we some how end up here (we shouldn't), return "true" as a safe fallback
Expand Down
4 changes: 4 additions & 0 deletions src/is-assignable/is-assignable-to-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ function convertValueToSimpleType(value: unknown, { visitValueSet, widening }: {
return {
kind: "DATE"
};
} else if (value instanceof RegExp) {
return {
kind: "REGEXP"
};
} else if (typeof value === "object" && value != null) {
visitValueSet.add(value);

Expand Down
10 changes: 9 additions & 1 deletion src/simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export type SimpleTypeKind =
| "ARRAY"
// Special types
| "DATE"
| "PROMISE";
| "PROMISE"
| "REGEXP";

export type SimpleTypeModifierKind = "EXPORT" | "AMBIENT" | "PUBLIC" | "PRIVATE" | "PROTECTED" | "STATIC" | "READONLY" | "ABSTRACT" | "ASYNC" | "DEFAULT";

Expand Down Expand Up @@ -272,6 +273,10 @@ export interface SimpleTypePromise extends SimpleTypeBase {
readonly type: SimpleType;
}

export interface SimpleTypeRegExp extends SimpleTypeBase {
readonly kind: "REGEXP";
}

export type SimpleType =
| SimpleTypeBigIntLiteral
| SimpleTypeEnumMember
Expand Down Expand Up @@ -304,6 +309,7 @@ export type SimpleType =
| SimpleTypeUnknown
| SimpleTypeAlias
| SimpleTypeDate
| SimpleTypeRegExp
| SimpleTypeGenericArguments
| SimpleTypeGenericParameter;

Expand All @@ -329,6 +335,7 @@ const SIMPLE_TYPE_MAP: Record<SimpleTypeKind, "primitive" | "primitive_literal"
ARRAY: undefined,
CLASS: undefined,
DATE: undefined,
REGEXP: undefined,
ENUM: undefined,
FUNCTION: undefined,
GENERIC_ARGUMENTS: undefined,
Expand Down Expand Up @@ -397,5 +404,6 @@ export type SimpleTypeKindMap = {
TUPLE: SimpleTypeTuple;
ARRAY: SimpleTypeArray;
DATE: SimpleTypeDate;
REGEXP: SimpleTypeRegExp;
PROMISE: SimpleTypePromise;
};
6 changes: 6 additions & 0 deletions src/transform/to-simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
isObject,
isObjectTypeReference,
isPromise,
isRegExp,
isString,
isSymbol,
isThisType,
Expand Down Expand Up @@ -345,6 +346,11 @@ function toSimpleTypeInternal(type: Type, options: ToSimpleTypeInternalOptions):
kind: "DATE",
name
};
} else if (isRegExp(type, ts)) {
simpleType = {
kind: "REGEXP",
name
};
}

// Array
Expand Down
7 changes: 7 additions & 0 deletions src/utils/ts-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ export function isDate(type: Type, ts: typeof tsModule): type is ObjectType {
return symbol.getName() === "Date";
}

export function isRegExp(type: Type, ts: typeof tsModule): type is ObjectType {
if (!isObject(type, ts)) return false;
const symbol = type.getSymbol();
if (symbol == null) return false;
return symbol.getName() === "RegExp";
}

export function isTupleTypeReference(type: Type, ts: typeof tsModule): type is TupleTypeReference {
const target = getTargetType(type, ts);
if (target == null) return false;
Expand Down
Loading