@zgeoff/oxlint-config 0.6.0 → 0.6.1
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/collect-type-kinds.js +89 -0
- package/package.json +2 -1
- package/verb-contract.js +32 -86
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// the kind of each member of a declared type, read without type information:
|
|
2
|
+
// a union splits into its members, a reference follows the file's own type
|
|
3
|
+
// aliases, and `resolvePromise` reads a promise as the value it resolves to
|
|
4
|
+
export default function collectTypeKinds(
|
|
5
|
+
type,
|
|
6
|
+
localTypes,
|
|
7
|
+
resolvePromise = false,
|
|
8
|
+
seen = new Set(),
|
|
9
|
+
) {
|
|
10
|
+
if (type.type === 'TSUnionType') {
|
|
11
|
+
return type.types.flatMap((member) =>
|
|
12
|
+
collectTypeKinds(member, localTypes, resolvePromise, seen),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (type.type === 'TSParenthesizedType' || isReadonlyOperator(type)) {
|
|
17
|
+
return collectTypeKinds(type.typeAnnotation, localTypes, resolvePromise, seen);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (type.type !== 'TSTypeReference') {
|
|
21
|
+
return [pickTypeKind(type)];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const name = type.typeName.type === 'Identifier' ? type.typeName.name : null;
|
|
25
|
+
const alias = name === null ? undefined : localTypes.aliases.get(name);
|
|
26
|
+
|
|
27
|
+
if (alias !== undefined && !seen.has(name)) {
|
|
28
|
+
return collectTypeKinds(alias, localTypes, resolvePromise, new Set([...seen, name]));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const typeArguments = type.typeArguments ?? type.typeParameters;
|
|
32
|
+
|
|
33
|
+
if (resolvePromise && name === 'Promise' && typeArguments?.params.length === 1) {
|
|
34
|
+
const [value] = typeArguments.params;
|
|
35
|
+
|
|
36
|
+
return collectTypeKinds(value, localTypes, resolvePromise, seen);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// an interface never holds null; any other reference, such as an import or
|
|
40
|
+
// a type parameter, is a type the rule cannot read
|
|
41
|
+
return [localTypes.interfaces.has(name) ? 'value' : 'open'];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isReadonlyOperator(type) {
|
|
45
|
+
return type.type === 'TSTypeOperator' && type.operator === 'readonly';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// a type whose members the rule can see and that never holds null; any other
|
|
49
|
+
// shape, such as an indexed access or a conditional type, is open
|
|
50
|
+
const valueTypes = new Set([
|
|
51
|
+
'TSStringKeyword',
|
|
52
|
+
'TSNumberKeyword',
|
|
53
|
+
'TSBigIntKeyword',
|
|
54
|
+
'TSSymbolKeyword',
|
|
55
|
+
'TSObjectKeyword',
|
|
56
|
+
'TSNeverKeyword',
|
|
57
|
+
'TSLiteralType',
|
|
58
|
+
'TSTemplateLiteralType',
|
|
59
|
+
'TSArrayType',
|
|
60
|
+
'TSTupleType',
|
|
61
|
+
'TSTypeLiteral',
|
|
62
|
+
'TSFunctionType',
|
|
63
|
+
'TSConstructorType',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
function pickTypeKind(type) {
|
|
67
|
+
if (
|
|
68
|
+
type.type === 'TSBooleanKeyword' ||
|
|
69
|
+
(type.type === 'TSTypePredicate' && !type.asserts) ||
|
|
70
|
+
(type.type === 'TSLiteralType' && typeof type.literal.value === 'boolean')
|
|
71
|
+
) {
|
|
72
|
+
return 'boolean';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (type.type === 'TSNullKeyword' || type.type === 'TSUndefinedKeyword') {
|
|
76
|
+
return 'nullish';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (type.type === 'TSVoidKeyword') {
|
|
80
|
+
return 'void';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// `asserts value is T` throws instead of returning a boolean
|
|
84
|
+
if (type.type === 'TSTypePredicate' || valueTypes.has(type.type)) {
|
|
85
|
+
return 'value';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return 'open';
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/oxlint-config",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Shareable oxlint config: strict categories, house-style rules, and a custom JS plugin",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"plugin.js",
|
|
14
14
|
"function-verb.js",
|
|
15
15
|
"verb-contract.js",
|
|
16
|
+
"collect-type-kinds.js",
|
|
16
17
|
"module-order.js",
|
|
17
18
|
"collect-module-entries.js",
|
|
18
19
|
"build-module-order-fix.js",
|
package/verb-contract.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import collectTypeKinds from './collect-type-kinds.js';
|
|
1
2
|
import findFunctionName from './find-function-name.js';
|
|
2
3
|
import isVerbMatch from './is-verb-match.js';
|
|
3
4
|
|
|
@@ -251,91 +252,6 @@ function collectLocalTypes(program) {
|
|
|
251
252
|
return localTypes;
|
|
252
253
|
}
|
|
253
254
|
|
|
254
|
-
// the kind of each member of a declared type, read without type information:
|
|
255
|
-
// a union splits into its members, a reference follows the file's own type
|
|
256
|
-
// aliases, and `resolvePromise` reads a promise as the value it resolves to
|
|
257
|
-
function collectTypeKinds(type, localTypes, resolvePromise = false, seen = new Set()) {
|
|
258
|
-
if (type.type === 'TSUnionType') {
|
|
259
|
-
return type.types.flatMap((member) =>
|
|
260
|
-
collectTypeKinds(member, localTypes, resolvePromise, seen),
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (type.type === 'TSParenthesizedType' || isReadonlyOperator(type)) {
|
|
265
|
-
return collectTypeKinds(type.typeAnnotation, localTypes, resolvePromise, seen);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (type.type !== 'TSTypeReference') {
|
|
269
|
-
return [pickTypeKind(type)];
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const name = type.typeName.type === 'Identifier' ? type.typeName.name : null;
|
|
273
|
-
const alias = name === null ? undefined : localTypes.aliases.get(name);
|
|
274
|
-
|
|
275
|
-
if (alias !== undefined && !seen.has(name)) {
|
|
276
|
-
return collectTypeKinds(alias, localTypes, resolvePromise, new Set([...seen, name]));
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
const typeArguments = type.typeArguments ?? type.typeParameters;
|
|
280
|
-
|
|
281
|
-
if (resolvePromise && name === 'Promise' && typeArguments?.params.length === 1) {
|
|
282
|
-
const [value] = typeArguments.params;
|
|
283
|
-
|
|
284
|
-
return collectTypeKinds(value, localTypes, resolvePromise, seen);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// an interface never holds null; any other reference, such as an import or
|
|
288
|
-
// a type parameter, is a type the rule cannot read
|
|
289
|
-
return [localTypes.interfaces.has(name) ? 'value' : 'open'];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function isReadonlyOperator(type) {
|
|
293
|
-
return type.type === 'TSTypeOperator' && type.operator === 'readonly';
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// a type whose members the rule can see and that never holds null; any other
|
|
297
|
-
// shape, such as an indexed access or a conditional type, is open
|
|
298
|
-
const valueTypes = new Set([
|
|
299
|
-
'TSStringKeyword',
|
|
300
|
-
'TSNumberKeyword',
|
|
301
|
-
'TSBigIntKeyword',
|
|
302
|
-
'TSSymbolKeyword',
|
|
303
|
-
'TSObjectKeyword',
|
|
304
|
-
'TSNeverKeyword',
|
|
305
|
-
'TSLiteralType',
|
|
306
|
-
'TSTemplateLiteralType',
|
|
307
|
-
'TSArrayType',
|
|
308
|
-
'TSTupleType',
|
|
309
|
-
'TSTypeLiteral',
|
|
310
|
-
'TSFunctionType',
|
|
311
|
-
'TSConstructorType',
|
|
312
|
-
]);
|
|
313
|
-
|
|
314
|
-
function pickTypeKind(type) {
|
|
315
|
-
if (
|
|
316
|
-
type.type === 'TSBooleanKeyword' ||
|
|
317
|
-
(type.type === 'TSTypePredicate' && !type.asserts) ||
|
|
318
|
-
(type.type === 'TSLiteralType' && typeof type.literal.value === 'boolean')
|
|
319
|
-
) {
|
|
320
|
-
return 'boolean';
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (type.type === 'TSNullKeyword' || type.type === 'TSUndefinedKeyword') {
|
|
324
|
-
return 'nullish';
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (type.type === 'TSVoidKeyword') {
|
|
328
|
-
return 'void';
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// `asserts value is T` throws instead of returning a boolean
|
|
332
|
-
if (type.type === 'TSTypePredicate' || valueTypes.has(type.type)) {
|
|
333
|
-
return 'value';
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return 'open';
|
|
337
|
-
}
|
|
338
|
-
|
|
339
255
|
function isBooleanOrOpen(kinds) {
|
|
340
256
|
return kinds.every((kind) => kind === 'boolean' || kind === 'open');
|
|
341
257
|
}
|
|
@@ -386,7 +302,7 @@ function findCallIO(context, node, frameNode) {
|
|
|
386
302
|
name: callee.type === 'Identifier' ? callee.name : memberName,
|
|
387
303
|
isMethod: memberName !== null,
|
|
388
304
|
},
|
|
389
|
-
|
|
305
|
+
buildCalledName(context, node, callee),
|
|
390
306
|
];
|
|
391
307
|
|
|
392
308
|
const effectful = names.find((entry) => {
|
|
@@ -591,6 +507,36 @@ function isLocalVariable(binding, frameNode) {
|
|
|
591
507
|
);
|
|
592
508
|
}
|
|
593
509
|
|
|
510
|
+
function buildCalledName(context, node, callee) {
|
|
511
|
+
let current = callee;
|
|
512
|
+
let scopeNode = node;
|
|
513
|
+
|
|
514
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
515
|
+
if (current.type === 'MemberExpression') {
|
|
516
|
+
return { name: findMemberName(current), isMethod: true };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (current.type !== 'Identifier') {
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const declarator = findAliasDeclarator(findBinding(context, scopeNode, current.name));
|
|
524
|
+
|
|
525
|
+
if (declarator === null) {
|
|
526
|
+
return { name: current.name, isMethod: false };
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if (declarator.id.type !== 'Identifier') {
|
|
530
|
+
return { name: findPatternKey(declarator.id, current.name), isMethod: true };
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
current = declarator.init;
|
|
534
|
+
scopeNode = declarator.init;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return { name: null, isMethod: false };
|
|
538
|
+
}
|
|
539
|
+
|
|
594
540
|
function isEnvRead(context, node) {
|
|
595
541
|
return (
|
|
596
542
|
findMemberName(node) === 'env' &&
|