Skip to content

Commit

Permalink
feat(valid-title): support ignoring leading and trailing whitespace (#…
Browse files Browse the repository at this point in the history
…1433)

* feat: add ignoreSpaces option to valid-title rule

* chore: fix format
  • Loading branch information
sanyatuning committed Sep 15, 2023
1 parent bc4eae4 commit bc96473
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 9 additions & 1 deletion docs/rules/valid-title.md
Expand Up @@ -125,7 +125,8 @@ describe('foo', () => {

**accidentalSpace**

A `describe` / `test` block should not contain accidentalSpace
A `describe` / `test` block should not contain accidentalSpace, but can be
turned off via the `ignoreSpaces` option:

Examples of **incorrect** code for this rule

Expand Down Expand Up @@ -161,13 +162,20 @@ describe('foo', () => {

```ts
interface Options {
ignoreSpaces?: boolean;
ignoreTypeOfDescribeName?: boolean;
disallowedWords?: string[];
mustNotMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
mustMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
}
```

#### `ignoreSpaces`

Default: `false`

When enabled, the leading and trailing spaces won't be checked.

#### `ignoreTypeOfDescribeName`

Default: `false`
Expand Down
4 changes: 4 additions & 0 deletions src/rules/__tests__/valid-title.test.ts
Expand Up @@ -848,6 +848,10 @@ ruleTester.run('no-accidental-space', rule, {
it('bar', () => {})
})
`,
{
code: 'it(`GIVEN... \n `, () => {});',
options: [{ ignoreSpaces: true }],
},
],
invalid: [
{
Expand Down
16 changes: 14 additions & 2 deletions src/rules/valid-title.ts
Expand Up @@ -85,6 +85,7 @@ const MatcherAndMessageSchema: JSONSchema.JSONSchema7 = {
type MatcherGroups = 'describe' | 'test' | 'it';

interface Options {
ignoreSpaces?: boolean;
ignoreTypeOfDescribeName?: boolean;
disallowedWords?: string[];
mustNotMatch?:
Expand Down Expand Up @@ -132,6 +133,10 @@ export default createRule<[Options], MessageIds>({
{
type: 'object',
properties: {
ignoreSpaces: {
type: 'boolean',
default: false,
},
ignoreTypeOfDescribeName: {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -161,11 +166,18 @@ export default createRule<[Options], MessageIds>({
],
fixable: 'code',
},
defaultOptions: [{ ignoreTypeOfDescribeName: false, disallowedWords: [] }],
defaultOptions: [
{
ignoreSpaces: false,
ignoreTypeOfDescribeName: false,
disallowedWords: [],
},
],
create(
context,
[
{
ignoreSpaces,
ignoreTypeOfDescribeName,
disallowedWords = [],
mustNotMatch,
Expand Down Expand Up @@ -247,7 +259,7 @@ export default createRule<[Options], MessageIds>({
}
}

if (title.trim().length !== title.length) {
if (ignoreSpaces === false && title.trim().length !== title.length) {
context.report({
messageId: 'accidentalSpace',
node: argument,
Expand Down

0 comments on commit bc96473

Please sign in to comment.