From c8e9b12dfdbb0f56da279514e3ab0bd051806512 Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Sat, 16 Apr 2022 02:22:03 +0200 Subject: [PATCH] Add support for RegExp --- package-lock.json | 161 ++++++++++++++++-- package.json | 1 + .../is-assignable-to-simple-type.ts | 5 + src/is-assignable/is-assignable-to-value.ts | 4 + src/simple-type.ts | 10 +- src/transform/to-simple-type.ts | 6 + src/utils/ts-util.ts | 7 + test/is-assignable-to-value.spec.ts | 21 +++ 8 files changed, 201 insertions(+), 14 deletions(-) create mode 100644 test/is-assignable-to-value.spec.ts diff --git a/package-lock.json b/package-lock.json index 1cbf6a3..592b5c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "ts-simple-type", "version": "2.0.0-next.0", "license": "MIT", "devDependencies": { @@ -20,6 +21,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" } @@ -1834,6 +1836,39 @@ "node": ">=6" } }, + "node_modules/@ts-morph/common": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.13.0.tgz", + "integrity": "sha512-fEJ6j7Cu8yiWjA4UmybOBH9Efgb/64ZTWuvCF4KysGu4xz8ettfyaqFt8WZ1btCxXsGZJjZ2/3svOF6rL+UFdQ==", + "dev": true, + "dependencies": { + "fast-glob": "^3.2.11", + "minimatch": "^5.0.1", + "mkdirp": "^1.0.4", + "path-browserify": "^1.0.1" + } + }, + "node_modules/@ts-morph/common/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@ts-morph/common/node_modules/minimatch": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.0.1.tgz", + "integrity": "sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/@types/babel__core": { "version": "7.1.15", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.15.tgz", @@ -2969,6 +3004,15 @@ "mimic-response": "^1.0.0" } }, + "node_modules/code-block-writer": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-11.0.0.tgz", + "integrity": "sha512-GEqWvEWWsOvER+g9keO4ohFoD3ymwyCnqY3hoTr7GZipYFwEhMHJw+TtV0rfgRhNImM6QWZGO2XYjlJVyYT62w==", + "dev": true, + "dependencies": { + "tslib": "2.3.1" + } + }, "node_modules/code-excerpt": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/code-excerpt/-/code-excerpt-3.0.0.tgz", @@ -3755,9 +3799,9 @@ "dev": true }, "node_modules/fast-glob": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.7.tgz", - "integrity": "sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==", + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.11.tgz", + "integrity": "sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==", "dev": true, "dependencies": { "@nodelib/fs.stat": "^2.0.2", @@ -3767,7 +3811,7 @@ "micromatch": "^4.0.4" }, "engines": { - "node": ">=8" + "node": ">=8.6.0" } }, "node_modules/fast-json-stable-stringify": { @@ -5130,6 +5174,18 @@ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "dev": true }, + "node_modules/mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true, + "bin": { + "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -5485,6 +5541,12 @@ "node": ">=6" } }, + "node_modules/path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==", + "dev": true + }, "node_modules/path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", @@ -6686,6 +6748,16 @@ "node": ">=0.10.0" } }, + "node_modules/ts-morph": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-14.0.0.tgz", + "integrity": "sha512-tO8YQ1dP41fw8GVmeQAdNsD8roZi1JMqB7YwZrqU856DvmG5/710e41q2XauzTYrygH9XmMryaFeLo+kdCziyA==", + "dev": true, + "dependencies": { + "@ts-morph/common": "~0.13.0", + "code-block-writer": "^11.0.0" + } + }, "node_modules/ts-node": { "version": "9.1.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.1.1.tgz", @@ -6713,9 +6785,9 @@ } }, "node_modules/tslib": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", - "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", + "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==", "dev": true }, "node_modules/tsutils": { @@ -8417,6 +8489,38 @@ "defer-to-connect": "^1.0.1" } }, + "@ts-morph/common": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.13.0.tgz", + "integrity": "sha512-fEJ6j7Cu8yiWjA4UmybOBH9Efgb/64ZTWuvCF4KysGu4xz8ettfyaqFt8WZ1btCxXsGZJjZ2/3svOF6rL+UFdQ==", + "dev": true, + "requires": { + "fast-glob": "^3.2.11", + "minimatch": "^5.0.1", + "mkdirp": "^1.0.4", + "path-browserify": "^1.0.1" + }, + "dependencies": { + "brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0" + } + }, + "minimatch": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.0.1.tgz", + "integrity": "sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==", + "dev": true, + "requires": { + "brace-expansion": "^2.0.1" + } + } + } + }, "@types/babel__core": { "version": "7.1.15", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.15.tgz", @@ -9235,6 +9339,15 @@ "mimic-response": "^1.0.0" } }, + "code-block-writer": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-11.0.0.tgz", + "integrity": "sha512-GEqWvEWWsOvER+g9keO4ohFoD3ymwyCnqY3hoTr7GZipYFwEhMHJw+TtV0rfgRhNImM6QWZGO2XYjlJVyYT62w==", + "dev": true, + "requires": { + "tslib": "2.3.1" + } + }, "code-excerpt": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/code-excerpt/-/code-excerpt-3.0.0.tgz", @@ -9844,9 +9957,9 @@ "dev": true }, "fast-glob": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.7.tgz", - "integrity": "sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==", + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.11.tgz", + "integrity": "sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==", "dev": true, "requires": { "@nodelib/fs.stat": "^2.0.2", @@ -10870,6 +10983,12 @@ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "dev": true }, + "mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true + }, "ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -11132,6 +11251,12 @@ "integrity": "sha512-kHt7kzLoS9VBZfUsiKjv43mr91ea+U05EyKkEtqp7vNbHxmaVuEqN7XxeEVnGrMtYOAxGrDElSi96K7EgO1zCA==", "dev": true }, + "path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==", + "dev": true + }, "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", @@ -12015,6 +12140,16 @@ "integrity": "sha1-n5up2e+odkw4dpi8v+sshI8RrbM=", "dev": true }, + "ts-morph": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-14.0.0.tgz", + "integrity": "sha512-tO8YQ1dP41fw8GVmeQAdNsD8roZi1JMqB7YwZrqU856DvmG5/710e41q2XauzTYrygH9XmMryaFeLo+kdCziyA==", + "dev": true, + "requires": { + "@ts-morph/common": "~0.13.0", + "code-block-writer": "^11.0.0" + } + }, "ts-node": { "version": "9.1.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.1.1.tgz", @@ -12030,9 +12165,9 @@ } }, "tslib": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", - "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", + "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==", "dev": true }, "tsutils": { diff --git a/package.json b/package.json index 9781b6f..6a62dc9 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/is-assignable/is-assignable-to-simple-type.ts b/src/is-assignable/is-assignable-to-simple-type.ts index 61f9bfb..acb8392 100644 --- a/src/is-assignable/is-assignable-to-simple-type.ts +++ b/src/is-assignable/is-assignable-to-simple-type.ts @@ -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 diff --git a/src/is-assignable/is-assignable-to-value.ts b/src/is-assignable/is-assignable-to-value.ts index 84f6319..16c842f 100644 --- a/src/is-assignable/is-assignable-to-value.ts +++ b/src/is-assignable/is-assignable-to-value.ts @@ -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); diff --git a/src/simple-type.ts b/src/simple-type.ts index 435aeb9..b0a3fa2 100644 --- a/src/simple-type.ts +++ b/src/simple-type.ts @@ -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"; @@ -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 @@ -304,6 +309,7 @@ export type SimpleType = | SimpleTypeUnknown | SimpleTypeAlias | SimpleTypeDate + | SimpleTypeRegExp | SimpleTypeGenericArguments | SimpleTypeGenericParameter; @@ -329,6 +335,7 @@ const SIMPLE_TYPE_MAP: Record { + const [type, typeChecker] = getType("type Type = RegExp"); + t.truthy(isAssignableToValue(type, /test/, typeChecker)); + t.falsy(isAssignableToValue(type, {}, typeChecker)); +});