Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 9 additions & 24 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"use strict";

// @ts-expect-error -- TODO: Update plugin version to fix
const js = require("@eslint/js");

// @ts-expect-error -- TODO: Update plugin version to fix
const { FlatCompat } = require("@eslint/eslintrc");

// @ts-expect-error -- TODO: Update plugin version to fix
const eslintPluginEslintPluginAll = require("eslint-plugin-eslint-plugin/configs/all");

// @ts-expect-error -- TODO: Update plugin version to fix
const eslintPluginMarkdown = require("eslint-plugin-markdown");
const globals = require("globals");

Expand All @@ -24,6 +31,8 @@ module.exports = [
// Apply mocha config only to tests.
...compat
.extends("plugin:mocha/recommended")

// @ts-expect-error -- TODO: Update plugin version to fix
.map((config) => ({ ...config, files: ["tests/**/*.js"] })),

{
Expand All @@ -43,21 +52,6 @@ module.exports = [
eqeqeq: "error",
"func-style": ["error", "declaration"],
"guard-for-in": "error",
"lines-around-comment": [
Copy link
Member Author

Choose a reason for hiding this comment

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

There's a number of old rules I had to remove because they conflict with prettier.

"error",
{
beforeBlockComment: false,
afterBlockComment: false,
beforeLineComment: true,
afterLineComment: false,
allowBlockStart: true,
allowBlockEnd: true,
allowObjectStart: true,
allowObjectEnd: true,
allowArrayStart: true,
allowArrayEnd: true,
},
],
"max-depth": ["error", 5],
"new-cap": ["error", { newIsCap: true, capIsNew: true }],
"no-array-constructor": "error",
Expand Down Expand Up @@ -113,7 +107,6 @@ module.exports = [
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef": "error",
"no-undefined": "error",
"no-underscore-dangle": "error",
"no-unexpected-multiline": "error",
"no-unmodified-loop-condition": "error",
Expand All @@ -137,14 +130,6 @@ module.exports = [
"spaced-comment": ["error", "always", { exceptions: ["-"] }],
strict: ["error", "global"],
"use-isnan": "error",
"valid-jsdoc": [
Copy link
Member Author

@bmish bmish Jun 21, 2025

Choose a reason for hiding this comment

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

This rule is deprecated and didn't appear to be compatible with the jsdoc TypeScript comments I added. We can replace with https://github.com/gajus/eslint-plugin-jsdoc later.

"error",
{
prefer: {
return: "returns",
},
},
],
"valid-typeof": "error",
yoda: ["error", "never"],

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"use strict";

const requireIndex = require("requireindex");

// @ts-expect-error -- ESM/TypeScript conversion should fix this.
const pkg = require("./package.json");

module.exports = {
Expand Down
37 changes: 30 additions & 7 deletions lib/rules/assert-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ module.exports = {
},

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

/**
* @param {import('estree').Node} argNode
* @returns {import('estree').Node}
*/
function isPossibleMessage(argNode) {
// For now, we will allow all nodes. Hoping to allow user-driven
// configuration later.
Expand All @@ -48,16 +53,31 @@ module.exports = {
return argNode;
}

/**
* @returns {string | null}
*/
function getAssertContext() {
assert.ok(testStack.length);

return testStack[testStack.length - 1].assertContextVar;
}

/**
* @param {import('estree').Node} callExpressionNode
*/
function checkAssertArity(callExpressionNode) {
if (callExpressionNode.type !== "CallExpression") {
return;
}

const assertContextVar = getAssertContext();
if (!assertContextVar) {
return;
}

const allowedArities = utils.getAllowedArities(
callExpressionNode.callee,
getAssertContext(),
assertContextVar,
),
assertArgs = callExpressionNode.arguments,
lastArg = assertArgs[assertArgs.length - 1],
Expand All @@ -84,7 +104,7 @@ module.exports = {
: "unexpectedArgCountNoMessage",
data: {
callee: sourceCode.getText(callExpressionNode.callee),
argCount: assertArgs.length,
argCount: assertArgs.length.toString(),
},
});
}
Expand All @@ -97,11 +117,14 @@ module.exports = {
node.arguments,
),
});
} else if (
testStack.length > 0 &&
utils.isAssertion(node.callee, getAssertContext())
) {
checkAssertArity(node);
} else if (testStack.length > 0) {
const assertContext = getAssertContext();
if (
assertContext &&
utils.isAssertion(node.callee, assertContext)
) {
checkAssertArity(node);
}
}
},

Expand Down
65 changes: 44 additions & 21 deletions lib/rules/literal-compare-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ const assert = require("node:assert"),
// Rule Definition
//------------------------------------------------------------------------------

function swapFirstTwoNodesInList(sourceCode, fixer, list) {
const node0Text = sourceCode.getText(list[0]);
const node1Text = sourceCode.getText(list[1]);
return [
fixer.replaceText(list[0], node1Text),
fixer.replaceText(list[1], node0Text),
];
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -45,6 +36,7 @@ module.exports = {
},

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

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

/**
* @param {any} fixer
* @param {import('estree').Node[]} list
* @returns {import('eslint').Rule.Fix[]}
*/
function swapFirstTwoNodesInList(fixer, list) {
const node0Text = sourceCode.getText(list[0]);
const node1Text = sourceCode.getText(list[1]);
return [
fixer.replaceText(list[0], node1Text),
fixer.replaceText(list[1], node0Text),
];
}

/**
* @param {import('estree').Node[]} args
* @param {boolean} compareActualFirst
*/
function checkLiteralCompareOrder(args, compareActualFirst) {
if (args.length < 2) {
return;
Expand All @@ -73,7 +83,7 @@ module.exports = {
actual: sourceCode.getText(args[1]),
},
fix(fixer) {
return swapFirstTwoNodesInList(sourceCode, fixer, args);
return swapFirstTwoNodesInList(fixer, args);
},
});
} else if (
Expand All @@ -89,19 +99,29 @@ module.exports = {
actual: sourceCode.getText(args[1]),
},
fix(fixer) {
return swapFirstTwoNodesInList(sourceCode, fixer, args);
return swapFirstTwoNodesInList(fixer, args);
},
});
}
}

