Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: disallow SourceCode#getComments() in RuleTester (refs #14744) #14769

Merged
merged 2 commits into from Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/rule-tester/rule-tester.js
Expand Up @@ -55,6 +55,8 @@ const ajv = require("../shared/ajv")({ strictDefaults: true });
const espreePath = require.resolve("espree");
const parserSymbol = Symbol.for("eslint.RuleTester.parser");

const { SourceCode } = require("../source-code");

//------------------------------------------------------------------------------
// Typedefs
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -284,6 +286,17 @@ function wrapParser(parser) {
};
}

/**
* Function to replace `SourceCode.prototype.getComments`.
* @returns {void}
* @throws {Error} Deprecation message.
*/
function getCommentsDeprecation() {
throw new Error(
"`SourceCode#getComments()` is deprecated and will be removed in a future major version. Use `getCommentsBefore()`, `getCommentsAfter()`, and `getCommentsInside()`."
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
);
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -607,7 +620,16 @@ class RuleTester {
validate(config, "rule-tester", id => (id === ruleName ? rule : null));

// Verify the code.
const messages = linter.verify(code, config, filename);
const { getComments } = SourceCode.prototype;
let messages;

try {
SourceCode.prototype.getComments = getCommentsDeprecation;
messages = linter.verify(code, config, filename);
} finally {
SourceCode.prototype.getComments = getComments;
}

const fatalErrorMessage = messages.find(m => m.fatal);

assert(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`);
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2462,4 +2462,37 @@ describe("RuleTester", () => {

});

describe("SourceCode#getComments()", () => {
const useGetCommentsRule = {
create: context => ({
Program(node) {
const sourceCode = context.getSourceCode();

sourceCode.getComments(node);
}
})
};

it("should throw if called from a valid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [""],
invalid: []
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});

it("should throw if called from an invalid test case", () => {
assert.throws(() => {
ruleTester.run("use-get-comments", useGetCommentsRule, {
valid: [],
invalid: [{
code: "",
errors: [{}]
}]
});
}, /`SourceCode#getComments\(\)` is deprecated/u);
});
});

});