-
Notifications
You must be signed in to change notification settings - Fork 49
Add no-inline-assertions
rule
#262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
bbc96b6
1ecdbdb
c604850
6d79c90
8e14e81
8d36135
35c1665
75cfe5b
c85588b
fab3dc8
b44c571
f23fd3b
a7f78b3
b07ccb5
69144d8
aa7752e
d2f49a4
346145b
a5377cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Ensure assertions are not called from an inline function | ||
|
||
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/no-inline-assertions.md) | ||
|
||
Prevent assertions being called from an inline function, to make it clear that it does not return. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is repeating the title too much. It should also use the wording "test implementation". |
||
|
||
## Fail | ||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => t.true(fn())); | ||
``` | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => { t.true(fn()) }); | ||
``` | ||
|
||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => | ||
t.true(fn()) | ||
); | ||
``` | ||
|
||
## Pass | ||
```js | ||
import test from 'ava'; | ||
|
||
test('foo', t => { | ||
t.true(fn()) | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||
'use strict'; | ||||||
const {visitIf} = require('enhance-visitors'); | ||||||
const createAvaRule = require('../create-ava-rule'); | ||||||
const util = require('../util'); | ||||||
|
||||||
function report({node, context}) { | ||||||
context.report({ | ||||||
node, | ||||||
message: 'Assertions should not be called from an inline function.' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be more correct:
Suggested change
But would be nice to find a way to shorten the above slightly. |
||||||
}); | ||||||
} | ||||||
|
||||||
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') { | ||||||
report({node, context}); | ||||||
return; | ||||||
} | ||||||
|
||||||
if (body.type === 'BlockStatement') { | ||||||
if (body.loc.start.line !== body.loc.end.line) { | ||||||
return; | ||||||
} | ||||||
|
||||||
if (body.body.length === 0) { | ||||||
return; | ||||||
} | ||||||
|
||||||
report({node, context}); | ||||||
} | ||||||
}) | ||||||
}); | ||||||
}; | ||||||
|
||||||
module.exports = { | ||||||
create, | ||||||
meta: { | ||||||
docs: { | ||||||
url: util.getDocsUrl(__filename) | ||||||
}, | ||||||
type: 'suggestion' | ||||||
} | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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: [ | ||
header + 'test("my test name", t => {\n t.true(fn()); \n});', | ||
// 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 | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => t.skip());', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => t.true(fn()));', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => \n t.true(fn()));', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => { t.true(fn()) });', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test("my test name", function(t) { foo(); });', | ||
errors | ||
}, | ||
{ | ||
code: header + 'test("my test name", t => { return t.true(fn()) });', | ||
errors | ||
} | ||
] | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.