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

fix: ensure rule fixes produce valid code when function params and args have trailing commas #1282

Merged
merged 1 commit into from
Nov 24, 2022
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
42 changes: 30 additions & 12 deletions src/rules/__tests__/no-done-callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ ruleTester.run('no-done-callback', rule, {
},
],
},
{
code: 'test("something", (done,) => {done();})',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 20,
suggestions: [
{
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'test("something", () => {return new Promise(done => {done();})})',
},
],
},
],
},
{
code: 'test("something", finished => {finished();})',
errors: [
Expand Down Expand Up @@ -89,7 +107,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'test("something", () => {return new Promise((done) => {done();})})',
'test("something", () => {return new Promise(done => {done();})})',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should probably preserve the parens if they were there originally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best practice for fixers are to avoid doing stylistic fixes since that should be enforced by another rule (which will be automatically fixed if possible), to help keep fixes as small as possible.

},
],
},
Expand Down Expand Up @@ -123,7 +141,7 @@ ruleTester.run('no-done-callback', rule, {
{
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output: 'test("something", () => new Promise((done) => done()))',
output: 'test("something", () => new Promise(done => done()))',
},
],
},
Expand All @@ -141,7 +159,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'test("something", function() {return new Promise((done) => {done();})})',
'test("something", function() {return new Promise(done => {done();})})',
},
],
},
Expand All @@ -159,7 +177,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'test("something", function () {return new Promise((done) => {done();})})',
'test("something", function () {return new Promise(done => {done();})})',
},
],
},
Expand Down Expand Up @@ -203,7 +221,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output: dedent`
test('something', () => {return new Promise((done) => {
test('something', () => {return new Promise(done => {
done();
})});
`,
Expand Down Expand Up @@ -270,7 +288,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'beforeEach(() => {return new Promise((done) => {done();})})',
'beforeEach(() => {return new Promise(done => {done();})})',
},
],
},
Expand Down Expand Up @@ -304,7 +322,7 @@ ruleTester.run('no-done-callback', rule, {
{
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output: 'afterEach(() => new Promise((done) => done()))',
output: 'afterEach(() => new Promise(done => done()))',
},
],
},
Expand All @@ -322,7 +340,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'beforeAll(function() {return new Promise((done) => {done();})})',
'beforeAll(function() {return new Promise(done => {done();})})',
},
],
},
Expand All @@ -340,7 +358,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output:
'afterEach(function () {return new Promise((done) => {done();})})',
'afterEach(function () {return new Promise(done => {done();})})',
},
],
},
Expand Down Expand Up @@ -383,7 +401,7 @@ ruleTester.run('no-done-callback', rule, {
messageId: 'suggestWrappingInPromise',
data: { callback: 'done' },
output: dedent`
beforeEach(() => {return new Promise((done) => {
beforeEach(() => {return new Promise(done => {
done();
})});
`,
Expand Down Expand Up @@ -413,7 +431,7 @@ ruleTester.run('no-done-callback', rule, {
output: dedent`
import { beforeEach } from '@jest/globals';

beforeEach(() => {return new Promise((done) => {
beforeEach(() => {return new Promise(done => {
done();
})});
`,
Expand Down Expand Up @@ -443,7 +461,7 @@ ruleTester.run('no-done-callback', rule, {
output: dedent`
import { beforeEach as atTheStartOfEachTest } from '@jest/globals';

atTheStartOfEachTest(() => {return new Promise((done) => {
atTheStartOfEachTest(() => {return new Promise(done => {
done();
})});
`,
Expand Down
13 changes: 13 additions & 0 deletions src/rules/__tests__/prefer-comparison-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ const generateInvalidCases = (
},
],
},
{
code: `expect(value ${operator} 1,).${equalityMatcher}(true,);`,
output: `expect(value,).${preferredMatcher}(1,);`,
parserOptions: { ecmaVersion: 2017 },
errors: [
{
messageId: 'useToBeComparison',
data: { preferredMatcher },
column: 19 + operator.length,
line: 1,
},
],
},
{
code: `expect(value ${operator} 1)['${equalityMatcher}'](true);`,
output: `expect(value).${preferredMatcher}(1);`,
Expand Down
14 changes: 14 additions & 0 deletions src/rules/__tests__/prefer-equality-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {
},
],
},
{
code: 'expect(a === b,).toBe(true,);',
parserOptions: { ecmaVersion: 2017 },
errors: [
{
messageId: 'useEqualityMatcher',
suggestions: expectSuggestions(
equalityMatcher => `expect(a,).${equalityMatcher}(b,);`,
),
column: 18,
line: 1,
},
],
},
{
code: 'expect(a === b).toBe(false);',
errors: [
Expand Down
34 changes: 33 additions & 1 deletion src/rules/__tests__/prefer-expect-assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,23 @@ ruleTester.run('prefer-expect-assertions', rule, {
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.assertions(1);})',
output: 'it("it1", function() {expect.assertions(1,);})',
},
],
},
],
},
{
code: 'it("it1", function() {expect.assertions(1,2,);})',
errors: [
{
messageId: 'assertionsRequiresOneArgument',
column: 43,
line: 1,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.assertions(1,);})',
},
],
},
Expand Down Expand Up @@ -287,6 +303,22 @@ ruleTester.run('prefer-expect-assertions', rule, {
},
],
},
{
code: 'it("it1", function() {expect.hasAssertions("1",);})',
errors: [
{
messageId: 'hasAssertionsTakesNoArguments',
column: 30,
line: 1,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.hasAssertions();})',
},
],
},
],
},
{
code: 'it("it1", function() {expect.hasAssertions("1", "2");})',
errors: [
Expand Down
4 changes: 2 additions & 2 deletions src/rules/__tests__/prefer-expect-resolves.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ ruleTester.run('prefer-expect-resolves', rule, {
{
code: dedent`
it('passes', async () => {
expect(await someValue()).toBe(true);
expect(await someValue(),).toBe(true);
});
`,
output: dedent`
it('passes', async () => {
await expect(someValue()).resolves.toBe(true);
await expect(someValue(),).resolves.toBe(true);
});
`,
errors: [{ endColumn: 27, column: 10, messageId: 'expectResolves' }],
Expand Down
13 changes: 13 additions & 0 deletions src/rules/__tests__/prefer-mock-promise-shorthand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ ruleTester.run('prefer-mock-shorthand', rule, {
},
],
},
{
code: 'aVariable.mockImplementation(() => Promise.reject(42),)',
output: 'aVariable.mockRejectedValue(42,)',
parserOptions: { ecmaVersion: 2017 },
errors: [
{
messageId: 'useMockShorthand',
data: { replacement: 'mockRejectedValue' },
column: 11,
line: 1,
},
],
},
{
code: 'aVariable.mockImplementationOnce(() => Promise.resolve(42))',
output: 'aVariable.mockResolvedValueOnce(42)',
Expand Down
3 changes: 2 additions & 1 deletion src/rules/__tests__/prefer-spy-on.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ ruleTester.run('prefer-spy-on', rule, {
],
},
{
code: 'obj.a = jest.fn(() => 10)',
code: 'obj.a = jest.fn(() => 10,)',
output: "jest.spyOn(obj, 'a').mockImplementation(() => 10)",
parserOptions: { ecmaVersion: 2017 },
errors: [
{
messageId: 'useJestSpyOn',
Expand Down
17 changes: 17 additions & 0 deletions src/rules/__tests__/prefer-strict-equal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ ruleTester.run('prefer-strict-equal', rule, {
},
],
},
{
code: 'expect(something).toEqual(somethingElse,);',
parserOptions: { ecmaVersion: 2017 },
errors: [
{
messageId: 'useToStrictEqual',
column: 19,
line: 1,
suggestions: [
{
messageId: 'suggestReplaceWithStrictEqual',
output: 'expect(something).toStrictEqual(somethingElse,);',
},
],
},
],
},
{
code: 'expect(something)["toEqual"](somethingElse);',
errors: [
Expand Down
12 changes: 12 additions & 0 deletions src/rules/__tests__/prefer-to-be.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ ruleTester.run('prefer-to-be', rule, {
output: 'expect(value).toBe(1);',
errors: [{ messageId: 'useToBe', column: 15, line: 1 }],
},
{
code: 'expect(value).toStrictEqual(1,);',
output: 'expect(value).toBe(1,);',
parserOptions: { ecmaVersion: 2017 },
errors: [{ messageId: 'useToBe', column: 15, line: 1 }],
},
{
code: 'expect(value).toStrictEqual(-1);',
output: 'expect(value).toBe(-1);',
Expand Down Expand Up @@ -115,6 +121,12 @@ ruleTester.run('prefer-to-be: null', rule, {
output: 'expect(null).toBeNull();',
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
},
{
code: 'expect(null).toEqual(null,);',
output: 'expect(null).toBeNull();',
parserOptions: { ecmaVersion: 2017 },
errors: [{ messageId: 'useToBeNull', column: 14, line: 1 }],
},
{
code: 'expect(null).toStrictEqual(null);',
output: 'expect(null).toBeNull();',
Expand Down
6 changes: 6 additions & 0 deletions src/rules/__tests__/prefer-to-contain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ ruleTester.run('prefer-to-contain', rule, {
output: 'expect(a).toContain(b);',
errors: [{ messageId: 'useToContain', column: 23, line: 1 }],
},
{
code: 'expect(a.includes(b,),).toEqual(true,);',
output: 'expect(a,).toContain(b,);',
parserOptions: { ecmaVersion: 2017 },
errors: [{ messageId: 'useToContain', column: 25, line: 1 }],
},
{
code: "expect(a['includes'](b)).toEqual(true);",
output: 'expect(a).toContain(b);',
Expand Down
6 changes: 6 additions & 0 deletions src/rules/__tests__/prefer-to-have-length.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ ruleTester.run('prefer-to-have-length', rule, {
output: 'expect(files).toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
},
{
code: 'expect(files["length"]).toBe(1,);',
output: 'expect(files).toHaveLength(1,);',
parserOptions: { ecmaVersion: 2017 },
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
},
{
code: 'expect(files["length"])["not"].toBe(1);',
output: 'expect(files)["not"].toHaveLength(1);',
Expand Down
6 changes: 6 additions & 0 deletions src/rules/__tests__/prefer-todo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ ruleTester.run('prefer-todo', rule, {
output: 'test.todo("i need to write this test");',
errors: [{ messageId: 'unimplementedTest' }],
},
{
code: `test("i need to write this test",);`,
output: 'test.todo("i need to write this test",);',
parserOptions: { ecmaVersion: 2017 },
errors: [{ messageId: 'unimplementedTest' }],
},
{
code: 'test(`i need to write this test`);',
output: 'test.todo(`i need to write this test`);',
Expand Down
Loading