Skip to content

Commit

Permalink
[New] add types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 13, 2024
1 parent 5281391 commit 295d25d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .attw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ignoreRules": [
"named-exports"
]
}
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 150

[CHANGELOG.md]
indent_style = space
indent_size = 2

[*.json]
max_line_length = off

[Makefile]
max_line_length = off
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare namespace SLOT {
type InternalSlot = string; // `[[${string}]]`; // TODO: restrict this to require the brackets
}

declare const SLOT: {
assert(O: object, slot: SLOT.InternalSlot): void;
get(O: object, slot: SLOT.InternalSlot): unknown;
set(O: object, slot: SLOT.InternalSlot, value?: unknown): void;
has(O: object, slot: SLOT.InternalSlot): boolean;
}

export = SLOT;
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

/** @typedef {`$${import('.').InternalSlot}`} SaltedInternalSlot */
/** @typedef {{ [k in SaltedInternalSlot]?: unknown }} SlotsObject */

var hasOwn = require('hasown');
/** @type {import('side-channel').Channel<object, SlotsObject>} */
var channel = require('side-channel')();

var $TypeError = require('es-errors/type');

/** @type {import('.')} */
var SLOT = {
assert: function (O, slot) {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
Expand All @@ -26,7 +31,8 @@ var SLOT = {
throw new $TypeError('`slot` must be a string');
}
var slots = channel.get(O);
return slots && slots['$' + slot];
// eslint-disable-next-line no-extra-parens
return slots && slots[/** @type {SaltedInternalSlot} */ ('$' + slot)];
},
has: function (O, slot) {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
Expand All @@ -36,7 +42,8 @@ var SLOT = {
throw new $TypeError('`slot` must be a string');
}
var slots = channel.get(O);
return !!slots && hasOwn(slots, '$' + slot);
// eslint-disable-next-line no-extra-parens
return !!slots && hasOwn(slots, /** @type {SaltedInternalSlot} */ ('$' + slot));
},
set: function (O, slot, V) {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
Expand All @@ -50,7 +57,8 @@ var SLOT = {
slots = {};
channel.set(O, slots);
}
slots['$' + slot] = V;
// eslint-disable-next-line no-extra-parens
slots[/** @type {SaltedInternalSlot} */ ('$' + slot)] = V;
}
};

Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"",
"pretest": "npm run lint",
"lint": "eslint .",
"lint": "eslint --ext=js,mjs .",
"postlint": "tsc && attw -P",
"tests-only": "nyc tape 'test/**/*.js'",
"test": "npm run tests-only",
"posttest": "npx npm@'>= 10.2' audit --production"
Expand Down Expand Up @@ -41,7 +42,12 @@
"node": ">= 0.4"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
"@ljharb/eslint-config": "^21.1.1",
"@ljharb/tsconfig": "^0.2.2",
"@types/for-each": "^0.3.3",
"@types/object-inspect": "^1.13.0",
"@types/tape": "^5.6.5",
"auto-changelog": "^2.5.0",
"encoding": "^0.1.13",
"eslint": "=8.8.0",
Expand All @@ -51,7 +57,8 @@
"nyc": "^10.3.2",
"object-inspect": "^1.13.3",
"safe-publish-latest": "^2.0.0",
"tape": "^5.9.0"
"tape": "^5.9.0",
"typescript": "next"
},
"dependencies": {
"es-errors": "^1.3.0",
Expand Down
12 changes: 10 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var SLOT = require('../');
test('assert', function (t) {
forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
t['throws'](
// @ts-expect-error
function () { SLOT.assert(primitive, ''); },
TypeError,
inspect(primitive) + ' is not an Object'
Expand All @@ -17,14 +18,15 @@ test('assert', function (t) {

forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
t['throws'](
// @ts-expect-error
function () { SLOT.assert({}, nonString); },
TypeError,
inspect(nonString) + ' is not a String'
);
});

t['throws'](
function () { SLOT.assert({}, 'whatever'); },
function () { SLOT.assert({}, '[[whatever]]'); },
TypeError,
'nonexistent slot throws'
);
Expand All @@ -40,6 +42,7 @@ test('assert', function (t) {
test('has', function (t) {
forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
t['throws'](
// @ts-expect-error
function () { SLOT.has(primitive, ''); },
TypeError,
inspect(primitive) + ' is not an Object'
Expand All @@ -48,6 +51,7 @@ test('has', function (t) {

forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
t['throws'](
// @ts-expect-error
function () { SLOT.has({}, nonString); },
TypeError,
inspect(nonString) + ' is not a String'
Expand All @@ -56,7 +60,7 @@ test('has', function (t) {

var o = {};

t.equal(SLOT.has(o, 'nonexistent'), false, 'nonexistent slot yields false');
t.equal(SLOT.has(o, '[[nonexistent]]'), false, 'nonexistent slot yields false');

SLOT.set(o, 'foo');
t.equal(SLOT.has(o, 'foo'), true, 'existent slot yields true');
Expand All @@ -67,6 +71,7 @@ test('has', function (t) {
test('get', function (t) {
forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
t['throws'](
// @ts-expect-error
function () { SLOT.get(primitive, ''); },
TypeError,
inspect(primitive) + ' is not an Object'
Expand All @@ -75,6 +80,7 @@ test('get', function (t) {

forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
t['throws'](
// @ts-expect-error
function () { SLOT.get({}, nonString); },
TypeError,
inspect(nonString) + ' is not a String'
Expand All @@ -94,6 +100,7 @@ test('get', function (t) {
test('set', function (t) {
forEach([null, undefined, true, false, 'foo', '', 42, 0], function (primitive) {
t['throws'](
// @ts-expect-error
function () { SLOT.set(primitive, ''); },
TypeError,
inspect(primitive) + ' is not an Object'
Expand All @@ -102,6 +109,7 @@ test('set', function (t) {

forEach([null, undefined, true, false, 42, 0, {}, [], function () {}, /a/g], function (nonString) {
t['throws'](
// @ts-expect-error
function () { SLOT.set({}, nonString); },
TypeError,
inspect(nonString) + ' is not a String'
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@ljharb/tsconfig",
"compilerOptions": {
//"target": "es2021",
},
"exclude": [
"coverage",
],
}

0 comments on commit 295d25d

Please sign in to comment.