-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bbc96b6
docs: add `no-inline-assertions` rule example
JLHwung 1ecdbdb
docs: add more invalid examples
JLHwung c604850
feat: `no-inline-assertions` rule
JLHwung 6d79c90
refactor: make xo happy
JLHwung 8e14e81
docs: add rule link to readme
JLHwung 8d36135
feat: add no-inline-assertions to recommended config
JLHwung 35c1665
fix: missing trailing period
JLHwung 75cfe5b
docs: remove dummy link
JLHwung c85588b
docs: align title
JLHwung fab3dc8
feat: ignore BlockStatement
JLHwung b44c571
docs: update fail examples per new behavior
JLHwung f23fd3b
Update no-inline-assertions.js
sindresorhus a7f78b3
Update docs/rules/no-inline-assertions.md
JLHwung b07ccb5
docs: fix writing issues
JLHwung 69144d8
feat: add fixer to no-inline-assertions
JLHwung aa7752e
Update no-inline-assertions.md
sindresorhus d2f49a4
docs: no-inline-assertions is fixable
JLHwung 346145b
Update no-inline-assertions.md
sindresorhus a5377cd
Update readme.md
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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())});' | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.