Skip to content

Commit

Permalink
feat(valid-title): support detecting & fixing trailing spaces in titl…
Browse files Browse the repository at this point in the history
…es (#449)
  • Loading branch information
G-Rath committed Oct 27, 2019
1 parent a3c2ce3 commit 8c850ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/rules/valid-title.md
Expand Up @@ -54,6 +54,10 @@ describe('foo', () => {
describe(' foo', () => {
test('bar', () => {});
});

describe('foo ', () => {
test('bar', () => {});
});
```

Examples of **correct** code for this rule
Expand Down
20 changes: 20 additions & 0 deletions src/rules/__tests__/valid-title.test.ts
Expand Up @@ -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 }],
Expand All @@ -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 }],
Expand All @@ -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', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/rules/valid-title.ts
Expand Up @@ -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: [],
Expand All @@ -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,
Expand All @@ -63,7 +63,9 @@ export default createRule({
return [
fixer.replaceTextRange(
argument.range,
stringValue.replace(/^([`'"]) +?/, '$1'),
stringValue
.replace(/^([`'"]) +?/, '$1')
.replace(/ +?([`'"])$/, '$1'),
),
];
},
Expand Down

0 comments on commit 8c850ff

Please sign in to comment.