Skip to content

Commit

Permalink
feat(rule): support fixer
Browse files Browse the repository at this point in the history
textlint-rule-eslint can fix the code now,
  • Loading branch information
azu committed Jul 4, 2016
1 parent 2b30200 commit 740187d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
31 changes: 24 additions & 7 deletions src/textlint-rule-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const defaultOptions = {
// recognize lang of CodeBlock
"langs": ["js", "javascript", "node", "jsx"]
};
module.exports = function(context, options) {
const {Syntax, RuleError, report, getSource} = context;
const reporter = (context, options) => {
const {Syntax, RuleError, report, fixer, getSource} = context;
if (!options.configFile) {
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
}
Expand All @@ -27,6 +27,7 @@ module.exports = function(context, options) {
if (availableLang.indexOf(node.lang) === -1) {
return;
}
const raw = getSource(node);
const code = node.value;
const resultLinting = engine.executeOnText(code, extname);
if (resultLinting.errorCount === 0) {
Expand All @@ -43,13 +44,29 @@ module.exports = function(context, options) {
ESLint message line and column start with 1
*/
const error = new RuleError(`${message.ruleId}: ${message.message}`, {
line: message.line,
column: message.column - 1
});
report(node, error);
if (message.fix) {
const paddingIndex = raw.indexOf(code);
const fixedRange = message.fix.range;
const fixedText = message.fix.text;
const fixedWithPadding = [fixedRange[0] + paddingIndex, fixedRange[1] + paddingIndex];
report(node, new RuleError(`${message.ruleId}: ${message.message}`, {
line: message.line,
column: message.column - 1,
fix: fixer.replaceTextRange(fixedWithPadding, fixedText)
}));
} else {
report(node, new RuleError(`${message.ruleId}: ${message.message}`, {
line: message.line,
column: message.column - 1
}));
}

});
});
}
}
};
module.exports = {
linter: reporter,
fixer: reporter
};
42 changes: 28 additions & 14 deletions test/textlint-rule-eslint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ tester.run("textlint-rule-eslint", rule, {
text: "```js\n" +
WrongCode1 + "\n" +
"```",
errors: [{
message: "semi: Missing semicolon.",
line: 2,
column: 10
}],
output: "```js\n" +
WrongCode1 + ";\n" +
"```",
errors: [
{
message: "semi: Missing semicolon.",
line: 2,
column: 10
}
],
options: {
configFile: __dirname + "/fixtures/style.eslintconfig.js"
}
Expand All @@ -37,15 +42,24 @@ tester.run("textlint-rule-eslint", rule, {
"```js\n" +
WrongCode2 + "\n" +
"```",
errors: [{
message: "semi: Missing semicolon.",
line: 2,
column: 10
}, {
message: "semi: Missing semicolon.",
line: 6,
column: 21
}],
output: "```js\n" +
WrongCode1 + ";\n" +
"```\n" +
"This is text.\n" +
"```js\n" +
WrongCode2 + ";\n" +
"```",
errors: [
{
message: "semi: Missing semicolon.",
line: 2,
column: 10
}, {
message: "semi: Missing semicolon.",
line: 6,
column: 21
}
],
options: {
configFile: __dirname + "/fixtures/style.eslintconfig.js"
}
Expand Down

0 comments on commit 740187d

Please sign in to comment.