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(valid-title): support detecting & fixing trailing spaces in titles #449

Merged
merged 1 commit into from Oct 27, 2019
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
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