@interlace/eslint-devkit 1.0.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/AGENTS.md +352 -0
- package/CHANGELOG.md +88 -0
- package/LICENSE +22 -0
- package/README.md +528 -0
- package/package.json +88 -0
- package/src/aria-definitions.d.ts +9 -0
- package/src/aria-definitions.js +973 -0
- package/src/aria-definitions.js.map +1 -0
- package/src/ast/ast-utils.d.ts +81 -0
- package/src/ast/ast-utils.js +201 -0
- package/src/ast/ast-utils.js.map +1 -0
- package/src/ast/index.d.ts +1 -0
- package/src/ast/index.js +6 -0
- package/src/ast/index.js.map +1 -0
- package/src/index.d.ts +30 -0
- package/src/index.js +54 -0
- package/src/index.js.map +1 -0
- package/src/messaging/constants.d.ts +120 -0
- package/src/messaging/constants.js +744 -0
- package/src/messaging/constants.js.map +1 -0
- package/src/messaging/formatters.d.ts +124 -0
- package/src/messaging/formatters.js +330 -0
- package/src/messaging/formatters.js.map +1 -0
- package/src/messaging/index.d.ts +3 -0
- package/src/messaging/index.js +8 -0
- package/src/messaging/index.js.map +1 -0
- package/src/messaging/types.d.ts +122 -0
- package/src/messaging/types.js +8 -0
- package/src/messaging/types.js.map +1 -0
- package/src/node/fs.d.ts +65 -0
- package/src/node/fs.js +129 -0
- package/src/node/fs.js.map +1 -0
- package/src/node/index.d.ts +3 -0
- package/src/node/index.js +8 -0
- package/src/node/index.js.map +1 -0
- package/src/node/os.d.ts +24 -0
- package/src/node/os.js +49 -0
- package/src/node/os.js.map +1 -0
- package/src/node/path.d.ts +121 -0
- package/src/node/path.js +209 -0
- package/src/node/path.js.map +1 -0
- package/src/resolver/dependency-analysis.d.ts +421 -0
- package/src/resolver/dependency-analysis.js +934 -0
- package/src/resolver/dependency-analysis.js.map +1 -0
- package/src/resolver/index.d.ts +4 -0
- package/src/resolver/index.js +9 -0
- package/src/resolver/index.js.map +1 -0
- package/src/resolver/resolver-adapter.d.ts +53 -0
- package/src/resolver/resolver-adapter.js +126 -0
- package/src/resolver/resolver-adapter.js.map +1 -0
- package/src/resolver/resolver-detection.d.ts +36 -0
- package/src/resolver/resolver-detection.js +219 -0
- package/src/resolver/resolver-detection.js.map +1 -0
- package/src/resolver/resolver.d.ts +52 -0
- package/src/resolver/resolver.js +294 -0
- package/src/resolver/resolver.js.map +1 -0
- package/src/resolver.d.ts +1 -0
- package/src/resolver.js +5 -0
- package/src/resolver.js.map +1 -0
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +6 -0
- package/src/rule-creation/index.js.map +1 -0
- package/src/rule-creation/rule-creator.d.ts +42 -0
- package/src/rule-creation/rule-creator.js +42 -0
- package/src/rule-creation/rule-creator.js.map +1 -0
- package/src/security/index.d.ts +1 -0
- package/src/security/index.js +6 -0
- package/src/security/index.js.map +1 -0
- package/src/security/security-utils.d.ts +303 -0
- package/src/security/security-utils.js +607 -0
- package/src/security/security-utils.js.map +1 -0
- package/src/types/index.d.ts +1 -0
- package/src/types/index.js +6 -0
- package/src/types/index.js.map +1 -0
- package/src/types/type-utils.d.ts +146 -0
- package/src/types/type-utils.js +329 -0
- package/src/types/type-utils.js.map +1 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Utilities - Helper functions for working with TypeScript types
|
|
3
|
+
*
|
|
4
|
+
* Inspired by typescript-eslint's type-utils
|
|
5
|
+
* Provides utilities for type-aware linting rules
|
|
6
|
+
*/
|
|
7
|
+
import type { ParserServices, TSESTree } from '@typescript-eslint/utils';
|
|
8
|
+
import * as ts from 'typescript';
|
|
9
|
+
/**
|
|
10
|
+
* Context type that can have parser services
|
|
11
|
+
* Compatible with both ESLint 8 (context.parserServices) and ESLint 9 (context.sourceCode.parserServices)
|
|
12
|
+
* Uses Partial<ParserServices> to be compatible with ESLint's RuleContext which uses Partial
|
|
13
|
+
*/
|
|
14
|
+
type ContextWithParserServices = {
|
|
15
|
+
sourceCode?: {
|
|
16
|
+
parserServices?: Partial<ParserServices>;
|
|
17
|
+
};
|
|
18
|
+
parserServices?: Partial<ParserServices>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Get the TypeScript Program from ESLint parser services
|
|
22
|
+
*
|
|
23
|
+
* @throws Error if parser services are not available
|
|
24
|
+
*/
|
|
25
|
+
export declare function getParserServices(context: ContextWithParserServices): ParserServices;
|
|
26
|
+
/**
|
|
27
|
+
* Check if parser services are available without throwing
|
|
28
|
+
*/
|
|
29
|
+
export declare function hasParserServices(context: ContextWithParserServices): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Get the TypeScript type of an ESTree node
|
|
32
|
+
*/
|
|
33
|
+
export declare function getTypeOfNode(node: TSESTree.Node, parserServices: ParserServices): ts.Type;
|
|
34
|
+
/**
|
|
35
|
+
* Check if a type is any type
|
|
36
|
+
*/
|
|
37
|
+
export declare function isAnyType(type: ts.Type): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a type is unknown type
|
|
40
|
+
*/
|
|
41
|
+
export declare function isUnknownType(type: ts.Type): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Check if a type is never type
|
|
44
|
+
*/
|
|
45
|
+
export declare function isNeverType(type: ts.Type): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Check if a type is null or undefined
|
|
48
|
+
*/
|
|
49
|
+
export declare function isNullableType(type: ts.Type): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Check if a type is a string type
|
|
52
|
+
*/
|
|
53
|
+
export declare function isStringType(type: ts.Type): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Check if a type is a number type
|
|
56
|
+
*/
|
|
57
|
+
export declare function isNumberType(type: ts.Type): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Check if a type is a boolean type
|
|
60
|
+
*/
|
|
61
|
+
export declare function isBooleanType(type: ts.Type): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Check if a type is an array type
|
|
64
|
+
*/
|
|
65
|
+
export declare function isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Check if a type is a promise type
|
|
68
|
+
*/
|
|
69
|
+
export declare function isPromiseType(type: ts.Type): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Get the type arguments of a generic type
|
|
72
|
+
*/
|
|
73
|
+
export declare function getTypeArguments(type: ts.Type, checker: ts.TypeChecker): readonly ts.Type[];
|
|
74
|
+
/**
|
|
75
|
+
* Check if a type satisfies a predicate (including union types)
|
|
76
|
+
*/
|
|
77
|
+
export declare function typeMatchesPredicateRecursive(type: ts.Type, predicate: (type: ts.Type) => boolean): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Check if a type is a string literal type (e.g., 'name', 'email')
|
|
80
|
+
*
|
|
81
|
+
* This is useful for detecting statically constrained property keys
|
|
82
|
+
* that are safe from object injection attacks.
|
|
83
|
+
*/
|
|
84
|
+
export declare function isStringLiteralType(type: ts.Type): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Check if a type is a number literal type (e.g., 1, 2, 3)
|
|
87
|
+
*/
|
|
88
|
+
export declare function isNumberLiteralType(type: ts.Type): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Check if a type is a union of only literal types (string, number, or boolean literals)
|
|
91
|
+
*
|
|
92
|
+
* This is the key function for type-aware security rules.
|
|
93
|
+
* A union like 'name' | 'email' is statically constrained and safe,
|
|
94
|
+
* while string | number could be any value at runtime.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // Returns true - safe, statically constrained:
|
|
98
|
+
* type Key = 'name' | 'email' | 'age';
|
|
99
|
+
*
|
|
100
|
+
* // Returns false - unsafe, could be any string:
|
|
101
|
+
* type Key = string;
|
|
102
|
+
* type Key = string | number;
|
|
103
|
+
* type Key = 'name' | string; // string absorbs the literal
|
|
104
|
+
*/
|
|
105
|
+
export declare function isUnionOfLiterals(type: ts.Type): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Check if a type is a union of only string literals
|
|
108
|
+
*
|
|
109
|
+
* More specific version of isUnionOfLiterals that only allows string literals.
|
|
110
|
+
* This is useful for property key validation where only string keys are expected.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Returns true:
|
|
114
|
+
* type Key = 'name' | 'email';
|
|
115
|
+
*
|
|
116
|
+
* // Returns false:
|
|
117
|
+
* type Key = 'name' | 1; // has number literal
|
|
118
|
+
* type Key = string;
|
|
119
|
+
*/
|
|
120
|
+
export declare function isUnionOfStringLiterals(type: ts.Type): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Get the literal values from a union of string literals
|
|
123
|
+
*
|
|
124
|
+
* Extracts the actual string values from a type like 'name' | 'email'
|
|
125
|
+
* Returns null if the type is not a union of string literals.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* // For type 'name' | 'email':
|
|
129
|
+
* getStringLiteralValues(type) // ['name', 'email']
|
|
130
|
+
*
|
|
131
|
+
* // For type string:
|
|
132
|
+
* getStringLiteralValues(type) // null
|
|
133
|
+
*/
|
|
134
|
+
export declare function getStringLiteralValues(type: ts.Type): string[] | null;
|
|
135
|
+
/**
|
|
136
|
+
* Check if a union of string literals contains any dangerous property names
|
|
137
|
+
*
|
|
138
|
+
* Even if a type is constrained to string literals, it could still be dangerous
|
|
139
|
+
* if it includes properties like '__proto__', 'constructor', or 'prototype'.
|
|
140
|
+
*
|
|
141
|
+
* @param type - The TypeScript type to check
|
|
142
|
+
* @param dangerousProperties - List of dangerous property names (defaults to common prototype pollution vectors)
|
|
143
|
+
* @returns true if the type is safe (no dangerous properties), false otherwise
|
|
144
|
+
*/
|
|
145
|
+
export declare function isUnionOfSafeStringLiterals(type: ts.Type, dangerousProperties?: string[]): boolean;
|
|
146
|
+
export {};
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getParserServices = getParserServices;
|
|
4
|
+
exports.hasParserServices = hasParserServices;
|
|
5
|
+
exports.getTypeOfNode = getTypeOfNode;
|
|
6
|
+
exports.isAnyType = isAnyType;
|
|
7
|
+
exports.isUnknownType = isUnknownType;
|
|
8
|
+
exports.isNeverType = isNeverType;
|
|
9
|
+
exports.isNullableType = isNullableType;
|
|
10
|
+
exports.isStringType = isStringType;
|
|
11
|
+
exports.isNumberType = isNumberType;
|
|
12
|
+
exports.isBooleanType = isBooleanType;
|
|
13
|
+
exports.isArrayType = isArrayType;
|
|
14
|
+
exports.isPromiseType = isPromiseType;
|
|
15
|
+
exports.getTypeArguments = getTypeArguments;
|
|
16
|
+
exports.typeMatchesPredicateRecursive = typeMatchesPredicateRecursive;
|
|
17
|
+
exports.isStringLiteralType = isStringLiteralType;
|
|
18
|
+
exports.isNumberLiteralType = isNumberLiteralType;
|
|
19
|
+
exports.isUnionOfLiterals = isUnionOfLiterals;
|
|
20
|
+
exports.isUnionOfStringLiterals = isUnionOfStringLiterals;
|
|
21
|
+
exports.getStringLiteralValues = getStringLiteralValues;
|
|
22
|
+
exports.isUnionOfSafeStringLiterals = isUnionOfSafeStringLiterals;
|
|
23
|
+
const tslib_1 = require("tslib");
|
|
24
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
25
|
+
/**
|
|
26
|
+
* Get the TypeScript Program from ESLint parser services
|
|
27
|
+
*
|
|
28
|
+
* @throws Error if parser services are not available
|
|
29
|
+
*/
|
|
30
|
+
function getParserServices(context) {
|
|
31
|
+
// Handle both ESLint 8 and ESLint 9 contexts
|
|
32
|
+
const parserServices = context.sourceCode?.parserServices ?? context.parserServices;
|
|
33
|
+
if (!parserServices ||
|
|
34
|
+
!parserServices.program ||
|
|
35
|
+
!parserServices.esTreeNodeToTSNodeMap) {
|
|
36
|
+
throw new Error('You have used a rule which requires parserServices to be generated. ' +
|
|
37
|
+
'You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.');
|
|
38
|
+
}
|
|
39
|
+
// At this point we've verified the required properties exist, safe to cast
|
|
40
|
+
return parserServices;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if parser services are available without throwing
|
|
44
|
+
*/
|
|
45
|
+
function hasParserServices(context) {
|
|
46
|
+
const parserServices = context.sourceCode?.parserServices ?? context.parserServices;
|
|
47
|
+
return !!(parserServices &&
|
|
48
|
+
parserServices.program &&
|
|
49
|
+
parserServices.esTreeNodeToTSNodeMap);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the TypeScript type of an ESTree node
|
|
53
|
+
*/
|
|
54
|
+
function getTypeOfNode(node, parserServices) {
|
|
55
|
+
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
56
|
+
const program = parserServices.program;
|
|
57
|
+
if (!program) {
|
|
58
|
+
throw new Error('Program is not available');
|
|
59
|
+
}
|
|
60
|
+
const checker = program.getTypeChecker();
|
|
61
|
+
return checker.getTypeAtLocation(tsNode);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if a type is any type
|
|
65
|
+
*/
|
|
66
|
+
function isAnyType(type) {
|
|
67
|
+
if (type.flags & ts.TypeFlags.Any) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (type.isUnion()) {
|
|
71
|
+
return type.types.some(isAnyType);
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Check if a type is unknown type
|
|
77
|
+
*/
|
|
78
|
+
function isUnknownType(type) {
|
|
79
|
+
if (type.flags & ts.TypeFlags.Unknown) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (type.isUnion()) {
|
|
83
|
+
return type.types.some(isUnknownType);
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if a type is never type
|
|
89
|
+
*/
|
|
90
|
+
function isNeverType(type) {
|
|
91
|
+
return (type.flags & ts.TypeFlags.Never) !== 0;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Check if a type is null or undefined
|
|
95
|
+
*/
|
|
96
|
+
function isNullableType(type) {
|
|
97
|
+
if (type.isUnion()) {
|
|
98
|
+
return type.types.some((t) => (t.flags & ts.TypeFlags.Null) !== 0 ||
|
|
99
|
+
(t.flags & ts.TypeFlags.Undefined) !== 0);
|
|
100
|
+
}
|
|
101
|
+
return ((type.flags & ts.TypeFlags.Null) !== 0 ||
|
|
102
|
+
(type.flags & ts.TypeFlags.Undefined) !== 0);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Check if a type is a string type
|
|
106
|
+
*/
|
|
107
|
+
function isStringType(type) {
|
|
108
|
+
if (type.flags & ts.TypeFlags.String) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (type.flags & ts.TypeFlags.StringLiteral) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
if (type.isUnion()) {
|
|
115
|
+
return type.types.every(isStringType);
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if a type is a number type
|
|
121
|
+
*/
|
|
122
|
+
function isNumberType(type) {
|
|
123
|
+
if (type.flags & ts.TypeFlags.Number) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (type.flags & ts.TypeFlags.NumberLiteral) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (type.isUnion()) {
|
|
130
|
+
return type.types.every(isNumberType);
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if a type is a boolean type
|
|
136
|
+
*/
|
|
137
|
+
function isBooleanType(type) {
|
|
138
|
+
if (type.flags & ts.TypeFlags.Boolean) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (type.flags & ts.TypeFlags.BooleanLiteral) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
if (type.isUnion()) {
|
|
145
|
+
return type.types.every(isBooleanType);
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if a type is an array type
|
|
151
|
+
*/
|
|
152
|
+
function isArrayType(type, checker) {
|
|
153
|
+
const symbol = type.getSymbol();
|
|
154
|
+
if (!symbol) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return symbol.getName() === 'Array' && checker.isArrayType(type);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Check if a type is a promise type
|
|
161
|
+
*/
|
|
162
|
+
function isPromiseType(type) {
|
|
163
|
+
const symbol = type.getSymbol();
|
|
164
|
+
if (!symbol) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return symbol.getName() === 'Promise';
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get the type arguments of a generic type
|
|
171
|
+
*/
|
|
172
|
+
function getTypeArguments(type, checker) {
|
|
173
|
+
if (checker.isArrayType?.(type)) {
|
|
174
|
+
return checker.getTypeArguments(type);
|
|
175
|
+
}
|
|
176
|
+
const typeArgs = type.typeArguments;
|
|
177
|
+
if (typeArgs) {
|
|
178
|
+
return typeArgs;
|
|
179
|
+
}
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Check if a type satisfies a predicate (including union types)
|
|
184
|
+
*/
|
|
185
|
+
function typeMatchesPredicateRecursive(type, predicate) {
|
|
186
|
+
if (predicate(type)) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
if (type.isUnion()) {
|
|
190
|
+
return type.types.some((t) => typeMatchesPredicateRecursive(t, predicate));
|
|
191
|
+
}
|
|
192
|
+
if (type.isIntersection()) {
|
|
193
|
+
return type.types.every((t) => typeMatchesPredicateRecursive(t, predicate));
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Check if a type is a string literal type (e.g., 'name', 'email')
|
|
199
|
+
*
|
|
200
|
+
* This is useful for detecting statically constrained property keys
|
|
201
|
+
* that are safe from object injection attacks.
|
|
202
|
+
*/
|
|
203
|
+
function isStringLiteralType(type) {
|
|
204
|
+
return (type.flags & ts.TypeFlags.StringLiteral) !== 0;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Check if a type is a number literal type (e.g., 1, 2, 3)
|
|
208
|
+
*/
|
|
209
|
+
function isNumberLiteralType(type) {
|
|
210
|
+
return (type.flags & ts.TypeFlags.NumberLiteral) !== 0;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Check if a type is a union of only literal types (string, number, or boolean literals)
|
|
214
|
+
*
|
|
215
|
+
* This is the key function for type-aware security rules.
|
|
216
|
+
* A union like 'name' | 'email' is statically constrained and safe,
|
|
217
|
+
* while string | number could be any value at runtime.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* // Returns true - safe, statically constrained:
|
|
221
|
+
* type Key = 'name' | 'email' | 'age';
|
|
222
|
+
*
|
|
223
|
+
* // Returns false - unsafe, could be any string:
|
|
224
|
+
* type Key = string;
|
|
225
|
+
* type Key = string | number;
|
|
226
|
+
* type Key = 'name' | string; // string absorbs the literal
|
|
227
|
+
*/
|
|
228
|
+
function isUnionOfLiterals(type) {
|
|
229
|
+
// Single literal type
|
|
230
|
+
if (isStringLiteralType(type) || isNumberLiteralType(type)) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
// Boolean literal (true | false)
|
|
234
|
+
if (type.flags & ts.TypeFlags.BooleanLiteral) {
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
// Union type - check if ALL members are literals
|
|
238
|
+
if (type.isUnion()) {
|
|
239
|
+
return type.types.every((t) => {
|
|
240
|
+
// Each type in the union must be a literal
|
|
241
|
+
return ((t.flags & ts.TypeFlags.StringLiteral) !== 0 ||
|
|
242
|
+
(t.flags & ts.TypeFlags.NumberLiteral) !== 0 ||
|
|
243
|
+
(t.flags & ts.TypeFlags.BooleanLiteral) !== 0);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Check if a type is a union of only string literals
|
|
250
|
+
*
|
|
251
|
+
* More specific version of isUnionOfLiterals that only allows string literals.
|
|
252
|
+
* This is useful for property key validation where only string keys are expected.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* // Returns true:
|
|
256
|
+
* type Key = 'name' | 'email';
|
|
257
|
+
*
|
|
258
|
+
* // Returns false:
|
|
259
|
+
* type Key = 'name' | 1; // has number literal
|
|
260
|
+
* type Key = string;
|
|
261
|
+
*/
|
|
262
|
+
function isUnionOfStringLiterals(type) {
|
|
263
|
+
// Single string literal
|
|
264
|
+
if (isStringLiteralType(type)) {
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
// Union type - check if ALL members are string literals
|
|
268
|
+
if (type.isUnion()) {
|
|
269
|
+
return type.types.every((t) => isStringLiteralType(t));
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Get the literal values from a union of string literals
|
|
275
|
+
*
|
|
276
|
+
* Extracts the actual string values from a type like 'name' | 'email'
|
|
277
|
+
* Returns null if the type is not a union of string literals.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* // For type 'name' | 'email':
|
|
281
|
+
* getStringLiteralValues(type) // ['name', 'email']
|
|
282
|
+
*
|
|
283
|
+
* // For type string:
|
|
284
|
+
* getStringLiteralValues(type) // null
|
|
285
|
+
*/
|
|
286
|
+
function getStringLiteralValues(type) {
|
|
287
|
+
// Single string literal
|
|
288
|
+
if (isStringLiteralType(type)) {
|
|
289
|
+
const literalType = type;
|
|
290
|
+
if (typeof literalType.value === 'string') {
|
|
291
|
+
return [literalType.value];
|
|
292
|
+
}
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
// Union type - extract all string literal values
|
|
296
|
+
if (type.isUnion()) {
|
|
297
|
+
const values = [];
|
|
298
|
+
for (const t of type.types) {
|
|
299
|
+
if (!isStringLiteralType(t)) {
|
|
300
|
+
return null; // Not all members are string literals
|
|
301
|
+
}
|
|
302
|
+
const literalType = t;
|
|
303
|
+
if (typeof literalType.value === 'string') {
|
|
304
|
+
values.push(literalType.value);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return values.length > 0 ? values : null;
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Check if a union of string literals contains any dangerous property names
|
|
313
|
+
*
|
|
314
|
+
* Even if a type is constrained to string literals, it could still be dangerous
|
|
315
|
+
* if it includes properties like '__proto__', 'constructor', or 'prototype'.
|
|
316
|
+
*
|
|
317
|
+
* @param type - The TypeScript type to check
|
|
318
|
+
* @param dangerousProperties - List of dangerous property names (defaults to common prototype pollution vectors)
|
|
319
|
+
* @returns true if the type is safe (no dangerous properties), false otherwise
|
|
320
|
+
*/
|
|
321
|
+
function isUnionOfSafeStringLiterals(type, dangerousProperties = ['__proto__', 'prototype', 'constructor']) {
|
|
322
|
+
const values = getStringLiteralValues(type);
|
|
323
|
+
if (!values) {
|
|
324
|
+
return false; // Not a union of string literals
|
|
325
|
+
}
|
|
326
|
+
// Check if any literal value is dangerous
|
|
327
|
+
return !values.some((value) => dangerousProperties.includes(value));
|
|
328
|
+
}
|
|
329
|
+
//# sourceMappingURL=type-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-utils.js","sourceRoot":"","sources":["../../../../../packages/eslint-devkit/src/types/type-utils.ts"],"names":[],"mappings":";;AAwBA,8CAoBC;AAKD,8CASC;AAKD,sCAWC;AAKD,8BAUC;AAKD,sCAUC;AAKD,kCAEC;AAKD,wCAaC;AAKD,oCAcC;AAKD,oCAcC;AAKD,sCAcC;AAKD,kCAOC;AAKD,s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