Skip to content
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

feat: add fixer for lowercase-name rule #119

Merged
merged 1 commit into from May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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