Skip to content

Commit a3c2ce3

Browse files
authored
Add fixers for valid title (#448)
* feat(valid-title): add fixer for `accidentalSpace` * feat(valid-title): add fixer for `duplicatePrefix`
1 parent 3f95e2e commit a3c2ce3

File tree

2 files changed

+150
-7
lines changed

2 files changed

+150
-7
lines changed

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

Lines changed: 119 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ruleTester.run('no-accidental-space', rule, {
2222
'xit("foo", function () {})',
2323
'test("foo", function () {})',
2424
'xtest("foo", function () {})',
25+
'xtest(`foo`, function () {})',
2526
'someFn("foo", function () {})',
2627
`
2728
describe('foo', () => {
@@ -33,34 +34,62 @@ ruleTester.run('no-accidental-space', rule, {
3334
{
3435
code: 'describe(" foo", function () {})',
3536
errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }],
37+
output: 'describe("foo", function () {})',
38+
},
39+
{
40+
code: 'describe(" foo foe fum", function () {})',
41+
errors: [{ messageId: 'accidentalSpace', column: 10, line: 1 }],
42+
output: 'describe("foo foe fum", function () {})',
3643
},
3744
{
3845
code: 'fdescribe(" foo", function () {})',
3946
errors: [{ messageId: 'accidentalSpace', column: 11, line: 1 }],
47+
output: 'fdescribe("foo", function () {})',
48+
},
49+
{
50+
code: "fdescribe(' foo', function () {})",
51+
errors: [{ messageId: 'accidentalSpace', column: 11, line: 1 }],
52+
output: "fdescribe('foo', function () {})",
4053
},
4154
{
4255
code: 'xdescribe(" foo", function () {})',
4356
errors: [{ messageId: 'accidentalSpace', column: 11, line: 1 }],
57+
output: 'xdescribe("foo", function () {})',
4458
},
4559
{
4660
code: 'it(" foo", function () {})',
4761
errors: [{ messageId: 'accidentalSpace', column: 4, line: 1 }],
62+
output: 'it("foo", function () {})',
4863
},
4964
{
5065
code: 'fit(" foo", function () {})',
5166
errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }],
67+
output: 'fit("foo", function () {})',
5268
},
5369
{
5470
code: 'xit(" foo", function () {})',
5571
errors: [{ messageId: 'accidentalSpace', column: 5, line: 1 }],
72+
output: 'xit("foo", function () {})',
5673
},
5774
{
5875
code: 'test(" foo", function () {})',
5976
errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }],
77+
output: 'test("foo", function () {})',
78+
},
79+
{
80+
code: 'test(` foo`, function () {})',
81+
errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }],
82+
output: 'test(`foo`, function () {})',
83+
},
84+
{
85+
code: 'test(` foo bar bang`, function () {})',
86+
errors: [{ messageId: 'accidentalSpace', column: 6, line: 1 }],
87+
output: 'test(`foo bar bang`, function () {})',
6088
},
6189
{
6290
code: 'xtest(" foo", function () {})',
6391
errors: [{ messageId: 'accidentalSpace', column: 7, line: 1 }],
92+
output: 'xtest("foo", function () {})',
6493
},
6594
{
6695
code: `
@@ -69,6 +98,11 @@ ruleTester.run('no-accidental-space', rule, {
6998
})
7099
`,
71100
errors: [{ messageId: 'accidentalSpace', column: 16, line: 2 }],
101+
output: `
102+
describe('foo', () => {
103+
it('bar', () => {})
104+
})
105+
`,
72106
},
73107
{
74108
code: `
@@ -77,6 +111,11 @@ ruleTester.run('no-accidental-space', rule, {
77111
})
78112
`,
79113
errors: [{ messageId: 'accidentalSpace', column: 12, line: 3 }],
114+
output: `
115+
describe('foo', () => {
116+
it('bar', () => {})
117+
})
118+
`,
80119
},
81120
],
82121
});
@@ -86,33 +125,66 @@ ruleTester.run('no-duplicate-prefix ft describe', rule, {
86125
'describe("foo", function () {})',
87126
'fdescribe("foo", function () {})',
88127
'xdescribe("foo", function () {})',
128+
'xdescribe(`foo`, function () {})',
89129
],
90130
invalid: [
91131
{
92132
code: 'describe("describe foo", function () {})',
93133
errors: [{ messageId: 'duplicatePrefix', column: 10, line: 1 }],
134+
output: 'describe("foo", function () {})',
94135
},
95136
{
96137
code: 'fdescribe("describe foo", function () {})',
97138
errors: [{ messageId: 'duplicatePrefix', column: 11, line: 1 }],
139+
output: 'fdescribe("foo", function () {})',
98140
},
99141
{
100142
code: 'xdescribe("describe foo", function () {})',
101143
errors: [{ messageId: 'duplicatePrefix', column: 11, line: 1 }],
144+
output: 'xdescribe("foo", function () {})',
145+
},
146+
{
147+
code: "describe('describe foo', function () {})",
148+
errors: [{ messageId: 'duplicatePrefix', column: 10, line: 1 }],
149+
output: "describe('foo', function () {})",
150+
},
151+
{
152+
code: 'fdescribe(`describe foo`, function () {})',
153+
errors: [{ messageId: 'duplicatePrefix', column: 11, line: 1 }],
154+
output: 'fdescribe(`foo`, function () {})',
102155
},
103156
],
104157
});
105158

106159
ruleTester.run('no-duplicate-prefix ft test', rule, {
107-
valid: ['test("foo", function () {})', 'xtest("foo", function () {})'],
160+
valid: [
161+
'test("foo", function () {})',
162+
"test('foo', function () {})",
163+
'xtest("foo", function () {})',
164+
'xtest(`foo`, function () {})',
165+
'test("foo test", function () {})',
166+
'xtest("foo test", function () {})',
167+
],
108168
invalid: [
109169
{
110170
code: 'test("test foo", function () {})',
111171
errors: [{ messageId: 'duplicatePrefix', column: 6, line: 1 }],
172+
output: 'test("foo", function () {})',
112173
},
113174
{
114175
code: 'xtest("test foo", function () {})',
115176
errors: [{ messageId: 'duplicatePrefix', column: 7, line: 1 }],
177+
output: 'xtest("foo", function () {})',
178+
},
179+
{
180+
code: 'test(`test foo`, function () {})',
181+
errors: [{ messageId: 'duplicatePrefix', column: 6, line: 1 }],
182+
output: 'test(`foo`, function () {})',
183+
},
184+
{
185+
code: 'test(`test foo test`, function () {})',
186+
errors: [{ messageId: 'duplicatePrefix', column: 6, line: 1 }],
187+
output: 'test(`foo test`, function () {})',
116188
},
117189
],
118190
});
@@ -122,44 +194,85 @@ ruleTester.run('no-duplicate-prefix ft it', rule, {
122194
'it("foo", function () {})',
123195
'fit("foo", function () {})',
124196
'xit("foo", function () {})',
197+
'xit(`foo`, function () {})',
198+
'it("foos it correctly", function () {})',
125199
],
126200
invalid: [
127201
{
128202
code: 'it("it foo", function () {})',
129203
errors: [{ messageId: 'duplicatePrefix', column: 4, line: 1 }],
204+
output: 'it("foo", function () {})',
130205
},
131206
{
132207
code: 'fit("it foo", function () {})',
133208
errors: [{ messageId: 'duplicatePrefix', column: 5, line: 1 }],
209+
output: 'fit("foo", function () {})',
134210
},
135211
{
136212
code: 'xit("it foo", function () {})',
137213
errors: [{ messageId: 'duplicatePrefix', column: 5, line: 1 }],
214+
output: 'xit("foo", function () {})',
215+
},
216+
{
217+
code: 'it("it foos it correctly", function () {})',
218+
errors: [{ messageId: 'duplicatePrefix', column: 4, line: 1 }],
219+
output: 'it("foos it correctly", function () {})',
138220
},
139221
],
140222
});
141223

142224
ruleTester.run('no-duplicate-prefix ft nested', rule, {
143225
valid: [
144226
`
145-
describe('foo', () => {
146-
it('bar', () => {})
147-
})`,
227+
describe('foo', () => {
228+
it('bar', () => {})
229+
})
230+
`,
231+
`
232+
describe('foo', () => {
233+
it('describes things correctly', () => {})
234+
})
235+
`,
148236
],
149237
invalid: [
150238
{
151239
code: `
152240
describe('describe foo', () => {
153241
it('bar', () => {})
154-
})`,
242+
})
243+
`,
155244
errors: [{ messageId: 'duplicatePrefix', column: 16, line: 2 }],
245+
output: `
246+
describe('foo', () => {
247+
it('bar', () => {})
248+
})
249+
`,
250+
},
251+
{
252+
code: `
253+
describe('describe foo', () => {
254+
it('describes things correctly', () => {})
255+
})
256+
`,
257+
errors: [{ messageId: 'duplicatePrefix', column: 16, line: 2 }],
258+
output: `
259+
describe('foo', () => {
260+
it('describes things correctly', () => {})
261+
})
262+
`,
156263
},
157264
{
158265
code: `
159266
describe('foo', () => {
160267
it('it bar', () => {})
161-
})`,
268+
})
269+
`,
162270
errors: [{ messageId: 'duplicatePrefix', column: 12, line: 3 }],
271+
output: `
272+
describe('foo', () => {
273+
it('bar', () => {})
274+
})
275+
`,
163276
},
164277
],
165278
});

