Skip to content

Commit

Permalink
fix: getting rid of newLineBeforeExpect assumptions breaking the lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Suvorova authored and Nicolas Fernandez committed Jun 23, 2017
1 parent ddde071 commit ea967b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
31 changes: 10 additions & 21 deletions lib/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,27 @@ var hasPaddingBetweenTokens = require('../helpers/hasPaddingBetweenTokens')
var blockRegexp = /^((f|x)?(it|describe))$/

module.exports = function (context) {
var jasmineBlocks = []
var suiteDepth = 0
return {
CallExpression: function (node) {
if (blockRegexp.test(node.callee.name)) {
jasmineBlocks.push(node)
} else if (node.callee.name === 'expect' && jasmineBlocks.length > 0) {
const parent = jasmineBlocks[jasmineBlocks.length - 1]
if (!isFirstNodeInSuiteOrTest(node, parent)) {
const tokenBeforeExpect = context.getSourceCode().getTokenBefore(node)
if (!hasPaddingBetweenTokens(tokenBeforeExpect, node)) {
suiteDepth++
} else if (node.callee.name === 'expect' && suiteDepth > 0) {
const prevToken = context.getSourceCode().getTokenBefore(node)
if (prevToken) {
if (prevToken.type === 'Punctuator' && prevToken.value === '{') {
return
}
if (!hasPaddingBetweenTokens(prevToken, node)) {
context.report(node, 'No new line before expect')
}
}
}
},
'CallExpression:exit': function (node) {
if (blockRegexp.test(node.callee.name)) {
jasmineBlocks.pop(node)
suiteDepth--
}
}
}
}

/**
* Checks whether node is the first node inside a suite or a test
* @param {ASTNode} node - node to check
* @returns {boolean} Whether or not the node is the first node inside a suite or a test
* @author Diana Suvorova
*/

function isFirstNodeInSuiteOrTest (node, parent) {
const parentBody = parent.arguments && parent.arguments[1] && parent.arguments[1].body && parent.arguments[1].body.body
const first = Array.isArray(parentBody) ? parentBody[0] : parentBody
return first.start === node.start
}
29 changes: 27 additions & 2 deletions test/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,21 @@ eslintTester.run('new line before expect', rule, {
'});'
]),
linesToCode([
' notJasmineTestSuite()',
' expect(a)'
'notJasmineTestSuite()',
'expect(a)'
]),
linesToCode([
'it("", helper(function() {',
' expect(1).toEqual(1);',
'}));'
]),
linesToCode([
'it("", helper(function() {',
' ',
' expect(1).toEqual(1);',
'}));'
])

],
invalid: [
{
Expand All @@ -60,6 +72,19 @@ eslintTester.run('new line before expect', rule, {
message: 'No new line before expect'
}
]
},
{
code: linesToCode([
'it("", helper(function() {',
' var a = 1',
' expect(a).toEqual(1);',
'}));'
]),
errors: [
{
message: 'No new line before expect'
}
]
}
]
})

0 comments on commit ea967b0

Please sign in to comment.