Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ npm-debug.log

# eslint-remote-tester
/eslint-remote-tester-results/

# TypeScript output
dist/
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ For more details on how to extend your configuration from a plugin configuration

| | Name | Description |
| :--- | :--- | :--- |
| ✅ | `recommended` | This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you. For ESLint `.eslintrc.js` legacy config, extend from `"plugin:qunit/recommended"`. For ESLint `eslint.config.js` flat config, load from `require('eslint-plugin-qunit/configs/recommended')`. |
| ✅ | `recommended` | This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you. For ESLint `.eslintrc.js` legacy config, extend from `"plugin:qunit/recommended"`. For ESLint `eslint.config.js` or `eslint.config.ts` flat config, load from `require('eslint-plugin-qunit/configs/recommended')`. |

```ts
// eslint.config.ts
import eslintPluginQunitRecommended from 'eslint-plugin-qunit/configs/recommended';

export default [
eslintPluginQunitRecommended,
];
```

## Rules

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";

const eslintPluginQunitRecommended = require("./lib/configs/recommended");
import eslintPluginQunitRecommended from "./lib/configs/recommended.js";

/** @type {import('eslint-remote-tester').Config} */
module.exports = {
const config = {
/** Repositories to scan */
repositories: [
// A few dozen top repositories using QUnit or this plugin.
Expand Down Expand Up @@ -36,3 +34,5 @@ module.exports = {
...eslintPluginQunitRecommended,
},
};

export default config;
26 changes: 10 additions & 16 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";

const js = require("@eslint/js");

// @ts-expect-error -- TODO: no types yet
const eslintPluginEslintComments = require("@eslint-community/eslint-plugin-eslint-comments/configs");

// @ts-expect-error -- TODO: no types yet -- https://github.com/eslint-community/eslint-plugin-eslint-plugin/issues/310
const eslintPluginEslintPluginAll = require("eslint-plugin-eslint-plugin/configs/all");
const eslintPluginMarkdown = require("eslint-plugin-markdown");
const eslintPluginMocha = require("eslint-plugin-mocha");
Expand Down Expand Up @@ -41,21 +45,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 @@ -111,7 +100,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 Down Expand Up @@ -209,4 +197,10 @@ module.exports = [
strict: "off",
},
},
{
files: ["**/*.mjs"],
languageOptions: {
sourceType: "module",
},
},
];
6 changes: 4 additions & 2 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 All @@ -22,7 +24,7 @@ module.exports = {
configs: {
recommended: {
plugins: ["qunit"],
rules: {
rules: /** @type {import('eslint').Linter.RulesRecord} */ ({
Copy link
Member Author

Choose a reason for hiding this comment

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

This was necessary so that consumers who use our flat config in their eslint.config.ts has the right RuleEntry type instead of just generic string.

"qunit/assert-args": "error",
"qunit/literal-compare-order": "error",
"qunit/no-assert-equal": "error",
Expand Down Expand Up @@ -58,7 +60,7 @@ module.exports = {
"qunit/require-expect": "error",
"qunit/require-object-in-propequal": "error",
"qunit/resolve-async": "error",
},
}),
},
},
};
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
Loading