Repository: eslint/eslint
File: tests/lib/source-code/source-code.js (JavaScript)

Code before:
                                    spy = sinon.spy(node => {
                                        const sourceCode = context.getSourceCode();
                                        const ancestors = sourceCode.getAncestors(node);

                                        assert.strictEqual(ancestors.length, 0);
                                    });

                                    return { Program: spy };
                                }
                            }
                        }
                    }
                },
                rules: { "test/checker": "error" }
            };

            flatLinter.verify(code, config);
            assert(spy && spy.calledOnce, "Spy was not called.");
        });
    });


    describe("getDeclaredVariables(node)", () => {

        /**
         * Assert `context.getDeclaredVariables(node)` is valid.
         * @param {string} code A code to check.
         * @param {string} type A type string of ASTNode. This method checks variables on the node of the type.
         * @param {Array<Array<string>>} expectedNamesList An array of expected variable names. The expected variable names is an array of string.
         * @returns {void}
         */
        function verify(code, type, expectedNamesList) {
            linter.defineRules({
                test: {
                    create(context) {

                        const sourceCode = context.getSourceCode();

                        /**
                         * Assert `context.getDeclaredVariables(node)` is empty.
                         * @param {ASTNode} node A node to check.
                         * @returns {void}
                         */
                        function checkEmpty(node) {
                            assert.strictEqual(0, sourceCode.getDeclaredVariables(node).length);
                        }
                        const rule = {
                            Program: checkEmpty,
                            EmptyStatement: checkEmpty,
                            BlockStatement: checkEmpty,
                            ExpressionStatement: checkEmpty,

Review comment (suggestion):
```suggestion
         * Assert `sourceCode.getDeclaredVariables(node)` is valid.
```

Code after:
                                    spy = sinon.spy(node => {
                                        const sourceCode = context.getSourceCode();
                                        const ancestors = sourceCode.getAncestors(node);

                                        assert.strictEqual(ancestors.length, 0);
                                    });

                                    return { Program: spy };
                                }
                            }
                        }
                    }
                },
                rules: { "test/checker": "error" }
            };

            flatLinter.verify(code, config);
            assert(spy && spy.calledOnce, "Spy was not called.");
        });

        it("should throw an error when the argument is missing", () => {
            let spy;

            const config = {
                plugins: {
                    test: {
                        rules: {
                            checker: {
                                create(context) {
                                    spy = sinon.spy(() => {
                                        const sourceCode = context.getSourceCode();

                                        assert.throws(() => {
                                            sourceCode.getAncestors();
                                        }, /Missing required argument: node/u);

                                    });

                                    return { Program: spy };
                                }
                            }
                        }
                    }
                },
                rules: { "test/checker": "error" }
            };

            flatLinter.verify(code, config);
            assert(spy && spy.calledOnce, "Spy was not called.");
        });
    });