Skip to content

Commit

Permalink
[New] add types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 2, 2025
1 parent ebdc486 commit 5ba6244
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare namespace isAsyncFunction {
type AsyncFunction = (...args: any[]) => Promise<any>
}

declare function isAsyncFunction(fn: unknown): fn is isAsyncFunction.AsyncFunction;

export = isAsyncFunction;
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ var getAsyncFunc = function () { // eslint-disable-line consistent-return
} catch (e) {
}
};

/** @type {import('.').AsyncFunction | false} */
var AsyncFunction;

/** @type {import('.')} */
module.exports = function isAsyncFunction(fn) {
if (typeof fn !== 'function') {
return false;
Expand All @@ -37,7 +40,8 @@ module.exports = function isAsyncFunction(fn) {
}
if (typeof AsyncFunction === 'undefined') {
var asyncFunc = getAsyncFunc();
AsyncFunction = asyncFunc ? getProto(asyncFunc) : false;
// eslint-disable-next-line no-extra-parens
AsyncFunction = asyncFunc ? /** @type {import('.').AsyncFunction} */ (getProto(asyncFunc)) : false;
}
return getProto(fn) === AsyncFunction;
};
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:uglified": "node test/uglified",
"posttest": "npx npm@\">= 10.2\" audit --production",
"lint": "eslint --ext=js,mjs .",
"postlint": "tsc && attw -P",
"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)\")\""
},
Expand Down Expand Up @@ -46,7 +47,13 @@
"safe-regex-test": "^1.1.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",
"@ljharb/eslint-config": "^21.1.1",
"@ljharb/tsconfig": "^0.2.3",
"@types/for-each": "^0.3.3",
"@types/make-async-function": "^1.0.2",
"@types/make-generator-function": "^2.0.3",
"@types/tape": "^5.8.0",
"auto-changelog": "^2.5.0",
"encoding": "^0.1.13",
"eslint": "=8.8.0",
Expand All @@ -58,6 +65,7 @@
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.9.0",
"typescript": "next",
"uglify-register": "^1.0.1"
},
"testling": {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test('returns false for generator functions', function (t) {

test('returns false for non-async function with faked @@toStringTag', { skip: !hasToStringTag || asyncFuncs.length === 0 }, function (t) {
var asyncFunc = asyncFuncs[0];
/** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
var fakeAsyncFunction = {
toString: function () { return String(asyncFunc); },
valueOf: function () { return asyncFunc; }
Expand Down
1 change: 1 addition & 0 deletions test/uglified.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

// @ts-expect-error
require('uglify-register/api').register({
exclude: [/\/node_modules\//, /\/test\//],
uglify: { mangle: true }
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@ljharb/tsconfig",
"compilerOptions": {
"target": "ES2021",
"maxNodeModuleJsDepth": 0,
},
"exclude": [
"coverage",
],
}

0 comments on commit 5ba6244

Please sign in to comment.