@react-analyzer/core 0.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.js +76 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Rel1cx
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export { isFalseLiteralType, isTrueLiteralType } from 'ts-api-utils';
|
|
3
|
+
|
|
4
|
+
declare const isAnyType: (type: ts.Type) => boolean;
|
|
5
|
+
declare const isBigIntType: (type: ts.Type) => boolean;
|
|
6
|
+
declare const isBooleanType: (type: ts.Type) => boolean;
|
|
7
|
+
declare const isEnumType: (type: ts.Type) => boolean;
|
|
8
|
+
declare const isFalsyBigIntType: (type: ts.Type) => type is ts.LiteralType & {
|
|
9
|
+
freshType: ts.FreshableType;
|
|
10
|
+
regularType: ts.FreshableType;
|
|
11
|
+
flags: ts.TypeFlags;
|
|
12
|
+
symbol: ts.Symbol;
|
|
13
|
+
pattern?: ts.DestructuringPattern;
|
|
14
|
+
aliasSymbol?: ts.Symbol;
|
|
15
|
+
aliasTypeArguments?: readonly ts.Type[];
|
|
16
|
+
getFlags: () => ts.TypeFlags;
|
|
17
|
+
getSymbol: () => ts.Symbol | undefined;
|
|
18
|
+
getProperties: () => ts.Symbol[];
|
|
19
|
+
getProperty: (propertyName: string) => ts.Symbol | undefined;
|
|
20
|
+
getApparentProperties: () => ts.Symbol[];
|
|
21
|
+
getCallSignatures: () => readonly ts.Signature[];
|
|
22
|
+
getConstructSignatures: () => readonly ts.Signature[];
|
|
23
|
+
getStringIndexType: () => ts.Type | undefined;
|
|
24
|
+
getNumberIndexType: () => ts.Type | undefined;
|
|
25
|
+
getBaseTypes: () => ts.BaseType[] | undefined;
|
|
26
|
+
getNonNullableType: () => ts.Type;
|
|
27
|
+
getConstraint: () => ts.Type | undefined;
|
|
28
|
+
getDefault: () => ts.Type | undefined;
|
|
29
|
+
isUnion: () => this is ts.UnionType;
|
|
30
|
+
isIntersection: () => this is ts.IntersectionType;
|
|
31
|
+
isUnionOrIntersection: () => this is ts.UnionOrIntersectionType;
|
|
32
|
+
isLiteral: () => this is ts.LiteralType;
|
|
33
|
+
isStringLiteral: () => this is ts.StringLiteralType;
|
|
34
|
+
isNumberLiteral: () => this is ts.NumberLiteralType;
|
|
35
|
+
isTypeParameter: () => this is ts.TypeParameter;
|
|
36
|
+
isClassOrInterface: () => this is ts.InterfaceType;
|
|
37
|
+
isClass: () => this is ts.InterfaceType;
|
|
38
|
+
isIndexType: () => this is ts.IndexType;
|
|
39
|
+
value: {
|
|
40
|
+
negative: boolean;
|
|
41
|
+
base10Value: "0";
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
declare const isFalsyNumberType: (type: ts.Type) => boolean;
|
|
45
|
+
declare const isFalsyStringType: (type: ts.Type) => boolean;
|
|
46
|
+
declare const isNeverType: (type: ts.Type) => boolean;
|
|
47
|
+
declare const isNullishType: (type: ts.Type) => boolean;
|
|
48
|
+
declare const isNumberType: (type: ts.Type) => boolean;
|
|
49
|
+
declare const isObjectType: (type: ts.Type) => boolean;
|
|
50
|
+
declare const isStringType: (type: ts.Type) => boolean;
|
|
51
|
+
declare const isTruthyBigIntType: (type: ts.Type) => type is ts.LiteralType & {
|
|
52
|
+
freshType: ts.FreshableType;
|
|
53
|
+
regularType: ts.FreshableType;
|
|
54
|
+
flags: ts.TypeFlags;
|
|
55
|
+
symbol: ts.Symbol;
|
|
56
|
+
pattern?: ts.DestructuringPattern;
|
|
57
|
+
aliasSymbol?: ts.Symbol;
|
|
58
|
+
aliasTypeArguments?: readonly ts.Type[];
|
|
59
|
+
getFlags: () => ts.TypeFlags;
|
|
60
|
+
getSymbol: () => ts.Symbol | undefined;
|
|
61
|
+
getProperties: () => ts.Symbol[];
|
|
62
|
+
getProperty: (propertyName: string) => ts.Symbol | undefined;
|
|
63
|
+
getApparentProperties: () => ts.Symbol[];
|
|
64
|
+
getCallSignatures: () => readonly ts.Signature[];
|
|
65
|
+
getConstructSignatures: () => readonly ts.Signature[];
|
|
66
|
+
getStringIndexType: () => ts.Type | undefined;
|
|
67
|
+
getNumberIndexType: () => ts.Type | undefined;
|
|
68
|
+
getBaseTypes: () => ts.BaseType[] | undefined;
|
|
69
|
+
getNonNullableType: () => ts.Type;
|
|
70
|
+
getConstraint: () => ts.Type | undefined;
|
|
71
|
+
getDefault: () => ts.Type | undefined;
|
|
72
|
+
isUnion: () => this is ts.UnionType;
|
|
73
|
+
isIntersection: () => this is ts.IntersectionType;
|
|
74
|
+
isUnionOrIntersection: () => this is ts.UnionOrIntersectionType;
|
|
75
|
+
isLiteral: () => this is ts.LiteralType;
|
|
76
|
+
isStringLiteral: () => this is ts.StringLiteralType;
|
|
77
|
+
isNumberLiteral: () => this is ts.NumberLiteralType;
|
|
78
|
+
isTypeParameter: () => this is ts.TypeParameter;
|
|
79
|
+
isClassOrInterface: () => this is ts.InterfaceType;
|
|
80
|
+
isClass: () => this is ts.InterfaceType;
|
|
81
|
+
isIndexType: () => this is ts.IndexType;
|
|
82
|
+
value: ts.PseudoBigInt;
|
|
83
|
+
};
|
|
84
|
+
declare const isTruthyNumberType: (type: ts.Type) => boolean;
|
|
85
|
+
declare const isTruthyStringType: (type: ts.Type) => boolean;
|
|
86
|
+
declare const isUnknownType: (type: ts.Type) => boolean;
|
|
87
|
+
|
|
88
|
+
type Variant = "any" | "bigint" | "boolean" | "enum" | "never" | "nullish" | "number" | "object" | "string" | "unknown" | "falsy bigint" | "falsy boolean" | "falsy number" | "falsy string" | "truthy bigint" | "truthy boolean" | "truthy number" | "truthy string";
|
|
89
|
+
/**
|
|
90
|
+
* Ported from https://github.com/typescript-eslint/typescript-eslint/blob/eb736bbfc22554694400e6a4f97051d845d32e0b/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts#L826 with some enhancements
|
|
91
|
+
* Get the variants of an array of types.
|
|
92
|
+
* @param types The types to get the variants of
|
|
93
|
+
* @returns The variants of the types
|
|
94
|
+
*/
|
|
95
|
+
declare function getVariantsOfTypes(types: ts.Type[]): Set<Variant>;
|
|
96
|
+
|
|
97
|
+
export { type Variant, getVariantsOfTypes, isAnyType, isBigIntType, isBooleanType, isEnumType, isFalsyBigIntType, isFalsyNumberType, isFalsyStringType, isNeverType, isNullishType, isNumberType, isObjectType, isStringType, isTruthyBigIntType, isTruthyNumberType, isTruthyStringType, isUnknownType };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { isTypeFlagSet, isFalseLiteralType, isTrueLiteralType } from 'ts-api-utils';
|
|
2
|
+
export { isFalseLiteralType, isTrueLiteralType } from 'ts-api-utils';
|
|
3
|
+
import { isMatching, P, match } from 'ts-pattern';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
|
|
6
|
+
// src/type/type-is.ts
|
|
7
|
+
var isAnyType = (type) => isTypeFlagSet(type, ts.TypeFlags.TypeParameter | ts.TypeFlags.Any);
|
|
8
|
+
var isBigIntType = (type) => isTypeFlagSet(type, ts.TypeFlags.BigIntLike);
|
|
9
|
+
var isBooleanType = (type) => isTypeFlagSet(type, ts.TypeFlags.BooleanLike);
|
|
10
|
+
var isEnumType = (type) => isTypeFlagSet(type, ts.TypeFlags.EnumLike);
|
|
11
|
+
var isFalsyBigIntType = (type) => type.isLiteral() && isMatching({ value: { base10Value: "0" } }, type);
|
|
12
|
+
var isFalsyNumberType = (type) => type.isNumberLiteral() && type.value === 0;
|
|
13
|
+
var isFalsyStringType = (type) => type.isStringLiteral() && type.value === "";
|
|
14
|
+
var isNeverType = (type) => isTypeFlagSet(type, ts.TypeFlags.Never);
|
|
15
|
+
var isNullishType = (type) => isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.VoidLike);
|
|
16
|
+
var isNumberType = (type) => isTypeFlagSet(type, ts.TypeFlags.NumberLike);
|
|
17
|
+
var isObjectType = (type) => !isTypeFlagSet(
|
|
18
|
+
type,
|
|
19
|
+
ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.VoidLike | ts.TypeFlags.BooleanLike | ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.TypeParameter | ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never
|
|
20
|
+
);
|
|
21
|
+
var isStringType = (type) => isTypeFlagSet(type, ts.TypeFlags.StringLike);
|
|
22
|
+
var isTruthyBigIntType = (type) => type.isLiteral() && isMatching({ value: { base10Value: P.not("0") } }, type);
|
|
23
|
+
var isTruthyNumberType = (type) => type.isNumberLiteral() && type.value !== 0;
|
|
24
|
+
var isTruthyStringType = (type) => type.isStringLiteral() && type.value !== "";
|
|
25
|
+
var isUnknownType = (type) => isTypeFlagSet(type, ts.TypeFlags.Unknown);
|
|
26
|
+
function getVariantsOfTypes(types) {
|
|
27
|
+
const variants = /* @__PURE__ */ new Set();
|
|
28
|
+
if (types.some(isUnknownType)) {
|
|
29
|
+
variants.add("unknown");
|
|
30
|
+
return variants;
|
|
31
|
+
}
|
|
32
|
+
if (types.some(isNullishType)) {
|
|
33
|
+
variants.add("nullish");
|
|
34
|
+
}
|
|
35
|
+
const booleans = types.filter(isBooleanType);
|
|
36
|
+
const boolean0 = booleans[0];
|
|
37
|
+
if (booleans.length === 1 && boolean0 != null) {
|
|
38
|
+
if (isFalseLiteralType(boolean0)) {
|
|
39
|
+
variants.add("falsy boolean");
|
|
40
|
+
} else if (isTrueLiteralType(boolean0)) {
|
|
41
|
+
variants.add("truthy boolean");
|
|
42
|
+
}
|
|
43
|
+
} else if (booleans.length === 2) {
|
|
44
|
+
variants.add("boolean");
|
|
45
|
+
}
|
|
46
|
+
const strings = types.filter(isStringType);
|
|
47
|
+
if (strings.length > 0) {
|
|
48
|
+
const evaluated = match(strings).when((types2) => types2.every(isTruthyStringType), () => "truthy string").when((types2) => types2.every(isFalsyStringType), () => "falsy string").otherwise(() => "string");
|
|
49
|
+
variants.add(evaluated);
|
|
50
|
+
}
|
|
51
|
+
const bigints = types.filter(isBigIntType);
|
|
52
|
+
if (bigints.length > 0) {
|
|
53
|
+
const evaluated = match(bigints).when((types2) => types2.every(isTruthyBigIntType), () => "truthy bigint").when((types2) => types2.every(isFalsyBigIntType), () => "falsy bigint").otherwise(() => "bigint");
|
|
54
|
+
variants.add(evaluated);
|
|
55
|
+
}
|
|
56
|
+
const numbers = types.filter(isNumberType);
|
|
57
|
+
if (numbers.length > 0) {
|
|
58
|
+
const evaluated = match(numbers).when((types2) => types2.every(isTruthyNumberType), () => "truthy number").when((types2) => types2.every(isFalsyNumberType), () => "falsy number").otherwise(() => "number");
|
|
59
|
+
variants.add(evaluated);
|
|
60
|
+
}
|
|
61
|
+
if (types.some(isEnumType)) {
|
|
62
|
+
variants.add("enum");
|
|
63
|
+
}
|
|
64
|
+
if (types.some(isObjectType)) {
|
|
65
|
+
variants.add("object");
|
|
66
|
+
}
|
|
67
|
+
if (types.some(isAnyType)) {
|
|
68
|
+
variants.add("any");
|
|
69
|
+
}
|
|
70
|
+
if (types.some(isNeverType)) {
|
|
71
|
+
variants.add("never");
|
|
72
|
+
}
|
|
73
|
+
return variants;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { getVariantsOfTypes, isAnyType, isBigIntType, isBooleanType, isEnumType, isFalsyBigIntType, isFalsyNumberType, isFalsyStringType, isNeverType, isNullishType, isNumberType, isObjectType, isStringType, isTruthyBigIntType, isTruthyNumberType, isTruthyStringType, isUnknownType };
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-analyzer/core",
|
|
3
|
+
"version": "0.0.0-next.0",
|
|
4
|
+
"description": "React Analyzer utility module for static analysis of React core APIs and patterns.",
|
|
5
|
+
"homepage": "https://github.com/Rel1cx/react-analyzer",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Rel1cx/react-analyzer/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/Rel1cx/react-analyzer.git",
|
|
12
|
+
"directory": "packages/core"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Rel1cx<dokimondex@gmail.com>",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"./package.json"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"ts-pattern": "^5.7.1",
|
|
31
|
+
"@react-analyzer/kit": "0.0.0-next.0",
|
|
32
|
+
"@react-analyzer/eff": "0.0.0-next.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@tsconfig/node22": "^22.0.2",
|
|
36
|
+
"@types/node": "^24.0.10",
|
|
37
|
+
"tsup": "^8.5.0",
|
|
38
|
+
"type-fest": "^4.41.0",
|
|
39
|
+
"@local/configs": "0.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"ts-api-utils": "^2.1.0",
|
|
43
|
+
"tsl": "^1.0.18",
|
|
44
|
+
"typescript": "^5.8.3"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"bun": ">=1.2.18",
|
|
48
|
+
"node": ">=22.17.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup --dts-resolve",
|
|
52
|
+
"build:docs": "typedoc",
|
|
53
|
+
"lint:publish": "publint",
|
|
54
|
+
"lint:ts": "tsl"
|
|
55
|
+
}
|
|
56
|
+
}
|