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 ignoring leading and trailing whitespace #1433

Merged
merged 2 commits into from
Sep 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 14 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,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