Skip to content

Commit

Permalink
fix(rule): Support await/return expect() for new-line-before-expect r…
Browse files Browse the repository at this point in the history
…ule (#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 <bgrift@amazon.com>
  • Loading branch information
bmv437 and bmv437 committed Aug 24, 2021
1 parent 180e3b9 commit ee078a6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
20 changes: 20 additions & 0 deletions docs/rules/new-line-before-expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 58 additions & 2 deletions test/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -69,6 +80,11 @@ eslintTester.run('new line before expect', rule, {
' ',
' expect(1).toEqual(1);',
'}));'
]),
linesToCode([
'it("", function() {',
' return expect(1).toEqual(1);',
'});'
])

],
Expand Down Expand Up @@ -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() {',
Expand Down

0 comments on commit ee078a6

Please sign in to comment.