forked from qunitjs/eslint-plugin-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-global-assertions.js
More file actions
73 lines (66 loc) · 2.72 KB
/
no-global-assertions.js
File metadata and controls
73 lines (66 loc) · 2.72 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
/**
* @fileoverview Forbid the use of global QUnit assertions.
* @author Kevin Partington
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { getAssertionNames } = require("../utils");
const { ReferenceTracker } = require("@eslint-community/eslint-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow global QUnit assertions",
category: "Possible Errors",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/no-global-assertions.md",
},
messages: {
unexpectedGlobalAssertion:
"Unexpected global `{{ assertion }}` assertion.",
},
schema: [],
},
create: function (context) {
return {
Program: function (node) {
/* istanbul ignore next: deprecated code paths only followed by old eslint versions */
const sourceCode =
context.sourceCode ?? context.getSourceCode();
/* istanbul ignore next: deprecated code paths only followed by old eslint versions */
const scope = sourceCode.getScope
? sourceCode.getScope(node)
: // @ts-expect-error -- removed in ESLint 10, but needed for ESLint 8 compat
context.getScope();
const tracker = new ReferenceTracker(scope);
/** @type {Record<string, { [ReferenceTracker.CALL]: boolean }>} */
const traceMap = {};
for (const assertionName of getAssertionNames()) {
traceMap[assertionName] = { [ReferenceTracker.CALL]: true };
}
for (const { node } of tracker.iterateGlobalReferences(
traceMap,
)) {
if (node.type !== "CallExpression") {
continue;
}
if (node.callee.type !== "Identifier") {
continue;
}
context.report({
node: node,
messageId: "unexpectedGlobalAssertion",
data: {
assertion: node.callee.name,
},
});
}
},
};
},
};