Skip to content

Commit 8eca0b7

Browse files
authored
fix: ensure rule fixes produce valid code when function params and args have trailing commas (#1282)
1 parent c206e0c commit 8eca0b7

16 files changed

+218
-49
lines changed

src/rules/__tests__/no-done-callback.test.ts

+30-12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ ruleTester.run('no-done-callback', rule, {
5959
},
6060
],
6161
},
62+
{
63+
code: 'test("something", (done,) => {done();})',
64+
errors: [
65+
{
66+
messageId: 'noDoneCallback',
67+
line: 1,
68+
column: 20,
69+
suggestions: [
70+
{
71+
messageId: 'suggestWrappingInPromise',
72+
data: { callback: 'done' },
73+
output:
74+
'test("something", () => {return new Promise(done => {done();})})',
75+
},
76+
],
77+
},
78+
],
79+
},
6280
{
6381
code: 'test("something", finished => {finished();})',
6482
errors: [
@@ -89,7 +107,7 @@ ruleTester.run('no-done-callback', rule, {
89107
messageId: 'suggestWrappingInPromise',
90108
data: { callback: 'done' },
91109
output:
92-
'test("something", () => {return new Promise((done) => {done();})})',
110+
'test("something", () => {return new Promise(done => {done();})})',
93111
},
94112
],
95113
},
@@ -123,7 +141,7 @@ ruleTester.run('no-done-callback', rule, {
123141
{
124142
messageId: 'suggestWrappingInPromise',
125143
data: { callback: 'done' },
126-
output: 'test("something", () => new Promise((done) => done()))',
144+
output: 'test("something", () => new Promise(done => done()))',
127145
},
128146
],
129147
},
@@ -141,7 +159,7 @@ ruleTester.run('no-done-callback', rule, {
141159
messageId: 'suggestWrappingInPromise',
142160
data: { callback: 'done' },
143161
output:
144-
'test("something", function() {return new Promise((done) => {done();})})',
162+
'test("something", function() {return new Promise(done => {done();})})',
145163
},
146164
],
147165
},
@@ -159,7 +177,7 @@ ruleTester.run('no-done-callback', rule, {
159177
messageId: 'suggestWrappingInPromise',
160178
data: { callback: 'done' },
161179
output:
162-
'test("something", function () {return new Promise((done) => {done();})})',
180+
'test("something", function () {return new Promise(done => {done();})})',
163181
},
164182
],
165183
},
@@ -203,7 +221,7 @@ ruleTester.run('no-done-callback', rule, {
203221
messageId: 'suggestWrappingInPromise',
204222
data: { callback: 'done' },
205223
output: dedent`
206-
test('something', () => {return new Promise((done) => {
224+
test('something', () => {return new Promise(done => {
207225
done();
208226
})});
209227
`,
@@ -270,7 +288,7 @@ ruleTester.run('no-done-callback', rule, {
270288
messageId: 'suggestWrappingInPromise',
271289
data: { callback: 'done' },
272290
output:
273-
'beforeEach(() => {return new Promise((done) => {done();})})',
291+
'beforeEach(() => {return new Promise(done => {done();})})',
274292
},
275293
],
276294
},
@@ -304,7 +322,7 @@ ruleTester.run('no-done-callback', rule, {
304322
{
305323
messageId: 'suggestWrappingInPromise',
306324
data: { callback: 'done' },
307-
output: 'afterEach(() => new Promise((done) => done()))',
325+
output: 'afterEach(() => new Promise(done => done()))',
308326
},
309327
],
310328
},
@@ -322,7 +340,7 @@ ruleTester.run('no-done-callback', rule, {
322340
messageId: 'suggestWrappingInPromise',
323341
data: { callback: 'done' },
324342
output:
325-
'beforeAll(function() {return new Promise((done) => {done();})})',
343+
'beforeAll(function() {return new Promise(done => {done();})})',
326344
},
327345
],
328346
},
@@ -340,7 +358,7 @@ ruleTester.run('no-done-callback', rule, {
340358
messageId: 'suggestWrappingInPromise',
341359
data: { callback: 'done' },
342360
output:
343-
'afterEach(function () {return new Promise((done) => {done();})})',
361+
'afterEach(function () {return new Promise(done => {done();})})',
344362
},
345363
],
346364
},
@@ -383,7 +401,7 @@ ruleTester.run('no-done-callback', rule, {
383401
messageId: 'suggestWrappingInPromise',
384402
data: { callback: 'done' },
385403
output: dedent`
386-
beforeEach(() => {return new Promise((done) => {
404+
beforeEach(() => {return new Promise(done => {
387405
done();
388406
})});
389407
`,
@@ -413,7 +431,7 @@ ruleTester.run('no-done-callback', rule, {
413431
output: dedent`
414432
import { beforeEach } from '@jest/globals';
415433
416-
beforeEach(() => {return new Promise((done) => {
434+
beforeEach(() => {return new Promise(done => {
417435
done();
418436
})});
419437
`,
@@ -443,7 +461,7 @@ ruleTester.run('no-done-callback', rule, {
443461
output: dedent`
444462
import { beforeEach as atTheStartOfEachTest } from '@jest/globals';
445463
446-
atTheStartOfEachTest(() => {return new Promise((done) => {
464+
atTheStartOfEachTest(() => {return new Promise(done => {
447465
done();
448466
})});
449467
`,

src/rules/__tests__/prefer-comparison-matcher.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ const generateInvalidCases = (
2828
},
2929
],
3030
},
31+
{
32+
code: `expect(value ${operator} 1,).${equalityMatcher}(true,);`,
33+
output: `expect(value,).${preferredMatcher}(1,);`,
34+
parserOptions: { ecmaVersion: 2017 },
35+
errors: [
36+
{
37+
messageId: 'useToBeComparison',
38+
data: { preferredMatcher },
39+
column: 19 + operator.length,
40+
line: 1,
41+
},
42+
],
43+
},
3144
{
3245
code: `expect(value ${operator} 1)['${equalityMatcher}'](true);`,
3346
output: `expect(value).${preferredMatcher}(1);`,

src/rules/__tests__/prefer-equality-matcher.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
5454
},
5555
],
5656
},
57+
{
58+
code: 'expect(a === b,).toBe(true,);',
59+
parserOptions: { ecmaVersion: 2017 },
60+
errors: [
61+
{
62+
messageId: 'useEqualityMatcher',
63+
suggestions: expectSuggestions(
64+
equalityMatcher => `expect(a,).${equalityMatcher}(b,);`,
65+
),
66+
column: 18,
67+
line: 1,
68+
},
69+
],
70+
},
5771
{
5872
code: 'expect(a === b).toBe(false);',
5973
errors: [

src/rules/__tests__/prefer-expect-assertions.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,23 @@ ruleTester.run('prefer-expect-assertions', rule, {
254254
suggestions: [
255255
{
256256
messageId: 'suggestRemovingExtraArguments',
257-
output: 'it("it1", function() {expect.assertions(1);})',
257+
output: 'it("it1", function() {expect.assertions(1,);})',
258+
},
259+
],
260+
},
261+
],
262+
},
263+
{
264+
code: 'it("it1", function() {expect.assertions(1,2,);})',
265+
errors: [
266+
{
267+
messageId: 'assertionsRequiresOneArgument',
268+
column: 43,
269+
line: 1,
270+
suggestions: [
271+
{
272+
messageId: 'suggestRemovingExtraArguments',
273+
output: 'it("it1", function() {expect.assertions(1,);})',
258274
},
259275
],
260276
},
@@ -287,6 +303,22 @@ ruleTester.run('prefer-expect-assertions', rule, {
287303
},
288304
],
289305
},
306+
{
307+
code: 'it("it1", function() {expect.hasAssertions("1",);})',
308+
errors: [
309+
{
310+
messageId: 'hasAssertionsTakesNoArguments',
311+
column: 30,
312+
line: 1,
313+
suggestions: [
314+
{
315+
messageId: 'suggestRemovingExtraArguments',
316+
output: 'it("it1", function() {expect.hasAssertions();})',
317+
},
318+
],
319+
},
320+
],
321+
},
290322
{
291323
code: 'it("it1", function() {expect.hasAssertions("1", "2");})',
292324
errors: [

src/rules/__tests__/prefer-expect-resolves.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ ruleTester.run('prefer-expect-resolves', rule, {
3737
{
3838
code: dedent`
3939
it('passes', async () => {
40-
expect(await someValue()).toBe(true);
40+
expect(await someValue(),).toBe(true);
4141
});
4242
`,
4343
output: dedent`
4444
it('passes', async () => {
45-
await expect(someValue()).resolves.toBe(true);
45+
await expect(someValue(),).resolves.toBe(true);
4646
});
4747
`,
4848
errors: [{ endColumn: 27, column: 10, messageId: 'expectResolves' }],

src/rules/__tests__/prefer-mock-promise-shorthand.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ ruleTester.run('prefer-mock-shorthand', rule, {
127127
},
128128
],
129129
},
130+
{
131+
code: 'aVariable.mockImplementation(() => Promise.reject(42),)',
132+
output: 'aVariable.mockRejectedValue(42,)',
133+
parserOptions: { ecmaVersion: 2017 },
134+
errors: [
135+
{
136+
messageId: 'useMockShorthand',
137+
data: { replacement: 'mockRejectedValue' },
138+
column: 11,
139+
line: 1,
140+
},
141+
],
142+
},
130143
{
131144
code: 'aVariable.mockImplementationOnce(() => Promise.resolve(42))',
132145
output: 'aVariable.mockResolvedValueOnce(42)',

src/rules/__tests__/prefer-spy-on.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ ruleTester.run('prefer-spy-on', rule, {
7474
],
7575
},
7676
{
77-
code: 'obj.a = jest.fn(() => 10)',
77+
code: 'obj.a = jest.fn(() => 10,)',
7878
output: "jest.spyOn(obj, 'a').mockImplementation(() => 10)",
79+
parserOptions: { ecmaVersion: 2017 },
7980
errors: [
8081
{
8182
messageId: 'useJestSpyOn',

src/rules/__tests__/prefer-strict-equal.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ ruleTester.run('prefer-strict-equal', rule, {
2626
},
2727
],
2828
},
29+
{
30+
code: 'expect(something).toEqual(somethingElse,);',
31+
parserOptions: { ecmaVersion: 2017 },
32+
errors: [
33+
{
34+
messageId: 'useToStrictEqual',
35+
column: 19,
36+
line: 1,
37+
suggestions: [
38+
{
39+
messageId: 'suggestReplaceWithStrictEqual',
40+
output: 'expect(something).toStrictEqual(somethingElse,);',
41+
},
42+
],
43+
},
44+
],
45+
},
2946
{
3047
code: 'expect(something)["toEqual"](somethingElse);',
3148
errors: [

src/rules/__tests__/prefer-to-be.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ ruleTester.run('prefer-to-be', rule, {
4242
output: 'expect(value).toBe(1);',
4343
errors: [{ messageId: 'useToBe', column: 15, line: 1 }],
4444
},
45+
{
46+
code: 'expect(value).toStrictEqual(1,);',
47+
output: 'expect(value).toBe(1,);',
48+
parserOptions: { ecmaVersion: 2017 },
49+
errors: [{ messageId: 'useToBe', column: 15, line: 1 }],
50+
},
4551
{
4652
code: 'expect(value).toStrictEqual(-1);',
4753
output: 'expect(value).toBe(-1);',
@@ -115,6 +121,12 @@ ruleTester.run('prefer-to-be: null', rule, {
115121
output: 'expect(null).toBeNull();',
116122
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
117123
},
124+
{
125+
code: 'expect(null).toEqual(null,);',
126+
output: 'expect(null).toBeNull();',
127+
parserOptions: { ecmaVersion: 2017 },
128+
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
129+
},
118130
{
119131
code: 'expect(null).toStrictEqual(null);',
120132
output: 'expect(null).toBeNull();',

src/rules/__tests__/prefer-to-contain.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ ruleTester.run('prefer-to-contain', rule, {
4444
output: 'expect(a).toContain(b);',
4545
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
4646
},
47+
{
48+
code: 'expect(a.includes(b,),).toEqual(true,);',
49+
output: 'expect(a,).toContain(b,);',
50+
parserOptions: { ecmaVersion: 2017 },
51+
errors: [{ messageId: 'useToContain', column: 25, line: 1 }],
52+
},
4753
{
4854
code: "expect(a['includes'](b)).toEqual(true);",
4955
output: 'expect(a).toContain(b);',

src/rules/__tests__/prefer-to-have-length.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ ruleTester.run('prefer-to-have-length', rule, {
2828
output: 'expect(files).toHaveLength(1);',
2929
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
3030
},
31+
{
32+
code: 'expect(files["length"]).toBe(1,);',
33+
output: 'expect(files).toHaveLength(1,);',
34+
parserOptions: { ecmaVersion: 2017 },
35+
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
36+
},
3137
{
3238
code: 'expect(files["length"])["not"].toBe(1);',
3339
output: 'expect(files)["not"].toHaveLength(1);',

src/rules/__tests__/prefer-todo.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ ruleTester.run('prefer-todo', rule, {
3333
output: 'test.todo("i need to write this test");',
3434
errors: [{ messageId: 'unimplementedTest' }],
3535
},
36+
{
37+
code: `test("i need to write this test",);`,
38+
output: 'test.todo("i need to write this test",);',
39+
parserOptions: { ecmaVersion: 2017 },
40+
errors: [{ messageId: 'unimplementedTest' }],
41+
},
3642
{
3743
code: 'test(`i need to write this test`);',
3844
output: 'test.todo(`i need to write this test`);',

0 commit comments

Comments
 (0)