Repository: eslint/eslint
File: tests/lib/rules/utils/ast-utils.js (JavaScript)

Code before:
                        assert.strictEqual(astUtils.isTopLevelExpressionStatement(sourceCode, node), false);
                    })
                }))
            });

            linter.verify("var foo = () => \"use strict\";", { rules: { checker: "error" }, parserOptions: { ecmaVersion: 2022 } });
        });

        const expectedResults = [
            ["if (foo) { \"use strict\"; }", "\"use strict\";", false],
            ["{ \"use strict\"; }", "\"use strict\";", false],
            ["switch (foo) { case bar: \"use strict\"; }", "\"use strict\";", false],
            ["foo; bar;", "foo;", true],
            ["foo; bar;", "bar;", true],
            ["function foo() { bar; }", "bar;", true],
            ["var foo = function () { foo(); };", "foo();", true],
            ["var foo = () => { 'bar'; }", "'bar';", true],
            ["\"use strict\"", "\"use strict\"", true],
            ["(`use strict`)", "(`use strict`)", true]
        ];

        expectedResults.forEach(([code, nodeText, expectedRetVal]) => {
            it(`should return ${expectedRetVal} for \`${nodeText}\` in \`${code}\``, () => {
                linter.defineRule("checker", {
                    create: mustCall(context => {
                        const sourceCode = context.getSourceCode();
                        const assertForNode = mustCall(
                            node => assert.strictEqual(astUtils.isTopLevelExpressionStatement(node), expectedRetVal)
                        );

                        return ({
                            ExpressionStatement(node) {
                                if (sourceCode.getText(node) === nodeText) {
                                    assertForNode(node);
                                }
                            }
                        });
                    })
                });

                linter.verify(code, { rules: { checker: "error" }, parserOptions: { ecmaVersion: 2022 } });
            });
        });
    });
});


Review comment (suggestion):
```suggestion
                        const sourceCode = context.sourceCode;
```

`context.getSourceCode()` is now deprecated (https://github.com/eslint/eslint/pull/17107).

Code after:
                }))
            });

            linter.verify("var foo = () => \"use strict\";", { rules: { checker: "error" }, parserOptions: { ecmaVersion: 2022 } });
        });

        const expectedResults = [
            ["if (foo) { \"use strict\"; }", "\"use strict\";", false],
            ["{ \"use strict\"; }", "\"use strict\";", false],
            ["switch (foo) { case bar: \"use strict\"; }", "\"use strict\";", false],
            ["foo; bar;", "foo;", true],
            ["foo; bar;", "bar;", true],
            ["function foo() { bar; }", "bar;", true],
            ["var foo = function () { foo(); };", "foo();", true],
            ["var foo = () => { 'bar'; }", "'bar';", true],
            ["\"use strict\"", "\"use strict\"", true],
            ["(`use strict`)", "(`use strict`)", true]
        ];

        expectedResults.forEach(([code, nodeText, expectedRetVal]) => {
            it(`should return ${expectedRetVal} for \`${nodeText}\` in \`${code}\``, () => {
                linter.defineRule("checker", {
                    create: mustCall(context => {
                        const assertForNode = mustCall(
                            node => assert.strictEqual(astUtils.isTopLevelExpressionStatement(node), expectedRetVal)
                        );

                        return ({
                            ExpressionStatement(node) {
                                if (context.sourceCode.getText(node) === nodeText) {
                                    assertForNode(node);
                                }
                            }
                        });
                    })
                });

                linter.verify(code, { rules: { checker: "error" }, parserOptions: { ecmaVersion: 2022 } });
            });
        });
    });
});