Skip to content

Commit

Permalink
fix(rule): Add required meta.fixable attribute (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou authored Nov 7, 2021
1 parent ee078a6 commit 3a61ff2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 76 deletions.
81 changes: 43 additions & 38 deletions lib/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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--
}
}
}
}
Expand Down
47 changes: 26 additions & 21 deletions lib/rules/new-line-between-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
}
}
}
Expand Down
40 changes: 23 additions & 17 deletions lib/rules/no-promise-without-done-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down

0 comments on commit 3a61ff2

Please sign in to comment.