-
Notifications
You must be signed in to change notification settings - Fork 21
/
assert-args.js
115 lines (100 loc) · 3.88 KB
/
assert-args.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @fileoverview Check the number of arguments to QUnit's assertion functions.
* @author Kevin Partington
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("node:assert"),
utils = require("../utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "problem",
docs: {
description:
"enforce that the correct number of assert arguments are used",
category: "Possible Errors",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/assert-args.md",
},
messages: {
unexpectedArgCount:
"Unexpected call to {{callee}} with {{argCount}} arguments.",
unexpectedArgCountNoMessage:
"Unexpected call to {{callee}} with {{argCount}} arguments and no error message.",
},
schema: [],
},
create: function (context) {
const testStack = [],
sourceCode = context.getSourceCode();
function isPossibleMessage(argNode) {
// For now, we will allow all nodes. Hoping to allow user-driven
// configuration later.
// E.g., to allow string literals only:
// return lastArg.type === "Literal" && typeof lastArg.value === "string";
// For now, allowing all nodes to be possible messages.
return argNode;
}
function getAssertContext() {
assert.ok(testStack.length);
return testStack[testStack.length - 1].assertContextVar;
}
function checkAssertArity(callExpressionNode) {
const allowedArities = utils.getAllowedArities(
callExpressionNode.callee,
getAssertContext(),
),
assertArgs = callExpressionNode.arguments,
lastArg = assertArgs[assertArgs.length - 1],
mayHaveMessage = lastArg && isPossibleMessage(lastArg);
const definitelyTooFewArgs = allowedArities.every(function (arity) {
return assertArgs.length < arity;
});
if (
mayHaveMessage &&
allowedArities.includes(assertArgs.length - 1)
) {
return;
} else if (allowedArities.includes(assertArgs.length)) {
return;
}
context.report({
node: callExpressionNode,
messageId:
mayHaveMessage && !definitelyTooFewArgs
? "unexpectedArgCount"
: "unexpectedArgCountNoMessage",
data: {
callee: sourceCode.getText(callExpressionNode.callee),
argCount: assertArgs.length,
},
});
}
return {
CallExpression: function (node) {
if (utils.isTest(node.callee)) {
testStack.push({
assertContextVar: utils.getAssertContextNameForTest(
node.arguments,
),
});
} else if (
testStack.length > 0 &&
utils.isAssertion(node.callee, getAssertContext())
) {
checkAssertArity(node);
}
},
"CallExpression:exit": function (node) {
if (utils.isTest(node.callee)) {
testStack.pop();
}
},
};
},
};