/**
* @param {import('eslint').Rule.Node} node
* @param {string | null} assertVar
*/
function processAssertion(node, assertVar) {
if (node.type !== "CallExpression") {
return;
}

/* istanbul ignore else: correctly does nothing */
if (utils.isComparativeAssertion(node.callee, assertVar)) {
const compareActualFirst = utils.shouldCompareActualFirst(
node.callee,
assertVar,
);
if (
!assertVar ||
utils.isComparativeAssertion(node.callee, assertVar)
) {
const compareActualFirst =
!assertVar ||
utils.shouldCompareActualFirst(node.callee, assertVar);
checkLiteralCompareOrder(node.arguments, compareActualFirst);
}
}
Expand All @@ -115,11 +135,14 @@ module.exports = {
node.arguments,
),
});
} else if (
testStack.length > 0 &&
utils.isAssertion(node.callee, getAssertContext())
) {
processAssertion(node, getAssertContext());
} else if (testStack.length > 0) {
const assertVar = getAssertContext();
if (
!assertVar ||
utils.isAssertion(node.callee, assertVar)
) {
processAssertion(node, assertVar);
}
}
},

Expand Down
57 changes: 57 additions & 0 deletions lib/rules/no-arrow-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,38 @@ module.exports = {
// Fixer adapted from https://github.com/lo1tuma/eslint-plugin-mocha (MIT)
const sourceCode = context.getSourceCode();

/**
* @param {number} start
* @param {number} end
* @returns {string}
*/
function extractSourceTextByRange(start, end) {
return sourceCode.text.slice(start, end).trim();
}

/**
* @param {import('estree').FunctionExpression|import('estree').ArrowFunctionExpression} fn
* @returns {string}
*/
function formatFunctionHead(fn) {
if (
fn.type !== "FunctionExpression" &&
fn.type !== "ArrowFunctionExpression"
) {
return "";
}
const arrow = sourceCode.getTokenBefore(fn.body);
if (!arrow) {
return "";
}
const beforeArrowToken = sourceCode.getTokenBefore(arrow);
if (!beforeArrowToken) {
return "";
}
let firstToken = sourceCode.getFirstToken(fn);
if (!firstToken) {
return "";
}

let functionKeyword = "function";
let params = extractSourceTextByRange(
Expand All @@ -68,6 +92,14 @@ module.exports = {
firstToken = sourceCode.getTokenAfter(firstToken);
}

if (!firstToken) {
return "";
}

if (!fn.body.range) {
return "";
}

const beforeArrowComment = extractSourceTextByRange(
beforeArrowToken.range[1],
arrow.range[0],
Expand All @@ -84,7 +116,20 @@ module.exports = {
return `${functionKeyword}${paramsFullText} `;
}

/**
* @param {any} fixer
* @param {import('estree').Node} fn
*/
function fixArrowFunction(fixer, fn) {
if (
fn.type !== "FunctionExpression" &&
fn.type !== "ArrowFunctionExpression"
) {
return null;
}
if (!fn.range || !fn.body.range) {
return null;
}
if (fn.body.type === "BlockStatement") {
// When it((...) => { ... }),
// simply replace '(...) => ' with 'function () '
Expand All @@ -104,6 +149,9 @@ module.exports = {
);
}

/**
* @param {import('estree').Node} fn
*/
function checkCallback(fn) {
if (fn && fn.type === "ArrowFunctionExpression") {
context.report({
Expand All @@ -114,6 +162,10 @@ module.exports = {
}
}

/**
* @param {import('eslint').Rule.Node} propertyNode
* @returns {boolean}
*/
function isPropertyInModule(propertyNode) {
return (
propertyNode &&
Expand All @@ -125,8 +177,13 @@ module.exports = {
);
}

/**
* @param {import('eslint').Rule.Node} propertyNode
* @returns {boolean}
*/
function isModuleProperty(propertyNode) {
return (
propertyNode.type === "Property" &&
isPropertyInModule(propertyNode) &&
utils.isModuleHookPropertyKey(propertyNode.key)
);
Expand Down
Loading