Skip to content

Commit 740187d

Browse files
committed
feat(rule): support fixer
textlint-rule-eslint can fix the code now,
1 parent 2b30200 commit 740187d

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

src/textlint-rule-eslint.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const defaultOptions = {
88
// recognize lang of CodeBlock
99
"langs": ["js", "javascript", "node", "jsx"]
1010
};
11-
module.exports = function(context, options) {
12-
const {Syntax, RuleError, report, getSource} = context;
11+
const reporter = (context, options) => {
12+
const {Syntax, RuleError, report, fixer, getSource} = context;
1313
if (!options.configFile) {
1414
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
1515
}
@@ -27,6 +27,7 @@ module.exports = function(context, options) {
2727
if (availableLang.indexOf(node.lang) === -1) {
2828
return;
2929
}
30+
const raw = getSource(node);
3031
const code = node.value;
3132
const resultLinting = engine.executeOnText(code, extname);
3233
if (resultLinting.errorCount === 0) {
@@ -43,13 +44,29 @@ module.exports = function(context, options) {
4344
4445
ESLint message line and column start with 1
4546
*/
46-
const error = new RuleError(`${message.ruleId}: ${message.message}`, {
47-
line: message.line,
48-
column: message.column - 1
49-
});
50-
report(node, error);
47+
if (message.fix) {
48+
const paddingIndex = raw.indexOf(code);
49+
const fixedRange = message.fix.range;
50+
const fixedText = message.fix.text;
51+
const fixedWithPadding = [fixedRange[0] + paddingIndex, fixedRange[1] + paddingIndex];
52+
report(node, new RuleError(`${message.ruleId}: ${message.message}`, {
53+
line: message.line,
54+
column: message.column - 1,
55+
fix: fixer.replaceTextRange(fixedWithPadding, fixedText)
56+
}));
57+
} else {
58+
report(node, new RuleError(`${message.ruleId}: ${message.message}`, {
59+
line: message.line,
60+
column: message.column - 1
61+
}));
62+
}
63+
5164
});
5265
});
5366
}
5467
}
68+
};
69+
module.exports = {
70+
linter: reporter,
71+
fixer: reporter
5572
};

test/textlint-rule-eslint-test.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ tester.run("textlint-rule-eslint", rule, {
1919
text: "```js\n" +
2020
WrongCode1 + "\n" +
2121
"```",
22-
errors: [{
23-
message: "semi: Missing semicolon.",
24-
line: 2,
25-
column: 10
26-
}],
22+
output: "```js\n" +
23+
WrongCode1 + ";\n" +
24+
"```",
25+
errors: [
26+
{
27+
message: "semi: Missing semicolon.",
28+
line: 2,
29+
column: 10
30+
}
31+
],
2732
options: {
2833
configFile: __dirname + "/fixtures/style.eslintconfig.js"
2934
}
@@ -37,15 +42,24 @@ tester.run("textlint-rule-eslint", rule, {
3742
"```js\n" +
3843
WrongCode2 + "\n" +
3944
"```",
40-
errors: [{
41-
message: "semi: Missing semicolon.",
42-
line: 2,
43-
column: 10
44-
}, {
45-
message: "semi: Missing semicolon.",
46-
line: 6,
47-
column: 21
48-
}],
45+
output: "```js\n" +
46+
WrongCode1 + ";\n" +
47+
"```\n" +
48+
"This is text.\n" +
49+
"```js\n" +
50+
WrongCode2 + ";\n" +
51+
"```",
52+
errors: [
53+
{
54+
message: "semi: Missing semicolon.",
55+
line: 2,
56+
column: 10
57+
}, {
58+
message: "semi: Missing semicolon.",
59+
line: 6,
60+
column: 21
61+
}
62+
],
4963
options: {
5064
configFile: __dirname + "/fixtures/style.eslintconfig.js"
5165
}

0 commit comments

Comments
 (0)