From 3a61ff2ee9d82815d3f03ae61df8c75300f64838 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sun, 7 Nov 2021 04:41:39 +0100 Subject: [PATCH] fix(rule): Add required meta.fixable attribute (#341) See migration guide: https://eslint.org/docs/8.0.0/user-guide/migrating-to-8.0.0#fixes --- lib/rules/new-line-before-expect.js | 81 ++++++++++++---------- lib/rules/new-line-between-declarations.js | 47 +++++++------ lib/rules/no-promise-without-done-fail.js | 40 ++++++----- 3 files changed, 92 insertions(+), 76 deletions(-) diff --git a/lib/rules/new-line-before-expect.js b/lib/rules/new-line-before-expect.js index a90af26..698d159 100644 --- a/lib/rules/new-line-before-expect.js +++ b/lib/rules/new-line-before-expect.js @@ -11,48 +11,53 @@ var getLineIndentation = require('../helpers/getLineIndentation') var blockRegexp = /^((f|x)?(it|describe))$/ -module.exports = function (context) { - var suiteDepth = 0 - var lastExpectNode - return { - CallExpression: function (node) { - if (blockRegexp.test(node.callee.name)) { - lastExpectNode = null - suiteDepth++ - } else if (node.callee.name === 'expect' && suiteDepth > 0) { - if (lastExpectNode && linesDelta(node, lastExpectNode) === 1) { - lastExpectNode = node - return - } - lastExpectNode = node - var sourceCode = context.getSourceCode() - let prevToken = sourceCode.getTokenBefore(node) - if (prevToken.value === 'await' || prevToken.value === 'return') { - node = prevToken - prevToken = sourceCode.getTokenBefore(prevToken) - } - if (prevToken) { - if (prevToken.type === 'Punctuator' && prevToken.value === '{') { +module.exports = { + meta: { + fixable: 'whitespace' + }, + create: function (context) { + var suiteDepth = 0 + var lastExpectNode + return { + CallExpression: function (node) { + if (blockRegexp.test(node.callee.name)) { + lastExpectNode = null + suiteDepth++ + } else if (node.callee.name === 'expect' && suiteDepth > 0) { + if (lastExpectNode && linesDelta(node, lastExpectNode) === 1) { + lastExpectNode = node return } - if (!hasPaddingBetweenTokens(prevToken, node) && isFirstExpectOnLine(node, sourceCode)) { - context.report({ - fix (fixer) { - return fixer.replaceTextRange( - [prevToken.range[1], node.range[1]], - ['\n\n', withIndentation(node, sourceCode)].join('') - ) - }, - message: 'No new line before expect', - node - }) + lastExpectNode = node + var sourceCode = context.getSourceCode() + let prevToken = sourceCode.getTokenBefore(node) + if (prevToken.value === 'await' || prevToken.value === 'return') { + node = prevToken + prevToken = sourceCode.getTokenBefore(prevToken) + } + if (prevToken) { + if (prevToken.type === 'Punctuator' && prevToken.value === '{') { + return + } + if (!hasPaddingBetweenTokens(prevToken, node) && isFirstExpectOnLine(node, sourceCode)) { + context.report({ + fix (fixer) { + return fixer.replaceTextRange( + [prevToken.range[1], node.range[1]], + ['\n\n', withIndentation(node, sourceCode)].join('') + ) + }, + message: 'No new line before expect', + node + }) + } } } - } - }, - 'CallExpression:exit': function (node) { - if (blockRegexp.test(node.callee.name)) { - suiteDepth-- + }, + 'CallExpression:exit': function (node) { + if (blockRegexp.test(node.callee.name)) { + suiteDepth-- + } } } } diff --git a/lib/rules/new-line-between-declarations.js b/lib/rules/new-line-between-declarations.js index e88c753..b668466 100644 --- a/lib/rules/new-line-between-declarations.js +++ b/lib/rules/new-line-between-declarations.js @@ -9,27 +9,32 @@ var hasPaddingBetweenTokens = require('../helpers/hasPaddingBetweenTokens') var suiteRegexp = /^(f|x)?describe$/ -module.exports = function (context) { - return { - CallExpression: function (node) { - var declWithoutPadding = null - if (suiteRegexp.test(node.callee.name)) { - var declarations = getDescribeDeclarationsContent(node) - declarations.forEach((decl, i) => { - var next = declarations[i + 1] - if (next && !hasPaddingBetweenTokens(decl, next)) { - declWithoutPadding = decl - } - }) - } - if (declWithoutPadding) { - context.report({ - fix (fixer) { - return fixer.insertTextAfter(declWithoutPadding, '\n') - }, - message: 'No new line between declarations', - node - }) +module.exports = { + meta: { + fixable: 'whitespace' + }, + create: function (context) { + return { + CallExpression: function (node) { + var declWithoutPadding = null + if (suiteRegexp.test(node.callee.name)) { + var declarations = getDescribeDeclarationsContent(node) + declarations.forEach((decl, i) => { + var next = declarations[i + 1] + if (next && !hasPaddingBetweenTokens(decl, next)) { + declWithoutPadding = decl + } + }) + } + if (declWithoutPadding) { + context.report({ + fix (fixer) { + return fixer.insertTextAfter(declWithoutPadding, '\n') + }, + message: 'No new line between declarations', + node + }) + } } } } diff --git a/lib/rules/no-promise-without-done-fail.js b/lib/rules/no-promise-without-done-fail.js index fa196c4..1642d9f 100644 --- a/lib/rules/no-promise-without-done-fail.js +++ b/lib/rules/no-promise-without-done-fail.js @@ -65,24 +65,30 @@ function getDoneParamName (node) { * @fileoverview Disallow promises without done.fail * @author Yaara Cohen */ -module.exports = function (context) { - var isInsideSpec = false - var asyncParam - return { - CallExpression: function (node) { - if (isSpec(node)) { - isInsideSpec = true - asyncParam = getDoneParamName(node) - } +module.exports = { + meta: { + fixable: 'code' + }, - if (isInsideSpec && asyncParam && isPromiseThenWithoutCatch(node, asyncParam)) { - context.report(genReport(node, context)) - } - }, - 'CallExpression:exit': function (node) { - if (isSpec(node)) { - isInsideSpec = false - asyncParam = undefined + create: function (context) { + var isInsideSpec = false + var asyncParam + return { + CallExpression: function (node) { + if (isSpec(node)) { + isInsideSpec = true + asyncParam = getDoneParamName(node) + } + + if (isInsideSpec && asyncParam && isPromiseThenWithoutCatch(node, asyncParam)) { + context.report(genReport(node, context)) + } + }, + 'CallExpression:exit': function (node) { + if (isSpec(node)) { + isInsideSpec = false + asyncParam = undefined + } } } }