Skip to content

Commit

Permalink
feat(lowercase-name): add 'ignore' option (#92)
Browse files Browse the repository at this point in the history
Fixes #76
  • Loading branch information
with-heart authored and SimenB committed Mar 14, 2018
1 parent 80c9b33 commit 03a7cda
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
49 changes: 49 additions & 0 deletions docs/rules/lowercase-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,52 @@ it('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
```

## Options

```json
{
"jest/lowercase-name": [
"error",
{
"ignore": ["describe", "test"]
}
]
}
```

### `ignore`

This array option whitelists function names so that this rule does not report
their usage as being incorrect. There are three possible values:

* `"describe"`
* `"test"`
* `"it"`

By default, none of these options are enabled (the equivalent of
`{ "ignore": [] }`).

Example of **correct** code for the `{ "ignore": ["describe"] }` option:

```js
/* eslint jest/lowercase-name: ["error", { "ignore": ["describe"] }] */

describe('Uppercase description');
```

Example of **correct** code for the `{ "ignore": ["test"] }` option:

```js
/* eslint jest/lowercase-name: ["error", { "ignore": ["test"] }] */

test('Uppercase description');
```

Example of **correct** code for the `{ "ignore": ["it"] }` option:

```js
/* eslint jest/lowercase-name: ["error", { "ignore": ["it"] }] */

it('Uppercase description');
```
57 changes: 57 additions & 0 deletions rules/__tests__/lowercase-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ruleTester = new RuleTester({

ruleTester.run('lowercase-name', rule, {
valid: [
'it()',
"it(' ', function () {})",
'it(" ", function () {})',
'it(` `, function () {})',
Expand All @@ -20,12 +21,14 @@ ruleTester.run('lowercase-name', rule, {
'it("<Foo/>", function () {})',
'it("123 foo", function () {})',
'it(42, function () {})',
'test()',
"test('foo', function () {})",
'test("foo", function () {})',
'test(`foo`, function () {})',
'test("<Foo/>", function () {})',
'test("123 foo", function () {})',
'test("42", function () {})',
'describe()',
"describe('foo', function () {})",
'describe("foo", function () {})',
'describe(`foo`, function () {})',
Expand Down Expand Up @@ -128,3 +131,57 @@ ruleTester.run('lowercase-name', rule, {
},
],
});

ruleTester.run('lowercase-name with ignore=describe', rule, {
valid: [
{
code: "describe('Foo', function () {})",
options: [{ ignore: ['describe'] }],
},
{
code: 'describe("Foo", function () {})',
options: [{ ignore: ['describe'] }],
},
{
code: 'describe(`Foo`, function () {})',
options: [{ ignore: ['describe'] }],
},
],
invalid: [],
});

ruleTester.run('lowercase-name with ignore=test', rule, {
valid: [
{
code: "test('Foo', function () {})",
options: [{ ignore: ['test'] }],
},
{
code: 'test("Foo", function () {})',
options: [{ ignore: ['test'] }],
},
{
code: 'test(`Foo`, function () {})',
options: [{ ignore: ['test'] }],
},
],
invalid: [],
});

ruleTester.run('lowercase-name with ignore=it', rule, {
valid: [
{
code: "it('Foo', function () {})",
options: [{ ignore: ['it'] }],
},
{
code: 'it("Foo", function () {})',
options: [{ ignore: ['it'] }],
},
{
code: 'it(`Foo`, function () {})',
options: [{ ignore: ['it'] }],
},
],
invalid: [],
});
11 changes: 10 additions & 1 deletion rules/lowercase-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@ module.exports = {
},
},
create(context) {
const ignore = (context.options[0] && context.options[0].ignore) || [];
const ignoredFunctionNames = ignore.reduce((accumulator, value) => {
accumulator[value] = true;
return accumulator;
}, Object.create(null));

const isIgnoredFunctionName = node =>
ignoredFunctionNames[node.callee.name];

return {
CallExpression(node) {
const erroneousMethod = descriptionBeginsWithLowerCase(node);

if (erroneousMethod) {
if (erroneousMethod && !isIgnoredFunctionName(node)) {
context.report({
message: '`{{ method }}`s should begin with lowercase',
data: { method: erroneousMethod },
Expand Down

0 comments on commit 03a7cda

Please # to comment.