Skip to content

Commit c604850

Browse files
committed
feat: no-inline-assertions rule
1 parent 1ecdbdb commit c604850

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

rules/no-inline-assertions.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
const {visitIf} = require('enhance-visitors');
3+
const createAvaRule = require('../create-ava-rule');
4+
const util = require('../util');
5+
6+
function report({ node, context }) {
7+
context.report({
8+
node: node,
9+
message: 'Assertions should not be called from an inline function'
10+
})
11+
}
12+
const create = context => {
13+
const ava = createAvaRule();
14+
15+
return ava.merge({
16+
CallExpression: visitIf([
17+
ava.isInTestFile,
18+
ava.isTestNode
19+
])(node => {
20+
const functionArgIndex = node.arguments.length - 1;
21+
if (functionArgIndex > 1) {
22+
return;
23+
}
24+
25+
const functionArg = node.arguments[functionArgIndex];
26+
27+
if (!util.isFunctionExpression(functionArg)) {
28+
return;
29+
}
30+
31+
const { body } = functionArg;
32+
if (body.type === "CallExpression") {
33+
report({ node, context });
34+
return;
35+
}
36+
37+
if (body.type === "BlockStatement") {
38+
if (body.loc.start.line !== body.loc.end.line) {
39+
return;
40+
}
41+
42+
if (!body.body.length) {
43+
return;
44+
}
45+
46+
report({ node, context });
47+
}
48+
49+
})
50+
});
51+
};
52+
53+
module.exports = {
54+
create,
55+
meta: {
56+
docs: {
57+
url: util.getDocsUrl(__filename)
58+
},
59+
type: 'suggestion'
60+
}
61+
};

test/no-inline-assertions.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import test from 'ava';
2+
import avaRuleTester from 'eslint-ava-rule-tester';
3+
import rule from '../rules/no-inline-assertions';
4+
5+
const ruleTester = avaRuleTester(test, {
6+
env: {
7+
es6: true
8+
}
9+
});
10+
11+
const errors = [{ruleId: 'no-inline-assertions'}];
12+
const header = 'const test = require(\'ava\');\n';
13+
14+
ruleTester.run('no-todo-test', rule, {
15+
valid: [
16+
header + 'test("my test name", t => {\n t.true(fn()); \n});',
17+
// Shouldn't be triggered since test body is empty
18+
header + 'test("my test name", () => {});',
19+
header + 'test("my test name", (t) => {});',
20+
// Shouldn't be triggered since test body is ill-defined
21+
header + 'test("my test name", (t) => "foo");',
22+
// Shouldn't be triggered since it's not a test file
23+
'test.todo("my test name");',
24+
// Shouldn't be triggered since the signature is incorrect
25+
header + 'test.todo("my test name", "bar");',
26+
header + 'test.todo("my test name", undefined, t => {})'
27+
],
28+
invalid: [
29+
{
30+
code: header + 'test("my test name", t => { t.skip(); });',
31+
errors
32+
},
33+
{
34+
code: header + 'test("my test name", t => t.skip());',
35+
errors
36+
},
37+
{
38+
code: header + 'test("my test name", t => t.true(fn()));',
39+
errors
40+
},
41+
{
42+
code: header + 'test("my test name", t => \n t.true(fn()));',
43+
errors
44+
},
45+
{
46+
code: header + 'test("my test name", t => { t.true(fn()) });',
47+
errors
48+
},
49+
{
50+
code: header + 'test("my test name", function(t) { foo(); });',
51+
errors
52+
},
53+
{
54+
code: header + 'test("my test name", t => { return t.true(fn()) });',
55+
errors
56+
}
57+
]
58+
});

0 commit comments

Comments
 (0)