-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-inline-assertions
rule (#262)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
7ef827e
commit b829dbc
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Ensure assertions are not called from inline arrow functions | ||
|
||
The test implementation should not purely consist of an inline assertion as assertions do not return a value and having them inline also makes the tests less readable. | ||
|
||
This rule is fixable. It will wrap the assertion in braces `{}`. It will not do any whitespace or style changes. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => t.true(fn())); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => { | ||
t.true(fn()); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
const {visitIf} = require('enhance-visitors'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
const util = require('../util'); | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
return ava.merge({ | ||
CallExpression: visitIf([ | ||
ava.isInTestFile, | ||
ava.isTestNode | ||
])(node => { | ||
const functionArgIndex = node.arguments.length - 1; | ||
if (functionArgIndex > 1) { | ||
return; | ||
} | ||
|
||
const functionArg = node.arguments[functionArgIndex]; | ||
|
||
if (!util.isFunctionExpression(functionArg)) { | ||
return; | ||
} | ||
|
||
const {body} = functionArg; | ||
if (body.type === 'CallExpression') { | ||
context.report({ | ||
node, | ||
message: 'The test implementation should not be an inline arrow function.', | ||
fix: fixer => [fixer.insertTextBefore(body, '{'), fixer.insertTextAfter(body, '}')] | ||
}); | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: util.getDocsUrl(__filename) | ||
}, | ||
type: 'suggestion', | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/no-inline-assertions'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [{ruleId: 'no-inline-assertions'}]; | ||
const header = 'const test = require(\'ava\');\n'; | ||
|
||
ruleTester.run('no-todo-test', rule, { | ||
valid: [ | ||
// Shouldn't be triggered as the test implementation is not an inline arrow function | ||
header + 'test("my test name", t => {\n t.true(fn()); \n});', | ||
header + 'test("my test name", function (t) { foo(); });', | ||
// Shouldn't be triggered since test body is empty | ||
header + 'test("my test name", () => {});', | ||
header + 'test("my test name", t => {});', | ||
// Shouldn't be triggered since test body is ill-defined | ||
header + 'test("my test name", t => "foo");', | ||
// Shouldn't be triggered since it's not a test file | ||
'test.todo("my test name");', | ||
// Shouldn't be triggered since the signature is incorrect | ||
header + 'test.todo("my test name", "bar");', | ||
header + 'test.todo("my test name", undefined, t => {})' | ||
], | ||
invalid: [ | ||
{ | ||
code: header + 'test("my test name", t => t.skip());', | ||
errors, | ||
output: header + 'test("my test name", t => {t.skip()});' | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => t.true(fn()));', | ||
errors, | ||
output: header + 'test("my test name", t => {t.true(fn())});' | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => \n t.true(fn()));', | ||
errors, | ||
output: header + 'test("my test name", t => \n {t.true(fn())});' | ||
} | ||
] | ||
}); |