Skip to content

Commit 8c850ff

Browse files
authored
feat(valid-title): support detecting & fixing trailing spaces in titles (#449)
1 parent a3c2ce3 commit 8c850ff

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

docs/rules/valid-title.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describe('foo', () => {
5454
describe(' foo', () => {
5555
test('bar', () => {});
5656
});
57+
58+
describe('foo ', () => {
59+
test('bar', () => {});
60+
});
5761
```
5862

5963
Examples of **correct** code for this rule

src/rules/__tests__/valid-title.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ ruleTester.run('no-accidental-space', rule, {
4141
errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }],
4242
output: 'describe("foo foe fum", function () {})',
4343
},
44+
{
45+
code: 'describe("foo foe fum ", function () {})',
46+
errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }],
47+
output: 'describe("foo foe fum", function () {})',
48+
},
4449
{
4550
code: 'fdescribe(" foo", function () {})',
4651
errors: [{ messageId: 'accidentalSpace', column: 11, line: 1 }],
@@ -66,6 +71,11 @@ ruleTester.run('no-accidental-space', rule, {
6671
errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }],
6772
output: 'fit("foo", function () {})',
6873
},
74+
{
75+
code: 'fit("foo ", function () {})',
76+
errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }],
77+
output: 'fit("foo", function () {})',
78+
},
6979
{
7080
code: 'xit(" foo", function () {})',
7181
errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }],
@@ -86,11 +96,21 @@ ruleTester.run('no-accidental-space', rule, {
8696
errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }],
8797
output: 'test(`foo bar bang`, function () {})',
8898
},
99+
{
100+
code: 'test(` foo bar bang `, function () {})',
101+
errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }],
102+
output: 'test(`foo bar bang`, function () {})',
103+
},
89104
{
90105
code: 'xtest(" foo", function () {})',
91106
errors: [{ messageId: 'accidentalSpace', column: 7, line: 1 }],
92107
output: 'xtest("foo", function () {})',
93108
},
109+
{
110+
code: 'xtest(" foo ", function () {})',
111+
errors: [{ messageId: 'accidentalSpace', column: 7, line: 1 }],
112+
output: 'xtest("foo", function () {})',
113+
},
94114
{
95115
code: `
96116
describe(' foo', () => {

src/rules/valid-title.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default createRule({
2424
},
2525
messages: {
2626
duplicatePrefix: 'should not have duplicate prefix',
27-
accidentalSpace: 'should not have space in the beginning',
27+
accidentalSpace: 'should not have leading or trailing spaces',
2828
},
2929
type: 'suggestion',
3030
schema: [],
@@ -50,7 +50,7 @@ export default createRule({
5050
return;
5151
}
5252

53-
if (title.trimLeft().length !== title.length) {
53+
if (title.trim().length !== title.length) {
5454
context.report({
5555
messageId: 'accidentalSpace',
5656
node: argument,
@@ -63,7 +63,9 @@ export default createRule({
6363
return [
6464
fixer.replaceTextRange(
6565
argument.range,
66-
stringValue.replace(/^([`'"]) +?/, '$1'),
66+
stringValue
67+
.replace(/^([`'"]) +?/, '$1')
68+
.replace(/ +?([`'"])$/, '$1'),
6769
),
6870
];
6971
},

0 commit comments

Comments
 (0)