forked from qunitjs/eslint-plugin-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-throws-string.js
More file actions
124 lines (109 loc) · 3.88 KB
/
no-throws-string.js
File metadata and controls
124 lines (109 loc) · 3.88 KB
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
116
117
118
119
120
121
122
123
124
/**
* @fileoverview forbid assert.throws() with block, string, and message args
* @author Kevin Partington
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("node:assert"),
utils = require("../utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* @param {Array<{assertVar: string | null}>} testStack
* @returns {string | null}
*/
function getAssertVar(testStack) {
assert.ok(testStack && testStack.length > 0);
return testStack[testStack.length - 1].assertVar;
}
/**
* @param {import('estree').Node} calleeNode
* @param {string | null} assertVar
* @returns {boolean}
*/
function isThrows(calleeNode, assertVar) {
let result = false;
/* istanbul ignore else: correctly returns false */
if (calleeNode.type === "MemberExpression") {
result =
calleeNode.object.type === "Identifier" &&
calleeNode.object.name === assertVar &&
calleeNode.property.type === "Identifier" &&
["throws", "raises"].includes(calleeNode.property.name);
} else if (calleeNode.type === "Identifier") {
result = calleeNode.name === "throws";
}
return result;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"disallow assert.throws() with block, string, and message args",
category: "Possible errors",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/no-throws-string.md",
},
messages: {
noThrowsWithString: "Do not use {{callee}}(block, string, string).",
},
schema: [],
},
create: function (context) {
/** @type {Array<{assertVar: string | null}>} */
const testStack = [],
/* istanbul ignore next: deprecated code paths only followed by old eslint versions */
sourceCode = context.sourceCode ?? context.getSourceCode();
/**
* @param {import('eslint').Rule.Node} callExprNode
*/
function checkAndReport(callExprNode) {
if (callExprNode.type !== "CallExpression") {
return;
}
const args = callExprNode.arguments,
argCount = args.length;
if (
argCount > 2 &&
args[1].type === "Literal" &&
typeof args[1].value === "string"
) {
context.report({
node: callExprNode,
messageId: "noThrowsWithString",
data: {
callee: sourceCode.getText(callExprNode.callee),
},
});
}
}
return {
CallExpression: function (node) {
if (utils.isTest(node.callee)) {
testStack.push({
assertVar: utils.getAssertContextNameForTest(
node.arguments,
),
});
} else if (
testStack.length > 0 &&
isThrows(node.callee, getAssertVar(testStack))
) {
checkAndReport(node);
}
},
"CallExpression:exit": function (node) {
if (utils.isTest(node.callee)) {
testStack.pop();
}
},
};
},
};