• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

platinumazure / eslint-plugin-qunit / 4748569255

pending completion
4748569255

push

github

GitHub
Upgrade: Bump eslint from 8.36.0 to 8.38.0 (#335)

653 of 653 branches covered (100.0%)

878 of 878 relevant lines covered (100.0%)

82.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/rules/assert-args.js
1
/**
2
 * @fileoverview Check the number of arguments to QUnit's assertion functions.
3
 * @author Kevin Partington
4
 */
5
"use strict";
6

7
//------------------------------------------------------------------------------
8
// Requirements
9
//------------------------------------------------------------------------------
10

11
const assert = require("assert"),
1✔
12
    utils = require("../utils");
1✔
13

14
//------------------------------------------------------------------------------
15
// Rule Definition
16
//------------------------------------------------------------------------------
17

18
/** @type {import('eslint').Rule.RuleModule} */
19
module.exports = {
1✔
20
    meta: {
21
        type: "problem",
22
        docs: {
23
            description: "enforce that the correct number of assert arguments are used",
24
            category: "Possible Errors",
25
            url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/master/docs/rules/assert-args.md"
26
        },
27
        messages: {
28
            unexpectedArgCount: "Unexpected call to {{callee}} with {{argCount}} arguments.",
29
            unexpectedArgCountNoMessage: "Unexpected call to {{callee}} with {{argCount}} arguments and no error message."
30
        },
31
        schema: []
32
    },
33

34
    create: function (context) {
35
        const testStack = [],
165✔
36
            sourceCode = context.getSourceCode();
165✔
37

38
        function isPossibleMessage(argNode) {
39
            // For now, we will allow all nodes. Hoping to allow user-driven
40
            // configuration later.
41
            // E.g., to allow string literals only:
42
            // return lastArg.type === "Literal" && typeof lastArg.value === "string";
43

44
            // For now, allowing all nodes to be possible messages.
45
            return argNode;
132✔
46
        }
47

48
        function getAssertContext() {
49
            assert.ok(testStack.length);
325✔
50

51
            return testStack[testStack.length - 1].assertContextVar;
325✔
52
        }
53

54
        function checkAssertArity(callExpressionNode) {
55
            const allowedArities = utils.getAllowedArities(callExpressionNode.callee, getAssertContext()),
160✔
56
                assertArgs = callExpressionNode.arguments,
160✔
57
                lastArg = assertArgs[assertArgs.length - 1],
160✔
58
                mayHaveMessage = lastArg && isPossibleMessage(lastArg);
160✔
59

60
            const definitelyTooFewArgs = allowedArities.every(function (arity) {
160✔
61
                return assertArgs.length < arity;
164✔
62
            });
63

64
            if (mayHaveMessage && allowedArities.includes(assertArgs.length - 1)) {
160✔
65
                return;
66✔
66
            } else if (allowedArities.includes(assertArgs.length)) {
94✔
67
                return;
24✔
68
            }
69

70
            context.report({
70✔
71
                node: callExpressionNode,
72
                messageId: mayHaveMessage && !definitelyTooFewArgs ? "unexpectedArgCount" : "unexpectedArgCountNoMessage",
182✔
73
                data: {
74
                    callee: sourceCode.getText(callExpressionNode.callee),
75
                    argCount: assertArgs.length
76
                }
77
            });
78
        }
79

80
        return {
165✔
81
            "CallExpression": function (node) {
82
                if (utils.isTest(node.callee)) {
330✔
83
                    testStack.push({
164✔
84
                        assertContextVar: utils.getAssertContextNameForTest(node.arguments)
85
                    });
86
                } else if (testStack.length > 0 && utils.isAssertion(node.callee, getAssertContext())) {
166✔
87
                    checkAssertArity(node);
160✔
88
                }
89
            },
90

91
            "CallExpression:exit": function (node) {
92
                if (utils.isTest(node.callee)) {
330✔
93
                    testStack.pop();
164✔
94
                }
95
            }
96
        };
97
    }
98
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc