Skip to content

Commit a9a4652

Browse files
vitorbalnzakas
authored andcommitted
Fix: throw when rule uses fix but meta.fixable not set (fixes #5970) (#6043)
Throw an error when a rule uses `fix` but has no `meta.fixable` property set.
1 parent ad10106 commit a9a4652

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/eslint.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,14 @@ module.exports = (function() {
962962
source: sourceCode.lines[location.line - 1] || ""
963963
};
964964

965-
// ensure there's range and text properties as well as metadata switch, otherwise it's not a valid fix
966-
if (fix && Array.isArray(fix.range) && (typeof fix.text === "string") && (!meta || meta.fixable)) {
965+
// ensure there's range and text properties, otherwise it's not a valid fix
966+
if (fix && Array.isArray(fix.range) && (typeof fix.text === "string")) {
967+
968+
// If rule uses fix, has metadata, but has no metadata.fixable, we should throw
969+
if (meta && !meta.fixable) {
970+
throw new Error("Fixable rules should export a `meta.fixable` property.");
971+
}
972+
967973
problem.fix = fix;
968974
}
969975

tests/lib/eslint.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,31 @@ describe("eslint", function() {
806806
}, /Node must be an object$/);
807807
});
808808

809+
it("should throw an error if fix is passed but meta has no `fixable` property", function() {
810+
var meta = {
811+
docs: {},
812+
schema: []
813+
};
814+
815+
eslint.on("Program", function(node) {
816+
eslint.report("test-rule", 2, node, "hello world", [], { range: [1, 1], text: "" }, meta);
817+
});
818+
819+
assert.throws(function() {
820+
eslint.verify("0", config, "", true);
821+
}, /Fixable rules should export a `meta\.fixable` property.$/);
822+
});
823+
824+
it("should not throw an error if fix is passed and no metadata is passed", function() {
825+
eslint.on("Program", function(node) {
826+
eslint.report("test-rule", 2, node, "hello world", [], { range: [1, 1], text: "" });
827+
});
828+
829+
assert.doesNotThrow(function() {
830+
eslint.verify("0", config, "", true);
831+
});
832+
});
833+
809834
it("should correctly parse a message with object keys as numbers", function() {
810835
eslint.on("Program", function(node) {
811836
eslint.report("test-rule", 2, node, "my message {{name}}{{0}}", {0: "!", name: "testing"});

0 commit comments

Comments
 (0)