From b6677cbc6fe0a540ba3f2dfcc20d9270ed123ac1 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 12 Nov 2018 23:21:15 +0900 Subject: [PATCH] Change to use multiple-autofixes. https://eslint.org/blog/2017/06/eslint-v4.1.0-released#applying-multiple-autofixes-simultaneously --- lib/rules/attributes-order.js | 19 ++----------------- .../component-name-in-template-casing.js | 12 ++++-------- lib/rules/no-shared-component-data.js | 14 ++++---------- 3 files changed, 10 insertions(+), 35 deletions(-) diff --git a/lib/rules/attributes-order.js b/lib/rules/attributes-order.js index 37b8b39fd..c1a4647c3 100644 --- a/lib/rules/attributes-order.js +++ b/lib/rules/attributes-order.js @@ -77,25 +77,10 @@ function create (context) { const attributes = node.parent.attributes const shiftAttrs = attributes.slice(attributes.indexOf(previousNode), attributes.indexOf(node) + 1) - // If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by: - // return shiftAttrs.map((attr, i) => { - // const text = attr === previousNode ? sourceCode.getText(node) : sourceCode.getText(shiftAttrs[i - 1]) - // return fixer.replaceText(attr, text) - // }) - const replaceDataList = shiftAttrs.map((attr, i) => { + return shiftAttrs.map((attr, i) => { const text = attr === previousNode ? sourceCode.getText(node) : sourceCode.getText(shiftAttrs[i - 1]) - return { - range: attr.range, - text - } + return fixer.replaceText(attr, text) }) - const replaceRange = [previousNode.range[0], node.range[1]] - let text = sourceCode.text.slice(replaceRange[0], replaceRange[1]) - replaceDataList.reverse().forEach((data) => { - const textRange = data.range.map(r => r - replaceRange[0]) - text = text.slice(0, textRange[0]) + data.text + text.slice(textRange[1], text.length) - }) - return fixer.replaceTextRange(replaceRange, text) } }) } diff --git a/lib/rules/component-name-in-template-casing.js b/lib/rules/component-name-in-template-casing.js index 524017a67..bd3ef40d3 100644 --- a/lib/rules/component-name-in-template-casing.js +++ b/lib/rules/component-name-in-template-casing.js @@ -51,7 +51,6 @@ module.exports = { const caseType = allowedCaseOptions.indexOf(caseOption) !== -1 ? caseOption : defaultCase const ignores = options.ignores || [] const tokens = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() - const sourceCode = context.getSourceCode() let hasInvalidEOF = false @@ -88,13 +87,10 @@ module.exports = { return fixer.replaceText(open, `<${casingName}`) } const endTagOpen = tokens.getFirstToken(endTag) - // If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by: - // return [ - // fixer.replaceText(open, `<${casingName}`), - // fixer.replaceText(endTagOpen, `4.1.0`, this code can be replaced by: - // return [ - // fixer.insertTextBefore(tokens.first, 'function() {\nreturn '), - // fixer.insertTextAfter(tokens.last, ';\n}') - // ] - // See: https://eslint.org/blog/2017/06/eslint-v4.1.0-released#applying-multiple-autofixes-simultaneously - const range = [tokens.first.range[0], tokens.last.range[1]] - const valueText = sourceCode.text.slice(range[0], range[1]) - const replacement = `function() {\nreturn ${valueText};\n}` - return fixer.replaceTextRange(range, replacement) + return [ + fixer.insertTextBefore(tokens.first, 'function() {\nreturn '), + fixer.insertTextAfter(tokens.last, ';\n}') + ] } }) })