Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rule): Add required meta.fixable attribute #341

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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