From 8c850ffaabaf7ead5d96559130f801e6155efdc5 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 27 Oct 2019 16:57:25 +1300 Subject: [PATCH] feat(valid-title): support detecting & fixing trailing spaces in titles (#449) --- docs/rules/valid-title.md | 4 ++++ src/rules/__tests__/valid-title.test.ts | 20 ++++++++++++++++++++ src/rules/valid-title.ts | 8 +++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/docs/rules/valid-title.md b/docs/rules/valid-title.md index e85f6934f..e9a8fe678 100644 --- a/docs/rules/valid-title.md +++ b/docs/rules/valid-title.md @@ -54,6 +54,10 @@ describe('foo', () => { describe(' foo', () => { test('bar', () => {}); }); + +describe('foo ', () => { + test('bar', () => {}); +}); ``` Examples of **correct** code for this rule diff --git a/src/rules/__tests__/valid-title.test.ts b/src/rules/__tests__/valid-title.test.ts index 1863d8637..76e4aad11 100644 --- a/src/rules/__tests__/valid-title.test.ts +++ b/src/rules/__tests__/valid-title.test.ts @@ -41,6 +41,11 @@ ruleTester.run('no-accidental-space', rule, { errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }], output: 'describe("foo foe fum", function () {})', }, + { + code: 'describe("foo foe fum ", function () {})', + errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }], + output: 'describe("foo foe fum", function () {})', + }, { code: 'fdescribe(" foo", function () {})', errors: [{ messageId: 'accidentalSpace', column: 11, line: 1 }], @@ -66,6 +71,11 @@ ruleTester.run('no-accidental-space', rule, { errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }], output: 'fit("foo", function () {})', }, + { + code: 'fit("foo ", function () {})', + errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }], + output: 'fit("foo", function () {})', + }, { code: 'xit(" foo", function () {})', errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }], @@ -86,11 +96,21 @@ ruleTester.run('no-accidental-space', rule, { errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }], output: 'test(`foo bar bang`, function () {})', }, + { + code: 'test(` foo bar bang `, function () {})', + errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }], + output: 'test(`foo bar bang`, function () {})', + }, { code: 'xtest(" foo", function () {})', errors: [{ messageId: 'accidentalSpace', column: 7, line: 1 }], output: 'xtest("foo", function () {})', }, + { + code: 'xtest(" foo ", function () {})', + errors: [{ messageId: 'accidentalSpace', column: 7, line: 1 }], + output: 'xtest("foo", function () {})', + }, { code: ` describe(' foo', () => { diff --git a/src/rules/valid-title.ts b/src/rules/valid-title.ts index 6c76ba5d9..4a122aee3 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -24,7 +24,7 @@ export default createRule({ }, messages: { duplicatePrefix: 'should not have duplicate prefix', - accidentalSpace: 'should not have space in the beginning', + accidentalSpace: 'should not have leading or trailing spaces', }, type: 'suggestion', schema: [], @@ -50,7 +50,7 @@ export default createRule({ return; } - if (title.trimLeft().length !== title.length) { + if (title.trim().length !== title.length) { context.report({ messageId: 'accidentalSpace', node: argument, @@ -63,7 +63,9 @@ export default createRule({ return [ fixer.replaceTextRange( argument.range, - stringValue.replace(/^([`'"]) +?/, '$1'), + stringValue + .replace(/^([`'"]) +?/, '$1') + .replace(/ +?([`'"])$/, '$1'), ), ]; },