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 3caee87 commit 6dd27c4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function isGeneratorFunction(fn: unknown): fn is GeneratorFunction;

export = isGeneratorFunction;
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ var getGeneratorFunc = function () { // eslint-disable-line consistent-return
} catch (e) {
}
};
/** @type {undefined | false | null | GeneratorFunctionConstructor} */
var GeneratorFunction;

/** @type {import('.')} */
module.exports = function isGeneratorFunction(fn) {
if (typeof fn !== 'function') {
return false;
Expand All @@ -36,7 +38,10 @@ module.exports = function isGeneratorFunction(fn) {
}
if (typeof GeneratorFunction === 'undefined') {
var generatorFunc = getGeneratorFunc();
GeneratorFunction = generatorFunc ? getProto(generatorFunc) : false;
GeneratorFunction = generatorFunc
// eslint-disable-next-line no-extra-parens
? /** @type {GeneratorFunctionConstructor} */ (getProto(generatorFunc))
: false;
}
return getProto(fn) === GeneratorFunction;
};
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -48,7 +49,12 @@
"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-generator-function": "^2.0.3",
"@types/tape": "^5.8.0",
"auto-changelog": "^2.5.0",
"core-js": "^2.6.5 || ^3.20.0",
"encoding": "^0.1.13",
Expand All @@ -60,6 +66,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/corejs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

// @ts-ignore
require('core-js');

require('./');
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test('returns false for non-generator function with faked toString', function (t

test('returns false for non-generator function with faked @@toStringTag', { skip: !hasToStringTag || generatorFuncs.length === 0 }, function (t) {
var generatorFunc = generatorFuncs[0];
/** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown; }} */
var fakeGenFunction = {
toString: function () { return String(generatorFunc); },
valueOf: function () { return generatorFunc; }
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-ignore
require('uglify-register/api').register({
exclude: [/\/node_modules\//, /\/test\//],
uglify: { mangle: true }
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": {
"maxNodeModuleJsDepth": 0
},
"exclude": [
"coverage"
]
}

0 comments on commit 6dd27c4

Please sign in to comment.