From 740187dc4e20a658fb0f3a137f8b122aed87c87f Mon Sep 17 00:00:00 2001 From: azu Date: Mon, 4 Jul 2016 09:16:44 +0900 Subject: [PATCH] feat(rule): support fixer textlint-rule-eslint can fix the code now, --- src/textlint-rule-eslint.js | 31 +++++++++++++++++------ test/textlint-rule-eslint-test.js | 42 ++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/textlint-rule-eslint.js b/src/textlint-rule-eslint.js index 7a041d5..da05926 100644 --- a/src/textlint-rule-eslint.js +++ b/src/textlint-rule-eslint.js @@ -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" }`); } @@ -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) { @@ -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 }; \ No newline at end of file diff --git a/test/textlint-rule-eslint-test.js b/test/textlint-rule-eslint-test.js index 60ee847..3807052 100644 --- a/test/textlint-rule-eslint-test.js +++ b/test/textlint-rule-eslint-test.js @@ -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" } @@ -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" }