Skip to content

Commit

Permalink
feat: add fixer for lowercase-name rule (#119)
Browse files Browse the repository at this point in the history
Fixes #118
  • Loading branch information
macklinu authored and SimenB committed May 27, 2018
1 parent ca2d60b commit d7f3de3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -81,7 +81,7 @@ for more information about extending configuration files.
| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------------ | ----------------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------ |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce consistent test or it keyword | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [lowercase-name](docs/rules/lowercase-name.md) | Disallow capitalized test names | | |
| [lowercase-name](docs/rules/lowercase-name.md) | Disallow capitalized test names | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
Expand Down
23 changes: 23 additions & 0 deletions rules/__tests__/lowercase-name.test.js
Expand Up @@ -21,13 +21,15 @@ ruleTester.run('lowercase-name', rule, {
'it("<Foo/>", function () {})',
'it("123 foo", function () {})',
'it(42, function () {})',
'it(``)',
'test()',
"test('foo', function () {})",
'test("foo", function () {})',
'test(`foo`, function () {})',
'test("<Foo/>", function () {})',
'test("123 foo", function () {})',
'test("42", function () {})',
'test(``)',
'describe()',
"describe('foo', function () {})",
'describe("foo", function () {})',
Expand All @@ -36,11 +38,13 @@ ruleTester.run('lowercase-name', rule, {
'describe("123 foo", function () {})',
'describe("42", function () {})',
'describe(function () {})',
'describe(``)',
],

invalid: [
{
code: "it('Foo', function () {})",
output: "it('foo', function () {})",
errors: [
{
message: '`it`s should begin with lowercase',
Expand All @@ -51,6 +55,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'it("Foo", function () {})',
output: 'it("foo", function () {})',
errors: [
{
message: '`it`s should begin with lowercase',
Expand All @@ -61,6 +66,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'it(`Foo`, function () {})',
output: 'it(`foo`, function () {})',
errors: [
{
message: '`it`s should begin with lowercase',
Expand All @@ -71,6 +77,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: "test('Foo', function () {})",
output: "test('foo', function () {})",
errors: [
{
message: '`test`s should begin with lowercase',
Expand All @@ -81,6 +88,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'test("Foo", function () {})',
output: 'test("foo", function () {})',
errors: [
{
message: '`test`s should begin with lowercase',
Expand All @@ -91,6 +99,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'test(`Foo`, function () {})',
output: 'test(`foo`, function () {})',
errors: [
{
message: '`test`s should begin with lowercase',
Expand All @@ -101,6 +110,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: "describe('Foo', function () {})",
output: "describe('foo', function () {})",
errors: [
{
message: '`describe`s should begin with lowercase',
Expand All @@ -111,6 +121,7 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'describe("Foo", function () {})',
output: 'describe("foo", function () {})',
errors: [
{
message: '`describe`s should begin with lowercase',
Expand All @@ -121,6 +132,18 @@ ruleTester.run('lowercase-name', rule, {
},
{
code: 'describe(`Foo`, function () {})',
output: 'describe(`foo`, function () {})',
errors: [
{
message: '`describe`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'describe(`Some longer description`, function () {})',
output: 'describe(`some longer description`, function () {})',
errors: [
{
message: '`describe`s should begin with lowercase',
Expand Down
17 changes: 17 additions & 0 deletions rules/lowercase-name.js
Expand Up @@ -52,6 +52,7 @@ module.exports = {
docs: {
url: getDocsUrl(__filename),
},
fixable: 'code',
},
create(context) {
const ignore = (context.options[0] && context.options[0].ignore) || [];
Expand All @@ -72,6 +73,22 @@ module.exports = {
message: '`{{ method }}`s should begin with lowercase',
data: { method: erroneousMethod },
node,
fix(fixer) {
const firstArg = node.arguments[0];
const description = testDescription(node);

const rangeIgnoringQuotes = [
firstArg.start + 1,
firstArg.end - 1,
];
const newDescription =
description.substring(0, 1).toLowerCase() +
description.substring(1);

return [
fixer.replaceTextRange(rangeIgnoringQuotes, newDescription),
];
},
});
}
},
Expand Down

0 comments on commit d7f3de3

Please sign in to comment.