From 6dd27c4b6a3ebaa42ddbf4e93c20e2b4d90bad07 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 1 Jan 2025 23:15:31 -0800 Subject: [PATCH] [New] add types --- index.d.ts | 3 +++ index.js | 7 ++++++- package.json | 7 +++++++ test/corejs.js | 1 + test/index.js | 1 + test/uglified.js | 1 + tsconfig.json | 9 +++++++++ 7 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 index.d.ts create mode 100644 tsconfig.json diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..38eec07 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +declare function isGeneratorFunction(fn: unknown): fn is GeneratorFunction; + +export = isGeneratorFunction; \ No newline at end of file diff --git a/index.js b/index.js index 3d340ee..01abeae 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; }; diff --git a/package.json b/package.json index 432bbc3..a7cbad2 100644 --- a/package.json +++ b/package.json @@ -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)\")\"" }, @@ -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", @@ -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": { diff --git a/test/corejs.js b/test/corejs.js index 73f0c89..3cc109c 100644 --- a/test/corejs.js +++ b/test/corejs.js @@ -1,5 +1,6 @@ 'use strict'; +// @ts-ignore require('core-js'); require('./'); diff --git a/test/index.js b/test/index.js index 355bbf2..038f2a1 100644 --- a/test/index.js +++ b/test/index.js @@ -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; } diff --git a/test/uglified.js b/test/uglified.js index fd82b55..91c0545 100644 --- a/test/uglified.js +++ b/test/uglified.js @@ -1,5 +1,6 @@ 'use strict'; +// @ts-ignore require('uglify-register/api').register({ exclude: [/\/node_modules\//, /\/test\//], uglify: { mangle: true } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4bd3528 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@ljharb/tsconfig", + "compilerOptions": { + "maxNodeModuleJsDepth": 0 + }, + "exclude": [ + "coverage" + ] +}