From ee078a69f915e46a790b6118b384e0b94f1e35ec Mon Sep 17 00:00:00 2001 From: Brandon Vandegrift Date: Tue, 24 Aug 2021 14:55:54 -0400 Subject: [PATCH] fix(rule): Support await/return expect() for new-line-before-expect rule (#259) * fix(rule): Support await/return expect() for new-line-before-expect rule * docs(rule): add await/return examples for new-line-before-expect Co-authored-by: Brandon Vandegrift --- docs/rules/new-line-before-expect.md | 20 ++++++++++ lib/rules/new-line-before-expect.js | 6 ++- test/rules/new-line-before-expect.js | 60 +++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/docs/rules/new-line-before-expect.md b/docs/rules/new-line-before-expect.md index afef706..2ff8e7d 100644 --- a/docs/rules/new-line-before-expect.md +++ b/docs/rules/new-line-before-expect.md @@ -49,6 +49,26 @@ describe("", function() { }); ``` +```js +describe("", function() { + it("", async function() { + var a = 1; + + await expect(a).toBe(1); + }); +}); +``` + +```js +describe("", function() { + it("", function() { + var a = 1; + + return expect(a).toBe(1); + }); +}); +``` + ```js describe("", function() { it("", function() { diff --git a/lib/rules/new-line-before-expect.js b/lib/rules/new-line-before-expect.js index 8ca1162..a90af26 100644 --- a/lib/rules/new-line-before-expect.js +++ b/lib/rules/new-line-before-expect.js @@ -26,7 +26,11 @@ module.exports = function (context) { } lastExpectNode = node var sourceCode = context.getSourceCode() - const prevToken = sourceCode.getTokenBefore(node) + 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 diff --git a/test/rules/new-line-before-expect.js b/test/rules/new-line-before-expect.js index 6e3e8f8..97b30a6 100644 --- a/test/rules/new-line-before-expect.js +++ b/test/rules/new-line-before-expect.js @@ -3,8 +3,10 @@ var rule = require('../../lib/rules/new-line-before-expect') var linesToCode = require('../helpers/lines_to_code') var RuleTester = require('eslint').RuleTester - -var eslintTester = new RuleTester() +const parserOptions = { + ecmaVersion: 8 +} +var eslintTester = new RuleTester({ parserOptions }) eslintTester.run('new line before expect', rule, { valid: [ @@ -40,6 +42,15 @@ eslintTester.run('new line before expect', rule, { ' });', '});' ]), + linesToCode([ + 'describe("", function() {', + ' it("", async function(){', + ' var a = 1', + '', + ' await expect(a).toBe(1)', + ' });', + '});' + ]), linesToCode([ 'it("", helper(function() {', ' var a = 1', @@ -69,6 +80,11 @@ eslintTester.run('new line before expect', rule, { ' ', ' expect(1).toEqual(1);', '}));' + ]), + linesToCode([ + 'it("", function() {', + ' return expect(1).toEqual(1);', + '});' ]) ], @@ -117,6 +133,46 @@ eslintTester.run('new line before expect', rule, { '}));' ]) }, + { + code: linesToCode([ + 'it("", helper(async function() {', + ' var a = 1', + ' await expect(a).toEqual(1);', + '}));' + ]), + errors: [ + { + message: 'No new line before expect' + } + ], + output: linesToCode([ + 'it("", helper(async function() {', + ' var a = 1', + '', + ' await expect(a).toEqual(1);', + '}));' + ]) + }, + { + code: linesToCode([ + 'it("", helper(function() {', + ' var a = 1', + ' return expect(a).toEqual(1);', + '}));' + ]), + errors: [ + { + message: 'No new line before expect' + } + ], + output: linesToCode([ + 'it("", helper(function() {', + ' var a = 1', + '', + ' return expect(a).toEqual(1);', + '}));' + ]) + }, { code: linesToCode([ 'it("", helper(function() {',