Skip to content

Commit 3c7f990

Browse files
committed
[utils] add types
1 parent 20e3f29 commit 3c7f990

33 files changed

+397
-80
lines changed

.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@
209209
"exports": "always-multiline",
210210
"functions": "never"
211211
}],
212-
"prefer-destructuring": "warn",
212+
"prefer-destructuring": "off",
213213
"prefer-object-spread": "off",
214214
"prefer-rest-params": "off",
215-
"prefer-spread": "warn",
215+
"prefer-spread": "off",
216216
"prefer-template": "off",
217217
}
218218
},

utils/.attw.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ignoreRules": [
3+
"cjs-only-exports-default"
4+
]
5+
}

utils/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.attw.json

utils/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88
### Fixed
99
- `parse`: also delete `parserOptions.EXPERIMENTAL_useProjectService` ([#2963], thanks [@JoshuaKGoldberg])
1010

11+
### Changed
12+
- add types (thanks [@ljharb])
13+
1114
## v2.8.0 - 2023-04-14
1215

1316
### New

utils/ModuleCache.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { ESLintSettings } from "./types";
2+
3+
export type CacheKey = unknown;
4+
export type CacheObject = {
5+
result: unknown;
6+
lastSeen: ReturnType<typeof process.hrtime>;
7+
};
8+
9+
declare class ModuleCache {
10+
map: Map<CacheKey, CacheObject>;
11+
12+
constructor(map?: Map<CacheKey, CacheObject>);
13+
14+
get<T>(cacheKey: CacheKey, settings: ESLintSettings): T | undefined;
15+
16+
set<T>(cacheKey: CacheKey, result: T): T;
17+
18+
static getSettings(settings: ESLintSettings): { lifetime: number } & Omit<ESLintSettings['import/cache'], 'lifetime'>;
19+
}
20+
export default ModuleCache;
21+
22+
export type { ModuleCache }

utils/ModuleCache.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ exports.__esModule = true;
44

55
const log = require('debug')('eslint-module-utils:ModuleCache');
66

7+
/** @type {import('./ModuleCache').ModuleCache} */
78
class ModuleCache {
9+
/** @param {typeof import('./ModuleCache').ModuleCache.prototype.map} map */
810
constructor(map) {
9-
this.map = map || new Map();
11+
this.map = map || /** @type {{typeof import('./ModuleCache').ModuleCache.prototype.map} */ new Map();
1012
}
1113

12-
/**
13-
* returns value for returning inline
14-
* @param {[type]} cacheKey [description]
15-
* @param {[type]} result [description]
16-
*/
14+
/** @type {typeof import('./ModuleCache').ModuleCache.prototype.set} */
1715
set(cacheKey, result) {
1816
this.map.set(cacheKey, { result, lastSeen: process.hrtime() });
1917
log('setting entry for', cacheKey);
2018
return result;
2119
}
2220

21+
/** @type {typeof import('./ModuleCache').ModuleCache.prototype.get} */
2322
get(cacheKey, settings) {
2423
if (this.map.has(cacheKey)) {
2524
const f = this.map.get(cacheKey);
2625
// check freshness
26+
// @ts-expect-error TS can't narrow properly from `has` and `get`
2727
if (process.hrtime(f.lastSeen)[0] < settings.lifetime) { return f.result; }
2828
} else {
2929
log('cache miss for', cacheKey);
@@ -32,19 +32,21 @@ class ModuleCache {
3232
return undefined;
3333
}
3434

35-
}
36-
37-
ModuleCache.getSettings = function (settings) {
38-
const cacheSettings = Object.assign({
39-
lifetime: 30, // seconds
40-
}, settings['import/cache']);
35+
/** @type {typeof import('./ModuleCache').ModuleCache.getSettings} */
36+
static getSettings(settings) {
37+
/** @type {ReturnType<typeof ModuleCache.getSettings>} */
38+
const cacheSettings = Object.assign({
39+
lifetime: 30, // seconds
40+
}, settings['import/cache']);
41+
42+
// parse infinity
43+
// @ts-expect-error the lack of type overlap is because we're abusing `cacheSettings` as a temporary object
44+
if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
45+
cacheSettings.lifetime = Infinity;
46+
}
4147

42-
// parse infinity
43-
if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
44-
cacheSettings.lifetime = Infinity;
48+
return cacheSettings;
4549
}
46-
47-
return cacheSettings;
48-
};
50+
}
4951

5052
exports.default = ModuleCache;

utils/declaredScope.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Rule, Scope } from 'eslint';
2+
3+
declare function declaredScope(
4+
context: Rule.RuleContext,
5+
name: string
6+
): Scope.Scope['type'] | undefined;
7+
8+
export default declaredScope;

utils/declaredScope.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
exports.__esModule = true;
44

5+
/** @type {import('./declaredScope').default} */
56
exports.default = function declaredScope(context, name) {
67
const references = context.getScope().references;
78
const reference = references.find((x) => x.identifier.name === name);
8-
if (!reference) { return undefined; }
9+
if (!reference || !reference.resolved) { return undefined; }
910
return reference.resolved.scope.type;
1011
};

utils/hash.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Hash } from 'crypto';
2+
3+
declare function hashArray(value: Array<unknown>, hash?: Hash): Hash;
4+
5+
declare function hashObject<T extends object>(value: T, hash?: Hash): Hash;
6+
7+
declare function hashify(
8+
value: Array<unknown> | object | unknown,
9+
hash?: Hash,
10+
): Hash;
11+
12+
export default hashify;
13+
14+
export { hashArray, hashObject };

utils/hash.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const createHash = require('crypto').createHash;
1111

1212
const stringify = JSON.stringify;
1313

14+
/** @type {import('./hash').default} */
1415
function hashify(value, hash) {
1516
if (!hash) { hash = createHash('sha256'); }
1617

@@ -26,6 +27,7 @@ function hashify(value, hash) {
2627
}
2728
exports.default = hashify;
2829

30+
/** @type {import('./hash').hashArray} */
2931
function hashArray(array, hash) {
3032
if (!hash) { hash = createHash('sha256'); }
3133

@@ -41,13 +43,15 @@ function hashArray(array, hash) {
4143
hashify.array = hashArray;
4244
exports.hashArray = hashArray;
4345

44-
function hashObject(object, hash) {
45-
if (!hash) { hash = createHash('sha256'); }
46+
/** @type {import('./hash').hashObject} */
47+
function hashObject(object, optionalHash) {
48+
const hash = optionalHash || createHash('sha256');
4649

4750
hash.update('{');
4851
Object.keys(object).sort().forEach((key) => {
4952
hash.update(stringify(key));
5053
hash.update(':');
54+
// @ts-expect-error the key is guaranteed to exist on the object here
5155
hashify(object[key], hash);
5256
hash.update(',');
5357
});

0 commit comments

Comments
 (0)