Skip to content

Commit b957b65

Browse files
committed
[New] add types
1 parent 9df4de4 commit b957b65

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

getInferredName.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare function getInferredName(key: PropertyKey): string;
2+
3+
declare const x: typeof getInferredName | undefined | null;
4+
5+
export = x;

getInferredName.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22

3+
/** @type {NonNullable<import('./getInferredName')> | undefined} */
34
var getInferredName;
45
try {
5-
// eslint-disable-next-line no-new-func
6-
getInferredName = Function('s', 'return { [s]() {} }[s].name;');
6+
// eslint-disable-next-line no-new-func, no-extra-parens
7+
getInferredName = /** @type {NonNullable<import('./getInferredName')>} */ (Function('s', 'return { [s]() {} }[s].name;'));
78
} catch (e) {}
89

910
var inferred = function () {};
11+
12+
/** @type {import('./getInferredName')} */
1013
module.exports = getInferredName && inferred.name === 'inferred' ? getInferredName : null;

index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function getSymbolDescription(thisArg: symbol | Symbol): string | undefined;
2+
3+
export = getSymbolDescription;

index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ var callBound = require('call-bound');
66

77
var $SyntaxError = require('es-errors/syntax');
88
var getGlobalSymbolDescription = GetIntrinsic('%Symbol.keyFor%', true);
9+
/** @type {undefined | ((thisArg: symbol | Symbol) => symbol)} */
910
var thisSymbolValue = callBound('%Symbol.prototype.valueOf%', true);
11+
/** @type {undefined | ((thisArg: symbol | Symbol) => string)} */
1012
var symToStr = callBound('Symbol.prototype.toString', true);
13+
/** @type {(thisArg: string, start?: number, end?: number) => string} */
1114
var $strSlice = callBound('String.prototype.slice');
1215

1316
var getInferredName = require('./getInferredName');
1417

18+
/** @type {import('.')} */
1519
/* eslint-disable consistent-return */
1620
module.exports = callBound('%Symbol.prototype.description%', true) || function getSymbolDescription(symbol) {
1721
if (!thisSymbolValue) {
@@ -37,7 +41,8 @@ module.exports = callBound('%Symbol.prototype.description%', true) || function g
3741
}
3842
}
3943

40-
desc = $strSlice(symToStr(sym), 7, -1); // str.slice('Symbol('.length, -')'.length);
44+
// eslint-disable-next-line no-extra-parens
45+
desc = $strSlice(/** @type {NonNullable<typeof symToStr>} */ (symToStr)(sym), 7, -1); // str.slice('Symbol('.length, -')'.length);
4146
if (desc) {
4247
return desc;
4348
}

package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"prepack": "npmignore --auto --commentLines=autogenerated",
1313
"prepublish": "not-in-publish || npm run prepublishOnly",
1414
"prepublishOnly": "safe-publish-latest",
15+
"prelint": "evalmd README.md",
1516
"lint": "eslint --ext=.js,.mjs .",
16-
"postlint": "evalmd README.md",
17+
"postlint": "tsc && attw -P",
1718
"pretest": "npm run lint",
1819
"tests-only": "nyc tape 'test/**/*.js'",
1920
"test": "npm run tests-only",
@@ -46,7 +47,13 @@
4647
"get-intrinsic": "^1.2.6"
4748
},
4849
"devDependencies": {
50+
"@arethetypeswrong/cli": "^0.17.1",
4951
"@ljharb/eslint-config": "^21.1.1",
52+
"@ljharb/tsconfig": "^0.2.2",
53+
"@types/for-each": "^0.3.3",
54+
"@types/get-intrinsic": "^1.2.3",
55+
"@types/object-inspect": "^1.13.0",
56+
"@types/tape": "^5.7.0",
5057
"auto-changelog": "^2.5.0",
5158
"encoding": "^0.1.13",
5259
"es-value-fixtures": "^1.5.0",
@@ -59,7 +66,8 @@
5966
"nyc": "^10.3.2",
6067
"object-inspect": "^1.13.3",
6168
"safe-publish-latest": "^2.0.0",
62-
"tape": "^5.9.0"
69+
"tape": "^5.9.0",
70+
"typescript": "^5.8.0-dev.20241216"
6371
},
6472
"engines": {
6573
"node": ">= 0.4"

test/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var getInferredName = require('../getInferredName');
1212
test('getSymbolDescription', function (t) {
1313
t.test('no symbols', { skip: v.hasSymbols }, function (st) {
1414
st['throws'](
15+
// @ts-expect-error
1516
getSymbolDescription,
1617
SyntaxError,
1718
'requires Symbol support'
@@ -20,7 +21,11 @@ test('getSymbolDescription', function (t) {
2021
st.end();
2122
});
2223

23-
forEach(v.nonSymbolPrimitives.concat(v.objects), function (nonSymbol) {
24+
forEach([].concat(
25+
// @ts-expect-error TS sucks with concat
26+
v.nonSymbolPrimitives,
27+
v.objects
28+
), function (nonSymbol) {
2429
t['throws'](
2530
function () { getSymbolDescription(nonSymbol); },
2631
v.hasSymbols ? TypeError : SyntaxError,
@@ -30,13 +35,15 @@ test('getSymbolDescription', function (t) {
3035

3136
t.test('with symbols', { skip: !v.hasSymbols }, function (st) {
3237
forEach(
33-
[
38+
// eslint-disable-next-line no-extra-parens
39+
/** @type {[symbol, undefined | string][]} */ ([
3440
[Symbol(), undefined],
3541
[Symbol(undefined), undefined],
42+
// @ts-expect-error
3643
[Symbol(null), 'null'],
3744
[Symbol.iterator, 'Symbol.iterator'],
3845
[Symbol('foo'), 'foo']
39-
],
46+
]),
4047
function (pair) {
4148
var sym = pair[0];
4249
var desc = pair[1];

tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@ljharb/tsconfig",
3+
"compilerOptions": {
4+
"target": "ES2021",
5+
},
6+
"exclude": [
7+
"coverage",
8+
],
9+
}

0 commit comments

Comments
 (0)