src/rules/valid-title.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { TSESTree } from '@typescript-eslint/experimental-utils';
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
25
import {
36
createRule,
47
getNodeName,
@@ -25,6 +28,7 @@ export default createRule({
2528
},
2629
type: 'suggestion',
2730
schema: [],
31+
fixable: 'code',
2832
},
2933
defaultOptions: [],
3034
create(context) {
@@ -50,6 +54,19 @@ export default createRule({
5054
context.report({
5155
messageId: 'accidentalSpace',
5256
node: argument,
57+
fix(fixer) {
58+
const stringValue =
59+
argument.type === AST_NODE_TYPES.TemplateLiteral
60+
? `\`${argument.quasis[0].value.raw}\``
61+
: argument.raw;
62+
63+
return [
64+
fixer.replaceTextRange(
65+
argument.range,
66+
stringValue.replace(/^([`'"]) +?/, '$1'),
67+
),
68+
];
69+
},
5370
});
5471
}
5572

@@ -60,6 +77,19 @@ export default createRule({
6077
context.report({
6178
messageId: 'duplicatePrefix',
6279
node: argument,
80+
fix(fixer) {
81+
const stringValue =
82+
argument.type === AST_NODE_TYPES.TemplateLiteral
83+
? `\`${argument.quasis[0].value.raw}\``
84+
: argument.raw;
85+
86+
return [
87+
fixer.replaceTextRange(
88+
argument.range,
89+
stringValue.replace(/^([`'"]).+? /, '$1'),
90+
),
91+
];
92+
},
6393
});
6494
}
6595
},

0 commit comments

Comments
 (0)