Skip to content

Commit fcd878c

Browse files
committed
feat(rule): --fix support
1 parent 3efea14 commit fcd878c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/textlint-rule-report-node-types.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ const defaultOptions = {
77
*/
88
nodeTypes: []
99
};
10-
module.exports = function (context, options = defaultOptions) {
11-
const {report, RuleError} = context;
10+
var reporter = function (context, options = defaultOptions) {
11+
const {report, fixer, RuleError} = context;
1212
const nodeTypes = options.nodeTypes || defaultOptions.nodeTypes;
1313
assert(Array.isArray(nodeTypes) && nodeTypes.length > 0, `You forgot setting to options like { "nodeTypes" : ["Str"] }`);
1414
const rule = {};
1515
nodeTypes.forEach(type => {
1616
rule[type] = (node) => {
17-
report(node, new RuleError(node.type));
17+
report(node, new RuleError(node.type, {
18+
fix: fixer.replaceText(node, node.type)
19+
}));
1820
}
1921
});
2022
return rule;
23+
};
24+
module.exports = {
25+
linter: reporter,
26+
fixer: reporter
2127
};

test/textlint-rule-ignore-node-types-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ describe("textlint-rule-report-node-types", function () {
2121
});
2222
});
2323
});
24+
context("when --fix report type", function () {
25+
it("should fix with node type", function () {
26+
const textlint = new TextLintCore();
27+
textlint.setupRules({
28+
report: reportRule
29+
}, {
30+
report: {
31+
nodeTypes: [TextLintNodeType.Str]
32+
}
33+
});
34+
return textlint.fixText("text").then(({messages}) => {
35+
assert.equal(messages.length, 1);
36+
assert.equal(messages[0].message, TextLintNodeType.Str);
37+
assert.deepEqual(messages[0].fix.range, [0, 4]);
38+
assert.equal(messages[0].fix.text, "Str");
39+
});
40+
});
41+
});
2442
context("when report multiple nodes", function () {
2543
it("should report multiple messages", function () {
2644
const textlint = new TextLintCore();

0 commit comments

Comments
 (0)