Skip to content

Commit

Permalink
New: Add context.report({ messageId }) (fixes eslint#6740)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Aug 26, 2017
1 parent ffa021e commit 02824f3
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/rule-context.js
Expand Up @@ -185,6 +185,19 @@ class RuleContext {
const descriptor = nodeOrDescriptor;
const fix = getFix(descriptor, this.getSourceCode());

if (descriptor.messageId) {
const id = descriptor.messageId;
const messages = this.meta.messages;

if (descriptor.message) {
throw new TypeError("context.report() called with a message and a messageId. Please only pass one.");
}
if (!messages || !Object.keys(messages).includes(id)) {
throw new TypeError(`context.report() called with a messageId of '${id}' which is not present in the 'messages' config: ${JSON.stringify(messages, null, 2)}`);
}
descriptor.message = messages[id];
}

if (descriptor.loc) {
this._linter.report(
this.id,
Expand Down
98 changes: 97 additions & 1 deletion tests/lib/rule-context.js
Expand Up @@ -29,7 +29,11 @@ describe("RuleContext", () => {

beforeEach(() => {
eslint = leche.fake(realESLint);
ruleContext = new RuleContext("fake-rule", eslint, 2, {}, {}, {}, "espree");
ruleContext = new RuleContext("fake-rule", eslint, 2, {}, {}, {}, "espree", {
messages: {
testMessage: "Message"
}
});
});

describe("old-style call with location", () => {
Expand Down Expand Up @@ -99,6 +103,98 @@ describe("RuleContext", () => {
mockESLint.verify();
});

it("should call eslint.report() with a message from the messageId parameter", () => {
const node = {},
location = {},
messageId = "testMessage",
messageOpts = {},
fixerObj = {},
fix = sandbox.mock().returns(fixerObj).once();

const mockESLint = sandbox.mock(eslint);

mockESLint.expects("report")
.once()
.withArgs("fake-rule", 2, node, location, "Message", messageOpts, fixerObj);
mockESLint.expects("getSourceCode")
.once()
.returns(null);

ruleContext.report({
node,
loc: location,
messageId,
data: messageOpts,
fix
});

fix.verify();
mockESLint.verify();
});

it("should throw when both messageId and message are provided", () => {
const node = {},
location = {},
messageId = "testMessage",
messageOpts = {},
fixerObj = {},
fix = sandbox.mock().returns(fixerObj).once();

const mockESLint = sandbox.mock(eslint);

mockESLint.expects("getSourceCode")
.once()
.returns(null);

assert.throws(
() =>
ruleContext.report({
node,
loc: location,
messageId,
message: "Message Foo Foo Foo",
data: messageOpts,
fix
}),
TypeError,
"context.report() called with a message and a messageId. Please only pass one."
);

fix.verify();
mockESLint.verify();
});

it("should throw when an invalid messageId is provided", () => {
const node = {},
location = {},
messageId = "not a valid messageId",
messageOpts = {},
fixerObj = {},
fix = sandbox.mock().returns(fixerObj).once();

const mockESLint = sandbox.mock(eslint);

mockESLint.expects("getSourceCode")
.once()
.returns(null);

assert.throws(
() =>
ruleContext.report({
node,
loc: location,
messageId,
data: messageOpts,
fix
}),
TypeError,
/^context\.report\(\) called with a messageId of '[^']+' which is not present in the 'messages' config:/
);

fix.verify();
mockESLint.verify();
});

it("should merge fixes to one if 'fix' function returns an array of fixes.", () => {
const mockESLint = sandbox.mock(eslint);

Expand Down

0 comments on commit 02824f3

Please sign in to comment.