Skip to content

Commit 061ef6c

Browse files
committed
feat: add jsdoc typescript types
1 parent fda23d2 commit 061ef6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1387
-151
lines changed

eslint.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"use strict";
22

3+
// @ts-expect-error -- TODO: Update plugin version to fix
34
const js = require("@eslint/js");
5+
// @ts-expect-error -- TODO: Update plugin version to fix
46
const { FlatCompat } = require("@eslint/eslintrc");
7+
// @ts-expect-error -- TODO: Update plugin version to fix
58
const eslintPluginEslintPluginAll = require("eslint-plugin-eslint-plugin/configs/all");
9+
// @ts-expect-error -- TODO: Update plugin version to fix
610
const eslintPluginMarkdown = require("eslint-plugin-markdown");
711
const globals = require("globals");
812

@@ -24,6 +28,7 @@ module.exports = [
2428
// Apply mocha config only to tests.
2529
...compat
2630
.extends("plugin:mocha/recommended")
31+
// @ts-expect-error -- TODO: Update plugin version to fix
2732
.map((config) => ({ ...config, files: ["tests/**/*.js"] })),
2833

2934
{

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"use strict";
99

1010
const requireIndex = require("requireindex");
11+
// @ts-expect-error -- package.json is not a module.
1112
const pkg = require("./package.json");
1213

1314
module.exports = {

lib/rules/assert-args.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ module.exports = {
3535
},
3636

3737
create: function (context) {
38+
/** @type {Array<{assertContextVar: string | null}>} */
3839
const testStack = [],
3940
sourceCode = context.getSourceCode();
4041

42+
/**
43+
* @param {import('estree').Node} argNode
44+
* @returns {import('estree').Node}
45+
*/
4146
function isPossibleMessage(argNode) {
4247
// For now, we will allow all nodes. Hoping to allow user-driven
4348
// configuration later.
@@ -48,24 +53,41 @@ module.exports = {
4853
return argNode;
4954
}
5055

56+
/**
57+
* @returns {string | null}
58+
*/
5159
function getAssertContext() {
5260
assert.ok(testStack.length);
5361

5462
return testStack[testStack.length - 1].assertContextVar;
5563
}
5664

65+
/**
66+
* @param {import('estree').Node} callExpressionNode
67+
*/
5768
function checkAssertArity(callExpressionNode) {
69+
if (callExpressionNode.type !== "CallExpression") {
70+
return;
71+
}
72+
73+
const assertContextVar = getAssertContext();
74+
if (!assertContextVar) {
75+
return;
76+
}
77+
5878
const allowedArities = utils.getAllowedArities(
5979
callExpressionNode.callee,
60-
getAssertContext(),
80+
assertContextVar,
6181
),
6282
assertArgs = callExpressionNode.arguments,
6383
lastArg = assertArgs[assertArgs.length - 1],
6484
mayHaveMessage = lastArg && isPossibleMessage(lastArg);
6585

66-
const definitelyTooFewArgs = allowedArities.every(function (arity) {
67-
return assertArgs.length < arity;
68-
});
86+
const definitelyTooFewArgs = allowedArities.every(
87+
function (/** @type {number} */ arity) {
88+
return assertArgs.length < arity;
89+
},
90+
);
6991

7092
if (
7193
mayHaveMessage &&
@@ -84,7 +106,7 @@ module.exports = {
84106
: "unexpectedArgCountNoMessage",
85107
data: {
86108
callee: sourceCode.getText(callExpressionNode.callee),
87-
argCount: assertArgs.length,
109+
argCount: assertArgs.length.toString(),
88110
},
89111
});
90112
}
@@ -99,7 +121,7 @@ module.exports = {
99121
});
100122
} else if (
101123
testStack.length > 0 &&
102-
utils.isAssertion(node.callee, getAssertContext())
124+
utils.isAssertion(node.callee, getAssertContext() ?? "")
103125
) {
104126
checkAssertArity(node);
105127
}

lib/rules/literal-compare-order.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ const assert = require("node:assert"),
1515
// Rule Definition
1616
//------------------------------------------------------------------------------
1717

18-
function swapFirstTwoNodesInList(sourceCode, fixer, list) {
19-
const node0Text = sourceCode.getText(list[0]);
20-
const node1Text = sourceCode.getText(list[1]);
21-
return [
22-
fixer.replaceText(list[0], node1Text),
23-
fixer.replaceText(list[1], node0Text),
24-
];
25-
}
26-
2718
/** @type {import('eslint').Rule.RuleModule} */
2819
module.exports = {
2920
meta: {
@@ -45,6 +36,7 @@ module.exports = {
4536
},
4637

4738
create: function (context) {
39+
/** @type {Array<{assertContextVar: string | null}>} */
4840
const testStack = [],
4941
sourceCode = context.getSourceCode();
5042

@@ -54,6 +46,24 @@ module.exports = {
5446
return testStack[testStack.length - 1].assertContextVar;
5547
}
5648

49+
/**
50+
* @param {any} fixer
51+
* @param {import('estree').Node[]} list
52+
* @returns {import('eslint').Rule.Fix[]}
53+
*/
54+
function swapFirstTwoNodesInList(fixer, list) {
55+
const node0Text = sourceCode.getText(list[0]);
56+
const node1Text = sourceCode.getText(list[1]);
57+
return [
58+
fixer.replaceText(list[0], node1Text),
59+
fixer.replaceText(list[1], node0Text),
60+
];
61+
}
62+
63+
/**
64+
* @param {import('estree').Node[]} args
65+
* @param {boolean} compareActualFirst
66+
*/
5767
function checkLiteralCompareOrder(args, compareActualFirst) {
5868
if (args.length < 2) {
5969
return;
@@ -73,7 +83,7 @@ module.exports = {
7383
actual: sourceCode.getText(args[1]),
7484
},
7585
fix(fixer) {
76-
return swapFirstTwoNodesInList(sourceCode, fixer, args);
86+
return swapFirstTwoNodesInList(fixer, args);
7787
},
7888
});
7989
} else if (
@@ -89,13 +99,21 @@ module.exports = {
8999
actual: sourceCode.getText(args[1]),
90100
},
91101
fix(fixer) {
92-
return swapFirstTwoNodesInList(sourceCode, fixer, args);
102+
return swapFirstTwoNodesInList(fixer, args);
93103
},
94104
});
95105
}
96106
}
97107

108+
/**
109+
* @param {import('eslint').Rule.Node} node
110+
* @param {string | null} assertVar
111+
*/
98112
function processAssertion(node, assertVar) {
113+
if (node.type !== "CallExpression" || !assertVar) {
114+
return;
115+
}
116+
99117
/* istanbul ignore else: correctly does nothing */
100118
if (utils.isComparativeAssertion(node.callee, assertVar)) {
101119
const compareActualFirst = utils.shouldCompareActualFirst(
@@ -117,9 +135,9 @@ module.exports = {
117135
});
118136
} else if (
119137
testStack.length > 0 &&
120-
utils.isAssertion(node.callee, getAssertContext())
138+
utils.isAssertion(node.callee, getAssertContext() ?? "")
121139
) {
122-
processAssertion(node, getAssertContext());
140+
processAssertion(node, getAssertContext() ?? "");
123141
}
124142
},
125143

lib/rules/no-arrow-tests.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,35 @@ module.exports = {
4242
// Fixer adapted from https://github.com/lo1tuma/eslint-plugin-mocha (MIT)
4343
const sourceCode = context.getSourceCode();
4444

45+
/**
46+
* @param {number} start
47+
* @param {number} end
48+
* @returns {string}
49+
*/
4550
function extractSourceTextByRange(start, end) {
4651
return sourceCode.text.slice(start, end).trim();
4752
}
4853

54+
/**
55+
* @param {import('estree').FunctionExpression} fn
56+
* @returns {string}
57+
*/
4958
function formatFunctionHead(fn) {
59+
if (fn.type !== "FunctionExpression") {
60+
return "";
61+
}
5062
const arrow = sourceCode.getTokenBefore(fn.body);
63+
if (!arrow) {
64+
return "";
65+
}
5166
const beforeArrowToken = sourceCode.getTokenBefore(arrow);
67+
if (!beforeArrowToken) {
68+
return "";
69+
}
5270
let firstToken = sourceCode.getFirstToken(fn);
71+
if (!firstToken) {
72+
return "";
73+
}
5374

5475
let functionKeyword = "function";
5576
let params = extractSourceTextByRange(
@@ -68,6 +89,14 @@ module.exports = {
6889
firstToken = sourceCode.getTokenAfter(firstToken);
6990
}
7091

92+
if (!firstToken) {
93+
return "";
94+
}
95+
96+
if (!fn.body.range) {
97+
return "";
98+
}
99+
71100
const beforeArrowComment = extractSourceTextByRange(
72101
beforeArrowToken.range[1],
73102
arrow.range[0],
@@ -84,7 +113,17 @@ module.exports = {
84113
return `${functionKeyword}${paramsFullText} `;
85114
}
86115

116+
/**
117+
* @param {any} fixer
118+
* @param {import('estree').Node} fn
119+
*/
87120
function fixArrowFunction(fixer, fn) {
121+
if (fn.type !== "FunctionExpression") {
122+
return;
123+
}
124+
if (!fn.range || !fn.body.range) {
125+
return;
126+
}
88127
if (fn.body.type === "BlockStatement") {
89128
// When it((...) => { ... }),
90129
// simply replace '(...) => ' with 'function () '
@@ -104,6 +143,9 @@ module.exports = {
104143
);
105144
}
106145

146+
/**
147+
* @param {import('estree').Node} fn
148+
*/
107149
function checkCallback(fn) {
108150
if (fn && fn.type === "ArrowFunctionExpression") {
109151
context.report({
@@ -114,6 +156,10 @@ module.exports = {
114156
}
115157
}
116158

159+
/**
160+
* @param {import('eslint').Rule.Node} propertyNode
161+
* @returns {boolean}
162+
*/
117163
function isPropertyInModule(propertyNode) {
118164
return (
119165
propertyNode &&
@@ -125,8 +171,13 @@ module.exports = {
125171
);
126172
}
127173

174+
/**
175+
* @param {import('eslint').Rule.Node} propertyNode
176+
* @returns {boolean}
177+
*/
128178
function isModuleProperty(propertyNode) {
129179
return (
180+
propertyNode.type === "Property" &&
130181
isPropertyInModule(propertyNode) &&
131182
utils.isModuleHookPropertyKey(propertyNode.key)
132183
);

0 commit comments

Comments
 (0)