diff --git a/lib/rules/new-line-before-expect.js b/lib/rules/new-line-before-expect.js index 357de43..eac8fa9 100644 --- a/lib/rules/new-line-before-expect.js +++ b/lib/rules/new-line-before-expect.js @@ -10,16 +10,18 @@ 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') } } @@ -27,21 +29,8 @@ module.exports = function (context) { }, '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 -} diff --git a/test/rules/new-line-before-expect.js b/test/rules/new-line-before-expect.js index 1f9b9dc..3ff3859 100644 --- a/test/rules/new-line-before-expect.js +++ b/test/rules/new-line-before-expect.js @@ -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: [ { @@ -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' + } + ] } ] })