Skip to content

Commit

Permalink
Fix: avoid breaking eslint-plugin-eslint-comments (fixes #9193)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 1, 2017
1 parent 2db356b commit 7e06fe9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,20 @@ class Linter {
parserOptions: config.parserOptions,
parserPath: config.parser,
parserServices,
settings: config.settings
settings: config.settings,

/**
* This is used to avoid breaking rules that used to monkeypatch the `Linter#report` method
* by using the `_linter` property on rule contexts.
*
* This should be removed in a major release after we create a better way to
* lint for unused disable comments.
* https://github.com/eslint/eslint/issues/9193
*/
_linter: {
report() {},
on: emitter.on.bind(emitter)
}
}
)
);
Expand Down Expand Up @@ -926,6 +939,24 @@ class Linter {
if (!isDisabledByReportingConfig(this.reportingConfig, ruleId, problem)) {
this.messages.push(problem);
}

/*
* This is used to avoid breaking rules that used monkeypatch Linter, and relied on
* `linter.report` getting called with report info every time a rule reports a problem.
* To continue to support this, make sure that `context._linter.report` is called every
* time a problem is reported by a rule, even though `context._linter` is no longer a
* `Linter` instance.
*
* This should be removed in a major release after we create a better way to
* lint for unused disable comments.
* https://github.com/eslint/eslint/issues/9193
*/
sharedTraversalContext._linter.report( // eslint-disable-line no-underscore-dangle
ruleId,
severity,
{ loc: { start: { line: problem.line, column: problem.column - 1 } } },
problem.message
);
}
])
}
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,31 @@ describe("Linter", () => {
});
});

describe("when evaluating rules that monkeypatch Linter", () => {
it("should call `context._linter.report` appropriately", () => {
linter.defineRule("foo", context => ({
Program() {
context.report({ loc: { line: 5, column: 4 }, message: "foo" });
}
}));

const spy = sandbox.spy((ruleId, severity, node, message) => {
assert.strictEqual(ruleId, "foo");
assert.strictEqual(severity, 2);
assert.deepEqual(node.loc, { start: { line: 5, column: 4 } });
assert.strictEqual(message, "foo");
});

linter.defineRule("bar", context => {
context._linter.report = spy; // eslint-disable-line no-underscore-dangle
return {};
});

linter.verify("foo", { rules: { foo: "error", bar: "error" } });
assert(spy.calledOnce);
});
});

describe("when evaluating code with comments to enable and disable all reporting", () => {
it("should report a violation", () => {

Expand Down

0 comments on commit 7e06fe9

Please sign in to comment.