Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Playing around with .d.ts generation #4156

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ docs/_dist
mocha.js
.karma/
!lib/mocha.js
/types

#########################################
# NON-MOCHA STUFF GOES BELOW THIS THING #
Expand Down
11 changes: 11 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Mocha from './';

declare global {
const after: typeof Mocha.after;
const afterEach: typeof Mocha.afterEach;
const before: typeof Mocha.after;
const beforeEach: typeof Mocha.beforeEach;
const describe: typeof Mocha.describe;
const it: typeof Mocha.after;
const xit: typeof Mocha.xit;
}
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as Mocha from './types/mocha';
export = Mocha;
17 changes: 7 additions & 10 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ const parseMochaOpts = content =>

/**
* Given filepath in `args.opts`, attempt to load and parse a `mocha.opts` file.
* @param {Object} [args] - Arguments object
* @param {string|boolean} [args.opts] - Filepath to mocha.opts; defaults to whatever's in `mocharc.opts`, or `false` to skip
* @returns {external:yargsParser.Arguments|void} If read, object containing parsed arguments
* @param {import('./options_t').MochaArgs} [args] - Arguments object
* @returns {import('yargs-parser').Arguments | void} If read, object containing parsed arguments
* @memberof module:lib/cli/options
* @see {@link /#mochaopts|mocha.opts}
* @public
Expand Down Expand Up @@ -210,11 +209,10 @@ module.exports.loadMochaOpts = loadMochaOpts;

/**
* Given path to config file in `args.config`, attempt to load & parse config file.
* @param {Object} [args] - Arguments object
* @param {string|boolean} [args.config] - Path to config file or `false` to skip
* @param {import('./options_t').MochaArgs} [args] - Arguments object
* @public
* @memberof module:lib/cli/options
* @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.config` is `false`
* @returns {import('yargs-parser').Arguments | void} Parsed config, or nothing if `args.config` is `false`
*/
const loadRc = (args = {}) => {
if (args.config !== false) {
Expand All @@ -227,11 +225,10 @@ module.exports.loadRc = loadRc;

/**
* Given path to `package.json` in `args.package`, attempt to load config from `mocha` prop.
* @param {Object} [args] - Arguments object
* @param {string|boolean} [args.config] - Path to `package.json` or `false` to skip
* @param {import('./options_t').MochaArgs} [args] - Arguments object
* @public
* @memberof module:lib/cli/options
* @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.package` is `false`
* @returns {import('yargs-parser').Arguments | void} Parsed config, or nothing if `args.package` is `false`
*/
const loadPkgRc = (args = {}) => {
let result;
Expand Down Expand Up @@ -275,7 +272,7 @@ module.exports.loadPkgRc = loadPkgRc;
* @param {string|string[]} [argv] - Arguments to parse
* @public
* @memberof module:lib/cli/options
* @returns {external:yargsParser.Arguments} Parsed args from everything
* @returns {import('yargs-parser').Arguments} Parsed args from everything
*/
const loadOptions = (argv = []) => {
let args = parse(argv);
Expand Down
7 changes: 7 additions & 0 deletions lib/cli/options_t.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface MochaArgs {
/** Path to `package.json` or `false` to skip */
config?: string | false;
package?: boolean;
/** Filepath to mocha.opts; defaults to whatever's in `mocharc.opts`, or `false` to skip */
opts?: string | false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do this with a @typedef; I really don't want to add any typescript sources.

4 changes: 2 additions & 2 deletions lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Dictionary of yargs option types to list of options having said type
* @type {{string:string[]}}
* @type {Record<string, string[]>}
* @private
*/
exports.types = {
Expand Down Expand Up @@ -63,7 +63,7 @@ exports.types = {
/**
* Option aliases keyed by canonical option name.
* Arrays used to reduce
* @type {{string:string[]}}
* @type {Record<string, string[]>}
* @private
*/
exports.aliases = {
Expand Down
7 changes: 4 additions & 3 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ utils.inherits(Runnable, EventEmitter);
*
* @private
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value}
* @param {number|string} ms - Timeout threshold value.
* @param {number|string} [ms] - Timeout threshold value.
* @returns {Runnable} this
* @chainable
*/
Expand Down Expand Up @@ -260,13 +260,14 @@ Runnable.prototype.resetTimeout = function() {
return;
}
this.clearTimeout();
this.timer = setTimeout(function() {
/** @type {number} */
this.timer = /** @type {any} */ (setTimeout(function() {
if (!self._enableTimeouts) {
return;
}
self.callback(self._timeoutError(ms));
self.timedOut = true;
}, ms);
}, ms));
};

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ var assign = (exports.assign = require('object.assign').getPolyfill());
* @throws {TypeError} if either constructor is null, or if super constructor
* lacks a prototype.
*/
exports.inherits = util.inherits;
exports.inherits = function(ctor, superCtor) {
// Wrapper prevents type declarations from referring to @types/node

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just add a /** @type {whatever} */ annotation onto this declaration to override the inferred type, instead of changing the implementation.

return util.inherits.call(this, ctor, superCtor);
};

/**
* Escape special characters in the given string of html.
Expand Down Expand Up @@ -708,7 +711,7 @@ exports.isPromise = function isPromise(value) {
* Clamps a numeric value to an inclusive range.
*
* @param {number} value - Value to be clamped.
* @param {numer[]} range - Two element array specifying [min, max] range.
* @param {number[]} range - Two element array specifying [min, max] range.
* @returns {number} clamped value
*/
exports.clamp = function clamp(value, range) {
Expand Down
27 changes: 21 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ function test(testName, mochaParams) {
module.exports = {
scripts: {
build: {
script: `browserify -e browser-entry.js --plugin ./scripts/dedefine --ignore './lib/cli/*.js' --ignore 'chokidar' --ignore 'fs' --ignore 'glob' --ignore 'path' --ignore 'supports-color' -o mocha.js`,
description: 'Build browser bundle'
default: {
script: 'nps build.types build.browser',
description: 'Build'
},
types: {
script: 'tsc -p . || true', // swallowing until I finish fixing all semantic errors
description: 'Build type declarations'
},
browser: {
script: `browserify -e browser-entry.js --plugin ./scripts/dedefine --ignore './lib/cli/*.js' --ignore 'chokidar' --ignore 'fs' --ignore 'glob' --ignore 'path' --ignore 'supports-color' -o mocha.js`,
description: 'Build browser bundle'
}
},
lint: {
default: {
Expand Down Expand Up @@ -64,7 +74,7 @@ module.exports = {
},
test: {
default: {
script: 'nps lint test.node test.browser test.bundle',
script: 'nps lint test.node test.browser test.bundle test.types',
description: 'Run all linters and all tests'
},
node: {
Expand Down Expand Up @@ -251,6 +261,10 @@ module.exports = {
description: 'Run AMD bundle tests',
hiddenFromHelp: true
}
},
types: {
script: 'cd test/types && tsc -p .',
description: 'Test emitted type declarations.'
}
},
coveralls: {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@
"mocha": "./bin/mocha",
"_mocha": "./bin/_mocha"
},
"types": "types",
"directories": {
"lib": "./lib",
"test": "./test"
Expand Down Expand Up @@ -564,6 +565,8 @@
"@11ty/eleventy": "^0.8.3",
"@mocha/contributors": "^1.0.4",
"@mocha/docdash": "^2.1.2",
"@types/node": "^13.1.8",
"@types/yargs": "^15.0.0",
"acorn": "^7.0.0",
"assetgraph-builder": "^6.10.1",
"autoprefixer": "^9.6.0",
Expand Down Expand Up @@ -615,6 +618,7 @@
"svgo": "^1.2.2",
"through2": "^3.0.1",
"to-vfile": "^5.0.3",
"typescript": "^3.7.5",
"unexpected": "^10.40.2",
"unexpected-eventemitter": "^1.1.3",
"unexpected-sinon": "^10.11.2",
Expand Down
Loading