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

Fix: avoid breaking eslint-plugin-eslint-comments (fixes #9193) #9196

Merged
merged 1 commit into from Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 32 additions & 1 deletion lib/linter.js
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
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