Skip to content

Commit 03a7cda

Browse files
with-heartSimenB
authored andcommittedMar 14, 2018
feat(lowercase-name): add 'ignore' option (#92)
Fixes #76
1 parent 80c9b33 commit 03a7cda

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed
 

Diff for: ‎docs/rules/lowercase-name.md

+49
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,52 @@ it('adds 1 + 2 to equal 3', () => {
2121
expect(sum(1, 2)).toBe(3);
2222
});
2323
```
24+
25+
## Options
26+
27+
```json
28+
{
29+
"jest/lowercase-name": [
30+
"error",
31+
{
32+
"ignore": ["describe", "test"]
33+
}
34+
]
35+
}
36+
```
37+
38+
### `ignore`
39+
40+
This array option whitelists function names so that this rule does not report
41+
their usage as being incorrect. There are three possible values:
42+
43+
* `"describe"`
44+
* `"test"`
45+
* `"it"`
46+
47+
By default, none of these options are enabled (the equivalent of
48+
`{ "ignore": [] }`).
49+
50+
Example of **correct** code for the `{ "ignore": ["describe"] }` option:
51+
52+
```js
53+
/* eslint jest/lowercase-name: ["error", { "ignore": ["describe"] }] */
54+
55+
describe('Uppercase description');
56+
```
57+
58+
Example of **correct** code for the `{ "ignore": ["test"] }` option:
59+
60+
```js
61+
/* eslint jest/lowercase-name: ["error", { "ignore": ["test"] }] */
62+
63+
test('Uppercase description');
64+
```
65+
66+
Example of **correct** code for the `{ "ignore": ["it"] }` option:
67+
68+
```js
69+
/* eslint jest/lowercase-name: ["error", { "ignore": ["it"] }] */
70+
71+
it('Uppercase description');
72+
```

Diff for: ‎rules/__tests__/lowercase-name.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ruleTester = new RuleTester({
1111

1212
ruleTester.run('lowercase-name', rule, {
1313
valid: [
14+
'it()',
1415
"it(' ', function () {})",
1516
'it(" ", function () {})',
1617
'it(` `, function () {})',
@@ -20,12 +21,14 @@ ruleTester.run('lowercase-name', rule, {
2021
'it("<Foo/>", function () {})',
2122
'it("123 foo", function () {})',
2223
'it(42, function () {})',
24+
'test()',
2325
"test('foo', function () {})",
2426
'test("foo", function () {})',
2527
'test(`foo`, function () {})',
2628
'test("<Foo/>", function () {})',
2729
'test("123 foo", function () {})',
2830
'test("42", function () {})',
31+
'describe()',
2932
"describe('foo', function () {})",
3033
'describe("foo", function () {})',
3134
'describe(`foo`, function () {})',
@@ -128,3 +131,57 @@ ruleTester.run('lowercase-name', rule, {
128131
},
129132
],
130133
});
134+
135+
ruleTester.run('lowercase-name with ignore=describe', rule, {
136+
valid: [
137+
{
138+
code: "describe('Foo', function () {})",
139+
options: [{ ignore: ['describe'] }],
140+
},
141+
{
142+
code: 'describe("Foo", function () {})',
143+
options: [{ ignore: ['describe'] }],
144+
},
145+
{
146+
code: 'describe(`Foo`, function () {})',
147+
options: [{ ignore: ['describe'] }],
148+
},
149+
],
150+
invalid: [],
151+
});
152+
153+
ruleTester.run('lowercase-name with ignore=test', rule, {
154+
valid: [
155+
{
156+
code: "test('Foo', function () {})",
157+
options: [{ ignore: ['test'] }],
158+
},
159+
{
160+
code: 'test("Foo", function () {})',
161+
options: [{ ignore: ['test'] }],
162+
},
163+
{
164+
code: 'test(`Foo`, function () {})',
165+
options: [{ ignore: ['test'] }],
166+
},
167+
],
168+
invalid: [],
169+
});
170+
171+
ruleTester.run('lowercase-name with ignore=it', rule, {
172+
valid: [
173+
{
174+
code: "it('Foo', function () {})",
175+
options: [{ ignore: ['it'] }],
176+
},
177+
{
178+
code: 'it("Foo", function () {})',
179+
options: [{ ignore: ['it'] }],
180+
},
181+
{
182+
code: 'it(`Foo`, function () {})',
183+
options: [{ ignore: ['it'] }],
184+
},
185+
],
186+
invalid: [],
187+
});

Diff for: ‎rules/lowercase-name.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,20 @@ module.exports = {
5454
},
5555
},
5656
create(context) {
57+
const ignore = (context.options[0] && context.options[0].ignore) || [];
58+
const ignoredFunctionNames = ignore.reduce((accumulator, value) => {
59+
accumulator[value] = true;
60+
return accumulator;
61+
}, Object.create(null));
62+
63+
const isIgnoredFunctionName = node =>
64+
ignoredFunctionNames[node.callee.name];
65+
5766
return {
5867
CallExpression(node) {
5968
const erroneousMethod = descriptionBeginsWithLowerCase(node);
6069

61-
if (erroneousMethod) {
70+
if (erroneousMethod && !isIgnoredFunctionName(node)) {
6271
context.report({
6372
message: '`{{ method }}`s should begin with lowercase',
6473
data: { method: erroneousMethod },

0 commit comments

Comments
 (0)
Please sign in to comment.