Skip to content

Commit bc96473

Browse files
authored
feat(valid-title): support ignoring leading and trailing whitespace (#1433)
* feat: add ignoreSpaces option to valid-title rule * chore: fix format
1 parent bc4eae4 commit bc96473

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

docs/rules/valid-title.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ describe('foo', () => {
125125

126126
**accidentalSpace**
127127

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

130131
Examples of **incorrect** code for this rule
131132

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

162163
```ts
163164
interface Options {
165+
ignoreSpaces?: boolean;
164166
ignoreTypeOfDescribeName?: boolean;
165167
disallowedWords?: string[];
166168
mustNotMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
167169
mustMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
168170
}
169171
```
170172

173+
#### `ignoreSpaces`
174+
175+
Default: `false`
176+
177+
When enabled, the leading and trailing spaces won't be checked.
178+
171179
#### `ignoreTypeOfDescribeName`
172180

173181
Default: `false`

src/rules/__tests__/valid-title.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,10 @@ ruleTester.run('no-accidental-space', rule, {
848848
it('bar', () => {})
849849
})
850850
`,
851+
{
852+
code: 'it(`GIVEN... \n `, () => {});',
853+
options: [{ ignoreSpaces: true }],
854+
},
851855
],
852856
invalid: [
853857
{

src/rules/valid-title.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const MatcherAndMessageSchema: JSONSchema.JSONSchema7 = {
8585
type MatcherGroups = 'describe' | 'test' | 'it';
8686

8787
interface Options {
88+
ignoreSpaces?: boolean;
8889
ignoreTypeOfDescribeName?: boolean;
8990
disallowedWords?: string[];
9091
mustNotMatch?:
@@ -132,6 +133,10 @@ export default createRule<[Options], MessageIds>({
132133
{
133134
type: 'object',
134135
properties: {
136+
ignoreSpaces: {
137+
type: 'boolean',
138+
default: false,
139+
},
135140
ignoreTypeOfDescribeName: {
136141
type: 'boolean',
137142
default: false,
@@ -161,11 +166,18 @@ export default createRule<[Options], MessageIds>({
161166
],
162167
fixable: 'code',
163168
},
164-
defaultOptions: [{ ignoreTypeOfDescribeName: false, disallowedWords: [] }],
169+
defaultOptions: [
170+
{
171+
ignoreSpaces: false,
172+
ignoreTypeOfDescribeName: false,
173+
disallowedWords: [],
174+
},
175+
],
165176
create(
166177
context,
167178
[
168179
{
180+
ignoreSpaces,
169181
ignoreTypeOfDescribeName,
170182
disallowedWords = [],
171183
mustNotMatch,
@@ -247,7 +259,7 @@ export default createRule<[Options], MessageIds>({
247259
}
248260
}
249261

250-
if (title.trim().length !== title.length) {
262+
if (ignoreSpaces === false && title.trim().length !== title.length) {
251263
context.report({
252264
messageId: 'accidentalSpace',
253265
node: argument,

0 commit comments

Comments
 (0)