Skip to content

Commit

Permalink
Chore: Add some tests for suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
wdoug committed Oct 7, 2019
1 parent 00b0164 commit 4bc32da
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/linter/report-translator.js
Expand Up @@ -195,12 +195,10 @@ function mapSuggestions(descriptor, sourceCode) {
return [];
}

return descriptor.suggest.map(suggestInfo => Object.assign(
{},
suggestInfo,
{ fix: normalizeFixes(suggestInfo, sourceCode) }
));

return descriptor.suggest.map(suggestInfo => ({
desc: suggestInfo.desc,
fix: normalizeFixes(suggestInfo, sourceCode)
}));
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -4299,6 +4299,42 @@ describe("Linter", () => {
});
});

describe("suggestions", () => {
it("provides suggestion information for tools to use", () => {
linter.defineRule("rule-with-suggestions", context => ({
Program(node) {
context.report({
node,
message: "Incorrect spacing",
suggest: [{
desc: "Insert space at the beginning",
fix: fixer => fixer.insertTextBefore(node, " ")
}, {
desc: "Insert space at the end",
fix: fixer => fixer.insertTextAfter(node, " ")
}]
});
}
}));

const messages = linter.verify("var a = 1;", { rules: { "rule-with-suggestions": "error" } });

assert.deepStrictEqual(messages[0].suggestions, [{
desc: "Insert space at the beginning",
fix: {
range: [0, 0],
text: " "
}
}, {
desc: "Insert space at the end",
fix: {
range: [10, 10],
text: " "
}
}]);
});
});

describe("mutability", () => {
let linter1 = null;
let linter2 = null;
Expand Down
67 changes: 67 additions & 0 deletions tests/lib/linter/report-translator.js
Expand Up @@ -367,6 +367,73 @@ describe("createReportTranslator", () => {
});
});

describe("suggestions", () => {
it("should support multiple suggestions.", () => {
const reportDescriptor = {
node,
loc: location,
message,
suggest: [{
desc: "A first suggestion for the issue",
fix: () => [{ range: [1, 2], text: "foo" }]
}, {
desc: "A different suggestion for the issue",
fix: () => [{ range: [1, 3], text: "foobar" }]
}]
};

assert.deepStrictEqual(
translateReport(reportDescriptor),
{
ruleId: "foo-rule",
severity: 2,
message: "foo",
line: 2,
column: 1,
nodeType: "ExpressionStatement",
suggestions: [{
desc: "A first suggestion for the issue",
fix: { range: [1, 2], text: "foo" }
}, {
desc: "A different suggestion for the issue",
fix: { range: [1, 3], text: "foobar" }
}]
}
);
});

it("should merge suggestion fixes to one if 'fix' function returns an array of fixes.", () => {
const reportDescriptor = {
node,
loc: location,
message,
suggest: [{
desc: "A suggestion for the issue",
fix: () => [{ range: [1, 2], text: "foo" }, { range: [4, 5], text: "bar" }]
}]
};

assert.deepStrictEqual(
translateReport(reportDescriptor),
{
ruleId: "foo-rule",
severity: 2,
message: "foo",
line: 2,
column: 1,
nodeType: "ExpressionStatement",
suggestions: [{
desc: "A suggestion for the issue",
fix: {
range: [1, 5],
text: "fooo\nbar"
}
}]
}
);
});
});

describe("message interpolation", () => {
it("should correctly parse a message when being passed all options in an old-style report", () => {
assert.deepStrictEqual(
Expand Down

0 comments on commit 4bc32da

Please sign in to comment.