Skip to content

Commit

Permalink
feat: add ignoreSpaces option to valid-title rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyatuning committed Sep 15, 2023
1 parent 1b96756 commit d395e72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion docs/rules/valid-title.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,10 @@ ruleTester.run('no-accidental-space', rule, {
it('bar', () => {})
})
`,
{
code: 'it(`GIVEN... \n `, () => {});',
options: [{ ignoreSpaces: true }],
},
],
invalid: [
{
Expand Down
14 changes: 12 additions & 2 deletions src/rules/valid-title.ts
Original file line number Diff line number Diff line change
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,16 @@ 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 +257,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 d395e72

Please sign in to comment.