From d395e725f7e918e046972430420460db4335e7af Mon Sep 17 00:00:00 2001 From: Balazs Sandor Date: Fri, 15 Sep 2023 11:55:15 +0200 Subject: [PATCH] feat: add ignoreSpaces option to valid-title rule --- docs/rules/valid-title.md | 10 +++++++++- src/rules/__tests__/valid-title.test.ts | 4 ++++ src/rules/valid-title.ts | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/rules/valid-title.md b/docs/rules/valid-title.md index ba29e2391..09ef43bd1 100644 --- a/docs/rules/valid-title.md +++ b/docs/rules/valid-title.md @@ -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 @@ -161,6 +162,7 @@ describe('foo', () => { ```ts interface Options { + ignoreSpaces?: boolean; ignoreTypeOfDescribeName?: boolean; disallowedWords?: string[]; mustNotMatch?: Partial> | string; @@ -168,6 +170,12 @@ interface Options { } ``` +#### `ignoreSpaces` + +Default: `false` + +When enabled, the leading and trailing spaces won't be checked. + #### `ignoreTypeOfDescribeName` Default: `false` diff --git a/src/rules/__tests__/valid-title.test.ts b/src/rules/__tests__/valid-title.test.ts index 038f325c8..976413356 100644 --- a/src/rules/__tests__/valid-title.test.ts +++ b/src/rules/__tests__/valid-title.test.ts @@ -848,6 +848,10 @@ ruleTester.run('no-accidental-space', rule, { it('bar', () => {}) }) `, + { + code: 'it(`GIVEN... \n `, () => {});', + options: [{ ignoreSpaces: true }], + }, ], invalid: [ { diff --git a/src/rules/valid-title.ts b/src/rules/valid-title.ts index 65587be58..3326b873a 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -85,6 +85,7 @@ const MatcherAndMessageSchema: JSONSchema.JSONSchema7 = { type MatcherGroups = 'describe' | 'test' | 'it'; interface Options { + ignoreSpaces?: boolean; ignoreTypeOfDescribeName?: boolean; disallowedWords?: string[]; mustNotMatch?: @@ -132,6 +133,10 @@ export default createRule<[Options], MessageIds>({ { type: 'object', properties: { + ignoreSpaces: { + type: 'boolean', + default: false, + }, ignoreTypeOfDescribeName: { type: 'boolean', default: false, @@ -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, @@ -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,