From 09d8493fe069aa06c147c303c5bd56677756448f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 14:39:19 -0700 Subject: [PATCH 01/92] chore: turn on the rule --- .eslintrc.js | 13 ++++++++++++- .../tests/rules/plugin-test-formatting.test.ts | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 39ca61de5168..cee2ec901cca 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -174,7 +174,7 @@ module.exports = { '@typescript-eslint/internal/no-typescript-estree-import': 'error', }, }, - // rule source files + // plugin rule source files { files: [ 'packages/eslint-plugin-internal/src/rules/**/*.ts', @@ -187,6 +187,17 @@ module.exports = { 'import/no-default-export': 'off', }, }, + // plugin rule tests + { + files: [ + 'packages/eslint-plugin-internal/tests/rules/**/*.test.ts', + 'packages/eslint-plugin-tslint/tests/rules/**/*.test.ts', + 'packages/eslint-plugin/tests/rules/**/*.test.ts', + ], + rules: { + '@typescript-eslint/internal/plugin-test-formatting': 'error', + }, + }, // tools and tests { files: ['**/tools/**/*.ts', '**/tests/**/*.ts'], diff --git a/packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts b/packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts index 8d7e0b2c1d85..c2af09bec90a 100644 --- a/packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts +++ b/packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts @@ -71,6 +71,7 @@ const a = 1; ${PARENT_INDENT}\``, wrap`noFormat\`const a = 1;\``, // sanity check suggestion validation + // eslint-disable-next-line @typescript-eslint/internal/plugin-test-formatting ` ruleTester.run({ invalid: [ From eb44069c87dd0d13b98f9f3303ac02155ddf80f8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 14:42:46 -0700 Subject: [PATCH 02/92] chore: adjacent-overload-signatures --- .../adjacent-overload-signatures.test.ts | 399 +++++++++--------- 1 file changed, 200 insertions(+), 199 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts b/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts index c040362a1286..4cb7eef0dbcf 100644 --- a/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts +++ b/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts @@ -11,9 +11,9 @@ ruleTester.run('adjacent-overload-signatures', rule, { code: ` function error(a: string); function error(b: number); -function error(ab: string|number){ } +function error(ab: string | number) {} export { error }; - `, + `, parserOptions: { sourceType: 'module' }, }, { @@ -231,7 +231,7 @@ export function foo(n: number); export function bar(): void {} export function baz(): void {} export function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -248,7 +248,7 @@ export function foo(n: number); export type bar = number; export type baz = number | string; export function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -265,7 +265,7 @@ function foo(n: number); function bar(): void {} function baz(): void {} function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -282,7 +282,7 @@ function foo(n: number); type bar = number; type baz = number | string; function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -296,10 +296,10 @@ function foo(sn: string | number) {} code: ` function foo(s: string) {} function foo(n: number) {} -const a = ""; -const b = ""; +const a = ''; +const b = ''; function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -315,7 +315,7 @@ function foo(s: string) {} function foo(n: number) {} class Bar {} function foo(sn: string | number) {} - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -331,18 +331,18 @@ function foo(s: string) {} function foo(n: number) {} function foo(sn: string | number) {} class Bar { - foo(s: string); - foo(n: number); - name: string; - foo(sn: string | number) { } + foo(s: string); + foo(n: number); + name: string; + foo(sn: string | number) {} } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 9, - column: 5, + column: 3, }, ], }, @@ -353,7 +353,7 @@ declare function foo(n: number); declare function bar(): void; declare function baz(): void; declare function foo(sn: string | number); - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -367,10 +367,10 @@ declare function foo(sn: string | number); code: ` declare function foo(s: string); declare function foo(n: number); -const a = ""; -const b = ""; +const a = ''; +const b = ''; declare function foo(sn: string | number); - `, + `, errors: [ { messageId: 'adjacentSignature', @@ -382,437 +382,438 @@ declare function foo(sn: string | number); }, { code: ` -declare module "Foo" { - export function foo(s: string): void; - export function foo(n: number): void; - export function bar(): void; - export function baz(): void; - export function foo(sn: string | number): void; +declare module 'Foo' { + export function foo(s: string): void; + export function foo(n: number): void; + export function bar(): void; + export function baz(): void; + export function foo(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` -declare module "Foo" { - export function foo(s: string): void; - export function foo(n: number): void; - export function foo(sn: string | number): void; - function baz(s: string): void; - export function bar(): void; - function baz(n: number): void; - function baz(sn: string | number): void; +declare module 'Foo' { + export function foo(s: string): void; + export function foo(n: number): void; + export function foo(sn: string | number): void; + function baz(s: string): void; + export function bar(): void; + function baz(n: number): void; + function baz(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'baz' }, line: 8, - column: 5, + column: 3, }, ], }, { code: ` declare namespace Foo { - export function foo(s: string): void; - export function foo(n: number): void; - export function bar(): void; - export function baz(): void; - export function foo(sn: string | number): void; + export function foo(s: string): void; + export function foo(n: number): void; + export function bar(): void; + export function baz(): void; + export function foo(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` declare namespace Foo { - export function foo(s: string): void; - export function foo(n: number): void; - export function foo(sn: string | number): void; - function baz(s: string): void; - export function bar(): void; - function baz(n: number): void; - function baz(sn: string | number): void; -} - `, + export function foo(s: string): void; + export function foo(n: number): void; + export function foo(sn: string | number): void; + function baz(s: string): void; + export function bar(): void; + function baz(n: number): void; + function baz(sn: string | number): void; +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'baz' }, line: 8, - column: 5, + column: 3, }, ], }, { code: ` type Foo = { - foo(s: string): void; - foo(n: number): void; - bar(): void; - baz(): void; - foo(sn: string | number): void; -} - `, + foo(s: string): void; + foo(n: number): void; + bar(): void; + baz(): void; + foo(sn: string | number): void; +}; + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` type Foo = { - foo(s: string): void; - ["foo"](n: number): void; - bar(): void; - baz(): void; - foo(sn: string | number): void; -} - `, + foo(s: string): void; + ['foo'](n: number): void; + bar(): void; + baz(): void; + foo(sn: string | number): void; +}; + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` type Foo = { - foo(s: string): void; - name: string; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; -} - `, + foo(s: string): void; + name: string; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; +}; + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 5, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - (s: string): void; - foo(n: number): void; - (n: number): void; - (sn: string | number): void; - bar(): void; - baz(): void; -} - `, + (s: string): void; + foo(n: number): void; + (n: number): void; + (sn: string | number): void; + bar(): void; + baz(): void; +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'call' }, line: 5, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - foo(s: string): void; - foo(n: number): void; - bar(): void; - baz(): void; - foo(sn: string | number): void; + foo(s: string): void; + foo(n: number): void; + bar(): void; + baz(): void; + foo(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - foo(s: string): void; - ["foo"](n: number): void; - bar(): void; - baz(): void; - foo(sn: string | number): void; + foo(s: string): void; + ['foo'](n: number): void; + bar(): void; + baz(): void; + foo(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - foo(s: string): void; - "foo"(n: number): void; - bar(): void; - baz(): void; - foo(sn: string | number): void; + foo(s: string): void; + 'foo'(n: number): void; + bar(): void; + baz(): void; + foo(sn: string | number): void; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - foo(s: string): void; - name: string; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; -} - `, + foo(s: string): void; + name: string; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 5, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { + foo(): void; + bar: { + baz(s: string): void; + baz(n: number): void; foo(): void; - bar: { - baz(s: string): void; - baz(n: number): void; - foo(): void; - baz(sn: string | number): void; - } + baz(sn: string | number): void; + }; } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'baz' }, line: 8, - column: 9, + column: 5, }, ], }, { code: ` interface Foo { - new(s: string); - new(n: number); - foo(): void; - bar(): void; - new(sn: string | number); + new (s: string); + new (n: number); + foo(): void; + bar(): void; + new (sn: string | number); } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'new' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` interface Foo { - new(s: string); - foo(): void; - new(n: number); - bar(): void; - new(sn: string | number); + new (s: string); + foo(): void; + new (n: number); + bar(): void; + new (sn: string | number); } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'new' }, line: 5, - column: 5, + column: 3, }, { messageId: 'adjacentSignature', data: { name: 'new' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - constructor(s: string); - constructor(n: number); - bar(): void {} - baz(): void {} - constructor(sn: string | number) {} + constructor(s: string); + constructor(n: number); + bar(): void {} + baz(): void {} + constructor(sn: string | number) {} } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'constructor' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - foo(s: string): void; - foo(n: number): void; - bar(): void {} - baz(): void {} - foo(sn: string | number): void {} + foo(s: string): void; + foo(n: number): void; + bar(): void {} + baz(): void {} + foo(sn: string | number): void {} } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - foo(s: string): void; - ["foo"](n: number): void; - bar(): void {} - baz(): void {} - foo(sn: string | number): void {} + foo(s: string): void; + ['foo'](n: number): void; + bar(): void {} + baz(): void {} + foo(sn: string | number): void {} } - `, + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 7, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - foo(s: string): void; - "foo"(n: number): void; - bar(): void {} - baz(): void {} - foo(sn: string | number): void {} -} - `, + // prettier-ignore + "foo"(s: string): void; + foo(n: number): void; + bar(): void {} + baz(): void {} + foo(sn: string | number): void {} +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, - line: 7, - column: 5, + line: 8, + column: 3, }, ], }, { code: ` class Foo { - constructor(s: string); - name: string; - constructor(n: number); - constructor(sn: string | number) {} - bar(): void {} - baz(): void {} -} - `, + constructor(s: string); + name: string; + constructor(n: number); + constructor(sn: string | number) {} + bar(): void {} + baz(): void {} +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'constructor' }, line: 5, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - foo(s: string): void; - name: string; - foo(n: number): void; - foo(sn: string | number): void {} - bar(): void {} - baz(): void {} -} - `, + foo(s: string): void; + name: string; + foo(n: number): void; + foo(sn: string | number): void {} + bar(): void {} + baz(): void {} +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'foo' }, line: 5, - column: 5, + column: 3, }, ], }, { code: ` class Foo { - static foo(s: string): void; - name: string; - static foo(n: number): void; - static foo(sn: string | number): void {} - bar(): void {} - baz(): void {} -} - `, + static foo(s: string): void; + name: string; + static foo(n: number): void; + static foo(sn: string | number): void {} + bar(): void {} + baz(): void {} +} + `, errors: [ { messageId: 'adjacentSignature', data: { name: 'static foo' }, line: 5, - column: 5, + column: 3, }, ], }, From 9d712df6609c019d4122eee57b868f65c4d3e553 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 14:42:58 -0700 Subject: [PATCH 03/92] chore: array-type --- .../tests/rules/array-type.test.ts | 430 ++++++++++-------- 1 file changed, 244 insertions(+), 186 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index b74c06c09fd1..5f15ba5e8220 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -10,35 +10,35 @@ const ruleTester = new RuleTester({ ruleTester.run('array-type', rule, { valid: [ { - code: 'let a: readonly any[] = []', + code: 'let a: readonly any[] = [];', options: [{ default: 'array' }], }, { - code: 'let a = new Array()', + code: 'let a = new Array();', options: [{ default: 'array' }], }, { - code: 'let a: string[] = []', + code: 'let a: string[] = [];', options: [{ default: 'array' }], }, { - code: 'let a: (string | number)[] = []', + code: 'let a: (string | number)[] = [];', options: [{ default: 'array' }], }, { - code: 'let a: ({ foo: Bar[] })[] = []', + code: 'let a: { foo: Bar[] }[] = [];', options: [{ default: 'array' }], }, { - code: 'let a: Array = []', + code: 'let a: Array = [];', options: [{ default: 'generic' }], }, { - code: 'let a: Array = []', + code: 'let a: Array = [];', options: [{ default: 'generic' }], }, { - code: 'let a: Array<{ foo: Array }> = []', + code: 'let a: Array<{ foo: Array }> = [];', options: [{ default: 'generic' }], }, { @@ -46,7 +46,7 @@ ruleTester.run('array-type', rule, { options: [{ default: 'generic' }], }, { - code: 'function foo (a: Array): Array {}', + code: 'function foo(a: Array): Array {}', options: [{ default: 'generic' }], }, { @@ -54,15 +54,19 @@ ruleTester.run('array-type', rule, { options: [{ default: 'array-simple' }], }, { - code: `function fooFunction(foo: Array>) { - return foo.map(e => e.foo); -}`, + code: ` +function fooFunction(foo: Array>) { + return foo.map(e => e.foo); +} + `, options: [{ default: 'array-simple' }], }, { - code: `function bazFunction(baz: Arr>) { - return baz.map(e => e.baz); -}`, + code: ` +function bazFunction(baz: Arr>) { + return baz.map(e => e.baz); +} + `, options: [{ default: 'array-simple' }], }, { @@ -78,16 +82,20 @@ ruleTester.run('array-type', rule, { options: [{ default: 'array-simple' }], }, { - code: `namespace fooName { - type BarType = { bar: string }; - type BazType = Arr; -}`, + code: ` +namespace fooName { + type BarType = { bar: string }; + type BazType = Arr; +} + `, options: [{ default: 'array-simple' }], }, { - code: `interface FooInterface { - '.bar': {baz: string[];}; -}`, + code: ` +interface FooInterface { + '.bar': { baz: string[] }; +} + `, options: [{ default: 'array-simple' }], }, { @@ -95,19 +103,23 @@ ruleTester.run('array-type', rule, { options: [{ default: 'array' }], }, { - code: 'let ya = [[1, "2"]] as[number, string][];', + code: "let ya = [[1, '2']] as [number, string][];", options: [{ default: 'array' }], }, { - code: `function barFunction(bar: ArrayClass[]) { - return bar.map(e => e.bar); -}`, + code: ` +function barFunction(bar: ArrayClass[]) { + return bar.map(e => e.bar); +} + `, options: [{ default: 'array' }], }, { - code: `function bazFunction(baz: Arr>) { - return baz.map(e => e.baz); -}`, + code: ` +function bazFunction(baz: Arr>) { + return baz.map(e => e.baz); +} + `, options: [{ default: 'array' }], }, { @@ -115,7 +127,7 @@ ruleTester.run('array-type', rule, { options: [{ default: 'array' }], }, { - code: 'type barUnion = (string|number|boolean)[];', + code: 'type barUnion = (string | number | boolean)[];', options: [{ default: 'array' }], }, { @@ -123,18 +135,20 @@ ruleTester.run('array-type', rule, { options: [{ default: 'array' }], }, { - code: `interface FooInterface { - '.bar': {baz: string[];}; -}`, + code: ` +interface FooInterface { + '.bar': { baz: string[] }; +} + `, options: [{ default: 'array' }], }, { // https://github.com/typescript-eslint/typescript-eslint/issues/172 - code: 'type Unwrap = T extends (infer E)[] ? E : T', + code: 'type Unwrap = T extends (infer E)[] ? E : T;', options: [{ default: 'array' }], }, { - code: 'let z: Array = [3, "4"];', + code: "let z: Array = [3, '4'];", options: [{ default: 'generic' }], }, { @@ -146,15 +160,19 @@ ruleTester.run('array-type', rule, { options: [{ default: 'generic' }], }, { - code: `function fooFunction(foo: Array>) { - return foo.map(e => e.foo); -}`, + code: ` +function fooFunction(foo: Array>) { + return foo.map(e => e.foo); +} + `, options: [{ default: 'generic' }], }, { - code: `function bazFunction(baz: Arr>) { - return baz.map(e => e.baz); -}`, + code: ` +function bazFunction(baz: Arr>) { + return baz.map(e => e.baz); +} + `, options: [{ default: 'generic' }], }, { @@ -162,7 +180,7 @@ ruleTester.run('array-type', rule, { options: [{ default: 'generic' }], }, { - code: 'type fooUnion = Array;', + code: 'type fooUnion = Array;', options: [{ default: 'generic' }], }, { @@ -171,40 +189,40 @@ ruleTester.run('array-type', rule, { }, { // https://github.com/typescript-eslint/typescript-eslint/issues/172 - code: 'type Unwrap = T extends Array ? E : T', + code: 'type Unwrap = T extends Array ? E : T;', options: [{ default: 'generic' }], }, // readonly { - code: 'let a: string[] = []', + code: 'let a: string[] = [];', options: [{ default: 'array', readonly: 'generic' }], }, { - code: 'let a: ReadonlyArray = []', + code: 'let a: ReadonlyArray = [];', options: [{ default: 'array', readonly: 'generic' }], }, { - code: 'let a: ReadonlyArray = [[]]', + code: 'let a: ReadonlyArray = [[]];', options: [{ default: 'array', readonly: 'generic' }], }, { - code: 'let a: Array = []', + code: 'let a: Array = [];', options: [{ default: 'generic', readonly: 'array' }], }, { - code: 'let a: readonly number[] = []', + code: 'let a: readonly number[] = [];', options: [{ default: 'generic', readonly: 'array' }], }, { - code: 'let a: readonly Array[] = [[]]', + code: 'let a: readonly Array[] = [[]];', options: [{ default: 'generic', readonly: 'array' }], }, ], invalid: [ { - code: 'let a: Array = []', - output: 'let a: string[] = []', + code: 'let a: Array = [];', + output: 'let a: string[] = [];', options: [{ default: 'array' }], errors: [ { @@ -216,8 +234,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let a: Array = []', - output: 'let a: (string | number)[] = []', + code: 'let a: Array = [];', + output: 'let a: (string | number)[] = [];', options: [{ default: 'array' }], errors: [ { @@ -229,21 +247,21 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let a: ({ foo: Array })[] = []', - output: 'let a: ({ foo: Bar[] })[] = []', + code: 'let a: { foo: Array }[] = [];', + output: 'let a: { foo: Bar[] }[] = [];', options: [{ default: 'array' }], errors: [ { messageId: 'errorStringArray', data: { type: 'Bar' }, line: 1, - column: 16, + column: 15, }, ], }, { - code: 'let a: string[] = []', - output: 'let a: Array = []', + code: 'let a: string[] = [];', + output: 'let a: Array = [];', options: [{ default: 'generic' }], errors: [ { @@ -255,8 +273,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let a: (string | number)[] = []', - output: 'let a: Array = []', + code: 'let a: (string | number)[] = [];', + output: 'let a: Array = [];', options: [{ default: 'generic' }], errors: [ { @@ -268,8 +286,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let a: Array<{ foo: Bar[] }> = []', - output: 'let a: Array<{ foo: Array }> = []', + code: 'let a: Array<{ foo: Bar[] }> = [];', + output: 'let a: Array<{ foo: Array }> = [];', options: [{ default: 'generic' }], errors: [ { @@ -281,8 +299,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let a: Array<{ foo: Foo | Bar[] }> = []', - output: 'let a: Array<{ foo: Foo | Array }> = []', + code: 'let a: Array<{ foo: Foo | Bar[] }> = [];', + output: 'let a: Array<{ foo: Foo | Array }> = [];', options: [{ default: 'generic' }], errors: [ { @@ -294,27 +312,27 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'function foo (a: Array): Array {}', - output: 'function foo (a: Bar[]): Bar[] {}', + code: 'function foo(a: Array): Array {}', + output: 'function foo(a: Bar[]): Bar[] {}', options: [{ default: 'array' }], errors: [ { messageId: 'errorStringArray', data: { type: 'Bar' }, line: 1, - column: 18, + column: 17, }, { messageId: 'errorStringArray', data: { type: 'Bar' }, line: 1, - column: 31, + column: 30, }, ], }, { - code: 'let a: Array<>[] = []', - output: 'let a: any[][] = []', + code: 'let a: Array<>[] = [];', + output: 'let a: any[][] = [];', options: [{ default: 'array-simple' }], errors: [ { @@ -358,8 +376,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let y: string[] = >["2"];', - output: 'let y: string[] = ["2"];', + code: "let y: string[] = >['2'];", + output: "let y: string[] = ['2'];", options: [{ default: 'array-simple' }], errors: [ { @@ -371,8 +389,8 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let z: Array = [3, "4"];', - output: 'let z: any[] = [3, "4"];', + code: "let z: Array = [3, '4'];", + output: "let z: any[] = [3, '4'];", options: [{ default: 'array-simple' }], errors: [ { @@ -384,15 +402,15 @@ ruleTester.run('array-type', rule, { ], }, { - code: 'let ya = [[1, "2"]] as[number, string][];', - output: 'let ya = [[1, "2"]] as Array<[number, string]>;', + code: "let ya = [[1, '2']] as [number, string][];", + output: "let ya = [[1, '2']] as Array<[number, string]>;", options: [{ default: 'array-simple' }], errors: [ { messageId: 'errorStringGenericSimple', data: { type: 'T' }, line: 1, - column: 23, + column: 24, }, ], }, @@ -410,56 +428,68 @@ ruleTester.run('array-type', rule, { ], }, { - code: `// Ignore user defined aliases -let yyyy: Arr>[]> = [[[["2"]]]];`, - output: `// Ignore user defined aliases -let yyyy: Arr>>> = [[[["2"]]]];`, + code: ` +// Ignore user defined aliases +let yyyy: Arr>[]> = [[[['2']]]]; + `, + output: ` +// Ignore user defined aliases +let yyyy: Arr>>> = [[[['2']]]]; + `, options: [{ default: 'array-simple' }], errors: [ { messageId: 'errorStringGenericSimple', data: { type: 'T' }, - line: 2, + line: 3, column: 15, }, ], }, { - code: `interface ArrayClass { - foo: Array; - bar: T[]; - baz: Arr; - xyz: this[]; -}`, - output: `interface ArrayClass { - foo: T[]; - bar: T[]; - baz: Arr; - xyz: this[]; -}`, + code: ` +interface ArrayClass { + foo: Array; + bar: T[]; + baz: Arr; + xyz: this[]; +} + `, + output: ` +interface ArrayClass { + foo: T[]; + bar: T[]; + baz: Arr; + xyz: this[]; +} + `, options: [{ default: 'array-simple' }], errors: [ { messageId: 'errorStringArraySimple', data: { type: 'T' }, - line: 2, - column: 10, + line: 3, + column: 8, }, ], }, { - code: `function barFunction(bar: ArrayClass[]) { - return bar.map(e => e.bar); -}`, - output: `function barFunction(bar: Array>) { - return bar.map(e => e.bar); -}`, + code: ` +function barFunction(bar: ArrayClass[]) { + return bar.map(e => e.bar); +} + `, + output: ` +function barFunction(bar: Array>) { + return bar.map(e => e.bar); +} + `, options: [{ default: 'array-simple' }], errors: [ { messageId: 'errorStringGenericSimple', data: { type: 'T' }, - line: 1, + line: 2, column: 27, }, ], @@ -478,8 +508,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'type barUnion = (string|number|boolean)[];', - output: 'type barUnion = Array;', + code: 'type barUnion = (string | number | boolean)[];', + output: 'type barUnion = Array;', options: [{ default: 'array-simple' }], errors: [ { @@ -504,8 +534,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'let v: Array = [{ bar: "bar" }];', - output: 'let v: fooName.BarType[] = [{ bar: "bar" }];', + code: "let v: Array = [{ bar: 'bar' }];", + output: "let v: fooName.BarType[] = [{ bar: 'bar' }];", options: [{ default: 'array-simple' }], errors: [ { @@ -517,8 +547,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'let w: fooName.BazType[] = [["baz"]];', - output: 'let w: Array> = [["baz"]];', + code: "let w: fooName.BazType[] = [['baz']];", + output: "let w: Array> = [['baz']];", options: [{ default: 'array-simple' }], errors: [ { @@ -543,8 +573,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'let y: string[] = >["2"];', - output: 'let y: string[] = ["2"];', + code: "let y: string[] = >['2'];", + output: "let y: string[] = ['2'];", options: [{ default: 'array' }], errors: [ { @@ -556,8 +586,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'let z: Array = [3, "4"];', - output: 'let z: any[] = [3, "4"];', + code: "let z: Array = [3, '4'];", + output: "let z: any[] = [3, '4'];", options: [{ default: 'array' }], errors: [ { @@ -582,54 +612,66 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: `// Ignore user defined aliases -let yyyy: Arr>[]> = [[[["2"]]]];`, - output: `// Ignore user defined aliases -let yyyy: Arr[][]> = [[[["2"]]]];`, + code: ` +// Ignore user defined aliases +let yyyy: Arr>[]> = [[[['2']]]]; + `, + output: ` +// Ignore user defined aliases +let yyyy: Arr[][]> = [[[['2']]]]; + `, options: [{ default: 'array' }], errors: [ { messageId: 'errorStringArray', data: { type: 'T' }, - line: 2, + line: 3, column: 15, }, ], }, { - code: `interface ArrayClass { - foo: Array; - bar: T[]; - baz: Arr; -}`, - output: `interface ArrayClass { - foo: T[]; - bar: T[]; - baz: Arr; -}`, + code: ` +interface ArrayClass { + foo: Array; + bar: T[]; + baz: Arr; +} + `, + output: ` +interface ArrayClass { + foo: T[]; + bar: T[]; + baz: Arr; +} + `, options: [{ default: 'array' }], errors: [ { messageId: 'errorStringArray', data: { type: 'T' }, - line: 2, - column: 10, + line: 3, + column: 8, }, ], }, { - code: `function fooFunction(foo: Array>) { - return foo.map(e => e.foo); -}`, - output: `function fooFunction(foo: ArrayClass[]) { - return foo.map(e => e.foo); -}`, + code: ` +function fooFunction(foo: Array>) { + return foo.map(e => e.foo); +} + `, + output: ` +function fooFunction(foo: ArrayClass[]) { + return foo.map(e => e.foo); +} + `, options: [{ default: 'array' }], errors: [ { messageId: 'errorStringArray', data: { type: 'T' }, - line: 1, + line: 2, column: 27, }, ], @@ -648,8 +690,8 @@ let yyyy: Arr[][]> = [[[["2"]]]];`, ], }, { - code: 'type fooUnion = Array;', - output: 'type fooUnion = (string|number|boolean)[];', + code: 'type fooUnion = Array;', + output: 'type fooUnion = (string | number | boolean)[];', options: [{ default: 'array' }], errors: [ { @@ -711,8 +753,8 @@ let yyyy: Arr[][]> = [[[["2"]]]];`, ], }, { - code: 'let y: string[] = >["2"];', - output: 'let y: Array = >["2"];', + code: "let y: string[] = >['2'];", + output: "let y: Array = >['2'];", options: [{ default: 'generic' }], errors: [ { @@ -724,67 +766,79 @@ let yyyy: Arr[][]> = [[[["2"]]]];`, ], }, { - code: 'let ya = [[1, "2"]] as[number, string][];', - output: 'let ya = [[1, "2"]] as Array<[number, string]>;', + code: "let ya = [[1, '2']] as [number, string][];", + output: "let ya = [[1, '2']] as Array<[number, string]>;", options: [{ default: 'generic' }], errors: [ { messageId: 'errorStringGeneric', data: { type: 'T' }, line: 1, - column: 23, + column: 24, }, ], }, { - code: `// Ignore user defined aliases -let yyyy: Arr>[]> = [[[["2"]]]];`, - output: `// Ignore user defined aliases -let yyyy: Arr>>> = [[[["2"]]]];`, + code: ` +// Ignore user defined aliases +let yyyy: Arr>[]> = [[[['2']]]]; + `, + output: ` +// Ignore user defined aliases +let yyyy: Arr>>> = [[[['2']]]]; + `, options: [{ default: 'generic' }], errors: [ { messageId: 'errorStringGeneric', data: { type: 'T' }, - line: 2, + line: 3, column: 15, }, ], }, { - code: `interface ArrayClass { - foo: Array; - bar: T[]; - baz: Arr; -}`, - output: `interface ArrayClass { - foo: Array; - bar: Array; - baz: Arr; -}`, + code: ` +interface ArrayClass { + foo: Array; + bar: T[]; + baz: Arr; +} + `, + output: ` +interface ArrayClass { + foo: Array; + bar: Array; + baz: Arr; +} + `, options: [{ default: 'generic' }], errors: [ { messageId: 'errorStringGeneric', data: { type: 'T' }, - line: 3, - column: 10, + line: 4, + column: 8, }, ], }, { - code: `function barFunction(bar: ArrayClass[]) { - return bar.map(e => e.bar); -}`, - output: `function barFunction(bar: Array>) { - return bar.map(e => e.bar); -}`, + code: ` +function barFunction(bar: ArrayClass[]) { + return bar.map(e => e.bar); +} + `, + output: ` +function barFunction(bar: Array>) { + return bar.map(e => e.bar); +} + `, options: [{ default: 'generic' }], errors: [ { messageId: 'errorStringGeneric', data: { type: 'T' }, - line: 1, + line: 2, column: 27, }, ], @@ -803,8 +857,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'type barUnion = (string|number|boolean)[];', - output: 'type barUnion = Array;', + code: 'type barUnion = (string | number | boolean)[];', + output: 'type barUnion = Array;', options: [{ default: 'generic' }], errors: [ { @@ -829,26 +883,30 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: `interface FooInterface { - '.bar': {baz: string[];}; -}`, - output: `interface FooInterface { - '.bar': {baz: Array;}; -}`, + code: ` +interface FooInterface { + '.bar': { baz: string[] }; +} + `, + output: ` +interface FooInterface { + '.bar': { baz: Array }; +} + `, options: [{ default: 'generic' }], errors: [ { messageId: 'errorStringGeneric', data: { type: 'string' }, - line: 2, - column: 19, + line: 3, + column: 18, }, ], }, { // https://github.com/typescript-eslint/typescript-eslint/issues/172 - code: 'type Unwrap = T extends Array ? E : T', - output: 'type Unwrap = T extends (infer E)[] ? E : T', + code: 'type Unwrap = T extends Array ? E : T;', + output: 'type Unwrap = T extends (infer E)[] ? E : T;', options: [{ default: 'array' }], errors: [ { @@ -861,8 +919,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, }, { // https://github.com/typescript-eslint/typescript-eslint/issues/172 - code: 'type Unwrap = T extends (infer E)[] ? E : T', - output: 'type Unwrap = T extends Array ? E : T', + code: 'type Unwrap = T extends (infer E)[] ? E : T;', + output: 'type Unwrap = T extends Array ? E : T;', options: [{ default: 'generic' }], errors: [ { @@ -928,8 +986,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'const x: readonly number[] = []', - output: 'const x: ReadonlyArray = []', + code: 'const x: readonly number[] = [];', + output: 'const x: ReadonlyArray = [];', options: [{ default: 'array', readonly: 'generic' }], errors: [ { @@ -941,8 +999,8 @@ let yyyy: Arr>>> = [[[["2"]]]];`, ], }, { - code: 'const x: readonly number[][] = []', - output: 'const x: readonly Array[] = []', + code: 'const x: readonly number[][] = [];', + output: 'const x: readonly Array[] = [];', options: [{ default: 'generic', readonly: 'array' }], errors: [ { From 5807d58cdc5c5c253a871f0e1220bc4f8b02cbd9 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 15:11:16 -0700 Subject: [PATCH 04/92] chore: prefer-ast-types-enum --- .../tests/rules/prefer-ast-types-enum.test.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts b/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts index 2688e035b59c..c2ffd8e489e0 100644 --- a/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts +++ b/packages/eslint-plugin-internal/tests/rules/prefer-ast-types-enum.test.ts @@ -14,29 +14,29 @@ const ruleTester = new RuleTester({ ruleTester.run('prefer-ast-types-enum', rule, { valid: [ - 'node.type === "constructor"', - 'node.type === AST_NODE_TYPES.Literal', - 'node.type === AST_TOKEN_TYPES.Keyword', - 'node.type === 1', + "node.type === 'constructor';", + 'node.type === AST_NODE_TYPES.Literal;', + 'node.type === AST_TOKEN_TYPES.Keyword;', + 'node.type === 1;', ` - enum MY_ENUM { - Literal = 1 - } + enum MY_ENUM { + Literal = 1, + } `, ` - enum AST_NODE_TYPES { - Literal = 'Literal' - } + enum AST_NODE_TYPES { + Literal = 'Literal', + } `, ], invalid: batchedSingleLineTests({ code: ` -node.type === 'Literal' -node.type === 'Keyword' +node.type === 'Literal'; +node.type === 'Keyword'; `, output: ` -node.type === AST_NODE_TYPES.Literal -node.type === AST_TOKEN_TYPES.Keyword +node.type === AST_NODE_TYPES.Literal; +node.type === AST_TOKEN_TYPES.Keyword; `, errors: [ { From af9715e2821979f947747bd7a16513e681241019 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 15:28:22 -0700 Subject: [PATCH 05/92] chore: no-typescript-estree-import --- .../tests/rules/no-typescript-estree.test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts b/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts index a76e03b7c1d5..74b9478b58a1 100644 --- a/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts +++ b/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts @@ -10,20 +10,20 @@ const ruleTester = new RuleTester({ ruleTester.run('no-typescript-estree-import', rule, { valid: [ - 'import { foo } from "@typescript-eslint/experimental-utils";', - 'import foo from "@typescript-eslint/experimental-utils";', - 'import * as foo from "@typescript-eslint/experimental-utils";', + "import { foo } from '@typescript-eslint/experimental-utils';", + "import foo from '@typescript-eslint/experimental-utils';", + "import * as foo from '@typescript-eslint/experimental-utils';", ], invalid: batchedSingleLineTests({ code: ` -import { foo } from "@typescript-eslint/typescript-estree"; -import foo from "@typescript-eslint/typescript-estree"; -import * as foo from "@typescript-eslint/typescript-estree"; +import { foo } from '@typescript-eslint/typescript-estree'; +import foo from '@typescript-eslint/typescript-estree'; +import * as foo from '@typescript-eslint/typescript-estree'; `, output: ` -import { foo } from "@typescript-eslint/experimental-utils"; -import foo from "@typescript-eslint/experimental-utils"; -import * as foo from "@typescript-eslint/experimental-utils"; +import { foo } from '@typescript-eslint/experimental-utils'; +import foo from '@typescript-eslint/experimental-utils'; +import * as foo from '@typescript-eslint/experimental-utils'; `, errors: [ { From 5561c4b4878246e10c67eecd1497f1ed78f5bfe4 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 15:32:25 -0700 Subject: [PATCH 06/92] chore: unified-signatures --- .../tests/rules/unified-signatures.test.ts | 307 +++++++++--------- 1 file changed, 154 insertions(+), 153 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/unified-signatures.test.ts b/packages/eslint-plugin/tests/rules/unified-signatures.test.ts index e3438ff78856..3c109d67a03e 100644 --- a/packages/eslint-plugin/tests/rules/unified-signatures.test.ts +++ b/packages/eslint-plugin/tests/rules/unified-signatures.test.ts @@ -13,151 +13,152 @@ ruleTester.run('unified-signatures', rule, { function g(): void; function g(a: number, b: number): void; function g(a?: number, b?: number): void {} - `, + `, ` function rest(...xs: number[]): void; function rest(xs: number[], y: string): void; function rest(...args: any[]) {} -`, + `, ` class C { - constructor(); - constructor(a: number, b: number); - constructor(a?: number, b?: number) {} + constructor(); + constructor(a: number, b: number); + constructor(a?: number, b?: number) {} - a(): void; - a(a: number, b: number): void; - a(a?: number, b?: number): void {} + a(): void; + a(a: number, b: number): void; + a(a?: number, b?: number): void {} } -`, + `, // No error for arity difference greater than 1. ` interface I { - a2(): void; - a2(x: number, y: number): void; + a2(): void; + a2(x: number, y: number): void; } -`, + `, // No error for different return types. ` interface I { - a4(): void; - a4(x: number): number; + a4(): void; + a4(x: number): number; } -`, + `, // No error if one takes a type parameter and the other doesn't. ` interface I { - a5(x: T): T; - a5(x: number): number; + a5(x: T): T; + a5(x: number): number; } -`, + `, // No error if one is a rest parameter and other isn't. ` interface I { - b2(x: string): void; - b2(...x: number[]): void; + b2(x: string): void; + b2(...x: number[]): void; } -`, + `, // No error if both are rest parameters. (https://github.com/Microsoft/TypeScript/issues/5077) ` interface I { - b3(...x: number[]): void; - b3(...x: string[]): void; + b3(...x: number[]): void; + b3(...x: string[]): void; } -`, + `, // No error if one is optional and the other isn't. ` interface I { - c3(x: number): void; - c3(x?: string): void; + c3(x: number): void; + c3(x?: string): void; } -`, + `, // No error if they differ by 2 or more parameters. ` interface I { - d2(x: string, y: number): void; - d2(x: number, y: string): void; + d2(x: string, y: number): void; + d2(x: number, y: string): void; } -`, + `, // No conflict between static/non-static members. ` declare class D { - static a(); - a(x: number); + static a(); + a(x: number); } -`, + `, // Allow separate overloads if one is generic and the other isn't. ` interface Generic { - x(): void; - x(x: T[]): void; + x(): void; + x(x: T[]): void; } -`, + `, // Allow signatures if the type is not equal. ` interface I { - f(x1:number): void; - f(x1:boolean, x2?: number): void; + f(x1: number): void; + f(x1: boolean, x2?: number): void; } -`, + `, // AllowType parameters that are not equal ` function f(x: T[]): void; function f(x: T): void; - `, + `, // Same name, different scopes ` declare function foo(n: number): number; -declare module "hello" { +declare module 'hello' { function foo(n: number, s: string): number; } -`, + `, // children of block not checked to match TSLint ` { - function block(): number; - function block(n: number): number; - function block(n?: number): number { - return 3; - } + function block(): number; + function block(n: number): number; + function block(n?: number): number { + return 3; + } } -`, + `, ` export interface Foo { bar(baz: string): number[]; bar(): string[]; } -`, + `, ` -declare module "foo" { +declare module 'foo' { export default function(foo: number): string[]; } -`, + `, ` export default function(foo: number): string[]; -`, + `, // https://github.com/typescript-eslint/typescript-eslint/issues/740 ` -function p(key: string): Promise -function p(key: string, defaultValue: string): Promise -function p(key: string, defaultValue?: string): Promise -{ - const obj: Record = { } - return obj[key] || defaultValue +function p(key: string): Promise; +function p(key: string, defaultValue: string): Promise; +function p(key: string, defaultValue?: string): Promise { + const obj: Record = {}; + return obj[key] || defaultValue; } - `, + `, ` interface I { - p(x: T): Promise; - p(x: number): Promise; + p(x: T): Promise; + p(x: number): Promise; } - `, + `, ` function rest(...xs: number[]): Promise; function rest(xs: number[], y: string): Promise; -async function rest(...args: any[], y?: string): Promise { return y || args } -`, +async function rest(...args: any[], y?: string): Promise { + return y || args; +} + `, ], invalid: [ { @@ -165,9 +166,9 @@ async function rest(...args: any[], y?: string): Promise { re function f(x: number): void; function f(x: string): void; function f(x: any): any { - return x; + return x; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -187,7 +188,7 @@ function f(x: any): any { function opt(xs?: number[]): void; function opt(xs: number[], y: string): void; function opt(...args: any[]) {} -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -204,11 +205,11 @@ function opt(...args: any[]) {} // For 3 or more overloads, mentions the line. code: ` interface I { - a0(): void; - a0(x: string): string; - a0(x: number): void; + a0(): void; + a0(x: string): string; + a0(x: number): void; } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -217,7 +218,7 @@ interface I { 'This overload and the one on line 3 can be combined into one signature', }, line: 5, - column: 8, + column: 6, }, ], }, @@ -225,10 +226,10 @@ interface I { // Error for extra parameter. code: ` interface I { - a1(): void; - a1(x: number): void; + a1(): void; + a1(x: number): void; } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -237,7 +238,7 @@ interface I { 'These overloads can be combined into one signature', }, line: 4, - column: 8, + column: 6, }, ], }, @@ -245,10 +246,10 @@ interface I { // Error for arity difference greater than 1 if the additional parameters are all optional/rest. code: ` interface I { - a3(): void; - a3(x: number, y?: number, ...z: number[]): void; + a3(): void; + a3(x: number, y?: number, ...z: number[]): void; } -`, + `, errors: [ { messageId: 'omittingRestParameter', @@ -257,7 +258,7 @@ interface I { 'These overloads can be combined into one signature', }, line: 4, - column: 31, + column: 29, }, ], }, @@ -265,10 +266,10 @@ interface I { // Error if only one defines a rest parameter. code: ` interface I { - b(): void; - b(...x: number[]): void; + b(): void; + b(...x: number[]): void; } -`, + `, errors: [ { messageId: 'omittingRestParameter', @@ -277,7 +278,7 @@ interface I { 'These overloads can be combined into one signature', }, line: 4, - column: 7, + column: 5, }, ], }, @@ -285,10 +286,10 @@ interface I { // Error if only one defines an optional parameter. code: ` interface I { - c(): void; - c(x?: number): void; + c(): void; + c(x?: number): void; } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -297,7 +298,7 @@ interface I { 'These overloads can be combined into one signature', }, line: 4, - column: 7, + column: 5, }, ], }, @@ -305,10 +306,10 @@ interface I { // Error if both are optional. code: ` interface I { - c2(x?: number): void; - c2(x?: string): void; + c2(x?: number): void; + c2(x?: string): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -319,7 +320,7 @@ interface I { type2: 'string', }, line: 4, - column: 8, + column: 6, }, ], }, @@ -327,10 +328,10 @@ interface I { // Error for different types (could be a union) code: ` interface I { - d(x: number): void; - d(x: string): void; + d(x: number): void; + d(x: string): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -341,7 +342,7 @@ interface I { type2: 'string', }, line: 4, - column: 7, + column: 5, }, ], }, @@ -349,10 +350,10 @@ interface I { // Works for type literal and call signature too. code: ` type T = { - (): void; - (x: number): void; -} -`, + (): void; + (x: number): void; +}; + `, errors: [ { messageId: 'omittingSingleParameter', @@ -361,7 +362,7 @@ type T = { 'These overloads can be combined into one signature', }, line: 4, - column: 6, + column: 4, }, ], }, @@ -369,10 +370,10 @@ type T = { // Works for constructor. code: ` declare class C { - constructor(); - constructor(x: number); + constructor(); + constructor(x: number); } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -381,7 +382,7 @@ declare class C { 'These overloads can be combined into one signature', }, line: 4, - column: 17, + column: 15, }, ], }, @@ -389,10 +390,10 @@ declare class C { // Works with unions. code: ` interface I { - f(x: number); - f(x: string | boolean); + f(x: number); + f(x: string | boolean); } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -403,7 +404,7 @@ interface I { type2: 'string | boolean', }, line: 4, - column: 7, + column: 5, }, ], }, @@ -411,10 +412,10 @@ interface I { // Works with tuples. code: ` interface I { - f(x: number); - f(x: [string, boolean]); + f(x: number); + f(x: [string, boolean]); } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -425,17 +426,17 @@ interface I { type2: '[string, boolean]', }, line: 4, - column: 7, + column: 5, }, ], }, { code: ` interface Generic { - y(x: T[]): void; - y(x: T): void; + y(x: T[]): void; + y(x: T): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -446,7 +447,7 @@ interface Generic { type2: 'T', }, line: 4, - column: 7, + column: 5, }, ], }, @@ -455,7 +456,7 @@ interface Generic { code: ` function f(x: T[]): void; function f(x: T): void; -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -475,7 +476,7 @@ function f(x: T): void; code: ` function f(x: T[]): void; function f(x: T): void; -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -494,10 +495,10 @@ function f(x: T): void; // Works with abstract code: ` abstract class Foo { - public abstract f(x: number): void; - public abstract f(x: string): void; + public abstract f(x: number): void; + public abstract f(x: string): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -508,7 +509,7 @@ abstract class Foo { type2: 'string', }, line: 4, - column: 23, + column: 21, }, ], }, @@ -516,10 +517,10 @@ abstract class Foo { // Works with literals code: ` interface Foo { - "f"(x: string): void; - "f"(x: number): void; + 'f'(x: string): void; + 'f'(x: number): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -530,7 +531,7 @@ interface Foo { type2: 'number', }, line: 4, - column: 9, + column: 7, }, ], }, @@ -538,10 +539,10 @@ interface Foo { // Works with new constructor code: ` interface Foo { - new(x: string): Foo; - new(x: number): Foo; + new (x: string): Foo; + new (x: number): Foo; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -552,7 +553,7 @@ interface Foo { type2: 'number', }, line: 4, - column: 9, + column: 8, }, ], }, @@ -560,14 +561,14 @@ interface Foo { // Works with new computed properties code: ` enum Enum { - Func = "function", + Func = 'function', } interface IFoo { - [Enum.Func](x: string): void; - [Enum.Func](x: number): void; + [Enum.Func](x: string): void; + [Enum.Func](x: number): void; } -`, + `, errors: [ { messageId: 'singleParameterDifference', @@ -578,7 +579,7 @@ interface IFoo { type2: 'number', }, line: 8, - column: 17, + column: 15, }, ], }, @@ -586,10 +587,10 @@ interface IFoo { // Works with parameter properties. Note that this is invalid TypeScript syntax. code: ` class Foo { - constructor(readonly x: number); - constructor(readonly x: string); + constructor(readonly x: number); + constructor(readonly x: string); } - `, + `, errors: [ { messageId: 'singleParameterDifference', @@ -600,7 +601,7 @@ class Foo { type2: 'string', }, line: 4, - column: 17, + column: 15, }, ], }, @@ -608,10 +609,10 @@ class Foo { // Works with parameter properties. Note that this is invalid TypeScript syntax. code: ` class Foo { - constructor(readonly x: number); - constructor(readonly x: number, readonly y: string); + constructor(readonly x: number); + constructor(readonly x: number, readonly y: string); } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -620,7 +621,7 @@ class Foo { 'These overloads can be combined into one signature', }, line: 4, - column: 37, + column: 35, }, ], }, @@ -628,10 +629,10 @@ class Foo { // Works with parameter properties. Note that this is invalid TypeScript syntax. code: ` class Foo { - constructor(readonly x: number); - constructor(readonly x: number, readonly y?: string, readonly z?: string); + constructor(readonly x: number); + constructor(readonly x: number, readonly y?: string, readonly z?: string); } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -640,7 +641,7 @@ class Foo { 'These overloads can be combined into one signature', }, line: 4, - column: 58, + column: 56, }, ], }, @@ -648,7 +649,7 @@ class Foo { code: ` export function foo(line: number): number; export function foo(line: number, character?: number): number; -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -665,7 +666,7 @@ export function foo(line: number, character?: number): number; code: ` declare function foo(line: number): number; export function foo(line: number, character?: number): number; -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -680,11 +681,11 @@ export function foo(line: number, character?: number): number; }, { code: ` -declare module "foo" { +declare module 'foo' { export default function(foo: number): string[]; export default function(foo: number, bar?: string): string[]; } -`, + `, errors: [ { messageId: 'omittingSingleParameter', @@ -697,7 +698,7 @@ declare module "foo" { code: ` export default function(foo: number): string[]; export default function(foo: number, bar?: string): string[]; -`, + `, errors: [ { messageId: 'omittingSingleParameter', From 9275e778728d5891343fad662f813d184461c64f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 15:33:09 -0700 Subject: [PATCH 07/92] chore: unbound-method --- .../tests/rules/unbound-method.test.ts | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/unbound-method.test.ts b/packages/eslint-plugin/tests/rules/unbound-method.test.ts index 619b681c4ea5..10f0b5497621 100644 --- a/packages/eslint-plugin/tests/rules/unbound-method.test.ts +++ b/packages/eslint-plugin/tests/rules/unbound-method.test.ts @@ -44,10 +44,10 @@ function addContainsMethodsClassInvalid( ruleTester.run('unbound-method', rule, { valid: [ - 'Promise.resolve().then(console.log)', - '["1", "2", "3"].map(Number.parseInt)', + 'Promise.resolve().then(console.log);', + "['1', '2', '3'].map(Number.parseInt);", '[5.2, 7.1, 3.6].map(Math.floor);', - 'const x = console.log', + 'const x = console.log;', ...[ 'instance.bound();', 'instance.unbound();', @@ -150,14 +150,14 @@ ruleTester.run('unbound-method', rule, { ].map(addContainsMethodsClass), ` interface RecordA { - readonly type: "A" - readonly a: {} + readonly type: 'A'; + readonly a: {}; } interface RecordB { - readonly type: "B" - readonly b: {} + readonly type: 'B'; + readonly b: {}; } -type AnyRecord = RecordA | RecordB +type AnyRecord = RecordA | RecordB; function test(obj: AnyRecord) { switch (obj.type) { @@ -167,9 +167,9 @@ function test(obj: AnyRecord) { // https://github.com/typescript-eslint/typescript-eslint/issues/496 ` class CommunicationError { - constructor() { + constructor() { const x = CommunicationError.prototype; - } + } } `, ` @@ -192,8 +192,10 @@ function foo(instance: ContainsMethods | null) { instance?.bound++; - if (instance?.bound) { } - if (instance?.unbound) { } + if (instance?.bound) { + } + if (instance?.unbound) { + } typeof instance?.bound === 'function'; typeof instance?.unbound === 'function'; @@ -202,13 +204,13 @@ function foo(instance: ContainsMethods | null) { // https://github.com/typescript-eslint/typescript-eslint/issues/1425 ` interface OptionalMethod { - mightBeDefined?(): void + mightBeDefined?(): void; } const x: OptionalMethod = {}; declare const myCondition: boolean; -if(myCondition || x.mightBeDefined) { - console.log('hello world') +if (myCondition || x.mightBeDefined) { + console.log('hello world'); } `, ], @@ -296,7 +298,7 @@ class ContainsMethods { new ContainsMethods().unbound; ContainsMethods.unboundStatic; -`, + `, options: [ { ignoreStatic: true, @@ -326,7 +328,7 @@ const x = CommunicationError.prototype.foo; }, { // Promise.all is not auto-bound to Promise - code: 'const x = Promise.all', + code: 'const x = Promise.all;', errors: [ { line: 1, From dc960ba799fa21db040fe980d408d2b98e318a0d Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 15:51:15 -0700 Subject: [PATCH 08/92] chore: typedef --- .../eslint-plugin/tests/rules/typedef.test.ts | 412 +++++++++++------- 1 file changed, 262 insertions(+), 150 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/typedef.test.ts b/packages/eslint-plugin/tests/rules/typedef.test.ts index bbb1a49c0186..d6b37c811ed5 100644 --- a/packages/eslint-plugin/tests/rules/typedef.test.ts +++ b/packages/eslint-plugin/tests/rules/typedef.test.ts @@ -15,7 +15,7 @@ ruleTester.run('typedef', rule, { valid: [ // Array destructuring { - code: `const [a]: [number] = [1]`, + code: 'const [a]: [number] = [1];', options: [ { arrayDestructuring: true, @@ -23,7 +23,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const [a, b]: [number, number] = [1, 2]`, + code: 'const [a, b]: [number, number] = [1, 2];', options: [ { arrayDestructuring: true, @@ -31,7 +31,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const [a] = 1;`, + code: 'const [a] = 1;', options: [ { arrayDestructuring: false, @@ -39,7 +39,10 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const [key, val] of new Map([['key', 1]])) {}`, + code: ` + for (const [key, val] of new Map([['key', 1]])) { + } + `, options: [ { arrayDestructuring: true, @@ -47,7 +50,10 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const [[key]] of [[['key']]]) {}`, + code: ` + for (const [[key]] of [[['key']]]) { + } + `, options: [ { arrayDestructuring: true, @@ -55,20 +61,25 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const [[{ key }]] of [[[{ key: 'value' }]]]) {}`, + code: ` + for (const [[{ key }]] of [[[{ key: 'value' }]]]) { + } + `, options: [ { arrayDestructuring: true, }, ], }, - `let a: number; - [a] = [1];`, + ` + let a: number; + [a] = [1]; + `, // Arrow parameters - `((a: number): void => {})()`, - `((a: string, b: string): void => {})()`, + '((a: number): void => {})();', + '((a: string, b: string): void => {})();', { - code: `((a: number): void => { })()`, + code: '((a: number): void => {})();', options: [ { arrowParameter: false, @@ -76,7 +87,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `((a: string, b: string): void => { })()`, + code: '((a: string, b: string): void => {})();', options: [ { arrowParameter: false, @@ -84,16 +95,22 @@ ruleTester.run('typedef', rule, { ], }, // Member variable declarations - `class Test { - state: number; - }`, - `class Test { - state: number = 1; - }`, + ` + class Test { + state: number; + } + `, + ` + class Test { + state: number = 1; + } + `, { - code: `class Test { - state = 1; - }`, + code: ` + class Test { + state = 1; + } + `, options: [ { memberVariableDeclaration: false, @@ -102,7 +119,7 @@ ruleTester.run('typedef', rule, { }, // Object destructuring { - code: `const { a }: { a: number } = { a: 1 }`, + code: 'const { a }: { a: number } = { a: 1 };', options: [ { objectDestructuring: true, @@ -110,7 +127,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const { a, b }: { [i: string]: number } = { a: 1, b: 2 }`, + code: 'const { a, b }: { [i: string]: number } = { a: 1, b: 2 };', options: [ { objectDestructuring: true, @@ -118,7 +135,14 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const {p1: {p2: { p3 }}} of [{p1: {p2: {p3: 'value'}}}]) {}`, + code: ` + for (const { + p1: { + p2: { p3 }, + }, + } of [{ p1: { p2: { p3: 'value' } } }]) { + } + `, options: [ { objectDestructuring: true, @@ -126,7 +150,16 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const {p1: {p2: { p3: [key] }}} of [{p1: {p2: {p3: ['value']}}}]) {}`, + code: ` + for (const { + p1: { + p2: { + p3: [key], + }, + }, + } of [{ p1: { p2: { p3: ['value'] } } }]) { + } + `, options: [ { objectDestructuring: true, @@ -134,7 +167,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const { a } = { a: 1 };`, + code: 'const { a } = { a: 1 };', options: [ { objectDestructuring: false, @@ -142,7 +175,10 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const { key, val } of [{ key: 'key', val: 1 }]) {}`, + code: ` + for (const { key, val } of [{ key: 'key', val: 1 }]) { + } + `, options: [ { objectDestructuring: true, @@ -150,50 +186,76 @@ ruleTester.run('typedef', rule, { ], }, // Function parameters - `function receivesNumber(a: number): void { }`, - `function receivesStrings(a: string, b: string): void { }`, - `function receivesNumber([a]: [number]): void { }`, - `function receivesNumbers([a, b]: number[]): void { }`, - `function receivesString({ a }: { a: string }): void { }`, - `function receivesStrings({ a, b }: { [i: string ]: string }): void { }`, - `function receivesNumber(a: number = 123): void { }`, + 'function receivesNumber(a: number): void {}', + 'function receivesStrings(a: string, b: string): void {}', + 'function receivesNumber([a]: [number]): void {}', + 'function receivesNumbers([a, b]: number[]): void {}', + 'function receivesString({ a }: { a: string }): void {}', + 'function receivesStrings({ a, b }: { [i: string]: string }): void {}', + 'function receivesNumber(a: number = 123): void {}', // Constructor parameters - `class Test { - constructor() {} - }`, - `class Test { - constructor(param: string) {} - }`, - `class Test { - constructor(param: string = 'something') {} - }`, - `class Test { - constructor(private param: string = 'something') {} - }`, + ` + class Test { + constructor() {} + } + `, + ` + class Test { + constructor(param: string) {} + } + `, + ` + class Test { + constructor(param: string = 'something') {} + } + `, + ` + class Test { + constructor(private param: string = 'something') {} + } + `, // Method parameters - `class Test { - public method(x: number): number { return x; } - }`, - `class Test { - public method(x: number = 123): number { return x; } - }`, + ` + class Test { + public method(x: number): number { + return x; + } + } + `, + ` + class Test { + public method(x: number = 123): number { + return x; + } + } + `, // Property declarations - `type Test = { - member: number; - };`, - `type Test = { - [i: string]: number; - };`, - `interface Test { - member: string; - };`, - `interface Test { - [i: number]: string; - };`, - { - code: `type Test = { - member; - };`, + ` + type Test = { + member: number; + }; + `, + ` + type Test = { + [i: string]: number; + }; + `, + ` + interface Test { + member: string; + } + `, + ` + interface Test { + [i: number]: string; + } + `, + { + code: ` + type Test = { + member; + }; + `, options: [ { propertyDeclaration: false, @@ -201,9 +263,12 @@ ruleTester.run('typedef', rule, { ], }, { - code: `type Test = { - [i: string]; - };`, + code: ` + type Test = { + // prettier-ignore + [i: string]; + }; + `, options: [ { propertyDeclaration: false, @@ -212,7 +277,7 @@ ruleTester.run('typedef', rule, { }, // Variable declarations { - code: `const x: string = "";`, + code: "const x: string = '';", options: [ { variableDeclaration: true, @@ -220,7 +285,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `let x: string = "";`, + code: "let x: string = '';", options: [ { variableDeclaration: true, @@ -228,7 +293,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `let x: string;`, + code: 'let x: string;', options: [ { variableDeclaration: true, @@ -236,7 +301,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const a = 1;`, + code: 'const a = 1;', options: [ { variableDeclaration: false, @@ -244,7 +309,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `let a;`, + code: 'let a;', options: [ { variableDeclaration: false, @@ -252,7 +317,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `let a = 1;`, + code: 'let a = 1;', options: [ { variableDeclaration: false, @@ -260,7 +325,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const [a, b] = [1, 2];`, + code: 'const [a, b] = [1, 2];', options: [ { objectDestructuring: false, @@ -269,7 +334,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const { a, b } = { a: '', b: '' };`, + code: "const { a, b } = { a: '', b: '' };", options: [ { objectDestructuring: false, @@ -279,7 +344,10 @@ ruleTester.run('typedef', rule, { }, // Contexts where TypeScript doesn't allow annotations { - code: `for (x of [1, 2, 3]) { }`, + code: ` + for (x of [1, 2, 3]) { + } + `, options: [ { variableDeclaration: true, @@ -287,7 +355,10 @@ ruleTester.run('typedef', rule, { ], }, { - code: `for (const x in {}) { }`, + code: ` + for (const x in {}) { + } + `, options: [ { variableDeclaration: true, @@ -295,7 +366,10 @@ ruleTester.run('typedef', rule, { ], }, { - code: `try { } catch (e) { }`, + code: ` + try { + } catch (e) {} + `, options: [ { variableDeclaration: true, @@ -304,7 +378,7 @@ ruleTester.run('typedef', rule, { }, // variable declaration ignore function { - code: `const foo = function(): void {};`, + code: 'const foo = function(): void {};', options: [ { variableDeclaration: true, @@ -313,7 +387,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const foo = (): void => {};`, + code: 'const foo = (): void => {};', options: [ { variableDeclaration: true, @@ -322,7 +396,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const foo: () => void = (): void => {};`, + code: 'const foo: () => void = (): void => {};', options: [ { variableDeclaration: true, @@ -331,7 +405,7 @@ ruleTester.run('typedef', rule, { ], }, { - code: `const foo: () => void = function (): void {};`, + code: 'const foo: () => void = function(): void {};', options: [ { variableDeclaration: true, @@ -343,7 +417,7 @@ ruleTester.run('typedef', rule, { code: ` class Foo { a = (): void => {}; - b = function (): void {}; + b = function(): void {}; } `, options: [ @@ -357,7 +431,7 @@ class Foo { invalid: [ // Array destructuring { - code: `const [a] = [1]`, + code: 'const [a] = [1];', errors: [ { messageId: 'expectedTypedef', @@ -370,7 +444,7 @@ class Foo { ], }, { - code: `const [a, b] = [1, 2]`, + code: 'const [a, b] = [1, 2];', errors: [ { messageId: 'expectedTypedef', @@ -384,7 +458,7 @@ class Foo { }, // Object destructuring { - code: `const { a } = { a: 1 }`, + code: 'const { a } = { a: 1 };', errors: [ { messageId: 'expectedTypedef', @@ -397,7 +471,7 @@ class Foo { ], }, { - code: `const { a, b } = { a: 1, b: 2 }`, + code: 'const { a, b } = { a: 1, b: 2 };', errors: [ { messageId: 'expectedTypedef', @@ -411,7 +485,7 @@ class Foo { }, // Arrow parameters { - code: `const receivesNumber = (a): void => { }`, + code: 'const receivesNumber = (a): void => {};', errors: [ { data: { name: 'a' }, @@ -420,7 +494,7 @@ class Foo { ], }, { - code: `const receivesStrings = (a, b): void => { }`, + code: 'const receivesStrings = (a, b): void => {};', errors: [ { data: { name: 'a' }, @@ -434,9 +508,11 @@ class Foo { }, // Member variable declarations { - code: `class Test { - state = 1; - }`, + code: ` + class Test { + state = 1; + } + `, errors: [ { data: { name: 'state' }, @@ -445,9 +521,11 @@ class Foo { ], }, { - code: `class Test { - ["state"] = 1; - }`, + code: ` + class Test { + ['state'] = 1; + } + `, errors: [ { messageId: 'expectedTypedef', @@ -456,7 +534,7 @@ class Foo { }, // Function parameters { - code: `function receivesNumber(a): void { }`, + code: 'function receivesNumber(a): void {}', errors: [ { data: { name: 'a' }, @@ -465,7 +543,7 @@ class Foo { ], }, { - code: `function receivesStrings(a, b): void { }`, + code: 'function receivesStrings(a, b): void {}', errors: [ { data: { name: 'a' }, @@ -478,7 +556,7 @@ class Foo { ], }, { - code: `function receivesNumber([a]): void { }`, + code: 'function receivesNumber([a]): void {}', errors: [ { column: 25, @@ -487,7 +565,7 @@ class Foo { ], }, { - code: `function receivesNumbers([a, b]): void { }`, + code: 'function receivesNumbers([a, b]): void {}', errors: [ { column: 26, @@ -496,7 +574,7 @@ class Foo { ], }, { - code: `function receivesString({ a }): void { }`, + code: 'function receivesString({ a }): void {}', errors: [ { column: 25, @@ -505,7 +583,7 @@ class Foo { ], }, { - code: `function receivesStrings({ a, b }): void { }`, + code: 'function receivesStrings({ a, b }): void {}', errors: [ { column: 26, @@ -515,77 +593,95 @@ class Foo { }, // Constructor parameters { - code: `class Test { - constructor(param) {} - }`, + code: ` + class Test { + constructor(param) {} + } + `, errors: [ { - column: 21, + column: 23, messageId: 'expectedTypedefNamed', }, ], }, { - code: `class Test { - constructor(param = 'something') {} - }`, + code: ` + class Test { + constructor(param = 'something') {} + } + `, errors: [ { - column: 21, + column: 23, messageId: 'expectedTypedef', }, ], }, { - code: `class Test { - constructor(private param = 'something') {} - }`, + code: ` + class Test { + constructor(private param = 'something') {} + } + `, errors: [ { - column: 21, + column: 23, messageId: 'expectedTypedef', }, ], }, // Method parameters { - code: `class Test { - public method(x): number { return x; } - }`, + code: ` + class Test { + public method(x): number { + return x; + } + } + `, errors: [ { - column: 23, + column: 25, messageId: 'expectedTypedefNamed', }, ], }, { - code: `class Test { - public method(x = 123): number { return x; } - }`, + code: ` + class Test { + public method(x = 123): number { + return x; + } + } + `, errors: [ { - column: 23, + column: 25, messageId: 'expectedTypedef', }, ], }, { - code: `class Test { - public constructor(public x) { } - }`, + code: ` + class Test { + public constructor(public x) {} + } + `, errors: [ { - column: 28, + column: 30, messageId: 'expectedTypedef', }, ], }, // Property declarations { - code: `type Test = { - member; - };`, + code: ` + type Test = { + member; + }; + `, errors: [ { data: { name: 'member' }, @@ -594,9 +690,12 @@ class Foo { ], }, { - code: `type Test = { - [i: string]; - };`, + code: ` + type Test = { + // prettier-ignore + [i: string]; + }; + `, errors: [ { messageId: 'expectedTypedef', @@ -604,9 +703,11 @@ class Foo { ], }, { - code: `interface Test { - member; - };`, + code: ` + interface Test { + member; + } + `, errors: [ { data: { name: 'member' }, @@ -615,9 +716,12 @@ class Foo { ], }, { - code: `interface Test { - [i: string]; - };`, + code: ` + interface Test { + // prettier-ignore + [i: string]; + } + `, errors: [ { messageId: 'expectedTypedef', @@ -626,7 +730,7 @@ class Foo { }, // Variable declarations { - code: `const a = 1;`, + code: 'const a = 1;', errors: [ { data: { name: 'a' }, @@ -640,7 +744,11 @@ class Foo { ], }, { - code: `const a = 1, b: number = 2, c = 3;`, + code: ` + const a = 1, + b: number = 2, + c = 3; + `, errors: [ { data: { name: 'a' }, @@ -658,7 +766,7 @@ class Foo { ], }, { - code: `let a;`, + code: 'let a;', errors: [ { data: { name: 'a' }, @@ -672,7 +780,7 @@ class Foo { ], }, { - code: `let a = 1;`, + code: 'let a = 1;', errors: [ { data: { name: 'a' }, @@ -686,7 +794,11 @@ class Foo { ], }, { - code: `let a = 1, b: number, c = 2;`, + code: ` + let a = 1, + b: number, + c = 2; + `, errors: [ { data: { name: 'a' }, @@ -704,7 +816,7 @@ class Foo { ], }, { - code: `const foo = 'foo'`, + code: "const foo = 'foo';", errors: [ { messageId: 'expectedTypedefNamed', @@ -719,7 +831,7 @@ class Foo { ], }, { - code: `const foo = function(): void {};`, + code: 'const foo = function(): void {};', errors: [ { messageId: 'expectedTypedefNamed', @@ -734,7 +846,7 @@ class Foo { ], }, { - code: `const foo = (): void => {};`, + code: 'const foo = (): void => {};', errors: [ { messageId: 'expectedTypedefNamed', @@ -752,7 +864,7 @@ class Foo { code: ` class Foo { a = (): void => {}; - b = function (): void {}; + b = function(): void {}; } `, errors: [ From ea6891ffae99bb1fd0555b27b446d80a490dc6da Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 17:28:23 -0700 Subject: [PATCH 09/92] chore: type-annotation-spacing --- .../rules/type-annotation-spacing.test.ts | 909 +++++++++--------- 1 file changed, 457 insertions(+), 452 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts b/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts index 6b0a3682578c..de714cff43f4 100644 --- a/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import { TSESLint } from '@typescript-eslint/experimental-utils'; import { RuleTester } from '../RuleTester'; import rule from '../../src/rules/type-annotation-spacing'; @@ -19,7 +24,7 @@ ruleTester.run('type-annotation-spacing', rule, { interface resolve { resolver: (() => PromiseLike) | PromiseLike; } - `, + `, 'const foo = {} as Foo;', 'let foo: string;', 'function foo(): void {}', @@ -28,63 +33,63 @@ interface resolve { class Foo { name: string; } - `, + `, ` class Foo { constructor(message: string); } - `, + `, ` class Foo { greet(): string { return "hello"; } } - `, + `, ` class Foo { greet(name: string): string { return name; } } - `, + `, ` interface Foo { name: string; } - `, + `, ` interface Foo { greet(): string; } - `, + `, ` interface Foo { greet(name: string): string; } - `, + `, ` interface Foo { thing: { [key in string]: number }; } - `, + `, ` type Foo = { name: string; } - `, + `, ` type Foo = { greet(): string; } - `, + `, ` type Foo = { greet(name: string): string; } - `, + `, 'type Foo = (name: string) => string;', ` type Foo = { greet: (name: string) => string; } - `, + `, ` const isString = (): x is string => { } @@ -106,7 +111,7 @@ const isString = (): x is string => { class Foo { name: string; } - `, + `, options: [{ after: true }], }, { @@ -114,7 +119,7 @@ class Foo { class Foo { constructor(message: string); } - `, + `, options: [{ after: true }], }, { @@ -122,7 +127,7 @@ class Foo { class Foo { greet(): string { return "hello"; } } - `, + `, options: [{ after: true }], }, { @@ -130,7 +135,7 @@ class Foo { class Foo { greet(name: string): string { return name; } } - `, + `, options: [{ after: true }], }, { @@ -138,7 +143,7 @@ class Foo { interface Foo { name: string; } - `, + `, options: [{ after: true }], }, { @@ -146,7 +151,7 @@ interface Foo { interface Foo { greet(): string; } - `, + `, options: [{ after: true }], }, { @@ -154,7 +159,7 @@ interface Foo { interface Foo { greet(name: string): string; } - `, + `, options: [{ after: true }], }, { @@ -162,7 +167,7 @@ interface Foo { type Foo = { name: string; } - `, + `, options: [{ after: true }], }, { @@ -170,7 +175,7 @@ type Foo = { type Foo = { greet(): string; } - `, + `, options: [{ after: true }], }, { @@ -178,7 +183,7 @@ type Foo = { type Foo = { greet(name: string): string; } - `, + `, options: [{ after: true }], }, { @@ -190,7 +195,7 @@ type Foo = { type Foo = { greet: (name: string) => string; } - `, + `, options: [{ after: true }], }, { @@ -210,7 +215,7 @@ type Foo = { class Foo { name: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -218,7 +223,7 @@ class Foo { class Foo { constructor(message: string); } - `, + `, options: [{ after: true, before: false }], }, { @@ -226,7 +231,7 @@ class Foo { class Foo { greet(): string { return "hello"; } } - `, + `, options: [{ after: true, before: false }], }, { @@ -234,7 +239,7 @@ class Foo { class Foo { greet(name: string): string { return name; } } - `, + `, options: [{ after: true, before: false }], }, { @@ -242,7 +247,7 @@ class Foo { interface Foo { name: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -250,7 +255,7 @@ interface Foo { interface Foo { greet(): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -258,7 +263,7 @@ interface Foo { interface Foo { greet(name: string): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -266,7 +271,7 @@ interface Foo { type Foo = { name: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -274,7 +279,7 @@ type Foo = { type Foo = { greet(): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -282,7 +287,7 @@ type Foo = { type Foo = { greet(name: string): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -294,7 +299,7 @@ type Foo = { type Foo = { greet: (name: string)=> string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -314,7 +319,7 @@ type Foo = { class Foo { name : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -322,7 +327,7 @@ class Foo { class Foo { constructor(message : string); } - `, + `, options: [{ after: true, before: true }], }, { @@ -330,7 +335,7 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, options: [{ after: true, before: true }], }, { @@ -338,7 +343,7 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, options: [{ after: true, before: true }], }, { @@ -346,7 +351,7 @@ class Foo { interface Foo { name : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -354,7 +359,7 @@ interface Foo { interface Foo { greet() : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -362,7 +367,7 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -370,7 +375,7 @@ interface Foo { type Foo = { name : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -378,7 +383,7 @@ type Foo = { type Foo = { greet() : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -386,7 +391,7 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -398,7 +403,7 @@ type Foo = { type Foo = { greet : (name : string) => string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -418,7 +423,7 @@ type Foo = { class Foo { name :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -426,7 +431,7 @@ class Foo { class Foo { constructor(message :string); } - `, + `, options: [{ after: false, before: true }], }, { @@ -434,7 +439,7 @@ class Foo { class Foo { greet() :string { return "hello"; } } - `, + `, options: [{ after: false, before: true }], }, { @@ -442,7 +447,7 @@ class Foo { class Foo { greet(name :string) :string { return name; } } - `, + `, options: [{ after: false, before: true }], }, { @@ -450,7 +455,7 @@ class Foo { interface Foo { name :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -458,7 +463,7 @@ interface Foo { interface Foo { greet() :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -466,7 +471,7 @@ interface Foo { interface Foo { greet(name :string) :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -474,7 +479,7 @@ interface Foo { type Foo = { name :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -482,7 +487,7 @@ type Foo = { type Foo = { greet() :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -490,7 +495,7 @@ type Foo = { type Foo = { greet(name :string) :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -502,7 +507,7 @@ type Foo = { type Foo = { greet :(name :string) =>string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -522,7 +527,7 @@ type Foo = { class Foo { name : string; } - `, + `, options: [{ before: true }], }, { @@ -530,7 +535,7 @@ class Foo { class Foo { constructor(message : string); } - `, + `, options: [{ before: true }], }, { @@ -538,7 +543,7 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, options: [{ before: true }], }, { @@ -546,7 +551,7 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, options: [{ before: true }], }, { @@ -554,7 +559,7 @@ class Foo { interface Foo { name : string; } - `, + `, options: [{ before: true }], }, { @@ -562,7 +567,7 @@ interface Foo { interface Foo { greet() : string; } - `, + `, options: [{ before: true }], }, { @@ -570,7 +575,7 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, options: [{ before: true }], }, { @@ -578,7 +583,7 @@ interface Foo { type Foo = { name : string; } - `, + `, options: [{ before: true }], }, { @@ -586,7 +591,7 @@ type Foo = { type Foo = { greet() : string; } - `, + `, options: [{ before: true }], }, { @@ -594,7 +599,7 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, options: [{ before: true }], }, { @@ -606,7 +611,7 @@ type Foo = { type Foo = { greet : (name : string) => string; } - `, + `, options: [{ before: true }], }, { @@ -644,7 +649,7 @@ type Foo = { class Foo { name : string; } - `, + `, options: [ { before: false, @@ -658,7 +663,7 @@ class Foo { class Foo { constructor(message : string); } - `, + `, options: [ { before: false, @@ -672,7 +677,7 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, options: [ { before: false, @@ -686,7 +691,7 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, options: [ { before: false, @@ -700,7 +705,7 @@ class Foo { interface Foo { name : string; } - `, + `, options: [ { before: false, @@ -714,7 +719,7 @@ interface Foo { interface Foo { greet() : string; } - `, + `, options: [ { before: false, @@ -728,7 +733,7 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, options: [ { before: false, @@ -742,7 +747,7 @@ interface Foo { type Foo = { name : string; } - `, + `, options: [ { before: false, @@ -756,7 +761,7 @@ type Foo = { type Foo = { greet() : string; } - `, + `, options: [ { before: false, @@ -770,7 +775,7 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, options: [ { before: false, @@ -794,7 +799,7 @@ type Foo = { type Foo = { greet : (name : string)=>string; } - `, + `, options: [ { before: false, @@ -827,7 +832,7 @@ type Foo = { type Foo = { greet : (name : string) => string; } - `, + `, options: [ { before: false, @@ -868,7 +873,7 @@ type Foo = { type Foo = { greet : (name : string) =>string; } - `, + `, options: [ { before: false, @@ -890,7 +895,7 @@ type Foo = { interface Foo { thing: { [key in string]: number }; } - `, + `, options: [{ after: true }], }, { @@ -898,7 +903,7 @@ interface Foo { interface Foo { thing: { [key in string]: number }; } - `, + `, options: [{ after: true, before: false }], }, { @@ -906,7 +911,7 @@ interface Foo { interface Foo { thing : { [key in string] : number }; } - `, + `, options: [{ after: true, before: true }], }, { @@ -914,7 +919,7 @@ interface Foo { interface Foo { thing :{ [key in string] :number }; } - `, + `, options: [{ after: false, before: true }], }, { @@ -922,20 +927,20 @@ interface Foo { interface Foo { thing : { [key in string] : number }; } - `, + `, options: [{ before: true }], }, ` type Foo = { thing: { [key in string]: number }; } - `, + `, { code: ` type Foo = { thing: { [key in string]: number }; } - `, + `, options: [{ after: true }], }, { @@ -943,7 +948,7 @@ type Foo = { type Foo = { thing: { [key in string]: number }; } - `, + `, options: [{ after: true, before: false }], }, { @@ -951,7 +956,7 @@ type Foo = { type Foo = { thing : { [key in string] : number }; } - `, + `, options: [{ after: true, before: true }], }, { @@ -959,7 +964,7 @@ type Foo = { type Foo = { thing :{ [key in string] :number }; } - `, + `, options: [{ after: false, before: true }], }, { @@ -967,20 +972,20 @@ type Foo = { type Foo = { thing : { [key in string] : number }; } - `, + `, options: [{ before: true }], }, ` class Foo { greet: (name: string) => void = {} } - `, + `, { code: ` class Foo { greet: (name: string) => void = {} } - `, + `, options: [{ after: true }], }, { @@ -988,7 +993,7 @@ class Foo { class Foo { greet: (name: string)=> void = {} } - `, + `, options: [{ after: true, before: false }], }, { @@ -996,7 +1001,7 @@ class Foo { class Foo { greet : (name : string) => void = {} } - `, + `, options: [{ after: true, before: true }], }, { @@ -1004,7 +1009,7 @@ class Foo { class Foo { greet :(name :string) =>void = {} } - `, + `, options: [{ after: false, before: true }], }, { @@ -1012,14 +1017,14 @@ class Foo { class Foo { greet : (name : string) => void = {} } - `, + `, options: [{ before: true }], }, { code: ` interface Foo { a: string } type Bar = Record - `, + `, options: [ { after: true, @@ -1072,7 +1077,7 @@ type Bar = Record interface Foo { greet():string; } - `, + `, options: [ { overrides: { @@ -1092,7 +1097,7 @@ interface Foo { interface Foo { name:string; } - `, + `, options: [ { before: true, @@ -1219,12 +1224,12 @@ interface Foo { class Foo { name : string; } - `, + `, output: ` class Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1239,12 +1244,12 @@ class Foo { class Foo { constructor(message : string); } - `, + `, output: ` class Foo { constructor(message: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1259,12 +1264,12 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, output: ` class Foo { greet(): string { return "hello"; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1279,12 +1284,12 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, output: ` class Foo { greet(name: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1305,12 +1310,12 @@ class Foo { interface Foo { name : string; } - `, + `, output: ` interface Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1325,12 +1330,12 @@ interface Foo { interface Foo { greet() : string; } - `, + `, output: ` interface Foo { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1345,12 +1350,12 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, output: ` interface Foo { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1371,12 +1376,12 @@ interface Foo { type Foo = { name : string; } - `, + `, output: ` type Foo = { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1391,12 +1396,12 @@ type Foo = { type Foo = { greet() : string; } - `, + `, output: ` type Foo = { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1411,12 +1416,12 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, output: ` type Foo = { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1467,12 +1472,12 @@ type Foo = { type Foo = { greet: (name : string) => string; } - `, + `, output: ` type Foo = { greet: (name: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1487,12 +1492,12 @@ type Foo = { type Foo = { greet: (name : string)=> string; } - `, + `, output: ` type Foo = { greet: (name: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1552,13 +1557,13 @@ type Foo = { class Foo { name : string; } - `, + `, options: [{ after: true }], output: ` class Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1573,13 +1578,13 @@ class Foo { class Foo { constructor(message : string); } - `, + `, options: [{ after: true }], output: ` class Foo { constructor(message: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1594,13 +1599,13 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, options: [{ after: true }], output: ` class Foo { greet(): string { return "hello"; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1615,13 +1620,13 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, options: [{ after: true }], output: ` class Foo { greet(name: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1642,13 +1647,13 @@ class Foo { interface Foo { name : string; } - `, + `, options: [{ after: true }], output: ` interface Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1663,13 +1668,13 @@ interface Foo { interface Foo { greet() : string; } - `, + `, options: [{ after: true }], output: ` interface Foo { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1684,13 +1689,13 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, options: [{ after: true }], output: ` interface Foo { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1711,13 +1716,13 @@ interface Foo { type Foo = { name : string; } - `, + `, options: [{ after: true }], output: ` type Foo = { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1732,13 +1737,13 @@ type Foo = { type Foo = { greet() : string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1753,13 +1758,13 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1812,13 +1817,13 @@ type Foo = { type Foo = { greet: (name : string) => string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet: (name: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1833,13 +1838,13 @@ type Foo = { type Foo = { greet: (name : string)=> string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet: (name: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1899,13 +1904,13 @@ type Foo = { class Foo { name : string; } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1920,13 +1925,13 @@ class Foo { class Foo { constructor(message : string); } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { constructor(message: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1941,13 +1946,13 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { greet(): string { return "hello"; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1962,13 +1967,13 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { greet(name: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -1989,13 +1994,13 @@ class Foo { interface Foo { name : string; } - `, + `, options: [{ after: true, before: false }], output: ` interface Foo { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2010,13 +2015,13 @@ interface Foo { interface Foo { greet() : string; } - `, + `, options: [{ after: true, before: false }], output: ` interface Foo { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2031,13 +2036,13 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, options: [{ after: true, before: false }], output: ` interface Foo { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2058,13 +2063,13 @@ interface Foo { type Foo = { name : string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { name: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2079,13 +2084,13 @@ type Foo = { type Foo = { greet() : string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet(): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2100,13 +2105,13 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet(name: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2159,13 +2164,13 @@ type Foo = { type Foo = { greet: (name : string) => string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet: (name: string)=> string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2186,13 +2191,13 @@ type Foo = { type Foo = { greet: (name : string)=> string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet: (name: string)=> string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -2264,13 +2269,13 @@ type Foo = { class Foo { name:string; } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2291,13 +2296,13 @@ class Foo { class Foo { constructor(message:string); } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { constructor(message : string); } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2318,13 +2323,13 @@ class Foo { class Foo { greet():string { return "hello"; } } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { greet() : string { return "hello"; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2345,13 +2350,13 @@ class Foo { class Foo { greet(name:string):string { return name; } } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { greet(name : string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2384,13 +2389,13 @@ class Foo { interface Foo { name:string; } - `, + `, options: [{ after: true, before: true }], output: ` interface Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2411,13 +2416,13 @@ interface Foo { interface Foo { greet():string; } - `, + `, options: [{ after: true, before: true }], output: ` interface Foo { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2438,13 +2443,13 @@ interface Foo { interface Foo { greet(name:string):string; } - `, + `, options: [{ after: true, before: true }], output: ` interface Foo { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2477,13 +2482,13 @@ interface Foo { type Foo = { name:string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2504,13 +2509,13 @@ type Foo = { type Foo = { greet():string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2531,13 +2536,13 @@ type Foo = { type Foo = { greet(name:string):string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2602,13 +2607,13 @@ type Foo = { type Foo = { greet: (name: string)=> string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet : (name : string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -2635,13 +2640,13 @@ type Foo = { type Foo = { greet : (name : string)=> string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet : (name : string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -2713,13 +2718,13 @@ type Foo = { class Foo { name:string; } - `, + `, options: [{ before: true }], output: ` class Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2740,13 +2745,13 @@ class Foo { class Foo { constructor(message:string); } - `, + `, options: [{ before: true }], output: ` class Foo { constructor(message : string); } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2767,13 +2772,13 @@ class Foo { class Foo { greet():string { return "hello"; } } - `, + `, options: [{ before: true }], output: ` class Foo { greet() : string { return "hello"; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2794,13 +2799,13 @@ class Foo { class Foo { greet(name:string):string { return name; } } - `, + `, options: [{ before: true }], output: ` class Foo { greet(name : string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2833,13 +2838,13 @@ class Foo { interface Foo { name:string; } - `, + `, options: [{ before: true }], output: ` interface Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2860,13 +2865,13 @@ interface Foo { interface Foo { greet():string; } - `, + `, options: [{ before: true }], output: ` interface Foo { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2887,13 +2892,13 @@ interface Foo { interface Foo { greet(name:string):string; } - `, + `, options: [{ before: true }], output: ` interface Foo { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2926,13 +2931,13 @@ interface Foo { type Foo = { name:string; } - `, + `, options: [{ before: true }], output: ` type Foo = { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2953,13 +2958,13 @@ type Foo = { type Foo = { greet():string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -2980,13 +2985,13 @@ type Foo = { type Foo = { greet(name:string):string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3051,13 +3056,13 @@ type Foo = { type Foo = { greet: (name: string)=> string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet : (name : string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -3084,13 +3089,13 @@ type Foo = { type Foo = { greet : (name : string)=> string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet : (name : string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -3180,7 +3185,7 @@ type Foo = { class Foo { name:string; } - `, + `, options: [ { before: false, @@ -3192,7 +3197,7 @@ class Foo { class Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3213,7 +3218,7 @@ class Foo { class Foo { constructor(message:string); } - `, + `, options: [ { before: false, @@ -3225,7 +3230,7 @@ class Foo { class Foo { constructor(message : string); } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3246,7 +3251,7 @@ class Foo { class Foo { greet():string { return "hello"; } } - `, + `, options: [ { before: false, @@ -3258,7 +3263,7 @@ class Foo { class Foo { greet() : string { return "hello"; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3279,7 +3284,7 @@ class Foo { class Foo { greet(name:string):string { return name; } } - `, + `, options: [ { before: false, @@ -3291,7 +3296,7 @@ class Foo { class Foo { greet(name : string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3324,7 +3329,7 @@ class Foo { interface Foo { name:string; } - `, + `, options: [ { before: false, @@ -3336,7 +3341,7 @@ interface Foo { interface Foo { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3357,7 +3362,7 @@ interface Foo { interface Foo { greet():string; } - `, + `, options: [ { before: false, @@ -3369,7 +3374,7 @@ interface Foo { interface Foo { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3390,7 +3395,7 @@ interface Foo { interface Foo { greet(name:string):string; } - `, + `, options: [ { before: false, @@ -3402,7 +3407,7 @@ interface Foo { interface Foo { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3435,7 +3440,7 @@ interface Foo { type Foo = { name:string; } - `, + `, options: [ { before: false, @@ -3447,7 +3452,7 @@ type Foo = { type Foo = { name : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3468,7 +3473,7 @@ type Foo = { type Foo = { greet():string; } - `, + `, options: [ { before: false, @@ -3480,7 +3485,7 @@ type Foo = { type Foo = { greet() : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3501,7 +3506,7 @@ type Foo = { type Foo = { greet(name:string):string; } - `, + `, options: [ { before: false, @@ -3513,7 +3518,7 @@ type Foo = { type Foo = { greet(name : string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3571,7 +3576,7 @@ type Foo = { type Foo = { greet : (name:string)=>string; } - `, + `, options: [ { before: false, @@ -3583,7 +3588,7 @@ type Foo = { type Foo = { greet : (name : string)=>string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3650,7 +3655,7 @@ type Foo = { type Foo = { greet : (name:string)=>string; } - `, + `, options: [ { before: false, @@ -3671,7 +3676,7 @@ type Foo = { type Foo = { greet : (name : string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceAfter', @@ -3702,20 +3707,20 @@ type Foo = { // https://github.com/bradzacher/eslint-plugin-typescript/issues/152 { code: ` - class Some { - a : {some: string, other: {more: number}}; - someMethod : (args : {some: string, other: {more: number}}) => void; - doSomething(args : {some: string, other: {more: number}}) : void {} - } - `, + class Some { + a : {some: string, other: {more: number}}; + someMethod : (args : {some: string, other: {more: number}}) => void; + doSomething(args : {some: string, other: {more: number}}) : void {} + } + `, options: [{ after: true, before: true }], output: ` - class Some { - a : {some : string, other : {more : number}}; - someMethod : (args : {some : string, other : {more : number}}) => void; - doSomething(args : {some : string, other : {more : number}}) : void {} - } - `, + class Some { + a : {some : string, other : {more : number}}; + someMethod : (args : {some : string, other : {more : number}}) => void; + doSomething(args : {some : string, other : {more : number}}) : void {} + } + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -3723,7 +3728,7 @@ type Foo = { type: ':', }, line: 3, - column: 30, + column: 22, }, { messageId: 'expectedSpaceBefore', @@ -3731,7 +3736,7 @@ type Foo = { type: ':', }, line: 3, - column: 45, + column: 37, }, { messageId: 'expectedSpaceBefore', @@ -3739,7 +3744,7 @@ type Foo = { type: ':', }, line: 3, - column: 52, + column: 44, }, { messageId: 'expectedSpaceBefore', @@ -3747,7 +3752,7 @@ type Foo = { type: ':', }, line: 4, - column: 47, + column: 39, }, { messageId: 'expectedSpaceBefore', @@ -3755,7 +3760,7 @@ type Foo = { type: ':', }, line: 4, - column: 62, + column: 54, }, { messageId: 'expectedSpaceBefore', @@ -3763,7 +3768,7 @@ type Foo = { type: ':', }, line: 4, - column: 69, + column: 61, }, { messageId: 'expectedSpaceBefore', @@ -3771,7 +3776,7 @@ type Foo = { type: ':', }, line: 5, - column: 45, + column: 37, }, { messageId: 'expectedSpaceBefore', @@ -3779,7 +3784,7 @@ type Foo = { type: ':', }, line: 5, - column: 60, + column: 52, }, { messageId: 'expectedSpaceBefore', @@ -3787,7 +3792,7 @@ type Foo = { type: ':', }, line: 5, - column: 67, + column: 59, }, ], }, @@ -3804,54 +3809,54 @@ ruleTester.run('type-annotation-spacing', rule, { interface resolve { resolver?: (() => PromiseLike) | PromiseLike; } - `, + `, 'function foo(a?: string) {}', ` class Foo { name?: string; } - `, + `, ` class Foo { constructor(message?: string); } - `, + `, ` class Foo { greet(name?: string): string { return name; } } - `, + `, ` interface Foo { name?: string; } - `, + `, ` interface Foo { greet(name?: string): string; } - `, + `, ` interface Foo { thing?: { [key in string]?: number }; } - `, + `, ` type Foo = { name?: string; } - `, + `, ` type Foo = { greet(name?: string): string; } - `, + `, 'type Foo = (name?: string) => string;', ` type Foo = { greet?: (name?: string) => string; } - `, + `, { code: 'function foo(a?: string) {}', options: [{ after: true }], @@ -3861,7 +3866,7 @@ type Foo = { class Foo { name?: string; } - `, + `, options: [{ after: true }], }, { @@ -3869,7 +3874,7 @@ class Foo { class Foo { constructor(message?: string); } - `, + `, options: [{ after: true }], }, { @@ -3877,7 +3882,7 @@ class Foo { class Foo { greet(name?: string): string { return name; } } - `, + `, options: [{ after: true }], }, { @@ -3885,7 +3890,7 @@ class Foo { interface Foo { name?: string; } - `, + `, options: [{ after: true }], }, { @@ -3893,7 +3898,7 @@ interface Foo { interface Foo { greet(name?: string): string; } - `, + `, options: [{ after: true }], }, { @@ -3901,7 +3906,7 @@ interface Foo { type Foo = { name?: string; } - `, + `, options: [{ after: true }], }, { @@ -3909,7 +3914,7 @@ type Foo = { type Foo = { greet(name?: string): string; } - `, + `, options: [{ after: true }], }, { @@ -3921,7 +3926,7 @@ type Foo = { type Foo = { greet?: (name?: string) => string; } - `, + `, options: [{ after: true }], }, { @@ -3933,7 +3938,7 @@ type Foo = { class Foo { name?: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -3941,7 +3946,7 @@ class Foo { class Foo { constructor(message?: string); } - `, + `, options: [{ after: true, before: false }], }, { @@ -3949,7 +3954,7 @@ class Foo { class Foo { greet(name?: string): string { return name; } } - `, + `, options: [{ after: true, before: false }], }, { @@ -3957,7 +3962,7 @@ class Foo { interface Foo { name?: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -3965,7 +3970,7 @@ interface Foo { interface Foo { greet(name?: string): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -3973,7 +3978,7 @@ interface Foo { type Foo = { name?: string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -3981,7 +3986,7 @@ type Foo = { type Foo = { greet(name?: string): string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -3993,7 +3998,7 @@ type Foo = { type Foo = { greet?: (name?: string)=> string; } - `, + `, options: [{ after: true, before: false }], }, { @@ -4005,7 +4010,7 @@ type Foo = { class Foo { name ?: string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4013,7 +4018,7 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, options: [{ after: true, before: true }], }, { @@ -4021,7 +4026,7 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, options: [{ after: true, before: true }], }, { @@ -4029,7 +4034,7 @@ class Foo { interface Foo { name ?: string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4037,7 +4042,7 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4045,7 +4050,7 @@ interface Foo { type Foo = { name ?: string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4053,7 +4058,7 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4065,7 +4070,7 @@ type Foo = { type Foo = { greet ?: (name : string) => string; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4077,7 +4082,7 @@ type Foo = { class Foo { name ?:string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4085,7 +4090,7 @@ class Foo { class Foo { constructor(message ?:string); } - `, + `, options: [{ after: false, before: true }], }, { @@ -4093,7 +4098,7 @@ class Foo { class Foo { greet(name ?:string) :string { return name; } } - `, + `, options: [{ after: false, before: true }], }, { @@ -4101,7 +4106,7 @@ class Foo { interface Foo { name ?:string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4109,7 +4114,7 @@ interface Foo { interface Foo { greet(name ?:string) :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4117,7 +4122,7 @@ interface Foo { type Foo = { name ?:string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4125,7 +4130,7 @@ type Foo = { type Foo = { greet(name ?:string) :string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4137,7 +4142,7 @@ type Foo = { type Foo = { greet :(name ?:string) =>string; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4149,7 +4154,7 @@ type Foo = { class Foo { name ?: string; } - `, + `, options: [{ before: true }], }, { @@ -4157,7 +4162,7 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, options: [{ before: true }], }, { @@ -4165,7 +4170,7 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, options: [{ before: true }], }, { @@ -4173,7 +4178,7 @@ class Foo { interface Foo { name ?: string; } - `, + `, options: [{ before: true }], }, { @@ -4181,7 +4186,7 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, options: [{ before: true }], }, { @@ -4189,7 +4194,7 @@ interface Foo { type Foo = { name ?: string; } - `, + `, options: [{ before: true }], }, { @@ -4197,7 +4202,7 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, options: [{ before: true }], }, { @@ -4209,7 +4214,7 @@ type Foo = { type Foo = { greet : (name ?: string) => string; } - `, + `, options: [{ before: true }], }, { @@ -4227,7 +4232,7 @@ type Foo = { class Foo { name ?: string; } - `, + `, options: [ { before: false, @@ -4241,7 +4246,7 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, options: [ { before: false, @@ -4255,7 +4260,7 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, options: [ { before: false, @@ -4269,7 +4274,7 @@ class Foo { interface Foo { name ?: string; } - `, + `, options: [ { before: false, @@ -4283,7 +4288,7 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, options: [ { before: false, @@ -4297,7 +4302,7 @@ interface Foo { type Foo = { name ?: string; } - `, + `, options: [ { before: false, @@ -4311,7 +4316,7 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, options: [ { before: false, @@ -4335,7 +4340,7 @@ type Foo = { type Foo = { greet ?: (name ?: string)=>string; } - `, + `, options: [ { before: false, @@ -4368,7 +4373,7 @@ type Foo = { type Foo = { greet ?: (name ?: string) => string; } - `, + `, options: [ { before: false, @@ -4409,7 +4414,7 @@ type Foo = { type Foo = { greet : (name ?: string) =>string; } - `, + `, options: [ { before: false, @@ -4431,7 +4436,7 @@ type Foo = { interface Foo { thing?: { [key in string]?: number }; } - `, + `, options: [{ after: true }], }, { @@ -4439,7 +4444,7 @@ interface Foo { interface Foo { thing?: { [key in string]?: number }; } - `, + `, options: [{ after: true, before: false }], }, { @@ -4447,7 +4452,7 @@ interface Foo { interface Foo { thing ?: { [key in string] ?: number }; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4455,7 +4460,7 @@ interface Foo { interface Foo { thing ?:{ [key in string] ?:number }; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4463,20 +4468,20 @@ interface Foo { interface Foo { thing ?: { [key in string] ?: number }; } - `, + `, options: [{ before: true }], }, ` type Foo = { thing?: { [key in string]?: number }; } - `, + `, { code: ` type Foo = { thing?: { [key in string]?: number }; } - `, + `, options: [{ after: true }], }, { @@ -4484,7 +4489,7 @@ type Foo = { type Foo = { thing?: { [key in string]?: number }; } - `, + `, options: [{ after: true, before: false }], }, { @@ -4492,7 +4497,7 @@ type Foo = { type Foo = { thing ?: { [key in string] ?: number }; } - `, + `, options: [{ after: true, before: true }], }, { @@ -4500,7 +4505,7 @@ type Foo = { type Foo = { thing ?:{ [key in string] ?:number }; } - `, + `, options: [{ after: false, before: true }], }, { @@ -4508,20 +4513,20 @@ type Foo = { type Foo = { thing ?: { [key in string] ?: number }; } - `, + `, options: [{ before: true }], }, ` class Foo { greet: (name?: string) => void = {} } - `, + `, { code: ` class Foo { greet: (name?: string) => void = {} } - `, + `, options: [{ after: true }], }, { @@ -4529,7 +4534,7 @@ class Foo { class Foo { greet: (name?: string)=> void = {} } - `, + `, options: [{ after: true, before: false }], }, { @@ -4537,7 +4542,7 @@ class Foo { class Foo { greet : (name ?: string) => void = {} } - `, + `, options: [{ after: true, before: true }], }, { @@ -4545,7 +4550,7 @@ class Foo { class Foo { greet :(name ?:string) =>void = {} } - `, + `, options: [{ after: false, before: true }], }, { @@ -4553,14 +4558,14 @@ class Foo { class Foo { greet : (name ?: string) => void = {} } - `, + `, options: [{ before: true }], }, { code: ` interface Foo { a?: string } type Bar = Record - `, + `, options: [ { after: true, @@ -4593,12 +4598,12 @@ type Bar = Record class Foo { name ?: string; } - `, + `, output: ` class Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4613,12 +4618,12 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, output: ` class Foo { constructor(message?: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4633,12 +4638,12 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, output: ` class Foo { greet(name?: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4659,12 +4664,12 @@ class Foo { interface Foo { name ?: string; } - `, + `, output: ` interface Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4679,12 +4684,12 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, output: ` interface Foo { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4705,12 +4710,12 @@ interface Foo { type Foo = { name ?: string; } - `, + `, output: ` type Foo = { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4725,12 +4730,12 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, output: ` type Foo = { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4781,12 +4786,12 @@ type Foo = { type Foo = { greet: (name ?: string) => string; } - `, + `, output: ` type Foo = { greet: (name?: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4801,12 +4806,12 @@ type Foo = { type Foo = { greet: (name ?: string)=> string; } - `, + `, output: ` type Foo = { greet: (name?: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4840,13 +4845,13 @@ type Foo = { class Foo { name ?: string; } - `, + `, options: [{ after: true }], output: ` class Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4861,13 +4866,13 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, options: [{ after: true }], output: ` class Foo { constructor(message?: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4882,13 +4887,13 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, options: [{ after: true }], output: ` class Foo { greet(name?: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4909,13 +4914,13 @@ class Foo { interface Foo { name ?: string; } - `, + `, options: [{ after: true }], output: ` interface Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4930,13 +4935,13 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, options: [{ after: true }], output: ` interface Foo { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4957,13 +4962,13 @@ interface Foo { type Foo = { name ?: string; } - `, + `, options: [{ after: true }], output: ` type Foo = { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -4978,13 +4983,13 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5037,13 +5042,13 @@ type Foo = { type Foo = { greet: (name ?: string) => string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet: (name?: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5058,13 +5063,13 @@ type Foo = { type Foo = { greet: (name ?: string)=> string; } - `, + `, options: [{ after: true }], output: ` type Foo = { greet: (name?: string) => string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5098,13 +5103,13 @@ type Foo = { class Foo { name ?: string; } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5119,13 +5124,13 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { constructor(message?: string); } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5140,13 +5145,13 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, options: [{ after: true, before: false }], output: ` class Foo { greet(name?: string): string { return name; } } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5167,13 +5172,13 @@ class Foo { interface Foo { name ?: string; } - `, + `, options: [{ after: true, before: false }], output: ` interface Foo { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5188,13 +5193,13 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, options: [{ after: true, before: false }], output: ` interface Foo { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5215,13 +5220,13 @@ interface Foo { type Foo = { name ?: string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { name?: string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5236,13 +5241,13 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet(name?: string): string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5295,13 +5300,13 @@ type Foo = { type Foo = { greet: (name ?: string) => string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet: (name?: string)=> string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5322,13 +5327,13 @@ type Foo = { type Foo = { greet: (name ?: string)=> string; } - `, + `, options: [{ after: true, before: false }], output: ` type Foo = { greet: (name?: string)=> string; } - `, + `, errors: [ { messageId: 'unexpectedSpaceBefore', @@ -5362,13 +5367,13 @@ type Foo = { class Foo { name?:string; } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5389,13 +5394,13 @@ class Foo { class Foo { constructor(message?:string); } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { constructor(message ?: string); } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5416,13 +5421,13 @@ class Foo { class Foo { greet(name?:string):string { return name; } } - `, + `, options: [{ after: true, before: true }], output: ` class Foo { greet(name ?: string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5455,13 +5460,13 @@ class Foo { interface Foo { name?:string; } - `, + `, options: [{ after: true, before: true }], output: ` interface Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5482,13 +5487,13 @@ interface Foo { interface Foo { greet(name?:string):string; } - `, + `, options: [{ after: true, before: true }], output: ` interface Foo { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5521,13 +5526,13 @@ interface Foo { type Foo = { name?:string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5548,13 +5553,13 @@ type Foo = { type Foo = { greet(name?:string):string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5619,13 +5624,13 @@ type Foo = { type Foo = { greet?: (name?: string)=> string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet ?: (name ?: string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5652,13 +5657,13 @@ type Foo = { type Foo = { greet ?: (name ?: string)=> string; } - `, + `, options: [{ after: true, before: true }], output: ` type Foo = { greet ?: (name ?: string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5692,13 +5697,13 @@ type Foo = { class Foo { name?:string; } - `, + `, options: [{ before: true }], output: ` class Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5719,13 +5724,13 @@ class Foo { class Foo { constructor(message?:string); } - `, + `, options: [{ before: true }], output: ` class Foo { constructor(message ?: string); } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5746,13 +5751,13 @@ class Foo { class Foo { greet(name?:string):string { return name; } } - `, + `, options: [{ before: true }], output: ` class Foo { greet(name ?: string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5785,13 +5790,13 @@ class Foo { interface Foo { name?:string; } - `, + `, options: [{ before: true }], output: ` interface Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5812,13 +5817,13 @@ interface Foo { interface Foo { greet(name?:string):string; } - `, + `, options: [{ before: true }], output: ` interface Foo { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5851,13 +5856,13 @@ interface Foo { type Foo = { name?:string; } - `, + `, options: [{ before: true }], output: ` type Foo = { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5878,13 +5883,13 @@ type Foo = { type Foo = { greet(name?:string):string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5949,13 +5954,13 @@ type Foo = { type Foo = { greet?: (name?: string)=> string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet ?: (name ?: string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -5982,13 +5987,13 @@ type Foo = { type Foo = { greet ?: (name ?: string)=> string; } - `, + `, options: [{ before: true }], output: ` type Foo = { greet ?: (name ?: string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6028,7 +6033,7 @@ type Foo = { class Foo { name?:string; } - `, + `, options: [ { before: false, @@ -6040,7 +6045,7 @@ class Foo { class Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6061,7 +6066,7 @@ class Foo { class Foo { constructor(message?:string); } - `, + `, options: [ { before: false, @@ -6073,7 +6078,7 @@ class Foo { class Foo { constructor(message ?: string); } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6094,7 +6099,7 @@ class Foo { class Foo { greet(name?:string):string { return name; } } - `, + `, options: [ { before: false, @@ -6106,7 +6111,7 @@ class Foo { class Foo { greet(name ?: string) : string { return name; } } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6139,7 +6144,7 @@ class Foo { interface Foo { name?:string; } - `, + `, options: [ { before: false, @@ -6151,7 +6156,7 @@ interface Foo { interface Foo { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6172,7 +6177,7 @@ interface Foo { interface Foo { greet(name?:string):string; } - `, + `, options: [ { before: false, @@ -6184,7 +6189,7 @@ interface Foo { interface Foo { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6217,7 +6222,7 @@ interface Foo { type Foo = { name?:string; } - `, + `, options: [ { before: false, @@ -6229,7 +6234,7 @@ type Foo = { type Foo = { name ?: string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6250,7 +6255,7 @@ type Foo = { type Foo = { greet(name?:string):string; } - `, + `, options: [ { before: false, @@ -6262,7 +6267,7 @@ type Foo = { type Foo = { greet(name ?: string) : string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6320,7 +6325,7 @@ type Foo = { type Foo = { greet ?: (name?:string)=>string; } - `, + `, options: [ { before: false, @@ -6332,7 +6337,7 @@ type Foo = { type Foo = { greet ?: (name ?: string)=>string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', @@ -6399,7 +6404,7 @@ type Foo = { type Foo = { greet ?: (name?:string)=>string; } - `, + `, options: [ { before: false, @@ -6420,7 +6425,7 @@ type Foo = { type Foo = { greet ?: (name ?: string) => string; } - `, + `, errors: [ { messageId: 'expectedSpaceBefore', From 0ca9647b71cb948d0fe1a7bd0feec9f8fa9ffc40 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 17:29:12 -0700 Subject: [PATCH 10/92] chore: triple-slash-reference --- .../rules/triple-slash-reference.test.ts | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/triple-slash-reference.test.ts b/packages/eslint-plugin/tests/rules/triple-slash-reference.test.ts index 690526edf632..ab10ba5f6d97 100644 --- a/packages/eslint-plugin/tests/rules/triple-slash-reference.test.ts +++ b/packages/eslint-plugin/tests/rules/triple-slash-reference.test.ts @@ -12,93 +12,93 @@ ruleTester.run('triple-slash-reference', rule, { valid: [ { code: ` - // - // - // - import * as foo from "foo" - import * as bar from "bar" - import * as baz from "baz" + // + // + // + import * as foo from 'foo'; + import * as bar from 'bar'; + import * as baz from 'baz'; `, options: [{ path: 'never', types: 'never', lib: 'never' }], }, { code: ` - // - // - // - import foo = require("foo") - import bar = require("bar") - import baz = require("baz") + // + // + // + import foo = require('foo'); + import bar = require('bar'); + import baz = require('baz'); `, options: [{ path: 'never', types: 'never', lib: 'never' }], }, { code: ` - /// - /// - /// - import * as foo from "foo" - import * as bar from "bar" - import * as baz from "baz" + /// + /// + /// + import * as foo from 'foo'; + import * as bar from 'bar'; + import * as baz from 'baz'; `, options: [{ path: 'always', types: 'always', lib: 'always' }], }, { code: ` - /// - /// - /// - import foo = require("foo") - import bar = require("bar") - import baz = require("baz") + /// + /// + /// + import foo = require('foo'); + import bar = require('bar'); + import baz = require('baz'); `, options: [{ path: 'always', types: 'always', lib: 'always' }], }, { - code: 'import * as foo from "foo"', + code: "import * as foo from 'foo';", options: [{ path: 'never' }], }, { - code: 'import foo = require("foo");', + code: "import foo = require('foo');", options: [{ path: 'never' }], }, { - code: 'import * as foo from "foo"', + code: "import * as foo from 'foo';", options: [{ types: 'never' }], }, { - code: 'import foo = require("foo");', + code: "import foo = require('foo');", options: [{ types: 'never' }], }, { - code: 'import * as foo from "foo"', + code: "import * as foo from 'foo';", options: [{ lib: 'never' }], }, { - code: 'import foo = require("foo");', + code: "import foo = require('foo');", options: [{ lib: 'never' }], }, { - code: 'import * as foo from "foo"', + code: "import * as foo from 'foo';", options: [{ types: 'prefer-import' }], }, { - code: 'import foo = require("foo");', + code: "import foo = require('foo');", options: [{ types: 'prefer-import' }], }, { code: ` - /// - import * as bar from "bar" + /// + import * as bar from 'bar'; `, options: [{ types: 'prefer-import' }], }, { code: ` - /* - /// - */ - import * as foo from "foo" + /* + /// + */ + import * as foo from 'foo'; `, options: [{ path: 'never', types: 'never', lib: 'never' }], }, @@ -107,7 +107,7 @@ ruleTester.run('triple-slash-reference', rule, { { code: ` /// -import * as foo from "foo" +import * as foo from 'foo'; `, options: [{ types: 'prefer-import' }], errors: [ @@ -121,7 +121,7 @@ import * as foo from "foo" { code: ` /// -import foo = require("foo"); +import foo = require('foo'); `, options: [{ types: 'prefer-import' }], errors: [ From 1bb06fc9cae90703e43139980374e055d55a2d53 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 17:30:58 -0700 Subject: [PATCH 11/92] chore: switch-exhaustiveness-check --- .../rules/strict-boolean-expressions.test.ts | 184 +++++----- .../rules/switch-exhaustiveness-check.test.ts | 314 +++++++++++------- 2 files changed, 286 insertions(+), 212 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts index d3d5700c5a7b..daa619b84eeb 100644 --- a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts @@ -3,6 +3,7 @@ import { RuleTester, getFixturesRootDir, batchedSingleLineTests, + noFormat, } from '../RuleTester'; const rootPath = getFixturesRootDir(); @@ -48,7 +49,7 @@ ruleTester.run('strict-boolean-expressions', rule, { return; } - if ((bool1 && bool2) || (bool1 || bool2)) { + if ((bool1 && bool2) || bool1 || bool2) { return; } `, @@ -58,7 +59,7 @@ ruleTester.run('strict-boolean-expressions', rule, { const res1 = true ? true : false; const res2 = bool1 && bool2 ? true : false; const res3 = bool1 || bool2 ? true : false; - const res4 = (bool1 && bool2) || (bool1 || bool2) ? true : false; + const res4 = (bool1 && bool2) || bool1 || bool2 ? true : false; `, ` for (let i = 0; true; i++) { @@ -88,7 +89,7 @@ ruleTester.run('strict-boolean-expressions', rule, { ` const bool1 = true; const bool2 = false; - for (let i = 0; (bool1 && bool2) || (bool1 || bool2); i++) { + for (let i = 0; (bool1 && bool2) || bool1 || bool2; i++) { break; } `, @@ -120,7 +121,7 @@ ruleTester.run('strict-boolean-expressions', rule, { ` const bool1 = true; const bool2 = false; - while ((bool1 && bool2) || (bool1 || bool2)) { + while ((bool1 && bool2) || bool1 || bool2) { break; } `, @@ -154,10 +155,12 @@ ruleTester.run('strict-boolean-expressions', rule, { const bool2 = false; do { break; - } while ((bool1 && bool2) || (bool1 || bool2)); + } while ((bool1 && bool2) || bool1 || bool2); `, ` - function foo(arg: T) { return !arg; } + function foo(arg: T) { + return !arg; + } `, { options: [{ ignoreRhs: true }], @@ -171,10 +174,10 @@ ruleTester.run('strict-boolean-expressions', rule, { ...batchedSingleLineTests({ options: [{ allowNullable: true }], code: ` - const f1 = (x?: boolean) => x ? 1 : 0; - const f2 = (x: boolean | null) => x ? 1 : 0; - const f3 = (x?: true | null) => x ? 1 : 0; - const f4 = (x?: false) => x ? 1 : 0; + const f1 = (x?: boolean) => (x ? 1 : 0); + const f2 = (x: boolean | null) => (x ? 1 : 0); + const f3 = (x?: true | null) => (x ? 1 : 0); + const f4 = (x?: false) => (x ? 1 : 0); `, }), ` @@ -184,38 +187,39 @@ ruleTester.run('strict-boolean-expressions', rule, { ...batchedSingleLineTests({ options: [{ allowSafe: true }], code: ` - const f1 = (x: boolean | { a: string }) => x ? 1 : 0; - const f2 = (x: true | { a: string }) => x ? 1 : 0; - const f3 = (x: { a: string } | false) => x ? 1 : 0; + const f1 = (x: boolean | { a: string }) => (x ? 1 : 0); + const f2 = (x: true | { a: string }) => (x ? 1 : 0); + const f3 = (x: { a: string } | false) => (x ? 1 : 0); `, }), ...batchedSingleLineTests({ options: [{ allowNullable: true, allowSafe: true }], code: ` - const f1 = (x?: boolean | { a?: 1 }) => x ? 1 : 0; - const f2 = (x: { a?: 1 } | { b?: "a" } | null) => x ? 1 : 0; - const f3 = (x?: { a?: 1 } | { b?: "a" } | null) => x ? 1 : 0; - const f4 = (x?: { b?: "a" } | true) => x ? 1 : 0; - const f5 = (g?: (x: number) => number) => g ? g(1) : 0; + const f1 = (x?: boolean | { a?: 1 }) => (x ? 1 : 0); + const f2 = (x: { a?: 1 } | { b?: 'a' } | null) => (x ? 1 : 0); + const f3 = (x?: { a?: 1 } | { b?: 'a' } | null) => (x ? 1 : 0); + const f4 = (x?: { b?: 'a' } | true) => (x ? 1 : 0); + const f5 = (g?: (x: number) => number) => (g ? g(1) : 0); `, }), ...batchedSingleLineTests({ options: [{ allowNullable: true, allowSafe: true, ignoreRhs: true }], code: ` - const f1 = (x?: { a: null }) => x && x.foo && x.foo.bar - const f2 = (g?: (x: number) => number) => g && g(1) + const f1 = (x?: { a: null }) => x && x.foo && x.foo.bar; + const f2 = (g?: (x: number) => number) => g && g(1); `, }), ` declare let x: never; - if (x) {} + if (x) { + } `, ...batchedSingleLineTests({ - code: ` - function f1(x: never) { return !x } - function f2(x: never) { return x ? 1 : 0 } - function f3(x: never, y: never) { return x && y } - function f5(x: never | boolean) { if (!x) {} } + code: noFormat` + function f1(x: never) { return !x; } + function f2(x: never) { return x ? 1 : 0; } + function f3(x: never, y: never) { return x && y; } + function f5(x: never | boolean) { if (!x) { } } `, }), ], @@ -223,7 +227,7 @@ ruleTester.run('strict-boolean-expressions', rule, { invalid: [ { code: ` - let val = "foo"; + let val = 'foo'; let bool = !val; `, errors: [ @@ -302,7 +306,7 @@ ruleTester.run('strict-boolean-expressions', rule, { { code: ` let num = 1; - let str = "foo" + let str = 'foo'; let val = null; let bool = true && (val || num || str); `, @@ -354,7 +358,7 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { code: ` - let item = "foo"; + let item = 'foo'; if (item) { return; } @@ -480,7 +484,7 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { code: ` - const bool = "foo" ? true : false; + const bool = 'foo' ? true : false; `, errors: [ { @@ -672,7 +676,7 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { code: ` - let bool1 = "foo"; + let bool1 = 'foo'; let bool2 = true; for (let i = 0; bool1 && bool2; i++) { return; @@ -860,7 +864,7 @@ ruleTester.run('strict-boolean-expressions', rule, { code: ` do { return; - } while ("foo"); + } while ('foo'); `, errors: [ { @@ -980,13 +984,15 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { code: ` - function foo(arg: T) { return !arg; } + function foo(arg: T) { + return !arg; + } `, errors: [ { messageId: 'conditionErrorNumber', - line: 2, - column: 58, + line: 3, + column: 19, }, ], }, @@ -995,23 +1001,23 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorNullableBoolean', line: 2, - column: 47, + column: 48, }, { messageId: 'conditionErrorNullableBoolean', line: 3, - column: 37, + column: 38, }, { messageId: 'conditionErrorOther', line: 4, - column: 41, + column: 42, }, ], code: ` - const f1 = (x: boolean | null | undefined) => x ? 1 : 0; - const f2 = (x?: boolean) => x ? 1 : 0; - const f3 = (x: boolean | {}) => x ? 1 : 0; + const f1 = (x: boolean | null | undefined) => (x ? 1 : 0); + const f2 = (x?: boolean) => (x ? 1 : 0); + const f3 = (x: boolean | {}) => (x ? 1 : 0); `, }), { @@ -1045,15 +1051,17 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { messageId: 'conditionErrorOther', - line: 5, + line: 6, column: 13, }, ], code: ` const condition = () => false; const obj = { x: 1 }; - if (condition() || obj) {} - if (condition() && obj) {} + if (condition() || obj) { + } + if (condition() && obj) { + } `, }, { @@ -1066,15 +1074,17 @@ ruleTester.run('strict-boolean-expressions', rule, { }, { messageId: 'conditionErrorOther', - line: 5, + line: 6, column: 13, }, ], code: ` declare let condition: boolean; const obj = { x: 1 }; - if (condition || obj) {} - if (condition && obj) {} + if (condition || obj) { + } + if (condition && obj) { + } `, }, ...batchedSingleLineTests({ @@ -1083,29 +1093,29 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorNullish', line: 2, - column: 37, + column: 38, }, { messageId: 'conditionErrorNullableNumber', line: 3, - column: 36, + column: 37, }, { messageId: 'conditionErrorNullableString', line: 4, - column: 36, + column: 37, }, { messageId: 'conditionErrorOther', line: 5, - column: 45, + column: 46, }, ], code: ` - const f1 = (x: null | undefined) => x ? 1 : 0; - const f2 = (x?: number) => x ? 1 : 0; - const f3 = (x?: string) => x ? 1 : 0; - const f4 = (x?: string | number) => x ? 1 : 0; + const f1 = (x: null | undefined) => (x ? 1 : 0); + const f2 = (x?: number) => (x ? 1 : 0); + const f3 = (x?: string) => (x ? 1 : 0); + const f4 = (x?: string | number) => (x ? 1 : 0); `, }), { @@ -1113,18 +1123,18 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorOther', line: 3, - column: 43, + column: 44, }, { messageId: 'conditionErrorOther', line: 4, - column: 44, + column: 45, }, ], code: ` - type Type = { a: string; }; - const f1 = (x: Type | boolean) => x ? 1 : 0; - const f2 = (x?: Type | boolean) => x ? 1 : 0; + type Type = { a: string }; + const f1 = (x: Type | boolean) => (x ? 1 : 0); + const f2 = (x?: Type | boolean) => (x ? 1 : 0); `, }, ...batchedSingleLineTests({ @@ -1133,23 +1143,23 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorOther', line: 2, - column: 36, + column: 37, }, { messageId: 'conditionErrorOther', line: 3, - column: 44, + column: 45, }, { messageId: 'conditionErrorOther', line: 4, - column: 44, + column: 45, }, ], code: ` - const f1 = (x: object | string) => x ? 1 : 0; - const f2 = (x: object | number) => x ? 1 : 0; - const f3 = (x: number | string) => x ? 1 : 0; + const f1 = (x: object | string) => (x ? 1 : 0); + const f2 = (x: object | number) => (x ? 1 : 0); + const f3 = (x: number | string) => (x ? 1 : 0); `, }), { @@ -1157,24 +1167,28 @@ ruleTester.run('strict-boolean-expressions', rule, { errors: [ { messageId: 'conditionErrorNumber', - line: 8, - column: 34, + line: 12, + column: 35, }, { messageId: 'conditionErrorString', - line: 9, - column: 34, + line: 13, + column: 35, }, ], code: ` enum Enum1 { - A, B, C + A, + B, + C, } enum Enum2 { - A = 'A', B = 'B', C = 'C' + A = 'A', + B = 'B', + C = 'C', } - const f1 = (x: Enum1) => x ? 1 : 0; - const f2 = (x: Enum2) => x ? 1 : 0; + const f1 = (x: Enum1) => (x ? 1 : 0); + const f2 = (x: Enum2) => (x ? 1 : 0); `, }, { @@ -1183,18 +1197,18 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorOther', line: 3, - column: 43, + column: 44, }, { messageId: 'conditionErrorOther', line: 4, - column: 49, + column: 50, }, ], code: ` - type Type = { a: string; }; - const f1 = (x?: Type | string) => x ? 1 : 0; - const f2 = (x: Type | number | null) => x ? 1 : 0; + type Type = { a: string }; + const f1 = (x?: Type | string) => (x ? 1 : 0); + const f2 = (x: Type | number | null) => (x ? 1 : 0); `, }, ...batchedSingleLineTests({ @@ -1202,23 +1216,23 @@ ruleTester.run('strict-boolean-expressions', rule, { { messageId: 'conditionErrorObject', line: 2, - column: 31, + column: 32, }, { messageId: 'conditionErrorNullableObject', line: 3, - column: 40, + column: 41, }, { messageId: 'conditionErrorNullableObject', line: 4, - column: 47, + column: 48, }, ], code: ` - const f1 = (x: { x: any }) => x ? 1 : 0; - const f2 = (x?: { x: any }) => x ? 1 : 0; - const f3 = (x?: { x: any } | null) => x ? 1 : 0; + const f1 = (x: { x: any }) => (x ? 1 : 0); + const f2 = (x?: { x: any }) => (x ? 1 : 0); + const f3 = (x?: { x: any } | null) => (x ? 1 : 0); `, }), ], diff --git a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts index fd9462ddb1ec..3eb47fb6eb94 100644 --- a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts +++ b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts @@ -1,6 +1,6 @@ import path from 'path'; import switchExhaustivenessCheck from '../../src/rules/switch-exhaustiveness-check'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const rootPath = path.join(process.cwd(), 'tests/fixtures/'); @@ -16,183 +16,226 @@ ruleTester.run('switch-exhaustiveness-check', switchExhaustivenessCheck, { valid: [ // All branches matched ` -type Day = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday' - -const day = 'Monday' as Day -let result = 0 +type Day = + | 'Monday' + | 'Tuesday' + | 'Wednesday' + | 'Thursday' + | 'Friday' + | 'Saturday' + | 'Sunday'; + +const day = 'Monday' as Day; +let result = 0; switch (day) { case 'Monday': { - result = 1 - break + result = 1; + break; } case 'Tuesday': { - result = 2 - break + result = 2; + break; } case 'Wednesday': { - result = 3 - break + result = 3; + break; } case 'Thursday': { - result = 4 - break + result = 4; + break; } case 'Friday': { - result = 5 - break + result = 5; + break; } case 'Saturday': { - result = 6 - break + result = 6; + break; } case 'Sunday': { - result = 7 - break + result = 7; + break; } } -`, + `, // Other primitive literals work too ` -type Num = 0 | 1 | 2 +type Num = 0 | 1 | 2; function test(value: Num): number { switch (value) { - case 0: return 0 - case 1: return 1 - case 2: return 2 + case 0: + return 0; + case 1: + return 1; + case 2: + return 2; } } -`, + `, ` -type Bool = true | false +type Bool = true | false; function test(value: Bool): number { switch (value) { - case true: return 1 - case false: return 0 + case true: + return 1; + case false: + return 0; } } -`, + `, ` -type Mix = 0 | 1 | 'two' | 'three' | true +type Mix = 0 | 1 | 'two' | 'three' | true; function test(value: Mix): number { switch (value) { - case 0: return 0 - case 1: return 1 - case 'two': return 2 - case 'three': return 3 - case true: return 4 + case 0: + return 0; + case 1: + return 1; + case 'two': + return 2; + case 'three': + return 3; + case true: + return 4; } } -`, + `, // Works with type references ` -type A = 'a' -type B = 'b' -type C = 'c' -type Union = A | B | C +type A = 'a'; +type B = 'b'; +type C = 'c'; +type Union = A | B | C; function test(value: Union): number { switch (value) { - case 'a': return 1 - case 'b': return 2 - case 'c': return 3 + case 'a': + return 1; + case 'b': + return 2; + case 'c': + return 3; } } -`, + `, // Works with `typeof` ` -const A = 'a' -const B = 1 -const C = true +const A = 'a'; +const B = 1; +const C = true; -type Union = typeof A | typeof B | typeof C +type Union = typeof A | typeof B | typeof C; function test(value: Union): number { switch (value) { - case 'a': return 1 - case 1: return 2 - case true: return 3 + case 'a': + return 1; + case 1: + return 2; + case true: + return 3; } } -`, + `, // Switch contains default clause. ` -type Day = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday' - -const day = 'Monday' as Day -let result = 0 +type Day = + | 'Monday' + | 'Tuesday' + | 'Wednesday' + | 'Thursday' + | 'Friday' + | 'Saturday' + | 'Sunday'; + +const day = 'Monday' as Day; +let result = 0; switch (day) { case 'Monday': { - result = 1 - break + result = 1; + break; } default: { - result = 42 + result = 42; } } - `, + `, // Exhaustiveness check only works for union types... ` -const day = 'Monday' as string -let result = 0 +const day = 'Monday' as string; +let result = 0; switch (day) { case 'Monday': { - result = 1 - break + result = 1; + break; } case 'Tuesday': { - result = 2 - break + result = 2; + break; } } - `, + `, // ... and enums (at least for now). ` -enum Enum { A, B } +enum Enum { + A, + B, +} function test(value: Enum): number { switch (value) { - case Enum.A: return 1 - case Enum.B: return 2 + case Enum.A: + return 1; + case Enum.B: + return 2; } } -`, + `, // Object union types won't work either, unless it's a discriminated union ` -type ObjectUnion = { a: 1 } | { b: 2 } +type ObjectUnion = { a: 1 } | { b: 2 }; function test(value: ObjectUnion): number { switch (value.a) { - case 1: return 1 + case 1: + return 1; } } -`, + `, ], invalid: [ { // Matched only one branch out of seven. code: ` -type Day = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday' - -const day = 'Monday' as Day -let result = 0 +type Day = + | 'Monday' + | 'Tuesday' + | 'Wednesday' + | 'Thursday' + | 'Friday' + | 'Saturday' + | 'Sunday'; + +const day = 'Monday' as Day; +let result = 0; switch (day) { case 'Monday': { - result = 1 - break + result = 1; + break; } } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', - line: 7, + line: 14, column: 9, data: { missingBranches: @@ -204,18 +247,22 @@ switch (day) { { // Didn't match all enum variants code: ` -enum Enum { A, B } +enum Enum { + A, + B, +} function test(value: Enum): number { switch (value) { - case Enum.A: return 1 + case Enum.A: + return 1; } } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', - line: 5, + line: 8, column: 11, data: { missingBranches: 'Enum.B', @@ -225,17 +272,18 @@ function test(value: Enum): number { }, { code: ` -type A = 'a' -type B = 'b' -type C = 'c' -type Union = A | B | C +type A = 'a'; +type B = 'b'; +type C = 'c'; +type Union = A | B | C; function test(value: Union): number { switch (value) { - case 'a': return 1 + case 'a': + return 1; } } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', @@ -249,18 +297,19 @@ function test(value: Union): number { }, { code: ` -const A = 'a' -const B = 1 -const C = true +const A = 'a'; +const B = 1; +const C = true; -type Union = typeof A | typeof B | typeof C +type Union = typeof A | typeof B | typeof C; function test(value: Union): number { switch (value) { - case 'a': return 1 + case 'a': + return 1; } } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', @@ -274,14 +323,15 @@ function test(value: Union): number { }, { code: ` -type DiscriminatedUnion = { type: 'A', a: 1 } | { type: 'B', b: 2 } +type DiscriminatedUnion = { type: 'A'; a: 1 } | { type: 'B'; b: 2 }; function test(value: DiscriminatedUnion): number { switch (value.type) { - case 'A': return 1 + case 'A': + return 1; } } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', @@ -296,17 +346,24 @@ function test(value: DiscriminatedUnion): number { { // Still complains with empty switch code: ` -type Day = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday' +type Day = + | 'Monday' + | 'Tuesday' + | 'Wednesday' + | 'Thursday' + | 'Friday' + | 'Saturday' + | 'Sunday'; -const day = 'Monday' as Day +const day = 'Monday' as Day; switch (day) { } -`, + `, errors: [ { messageId: 'switchIsNotExhaustive', - line: 6, + line: 13, column: 9, data: { missingBranches: @@ -318,15 +375,15 @@ switch (day) { { // Still complains with union intersection part code: ` -type FooBar = string & { foo: void } | 'bar' +type FooBar = (string & { foo: void }) | 'bar'; -const foobar = 'bar' as FooBar -let result = 0 +const foobar = 'bar' as FooBar; +let result = 0; switch (foobar) { case 'bar': { - result = 42 - break + result = 42; + break; } } `, @@ -343,15 +400,16 @@ switch (foobar) { }, { code: ` -const a = Symbol('a') -const b = Symbol('b') -const c = Symbol('c') +const a = Symbol('a'); +const b = Symbol('b'); +const c = Symbol('c'); -type T = typeof a | typeof b | typeof c +type T = typeof a | typeof b | typeof c; function test(value: T): number { switch (value) { - case a: return 1; + case a: + return 1; } } `, @@ -370,30 +428,32 @@ function test(value: T): number { { // with existing cases present code: ` -type T = 1 | 2 +type T = 1 | 2; function test(value: T): number { switch (value) { - case 1: return 1; + case 1: + return 1; } } - `, + `.trimRight(), errors: [ { messageId: 'switchIsNotExhaustive', suggestions: [ { messageId: 'addMissingCases', - output: ` -type T = 1 | 2 + output: noFormat` +type T = 1 | 2; function test(value: T): number { switch (value) { - case 1: return 1; + case 1: + return 1; case 2: { throw new Error('Not implemented yet: 2 case') } } } - `, + `.trimRight(), }, ], }, @@ -402,21 +462,21 @@ function test(value: T): number { { // without existing cases code: ` -type T = 1 | 2 +type T = 1 | 2; function test(value: T): number { switch (value) { } } - `, + `.trimRight(), errors: [ { messageId: 'switchIsNotExhaustive', suggestions: [ { messageId: 'addMissingCases', - output: ` -type T = 1 | 2 + output: noFormat` +type T = 1 | 2; function test(value: T): number { switch (value) { @@ -424,7 +484,7 @@ function test(value: T): number { case 2: { throw new Error('Not implemented yet: 2 case') } } } - `, + `.trimRight(), }, ], }, From f21fa0463c121e01615bfd9ff2351db202c66978 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 18:43:58 -0700 Subject: [PATCH 12/92] chore: space-before-function-paren --- .../tests/rules/space-before-function-paren.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts b/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts index 7acb61fa85fa..eb515c1617cd 100644 --- a/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts +++ b/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import { TSESLint, AST_NODE_TYPES, From f314b0efa092042b7dae4ceadd0ebaa04ac88f30 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 18:48:49 -0700 Subject: [PATCH 13/92] chore: semi --- .../eslint-plugin/tests/rules/semi.test.ts | 464 +++++++++--------- 1 file changed, 232 insertions(+), 232 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/semi.test.ts b/packages/eslint-plugin/tests/rules/semi.test.ts index 95f22986b108..c8a8ff7f4791 100644 --- a/packages/eslint-plugin/tests/rules/semi.test.ts +++ b/packages/eslint-plugin/tests/rules/semi.test.ts @@ -133,129 +133,129 @@ class PanCamera extends FreeCamera { // https://github.com/eslint/eslint/issues/9521 { code: ` - do; while(a); - [1,2,3].forEach(doSomething) - `, + do; while(a); + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'any' }], }, { code: ` - do; while(a) - [1,2,3].forEach(doSomething) - `, + do; while(a) + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'any' }], }, { code: ` - import a from "a"; - [1,2,3].forEach(doSomething) - `, + import a from "a"; + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], }, { code: ` - var a = 0; export {a}; - [a] = b - `, + var a = 0; export {a}; + [a] = b + `, options: ['never', { beforeStatementContinuationChars: 'always' }], }, { code: ` - function wrap() { - return; - ({a} = b) - } - `, + function wrap() { + return; + ({a} = b) + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], parserOptions: { ecmaVersion: 2015 }, }, { code: ` - while (true) { - break; - +i - } - `, + while (true) { + break; + +i + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], }, { code: ` - while (true) { - continue; - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue; + [1,2,3].forEach(doSomething) + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], }, { code: ` - do; while(a); - [1,2,3].forEach(doSomething) - `, + do; while(a); + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], }, { code: ` - const f = () => {}; - [1,2,3].forEach(doSomething) - `, + const f = () => {}; + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], parserOptions: { ecmaVersion: 2015 }, }, { code: ` - import a from "a" - [1,2,3].forEach(doSomething) - `, + import a from "a" + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, }, { code: ` - var a = 0; export {a} - [a] = b - `, + var a = 0; export {a} + [a] = b + `, options: neverOptionWithoutContinuationChars, }, { code: ` - function wrap() { - return - ({a} = b) - } - `, + function wrap() { + return + ({a} = b) + } + `, options: neverOptionWithoutContinuationChars, parserOptions: { ecmaVersion: 2015 }, }, { code: ` - while (true) { - break - +i - } - `, + while (true) { + break + +i + } + `, options: neverOptionWithoutContinuationChars, }, { code: ` - while (true) { - continue - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue + [1,2,3].forEach(doSomething) + } + `, options: neverOptionWithoutContinuationChars, }, { code: ` - do; while(a) - [1,2,3].forEach(doSomething) - `, + do; while(a) + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, }, { code: ` - const f = () => {} - [1,2,3].forEach(doSomething) - `, + const f = () => {} + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, parserOptions: { ecmaVersion: 2015 }, }, @@ -665,11 +665,11 @@ class PanCamera extends FreeCamera { { code: ` - import a from "a" - (function() { - // ... - })() - `, + import a from "a" + (function() { + // ... + })() + `, options: [ 'never', { beforeStatementContinuationChars: 'always' }, @@ -678,11 +678,11 @@ class PanCamera extends FreeCamera { }, { code: ` - import a from "a" - ;(function() { - // ... - })() - `, + import a from "a" + ;(function() { + // ... + })() + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, @@ -767,299 +767,299 @@ class PanCamera extends FreeCamera { // https://github.com/eslint/eslint/issues/9521 { code: ` - import a from "a" - [1,2,3].forEach(doSomething) - `, + import a from "a" + [1,2,3].forEach(doSomething) + `, output: ` - import a from "a"; - [1,2,3].forEach(doSomething) - `, + import a from "a"; + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], errors: ['Missing semicolon.'], }, { code: ` - var a = 0; export {a} - [a] = b - `, + var a = 0; export {a} + [a] = b + `, output: ` - var a = 0; export {a}; - [a] = b - `, + var a = 0; export {a}; + [a] = b + `, options: ['never', { beforeStatementContinuationChars: 'always' }], errors: ['Missing semicolon.'], }, { code: ` - function wrap() { - return - ({a} = b) - } - `, + function wrap() { + return + ({a} = b) + } + `, output: ` - function wrap() { - return; - ({a} = b) - } - `, + function wrap() { + return; + ({a} = b) + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], parserOptions: { ecmaVersion: 2015 }, errors: ['Missing semicolon.'], }, { code: ` - while (true) { - break - +i - } - `, + while (true) { + break + +i + } + `, output: ` - while (true) { - break; - +i - } - `, + while (true) { + break; + +i + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], errors: ['Missing semicolon.'], }, { code: ` - while (true) { - continue - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue + [1,2,3].forEach(doSomething) + } + `, output: ` - while (true) { - continue; - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue; + [1,2,3].forEach(doSomething) + } + `, options: ['never', { beforeStatementContinuationChars: 'always' }], errors: ['Missing semicolon.'], }, { code: ` - do; while(a) - [1,2,3].forEach(doSomething) - `, + do; while(a) + [1,2,3].forEach(doSomething) + `, output: ` - do; while(a); - [1,2,3].forEach(doSomething) - `, + do; while(a); + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], errors: ['Missing semicolon.'], }, { code: ` - const f = () => {} - [1,2,3].forEach(doSomething) - `, + const f = () => {} + [1,2,3].forEach(doSomething) + `, output: ` - const f = () => {}; - [1,2,3].forEach(doSomething) - `, + const f = () => {}; + [1,2,3].forEach(doSomething) + `, options: ['never', { beforeStatementContinuationChars: 'always' }], parserOptions: { ecmaVersion: 2015 }, errors: ['Missing semicolon.'], }, { code: ` - import a from "a"; - [1,2,3].forEach(doSomething) - `, + import a from "a"; + [1,2,3].forEach(doSomething) + `, output: ` - import a from "a" - [1,2,3].forEach(doSomething) - `, + import a from "a" + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - var a = 0; export {a}; - [a] = b - `, + var a = 0; export {a}; + [a] = b + `, output: ` - var a = 0; export {a} - [a] = b - `, + var a = 0; export {a} + [a] = b + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - function wrap() { - return; - ({a} = b) - } - `, + function wrap() { + return; + ({a} = b) + } + `, output: ` - function wrap() { - return - ({a} = b) - } - `, + function wrap() { + return + ({a} = b) + } + `, options: neverOptionWithoutContinuationChars, parserOptions: { ecmaVersion: 2015 }, errors: [extraSemicolon], }, { code: ` - while (true) { - break; - +i - } - `, + while (true) { + break; + +i + } + `, output: ` - while (true) { - break - +i - } - `, + while (true) { + break + +i + } + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - while (true) { - continue; - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue; + [1,2,3].forEach(doSomething) + } + `, output: ` - while (true) { - continue - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue + [1,2,3].forEach(doSomething) + } + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - do; while(a); - [1,2,3].forEach(doSomething) - `, + do; while(a); + [1,2,3].forEach(doSomething) + `, output: ` - do; while(a) - [1,2,3].forEach(doSomething) - `, + do; while(a) + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - const f = () => {}; - [1,2,3].forEach(doSomething) - `, + const f = () => {}; + [1,2,3].forEach(doSomething) + `, output: ` - const f = () => {} - [1,2,3].forEach(doSomething) - `, + const f = () => {} + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, parserOptions: { ecmaVersion: 2015 }, errors: [extraSemicolon], }, { code: ` - import a from "a" - ;[1,2,3].forEach(doSomething) - `, + import a from "a" + ;[1,2,3].forEach(doSomething) + `, output: ` - import a from "a" - [1,2,3].forEach(doSomething) - `, + import a from "a" + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - var a = 0; export {a} - ;[1,2,3].forEach(doSomething) - `, + var a = 0; export {a} + ;[1,2,3].forEach(doSomething) + `, output: ` - var a = 0; export {a} - [1,2,3].forEach(doSomething) - `, + var a = 0; export {a} + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - function wrap() { - return - ;[1,2,3].forEach(doSomething) - } - `, + function wrap() { + return + ;[1,2,3].forEach(doSomething) + } + `, output: ` - function wrap() { - return - [1,2,3].forEach(doSomething) - } - `, + function wrap() { + return + [1,2,3].forEach(doSomething) + } + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - while (true) { - break - ;[1,2,3].forEach(doSomething) - } - `, + while (true) { + break + ;[1,2,3].forEach(doSomething) + } + `, output: ` - while (true) { - break - [1,2,3].forEach(doSomething) - } - `, + while (true) { + break + [1,2,3].forEach(doSomething) + } + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - while (true) { - continue - ;[1,2,3].forEach(doSomething) - } - `, + while (true) { + continue + ;[1,2,3].forEach(doSomething) + } + `, output: ` - while (true) { - continue - [1,2,3].forEach(doSomething) - } - `, + while (true) { + continue + [1,2,3].forEach(doSomething) + } + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - do; while(a) - ;[1,2,3].forEach(doSomething) - `, + do; while(a) + ;[1,2,3].forEach(doSomething) + `, output: ` - do; while(a) - [1,2,3].forEach(doSomething) - `, + do; while(a) + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, errors: [extraSemicolon], }, { code: ` - const f = () => {} - ;[1,2,3].forEach(doSomething) - `, + const f = () => {} + ;[1,2,3].forEach(doSomething) + `, output: ` - const f = () => {} - [1,2,3].forEach(doSomething) - `, + const f = () => {} + [1,2,3].forEach(doSomething) + `, options: neverOptionWithoutContinuationChars, parserOptions: { ecmaVersion: 2015 }, errors: [extraSemicolon], From 0405b4af086ee5c666e472238f9591cc1adb0a9a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 18:53:23 -0700 Subject: [PATCH 14/92] chore: return-await --- .../tests/rules/return-await.test.ts | 599 ++++++++++-------- 1 file changed, 344 insertions(+), 255 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/return-await.test.ts b/packages/eslint-plugin/tests/rules/return-await.test.ts index 8599a937e198..94c2c398733c 100644 --- a/packages/eslint-plugin/tests/rules/return-await.test.ts +++ b/packages/eslint-plugin/tests/rules/return-await.test.ts @@ -15,201 +15,246 @@ const ruleTester = new RuleTester({ // default rule is in-try-catch ruleTester.run('return-await', rule, { valid: [ - `function test() { - return; - }`, - `function test() { - return 1; - }`, - `async function test() { - return; - }`, - `async function test() { - return 1; - }`, - `const test = () => 1;`, - `const test = async () => 1;`, - `async function test() { - return Promise.resolve(1); - }`, - `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + ` + function test() { + return; } - }`, - `async function test() { - try { - const one = await Promise.resolve(1); - return one; - } catch (e) { - const two = await Promise.resolve(2); - return two; - } finally { - console.log('cleanup'); + `, + ` + function test() { + return 1; + } + `, + ` + async function test() { + return; + } + `, + ` + async function test() { + return 1; + } + `, + 'const test = () => 1;', + 'const test = async () => 1;', + ` + async function test() { + return Promise.resolve(1); + } + `, + ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } + } + `, + ` + async function test() { + try { + const one = await Promise.resolve(1); + return one; + } catch (e) { + const two = await Promise.resolve(2); + return two; + } finally { + console.log('cleanup'); + } } - }`, + `, { options: ['in-try-catch'], - code: `function test() { - return 1; - }`, + code: ` + function test() { + return 1; + } + `, }, { options: ['in-try-catch'], - code: `async function test() { - return 1; - }`, + code: ` + async function test() { + return 1; + } + `, }, { options: ['in-try-catch'], - code: `const test = () => 1;`, + code: 'const test = () => 1;', }, { options: ['in-try-catch'], - code: `const test = async () => 1;`, + code: 'const test = async () => 1;', }, { options: ['in-try-catch'], - code: `async function test() { - return Promise.resolve(1); - }`, + code: ` + async function test() { + return Promise.resolve(1); + } + `, }, { options: ['in-try-catch'], - code: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); - } - }`, + code: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } + } + `, }, { options: ['in-try-catch'], - code: `async function test() { - try { - const one = await Promise.resolve(1); - return one; - } catch (e) { - const two = await Promise.resolve(2); - return two; - } finally { - console.log('cleanup'); - } - }`, + code: ` + async function test() { + try { + const one = await Promise.resolve(1); + return one; + } catch (e) { + const two = await Promise.resolve(2); + return two; + } finally { + console.log('cleanup'); + } + } + `, }, { options: ['never'], - code: `async function test() { - return Promise.resolve(1); - }`, + code: ` + async function test() { + return Promise.resolve(1); + } + `, }, { options: ['never'], - code: `const test = async () => Promise.resolve(1);`, + code: 'const test = async () => Promise.resolve(1);', }, { options: ['never'], - code: `async function test() { - try { - return Promise.resolve(1); - } catch (e) { - return Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return Promise.resolve(1); + } catch (e) { + return Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, }, { options: ['always'], - code: `async function test() { - return await Promise.resolve(1); - }`, + code: ` + async function test() { + return await Promise.resolve(1); + } + `, }, { options: ['always'], - code: `const test = async () => await Promise.resolve(1);`, + code: 'const test = async () => await Promise.resolve(1);', }, { options: ['always'], - code: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, }, { - code: `async function test(): Promise { - const res = await Promise.resolve('{}'); - try { - return JSON.parse(res); - } catch (error) { - return res; + code: ` + async function test(): Promise { + const res = await Promise.resolve('{}'); + try { + return JSON.parse(res); + } catch (error) { + return res; + } } - }`, + `, }, ], invalid: [ { - code: `async function test() { - return await 1; - }`, - output: `async function test() { - return 1; - }`, + code: ` + async function test() { + return await 1; + } + `, + output: ` + async function test() { + return 1; + } + `, errors: [ { - line: 2, + line: 3, messageId: 'nonPromiseAwait', }, ], }, { - code: `async function test() { - const foo = 1; - return await{foo}; - }`, - output: `async function test() { - const foo = 1; - return {foo}; - }`, + code: ` + async function test() { + const foo = 1; + return await { foo }; + } + `, + output: ` + async function test() { + const foo = 1; + return { foo }; + } + `, errors: [ { - line: 3, + line: 4, messageId: 'nonPromiseAwait', }, ], }, { - code: `async function test() { - const foo = 1; - return await - foo; - }`, - output: `async function test() { - const foo = 1; - return foo; - }`, + code: ` + async function test() { + const foo = 1; + return await foo; + } + `, + output: ` + async function test() { + const foo = 1; + return foo; + } + `, errors: [ { - line: 3, + line: 4, messageId: 'nonPromiseAwait', }, ], }, { - code: `const test = async () => await 1;`, - output: `const test = async () => 1;`, + code: 'const test = async () => await 1;', + output: 'const test = async () => 1;', errors: [ { line: 1, @@ -218,8 +263,8 @@ ruleTester.run('return-await', rule, { ], }, { - code: `const test = async () => await/* comment */1;`, - output: `const test = async () => /* comment */1;`, + code: 'const test = async () => await /* comment */ 1;', + output: 'const test = async () => /* comment */ 1;', errors: [ { line: 1, @@ -228,8 +273,8 @@ ruleTester.run('return-await', rule, { ], }, { - code: `const test = async () => await Promise.resolve(1);`, - output: `const test = async () => Promise.resolve(1);`, + code: 'const test = async () => await Promise.resolve(1);', + output: 'const test = async () => Promise.resolve(1);', errors: [ { line: 1, @@ -238,68 +283,80 @@ ruleTester.run('return-await', rule, { ], }, { - code: `async function test() { - try { - return Promise.resolve(1); - } catch (e) { - return Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return Promise.resolve(1); + } catch (e) { + return Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, - output: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + `, + output: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, errors: [ { - line: 3, + line: 4, messageId: 'requiredPromiseAwait', }, { - line: 5, + line: 6, messageId: 'requiredPromiseAwait', }, ], }, { - code: `async function test() { - return await Promise.resolve(1); - }`, - output: `async function test() { - return Promise.resolve(1); - }`, + code: ` + async function test() { + return await Promise.resolve(1); + } + `, + output: ` + async function test() { + return Promise.resolve(1); + } + `, errors: [ { - line: 2, + line: 3, messageId: 'disallowedPromiseAwait', }, ], }, { options: ['in-try-catch'], - code: `async function test() { - return await 1; - }`, - output: `async function test() { - return 1; - }`, + code: ` + async function test() { + return await 1; + } + `, + output: ` + async function test() { + return 1; + } + `, errors: [ { - line: 2, + line: 3, messageId: 'nonPromiseAwait', }, ], }, { options: ['in-try-catch'], - code: `const test = async () => await 1;`, - output: `const test = async () => 1;`, + code: 'const test = async () => await 1;', + output: 'const test = async () => 1;', errors: [ { line: 1, @@ -309,8 +366,8 @@ ruleTester.run('return-await', rule, { }, { options: ['in-try-catch'], - code: `const test = async () => await Promise.resolve(1);`, - output: `const test = async () => Promise.resolve(1);`, + code: 'const test = async () => await Promise.resolve(1);', + output: 'const test = async () => Promise.resolve(1);', errors: [ { line: 1, @@ -320,175 +377,207 @@ ruleTester.run('return-await', rule, { }, { options: ['in-try-catch'], - code: `async function test() { - try { - return Promise.resolve(1); - } catch (e) { - return Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return Promise.resolve(1); + } catch (e) { + return Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, - output: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + `, + output: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, errors: [ { - line: 3, + line: 4, messageId: 'requiredPromiseAwait', }, { - line: 5, + line: 6, messageId: 'requiredPromiseAwait', }, ], }, { options: ['in-try-catch'], - code: `async function test() { - return await Promise.resolve(1); - }`, - output: `async function test() { - return Promise.resolve(1); - }`, + code: ` + async function test() { + return await Promise.resolve(1); + } + `, + output: ` + async function test() { + return Promise.resolve(1); + } + `, errors: [ { - line: 2, + line: 3, messageId: 'disallowedPromiseAwait', }, ], }, { options: ['never'], - code: `async function test() { - return await 1; - }`, - output: `async function test() { - return 1; - }`, + code: ` + async function test() { + return await 1; + } + `, + output: ` + async function test() { + return 1; + } + `, errors: [ { - line: 2, + line: 3, messageId: 'nonPromiseAwait', }, ], }, { options: ['never'], - code: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, - output: `async function test() { - try { - return Promise.resolve(1); - } catch (e) { - return Promise.resolve(2); - } finally { - console.log('cleanup'); + `, + output: ` + async function test() { + try { + return Promise.resolve(1); + } catch (e) { + return Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, errors: [ { - line: 3, + line: 4, messageId: 'disallowedPromiseAwait', }, { - line: 5, + line: 6, messageId: 'disallowedPromiseAwait', }, ], }, { options: ['never'], - code: `async function test() { - return await Promise.resolve(1); - }`, - output: `async function test() { - return Promise.resolve(1); - }`, + code: ` + async function test() { + return await Promise.resolve(1); + } + `, + output: ` + async function test() { + return Promise.resolve(1); + } + `, errors: [ { - line: 2, + line: 3, messageId: 'disallowedPromiseAwait', }, ], }, { options: ['always'], - code: `async function test() { - return await 1; - }`, - output: `async function test() { - return 1; - }`, + code: ` + async function test() { + return await 1; + } + `, + output: ` + async function test() { + return 1; + } + `, errors: [ { - line: 2, + line: 3, messageId: 'nonPromiseAwait', }, ], }, { options: ['always'], - code: `async function test() { - try { - return Promise.resolve(1); - } catch (e) { - return Promise.resolve(2); - } finally { - console.log('cleanup'); + code: ` + async function test() { + try { + return Promise.resolve(1); + } catch (e) { + return Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, - output: `async function test() { - try { - return await Promise.resolve(1); - } catch (e) { - return await Promise.resolve(2); - } finally { - console.log('cleanup'); + `, + output: ` + async function test() { + try { + return await Promise.resolve(1); + } catch (e) { + return await Promise.resolve(2); + } finally { + console.log('cleanup'); + } } - }`, + `, errors: [ { - line: 3, + line: 4, messageId: 'requiredPromiseAwait', }, { - line: 5, + line: 6, messageId: 'requiredPromiseAwait', }, ], }, { options: ['always'], - code: `async function test() { - return Promise.resolve(1); - }`, - output: `async function test() { - return await Promise.resolve(1); - }`, + code: ` + async function test() { + return Promise.resolve(1); + } + `, + output: ` + async function test() { + return await Promise.resolve(1); + } + `, errors: [ { - line: 2, + line: 3, messageId: 'requiredPromiseAwait', }, ], }, { options: ['always'], - code: `const test = async () => Promise.resolve(1);`, + code: 'const test = async () => Promise.resolve(1);', errors: [ { line: 1, From 7de523d64ce0cc82822a94fc2cf508c3d9800b77 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 19:34:00 -0700 Subject: [PATCH 15/92] chore: restrict-template-expressions --- .../rules/restrict-template-expressions.test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts b/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts index cdf59b0c8197..3e18e7ac1b1e 100644 --- a/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts @@ -15,15 +15,15 @@ ruleTester.run('restrict-template-expressions', rule, { valid: [ // Base case ` - const msg = \`arg = \${"foo"}\`; + const msg = \`arg = \${'foo'}\`; `, ` - const arg = "foo"; + const arg = 'foo'; const msg = \`arg = \${arg}\`; `, ` - const arg = "foo"; - const msg = \`arg = \${arg || "default"}\`; + const arg = 'foo'; + const msg = \`arg = \${arg || 'default'}\`; `, ` function test(arg: T) { @@ -50,14 +50,14 @@ ruleTester.run('restrict-template-expressions', rule, { options: [{ allowNumber: true }], code: ` const arg = 123; - const msg = \`arg = \${arg || "default"}\`; + const msg = \`arg = \${arg || 'default'}\`; `, }, { options: [{ allowNumber: true }], code: ` const arg = 123n; - const msg = \`arg = \${arg || "default"}\`; + const msg = \`arg = \${arg || 'default'}\`; `, }, { @@ -96,7 +96,7 @@ ruleTester.run('restrict-template-expressions', rule, { options: [{ allowBoolean: true }], code: ` const arg = true; - const msg = \`arg = \${arg || "default"}\`; + const msg = \`arg = \${arg || 'default'}\`; `, }, { @@ -150,7 +150,7 @@ ruleTester.run('restrict-template-expressions', rule, { { options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }], code: ` - type All = string | number | boolean | null | undefined + type All = string | number | boolean | null | undefined; function test(arg: T) { return \`arg = \${arg}\`; } From cc31234ebeb28bd092cef247a3d42bf4ebee2f7c Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 19:41:08 -0700 Subject: [PATCH 16/92] chore: restrict-plus-operands --- .../rules/restrict-plus-operands.test.ts | 171 +++++++++--------- 1 file changed, 89 insertions(+), 82 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts index 6270b61eec3d..43682806f595 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -13,20 +13,25 @@ const ruleTester = new RuleTester({ ruleTester.run('restrict-plus-operands', rule, { valid: [ - `var x = 5;`, - `var y = "10";`, - `var z = 8.2;`, - `var w = "6.5";`, - `var foo = 5 + 10;`, - `var foo = "5.5" + "10";`, - `var foo = parseInt("5.5", 10) + 10;`, - `var foo = parseFloat("5.5", 10) + 10;`, - `var foo = 1n + 1n;`, - `var foo = BigInt(1) + 1n`, - `var foo = 1n; foo + 2n`, + 'var x = 5;', + "var y = '10';", + 'var z = 8.2;', + "var w = '6.5';", + 'var foo = 5 + 10;', + "var foo = '5.5' + '10';", + "var foo = parseInt('5.5', 10) + 10;", + "var foo = parseFloat('5.5', 10) + 10;", + 'var foo = 1n + 1n;', + 'var foo = BigInt(1) + 1n;', ` -function test(s: string, n: number) : number { return 2; } -var foo = test("5.5", 10) + 10; + var foo = 1n; + foo + 2n; + `, + ` +function test(s: string, n: number): number { + return 2; +} +var foo = test('5.5', 10) + 10; `, ` var x = 5; @@ -34,57 +39,63 @@ var z = 8.2; var foo = x + z; `, ` -var w = "6.5"; -var y = "10"; +var w = '6.5'; +var y = '10'; var foo = y + w; `, 'var foo = 1 + 1;', "var foo = '1' + '1';", ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; +var pair: { first: number; second: string } = { first: 5, second: '10' }; var foo = pair.first + 10; `, ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; +var pair: { first: number; second: string } = { first: 5, second: '10' }; var foo = pair.first + (10 as number); `, ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; -var foo = "5.5" + pair.second; +var pair: { first: number; second: string } = { first: 5, second: '10' }; +var foo = '5.5' + pair.second; + `, + ` +var pair: { first: number; second: string } = { first: 5, second: '10' }; +var foo = ('5.5' as string) + pair.second; `, ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; -var foo = ("5.5" as string) + pair.second; + const foo = + 'hello' + + (someBoolean ? 'a' : 'b') + + (() => (someBoolean ? 'c' : 'd'))() + + 'e'; `, - `const foo = 'hello' + (someBoolean ? 'a' : 'b') + (() => someBoolean ? 'c' : 'd')() + 'e';`, - `const balls = true;`, - `balls === true;`, + 'const balls = true;', + 'balls === true;', // https://github.com/typescript-eslint/typescript-eslint/issues/230 ` function foo(a: T) { - return a + ""; + return a + ''; } `, ` -function foo(a: T) { - return a + ""; +function foo(a: T) { + return a + ''; } `, ` function foo(a: T) { - return a + 1; + return a + 1; } `, ` function foo(a: T) { - return a + 1; + return a + 1; } `, { code: ` let foo: number = 0; foo += 1; - `, + `, options: [ { checkCompoundAssignments: false, @@ -94,8 +105,8 @@ foo += 1; { code: ` let foo: number = 0; -foo += "string"; - `, +foo += 'string'; + `, options: [ { checkCompoundAssignments: false, @@ -125,7 +136,7 @@ foo += "string"; ], }, { - code: `var foo = 5 + "10";`, + code: "var foo = 5 + '10';", errors: [ { messageId: 'notStrings', @@ -135,7 +146,7 @@ foo += "string"; ], }, { - code: `var foo = [] + 5;`, + code: 'var foo = [] + 5;', errors: [ { messageId: 'notNumbers', @@ -145,7 +156,7 @@ foo += "string"; ], }, { - code: `var foo = [] + {};`, + code: 'var foo = [] + [];', errors: [ { messageId: 'notNumbers', @@ -155,7 +166,7 @@ foo += "string"; ], }, { - code: `var foo = [] + [];`, + code: 'var foo = 5 + [];', errors: [ { messageId: 'notNumbers', @@ -165,17 +176,7 @@ foo += "string"; ], }, { - code: `var foo = 5 + [];`, - errors: [ - { - messageId: 'notNumbers', - line: 1, - column: 11, - }, - ], - }, - { - code: `var foo = "5" + {};`, + code: "var foo = '5' + {};", errors: [ { messageId: 'notStrings', @@ -185,7 +186,7 @@ foo += "string"; ], }, { - code: `var foo = 5.5 + "5";`, + code: "var foo = 5.5 + '5';", errors: [ { messageId: 'notStrings', @@ -195,7 +196,7 @@ foo += "string"; ], }, { - code: `var foo = "5.5" + 5;`, + code: "var foo = '5.5' + 5;", errors: [ { messageId: 'notStrings', @@ -207,9 +208,9 @@ foo += "string"; { code: ` var x = 5; -var y = "10"; +var y = '10'; var foo = x + y; - `, + `, errors: [ { messageId: 'notStrings', @@ -221,9 +222,9 @@ var foo = x + y; { code: ` var x = 5; -var y = "10"; +var y = '10'; var foo = y + x; - `, + `, errors: [ { messageId: 'notStrings', @@ -236,7 +237,7 @@ var foo = y + x; code: ` var x = 5; var foo = x + {}; - `, + `, errors: [ { messageId: 'notNumbers', @@ -247,9 +248,9 @@ var foo = x + {}; }, { code: ` -var y = "10"; +var y = '10'; var foo = [] + y; - `, + `, errors: [ { messageId: 'notStrings', @@ -260,9 +261,9 @@ var foo = [] + y; }, { code: ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; -var foo = pair.first + "10"; - `, +var pair: { first: number; second: string } = { first: 5, second: '10' }; +var foo = pair.first + '10'; + `, errors: [ { messageId: 'notStrings', @@ -273,9 +274,9 @@ var foo = pair.first + "10"; }, { code: ` -var pair: { first: number, second: string } = { first: 5, second: "10" }; +var pair: { first: number; second: string } = { first: 5, second: '10' }; var foo = 5 + pair.second; - `, + `, errors: [ { messageId: 'notStrings', @@ -285,7 +286,7 @@ var foo = 5 + pair.second; ], }, { - code: `var foo = parseInt("5.5", 10) + "10";`, + code: "var foo = parseInt('5.5', 10) + '10';", errors: [ { messageId: 'notStrings', @@ -296,9 +297,9 @@ var foo = 5 + pair.second; }, { code: ` -var pair = { first: 5, second: "10" }; +var pair = { first: 5, second: '10' }; var foo = pair + pair; - `, + `, errors: [ { messageId: 'notNumbers', @@ -308,7 +309,7 @@ var foo = pair + pair; ], }, { - code: `var foo = 1n + 1`, + code: 'var foo = 1n + 1;', errors: [ { messageId: 'notBigInts', @@ -318,7 +319,7 @@ var foo = pair + pair; ], }, { - code: `var foo = 1 + 1n`, + code: 'var foo = 1 + 1n;', errors: [ { messageId: 'notBigInts', @@ -328,22 +329,28 @@ var foo = pair + pair; ], }, { - code: `var foo = 1n; foo + 1`, + code: ` + var foo = 1n; + foo + 1; + `, errors: [ { messageId: 'notBigInts', - line: 1, - column: 15, + line: 3, + column: 9, }, ], }, { - code: `var foo = 1; foo + 1n`, + code: ` + var foo = 1; + foo + 1n; + `, errors: [ { messageId: 'notBigInts', - line: 1, - column: 14, + line: 3, + column: 9, }, ], }, @@ -351,63 +358,63 @@ var foo = pair + pair; { code: ` function foo(a: T) { - return a + 1; + return a + 1; } `, errors: [ { messageId: 'notStrings', line: 3, - column: 12, + column: 10, }, ], }, { code: ` -function foo(a: T) { - return a + 1; +function foo(a: T) { + return a + 1; } `, errors: [ { messageId: 'notStrings', line: 3, - column: 12, + column: 10, }, ], }, { code: ` function foo(a: T) { - return a + ""; + return a + ''; } `, errors: [ { messageId: 'notStrings', line: 3, - column: 12, + column: 10, }, ], }, { code: ` function foo(a: T) { - return a + ""; + return a + ''; } `, errors: [ { messageId: 'notStrings', line: 3, - column: 12, + column: 10, }, ], }, { code: ` let foo: string | undefined; -foo += "some data"; +foo += 'some data'; `, options: [ { From 2a33b47bc21e7db6f5b72c37ee0d21991f20d88a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 20:02:19 -0700 Subject: [PATCH 17/92] chore: require-await --- .../tests/rules/require-await.test.ts | 469 +++++++++++------- 1 file changed, 294 insertions(+), 175 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/require-await.test.ts b/packages/eslint-plugin/tests/rules/require-await.test.ts index caea93fbfb95..05c4bf739b91 100644 --- a/packages/eslint-plugin/tests/rules/require-await.test.ts +++ b/packages/eslint-plugin/tests/rules/require-await.test.ts @@ -15,131 +15,168 @@ const ruleTester = new RuleTester({ ruleTester.run('require-await', rule, { valid: [ // Non-async function declaration - `function numberOne(): number { - return 1; - }`, + ` +function numberOne(): number { + return 1; +} + `, // Non-async function expression - `const numberOne = function(): number { - return 1; - }`, + ` +const numberOne = function(): number { + return 1; +}; + `, // Non-async arrow function expression (concise-body) - `const numberOne = (): number => 1;`, + ` + const numberOne = (): number => 1; + `, // Non-async arrow function expression (block-body) - `const numberOne = (): number => { - return 1; - };`, + ` +const numberOne = (): number => { + return 1; +}; + `, // Non-async function that returns a promise // https://github.com/typescript-eslint/typescript-eslint/issues/1226 ` - function delay() { - return Promise.resolve(); - } - `, +function delay() { + return Promise.resolve(); +} + `, ` - const delay = () => { - return Promise.resolve(); - } - `, - `const delay = () => Promise.resolve();`, +const delay = () => { + return Promise.resolve(); +}; + `, + 'const delay = () => Promise.resolve();', // Async function declaration with await - `async function numberOne(): Promise { - return await 1; - }`, + ` +async function numberOne(): Promise { + return await 1; +} + `, // Async function expression with await - `const numberOne = async function(): Promise { - return await 1; - }`, + ` +const numberOne = async function(): Promise { + return await 1; +}; + `, // Async arrow function expression with await (concise-body) - `const numberOne = async (): Promise => await 1;`, + 'const numberOne = async (): Promise => await 1;', // Async arrow function expression with await (block-body) - `const numberOne = async (): Promise => { - return await 1; - };`, + ` +const numberOne = async (): Promise => { + return await 1; +}; + `, // Async function declaration with promise return - `async function numberOne(): Promise { - return Promise.resolve(1); - }`, + ` +async function numberOne(): Promise { + return Promise.resolve(1); +} + `, // Async function expression with promise return - `const numberOne = async function(): Promise { - return Promise.resolve(1); - }`, + ` +const numberOne = async function(): Promise { + return Promise.resolve(1); +}; + `, // Async arrow function with promise return (concise-body) - `const numberOne = async (): Promise => Promise.resolve(1);`, + 'const numberOne = async (): Promise => Promise.resolve(1);', // Async arrow function with promise return (block-body) - `const numberOne = async (): Promise => { - return Promise.resolve(1); - };`, + ` +const numberOne = async (): Promise => { + return Promise.resolve(1); +}; + `, // Async function declaration with async function return - `async function numberOne(): Promise { - return getAsyncNumber(1); - } - async function getAsyncNumber(x: number): Promise { - return Promise.resolve(x); - }`, + ` +async function numberOne(): Promise { + return getAsyncNumber(1); +} +async function getAsyncNumber(x: number): Promise { + return Promise.resolve(x); +} + `, // Async function expression with async function return - `const numberOne = async function(): Promise { - return getAsyncNumber(1); - } - const getAsyncNumber = async function(x: number): Promise { - return Promise.resolve(x); - }`, + ` +const numberOne = async function(): Promise { + return getAsyncNumber(1); +}; +const getAsyncNumber = async function(x: number): Promise { + return Promise.resolve(x); +}; + `, // Async arrow function with async function return (concise-body) - `const numberOne = async (): Promise => getAsyncNumber(1); - const getAsyncNumber = async function(x: number): Promise { - return Promise.resolve(x); - }`, + ` +const numberOne = async (): Promise => getAsyncNumber(1); +const getAsyncNumber = async function(x: number): Promise { + return Promise.resolve(x); +}; + `, // Async arrow function with async function return (block-body) - `const numberOne = async (): Promise => { - return getAsyncNumber(1); - }; - const getAsyncNumber = async function(x: number): Promise { - return Promise.resolve(x); - }`, + ` +const numberOne = async (): Promise => { + return getAsyncNumber(1); +}; +const getAsyncNumber = async function(x: number): Promise { + return Promise.resolve(x); +}; + `, // https://github.com/typescript-eslint/typescript-eslint/issues/1188 ` - async function testFunction(): Promise { - await Promise.all([1, 2, 3].map( - // this should not trigger an error on the parent function - async value => Promise.resolve(value) - )) - } - `, +async function testFunction(): Promise { + await Promise.all( + [1, 2, 3].map( + // this should not trigger an error on the parent function + async value => Promise.resolve(value), + ), + ); +} + `, + 'async function* run() {}', ` - async function* run() { - await new Promise(resolve => setTimeout(resolve, 100)); - yield 'Hello'; - console.log('World'); - } - `, - 'async function* run() { }', - 'async function* asyncGenerator() { await Promise.resolve(); yield 1 }', - 'function* test6() { yield* syncGenerator() }', - 'function* test8() { yield syncGenerator() }', - 'function* syncGenerator() { yield 1 }', +async function* run() { + await new Promise(resolve => setTimeout(resolve, 100)); + yield 'Hello'; + console.log('World'); +} + `, ` - async function* asyncGenerator() { - await Promise.resolve() - yield 1 - } - async function* test1() {yield* asyncGenerator() } +function* test6() { + yield* syncGenerator(); +} + `, + ` +function* syncGenerator() { + yield 1; +} + `, + ` +async function* asyncGenerator() { + await Promise.resolve(); + yield 1; +} +async function* test1() { + yield* asyncGenerator(); +} + `, + 'const foo: () => void = async function*() {};', + ` +async function* foo(): Promise { + return new Promise(res => res(\`hello\`)); +} `, - `async function* foo() { - await Promise.resolve() - yield 1 - } - async function* bar() { - yield* foo() - }`, - 'const foo : () => void = async function *(){}', - 'async function* foo() : Promise { return new Promise((res) => res(`hello`)) }', ], invalid: [ { // Async function declaration with no await - code: `async function numberOne(): Promise { - return 1; - }`, + code: ` +async function numberOne(): Promise { + return 1; +} + `, errors: [ { messageId: 'missingAwait', @@ -151,9 +188,11 @@ ruleTester.run('require-await', rule, { }, { // Async function expression with no await - code: `const numberOne = async function(): Promise { - return 1; - }`, + code: ` +const numberOne = async function(): Promise { + return 1; +}; + `, errors: [ { messageId: 'missingAwait', @@ -165,7 +204,7 @@ ruleTester.run('require-await', rule, { }, { // Async arrow function expression with no await - code: `const numberOne = async (): Promise => 1;`, + code: 'const numberOne = async (): Promise => 1;', errors: [ { messageId: 'missingAwait', @@ -177,8 +216,13 @@ ruleTester.run('require-await', rule, { }, { // non-async function with await inside async function without await - code: - 'async function foo() { function nested() { await doSomething() } }', + code: ` + async function foo() { + function nested() { + await doSomething(); + } + } + `, errors: [ { messageId: 'missingAwait', @@ -189,7 +233,11 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function* foo() : void { doSomething() }', + code: ` +async function* foo(): void { + doSomething(); +} + `, errors: [ { messageId: 'missingAwait', @@ -200,7 +248,11 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function* foo() { yield 1 }', + code: ` +async function* foo() { + yield 1; +} + `, errors: [ { messageId: 'missingAwait', @@ -211,18 +263,11 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function* run() { console.log("bar") }', - errors: [ - { - messageId: 'missingAwait', - data: { - name: "Async generator function 'run'", - }, - }, - ], - }, - { - code: 'const foo = async function *(){ console.log("bar") }', + code: ` +const foo = async function*() { + console.log('bar'); +}; + `, errors: [ { messageId: 'missingAwait', @@ -233,34 +278,34 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function* syncGenerator() { yield 1 }', - errors: [ - { - messageId: 'missingAwait', - data: { - name: "Async generator function 'syncGenerator'", - }, - }, - ], - }, - { - code: 'async function* test3() { yield asyncGenerator() }', + code: ` +async function* asyncGenerator() { + yield 1; +} + `, errors: [ { messageId: 'missingAwait', data: { - name: "Async generator function 'test3'", + name: "Async generator function 'asyncGenerator'", }, }, ], }, { - code: 'async function* test7() { yield syncGenerator() }', + code: ` +function* syncGenerator() { + yield 1; +} +async function* asyncGenerator() { + yield* syncGenerator(); +} + `, errors: [ { messageId: 'missingAwait', data: { - name: "Async generator function 'test7'", + name: "Async generator function 'asyncGenerator'", }, }, ], @@ -271,36 +316,84 @@ ruleTester.run('require-await', rule, { // https://github.com/eslint/eslint/blob/03a69dbe86d5b5768a310105416ae726822e3c1c/tests/lib/rules/require-await.js#L25-L132 ruleTester.run('require-await', rule, { valid: [ - 'async function foo() { await doSomething() }', - '(async function() { await doSomething() })', - 'async () => { await doSomething() }', - 'async () => await doSomething()', - '({ async foo() { await doSomething() } })', - 'class A { async foo() { await doSomething() } }', - '(class { async foo() { await doSomething() } })', - 'async function foo() { await (async () => { await doSomething() }) }', + ` +async function foo() { + await doSomething(); +} + `, + ` +(async function() { + await doSomething(); +}); + `, + ` +async () => { + await doSomething(); +}; + `, + 'async () => await doSomething();', + ` +({ + async foo() { + await doSomething(); + }, +}); + `, + ` +class A { + async foo() { + await doSomething(); + } +} + `, + ` +(class { + async foo() { + await doSomething(); + } +}); + `, + ` +async function foo() { + await (async () => { + await doSomething(); + }); +} + `, // empty functions are ok. 'async function foo() {}', - 'async () => {}', + 'async () => {};', // normal functions are ok. - 'function foo() { doSomething() }', + ` +function foo() { + doSomething(); +} + `, // for-await-of - 'async function foo() { for await (x of xs); }', + ` +async function foo() { + for await (x of xs); +} + `, // global await { - code: 'await foo()', + code: 'await foo();', }, { code: ` - for await (let num of asyncIterable) { - console.log(num); - } - `, +for await (let num of asyncIterable) { + console.log(num); +} + `, }, ], invalid: [ { - code: 'async function foo() { doSomething() }', + code: ` + async function foo() { + doSomething(); + } + `, errors: [ { messageId: 'missingAwait', @@ -309,7 +402,11 @@ ruleTester.run('require-await', rule, { ], }, { - code: '(async function() { doSomething() })', + code: ` + (async function() { + doSomething(); + }); + `, errors: [ { messageId: 'missingAwait', @@ -318,7 +415,11 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async () => { doSomething() }', + code: ` + async () => { + doSomething(); + }; + `, errors: [ { messageId: 'missingAwait', @@ -327,7 +428,7 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async () => doSomething()', + code: 'async () => doSomething();', errors: [ { messageId: 'missingAwait', @@ -336,7 +437,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: '({ async foo() { doSomething() } })', + code: ` + ({ + async foo() { + doSomething(); + }, + }); + `, errors: [ { messageId: 'missingAwait', @@ -345,7 +452,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'class A { async foo() { doSomething() } }', + code: ` + class A { + async foo() { + doSomething(); + } + } + `, errors: [ { messageId: 'missingAwait', @@ -354,7 +467,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: '(class { async foo() { doSomething() } })', + code: ` + (class { + async foo() { + doSomething(); + } + }); + `, errors: [ { messageId: 'missingAwait', @@ -363,7 +482,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: "(class { async ''() { doSomething() } })", + code: ` + (class { + async ''() { + doSomething(); + } + }); + `, errors: [ { messageId: 'missingAwait', @@ -372,7 +497,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function foo() { async () => { await doSomething() } }', + code: ` + async function foo() { + async () => { + await doSomething(); + }; + } + `, errors: [ { messageId: 'missingAwait', @@ -381,7 +512,13 @@ ruleTester.run('require-await', rule, { ], }, { - code: 'async function foo() { await (async () => { doSomething() }) }', + code: ` + async function foo() { + await (async () => { + doSomething(); + }); + } + `, errors: [ { messageId: 'missingAwait', @@ -389,23 +526,5 @@ ruleTester.run('require-await', rule, { }, ], }, - { - code: 'async function* run() { yield * anotherAsyncGenerator() }', - errors: [ - { - messageId: 'missingAwait', - data: { name: "Async generator function 'run'" }, - }, - ], - }, - { - code: 'async function* run() { yield* 1 }', - errors: [ - { - messageId: 'missingAwait', - data: { name: "Async generator function 'run'" }, - }, - ], - }, ], }); From c1151faa2b4cbfea0a6663fabc4ed6e91e15ac08 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 20:03:24 -0700 Subject: [PATCH 18/92] chore: require-array-sort-compare --- .../rules/require-array-sort-compare.test.ts | 55 ++++++++++--------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts b/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts index d3a49c60f658..6362089d9c1f 100644 --- a/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts +++ b/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts @@ -15,75 +15,81 @@ ruleTester.run('require-array-sort-compare', rule, { valid: [ ` function f(a: any[]) { - a.sort(undefined) + a.sort(undefined); } `, ` function f(a: any[]) { - a.sort((a, b) => a - b) + a.sort((a, b) => a - b); } `, ` function f(a: Array) { - a.sort(undefined) + a.sort(undefined); } `, ` function f(a: Array) { - a.sort((a, b) => a - b) + a.sort((a, b) => a - b); } `, ` function f(a: { sort(): void }) { - a.sort() + a.sort(); } `, ` - class A { sort(): void {} } + class A { + sort(): void {} + } function f(a: A) { - a.sort() + a.sort(); } `, ` - interface A { sort(): void } + interface A { + sort(): void; + } function f(a: A) { - a.sort() + a.sort(); } `, ` - interface A { sort(): void } + interface A { + sort(): void; + } function f(a: T) { - a.sort() + a.sort(); } `, ` function f(a: any) { - a.sort() + a.sort(); } `, ` namespace UserDefined { interface Array { - sort(): void + sort(): void; } function f(a: Array) { - a.sort() + a.sort(); } } `, // optional chain ` function f(a: any[]) { - a?.sort((a, b) => a - b) + a?.sort((a, b) => a - b); } `, ` namespace UserDefined { interface Array { - sort(): void + sort(): void; } function f(a: Array) { - a?.sort() + a?.sort(); } } `, @@ -92,7 +98,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: Array) { - a.sort() + a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -100,7 +106,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: string[]) { - a.sort() + a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -108,8 +114,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: string | string[]) { - if (Array.isArray(a)) - a.sort() + if (Array.isArray(a)) a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -117,7 +122,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: number[] | string[]) { - a.sort() + a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -125,7 +130,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: T) { - a.sort() + a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -133,7 +138,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: U) { - a.sort() + a.sort(); } `, errors: [{ messageId: 'requireCompare' }], @@ -142,7 +147,7 @@ ruleTester.run('require-array-sort-compare', rule, { { code: ` function f(a: string[]) { - a?.sort() + a?.sort(); } `, errors: [{ messageId: 'requireCompare' }], From fc290c6b56658be668fdb0c9b7c55ca710262a34 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 21:25:22 -0700 Subject: [PATCH 19/92] chore: quotes --- .../eslint-plugin/tests/rules/quotes.test.ts | 123 +++++++++--------- 1 file changed, 64 insertions(+), 59 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/quotes.test.ts b/packages/eslint-plugin/tests/rules/quotes.test.ts index ad1487b8506f..f7b56a73ba3e 100644 --- a/packages/eslint-plugin/tests/rules/quotes.test.ts +++ b/packages/eslint-plugin/tests/rules/quotes.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests quotes, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/quotes'; import { RuleTester } from '../RuleTester'; @@ -31,7 +36,7 @@ const useBacktick: any = { ruleTester.run('quotes', rule, { valid: [ { - code: `declare module '*.html' {}`, + code: "declare module '*.html' {}", options: ['backtick'], }, { @@ -44,25 +49,25 @@ ruleTester.run('quotes', rule, { }, /** ESLint */ - `var foo = "bar";`, + 'var foo = "bar";', { - code: `var foo = 'bar';`, + code: "var foo = 'bar';", options: ['single'], }, { - code: `var foo = "bar";`, + code: 'var foo = "bar";', options: ['double'], }, { - code: `var foo = 1;`, + code: 'var foo = 1;', options: ['single'], }, { - code: `var foo = 1;`, + code: 'var foo = 1;', options: ['double'], }, { - code: `var foo = "'";`, + code: 'var foo = "\'";', options: [ 'single', { @@ -71,7 +76,7 @@ ruleTester.run('quotes', rule, { ], }, { - code: `var foo = '"';`, + code: "var foo = '\"';", options: [ 'double', { @@ -80,7 +85,7 @@ ruleTester.run('quotes', rule, { ], }, { - code: `var foo = <>Hello world;`, + code: 'var foo = <>Hello world;', options: ['single'], parserOptions: { ecmaFeatures: { @@ -89,7 +94,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo = <>Hello world;`, + code: 'var foo = <>Hello world;', options: ['double'], parserOptions: { ecmaFeatures: { @@ -98,7 +103,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo = <>Hello world;`, + code: 'var foo = <>Hello world;', options: [ 'double', { @@ -112,7 +117,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo = <>Hello world;`, + code: 'var foo = <>Hello world;', options: ['backtick'], parserOptions: { ecmaFeatures: { @@ -121,7 +126,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo =
Hello world
;`, + code: 'var foo =
Hello world
;', options: ['single'], parserOptions: { ecmaFeatures: { @@ -130,7 +135,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo =
;`, + code: 'var foo =
;', options: ['single'], parserOptions: { ecmaFeatures: { @@ -139,7 +144,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo =
Hello world
;`, + code: 'var foo =
Hello world
;', options: ['double'], parserOptions: { ecmaFeatures: { @@ -148,7 +153,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo =
Hello world
;`, + code: 'var foo =
Hello world
;', options: [ 'double', { @@ -174,7 +179,7 @@ ruleTester.run('quotes', rule, { options: ['backtick'], }, { - code: `var foo = 1;`, + code: 'var foo = 1;', options: ['backtick'], }, { @@ -187,7 +192,7 @@ ruleTester.run('quotes', rule, { ], }, { - code: `var foo =
;`, + code: 'var foo =
;', options: ['backtick'], parserOptions: { ecmaFeatures: { @@ -196,7 +201,7 @@ ruleTester.run('quotes', rule, { }, }, { - code: `var foo =
Hello world
;`, + code: 'var foo =
Hello world
;', options: ['backtick'], parserOptions: { ecmaFeatures: { @@ -299,33 +304,33 @@ ruleTester.run('quotes', rule, { // `backtick` should not warn import/export sources. { - code: `import "a"; import 'b';`, + code: 'import "a"; import \'b\';', options: ['backtick'], }, { - code: `import a from "a"; import b from 'b';`, + code: 'import a from "a"; import b from \'b\';', options: ['backtick'], }, { - code: `export * from "a"; export * from 'b';`, + code: 'export * from "a"; export * from \'b\';', options: ['backtick'], }, // `backtick` should not warn import with require. { - code: `import moment = require('moment');`, + code: "import moment = require('moment');", options: ['backtick'], }, // `backtick` should not warn property/method names (not computed). { - code: `var obj = {"key0": 0, 'key1': 1};`, + code: 'var obj = {"key0": 0, \'key1\': 1};', options: ['backtick'], }, { - code: `class Foo { 'bar'(){} }`, + code: "class Foo { 'bar'(){} }", options: ['backtick'], }, { - code: `class Foo { static ''(){} }`, + code: "class Foo { static ''(){} }", options: ['backtick'], }, @@ -505,30 +510,30 @@ abstract class Foo { invalid: [ { - code: `var foo = 'bar';`, - output: `var foo = "bar";`, + code: "var foo = 'bar';", + output: 'var foo = "bar";', errors: [useDoubleQuote], }, { - code: `var foo = "bar";`, - output: `var foo = 'bar';`, + code: 'var foo = "bar";', + output: "var foo = 'bar';", options: ['single'], errors: [useSingleQuote], }, { code: 'var foo = `bar`;', - output: `var foo = 'bar';`, + output: "var foo = 'bar';", options: ['single'], errors: [useSingleQuote], }, { - code: `var foo = 'don\\'t';`, - output: `var foo = "don't";`, + code: "var foo = 'don\\'t';", + output: 'var foo = "don\'t";', errors: [useDoubleQuote], }, { - code: `var msg = "Plugin '" + name + "' not found"`, - output: `var msg = 'Plugin \\'' + name + '\\' not found'`, + code: 'var msg = "Plugin \'" + name + "\' not found"', + output: "var msg = 'Plugin \\'' + name + '\\' not found'", options: ['single'], errors: [ { ...useSingleQuote, column: 11 }, @@ -536,20 +541,20 @@ abstract class Foo { ], }, { - code: `var foo = 'bar';`, - output: `var foo = "bar";`, + code: "var foo = 'bar';", + output: 'var foo = "bar";', options: ['double'], errors: [useDoubleQuote], }, { code: 'var foo = `bar`;', - output: `var foo = "bar";`, + output: 'var foo = "bar";', options: ['double'], errors: [useDoubleQuote], }, { - code: `var foo = "bar";`, - output: `var foo = 'bar';`, + code: 'var foo = "bar";', + output: "var foo = 'bar';", options: [ 'single', { @@ -559,8 +564,8 @@ abstract class Foo { errors: [useSingleQuote], }, { - code: `var foo = 'bar';`, - output: `var foo = "bar";`, + code: "var foo = 'bar';", + output: 'var foo = "bar";', options: [ 'double', { @@ -570,8 +575,8 @@ abstract class Foo { errors: [useDoubleQuote], }, { - code: `var foo = '\\\\';`, - output: `var foo = "\\\\\";`, // eslint-disable-line no-useless-escape + code: "var foo = '\\\\';", + output: 'var foo = "\\\\";', options: [ 'double', { @@ -581,8 +586,8 @@ abstract class Foo { errors: [useDoubleQuote], }, { - code: `var foo = "bar";`, - output: `var foo = 'bar';`, + code: 'var foo = "bar";', + output: "var foo = 'bar';", options: [ 'single', { @@ -592,8 +597,8 @@ abstract class Foo { errors: [useSingleQuote], }, { - code: `var foo = 'bar';`, - output: `var foo = "bar";`, + code: "var foo = 'bar';", + output: 'var foo = "bar";', options: [ 'double', { @@ -603,7 +608,7 @@ abstract class Foo { errors: [useDoubleQuote], }, { - code: `var foo = 'bar';`, + code: "var foo = 'bar';", output: 'var foo = `bar`;', options: ['backtick'], errors: [useBacktick], @@ -621,7 +626,7 @@ abstract class Foo { errors: [useBacktick], }, { - code: `var foo = "bar";`, + code: 'var foo = "bar";', output: 'var foo = `bar`;', options: [ 'backtick', @@ -632,7 +637,7 @@ abstract class Foo { errors: [useBacktick], }, { - code: `var foo = 'bar';`, + code: "var foo = 'bar';", output: 'var foo = `bar`;', options: [ 'backtick', @@ -665,14 +670,14 @@ abstract class Foo { // `backtick` should warn computed property names. { - code: `var obj = {["key0"]: 0, ['key1']: 1};`, + code: 'var obj = {["key0"]: 0, [\'key1\']: 1};', output: 'var obj = {[`key0`]: 0, [`key1`]: 1};', options: ['backtick'], parserOptions: { ecmaVersion: 6 }, errors: [useBacktick, useBacktick], }, { - code: `class Foo { ['a'](){} static ['b'](){} }`, + code: "class Foo { ['a'](){} static ['b'](){} }", output: 'class Foo { [`a`](){} static [`b`](){} }', options: ['backtick'], errors: [useBacktick, useBacktick], @@ -680,8 +685,8 @@ abstract class Foo { // https://github.com/eslint/eslint/issues/7084 { - code: `
`, - output: `
`, + code: '
', + output: "
", options: [`single`], parserOptions: { ecmaFeatures: { @@ -691,8 +696,8 @@ abstract class Foo { errors: [useSingleQuote], }, { - code: `
`, - output: `
`, + code: "
", + output: '
', options: ['double'], parserOptions: { ecmaFeatures: { @@ -702,7 +707,7 @@ abstract class Foo { errors: [useDoubleQuote], }, { - code: `
`, + code: "
", output: '
', options: ['backtick'], parserOptions: { @@ -736,7 +741,7 @@ abstract class Foo { }, { code: '() => { foo(); `use strict`; }', - output: `() => { foo(); "use strict"; }`, + output: '() => { foo(); "use strict"; }', errors: [useDoubleQuote], }, { From 33dd0616e6c10eb4bc08e8cb8aa1486bd06807a4 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 21:52:00 -0700 Subject: [PATCH 20/92] chore: promise-function-async --- .../rules/promise-function-async.test.ts | 132 ++++++++++++------ 1 file changed, 88 insertions(+), 44 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts index 66a65a21ffd3..d4da6e3b66c8 100644 --- a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts +++ b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts @@ -19,13 +19,19 @@ ruleTester.run('promise-function-async', rule, { const nonAsyncNonPromiseArrowFunction = (n: number) => n; `, ` -function nonAsyncNonPromiseFunctionDeclaration(n: number) { return n; } +function nonAsyncNonPromiseFunctionDeclaration(n: number) { + return n; +} `, ` -const asyncPromiseFunctionExpressionA = async function(p: Promise) { return p; }; +const asyncPromiseFunctionExpressionA = async function(p: Promise) { + return p; +}; `, ` -const asyncPromiseFunctionExpressionB = async function() { return new Promise(); }; +const asyncPromiseFunctionExpressionB = async function() { + return new Promise(); +}; `, ` class Test { @@ -75,13 +81,25 @@ const invalidAsyncModifiers = { }, set asyncGetterFunc(p: () => Promise) { return p; - } -} + }, +}; `, // https://github.com/typescript-eslint/typescript-eslint/issues/227 - `export function valid(n: number) { return n; }`, - `export default function invalid(n: number) { return n; }`, - `class Foo { constructor() { } }`, + ` + export function valid(n: number) { + return n; + } + `, + ` + export default function invalid(n: number) { + return n; + } + `, + ` + class Foo { + constructor() {} + } + `, { code: ` function returnsAny(): any { @@ -116,7 +134,7 @@ interface Options { type Return = ReadableStream | Promise; const foo = (options: Options): Return => { return options.stream ? asStream(options) : asPromise(options); -} +}; `, }, { @@ -164,8 +182,10 @@ function returnsUnknown(): unknown { }, { code: ` -const nonAsyncPromiseFunctionExpressionA = function(p: Promise) { return p; }; - `, +const nonAsyncPromiseFunctionExpressionA = function(p: Promise) { + return p; +}; + `, errors: [ { messageId, @@ -174,8 +194,10 @@ const nonAsyncPromiseFunctionExpressionA = function(p: Promise) { return p }, { code: ` -const nonAsyncPromiseFunctionExpressionB = function() { return new Promise(); }; - `, +const nonAsyncPromiseFunctionExpressionB = function() { + return new Promise(); +}; + `, errors: [ { messageId, @@ -184,8 +206,10 @@ const nonAsyncPromiseFunctionExpressionB = function() { return new Promise }, { code: ` -function nonAsyncPromiseFunctionDeclarationA(p: Promise) { return p; } - `, +function nonAsyncPromiseFunctionDeclarationA(p: Promise) { + return p; +} + `, errors: [ { messageId, @@ -194,8 +218,10 @@ function nonAsyncPromiseFunctionDeclarationA(p: Promise) { return p; } }, { code: ` -function nonAsyncPromiseFunctionDeclarationB() { return new Promise(); } - `, +function nonAsyncPromiseFunctionDeclarationB() { + return new Promise(); +} + `, errors: [ { messageId, @@ -205,7 +231,7 @@ function nonAsyncPromiseFunctionDeclarationB() { return new Promise(); } { code: ` const nonAsyncPromiseArrowFunctionA = (p: Promise) => p; - `, + `, errors: [ { messageId, @@ -215,7 +241,7 @@ const nonAsyncPromiseArrowFunctionA = (p: Promise) => p; { code: ` const nonAsyncPromiseArrowFunctionB = () => new Promise(); - `, + `, errors: [ { messageId, @@ -233,7 +259,7 @@ class Test { return new Promise(); } } - `, + `, errors: [ { line: 3, @@ -247,9 +273,13 @@ class Test { }, { code: ` -const nonAsyncPromiseFunctionExpression = function(p: Promise) { return p; }; +const nonAsyncPromiseFunctionExpression = function(p: Promise) { + return p; +}; -function nonAsyncPromiseFunctionDeclaration(p: Promise) { return p; } +function nonAsyncPromiseFunctionDeclaration(p: Promise) { + return p; +} const nonAsyncPromiseArrowFunction = (p: Promise) => p; @@ -258,7 +288,7 @@ class Test { return p; } } -`, + `, options: [ { checkArrowFunctions: false, @@ -270,20 +300,24 @@ class Test { messageId, }, { - line: 4, + line: 6, messageId, }, { - line: 9, + line: 13, messageId, }, ], }, { code: ` -const nonAsyncPromiseFunctionExpression = function(p: Promise) { return p; }; +const nonAsyncPromiseFunctionExpression = function(p: Promise) { + return p; +}; -function nonAsyncPromiseFunctionDeclaration(p: Promise) { return p; } +function nonAsyncPromiseFunctionDeclaration(p: Promise) { + return p; +} const nonAsyncPromiseArrowFunction = (p: Promise) => p; @@ -292,7 +326,7 @@ class Test { return p; } } -`, + `, options: [ { checkFunctionDeclarations: false, @@ -304,20 +338,24 @@ class Test { messageId, }, { - line: 6, + line: 10, messageId, }, { - line: 9, + line: 13, messageId, }, ], }, { code: ` -const nonAsyncPromiseFunctionExpression = function(p: Promise) { return p; }; +const nonAsyncPromiseFunctionExpression = function(p: Promise) { + return p; +}; -function nonAsyncPromiseFunctionDeclaration(p: Promise) { return p; } +function nonAsyncPromiseFunctionDeclaration(p: Promise) { + return p; +} const nonAsyncPromiseArrowFunction = (p: Promise) => p; @@ -326,7 +364,7 @@ class Test { return p; } } -`, + `, options: [ { checkFunctionExpressions: false, @@ -334,24 +372,28 @@ class Test { ], errors: [ { - line: 4, + line: 6, messageId, }, { - line: 6, + line: 10, messageId, }, { - line: 9, + line: 13, messageId, }, ], }, { code: ` -const nonAsyncPromiseFunctionExpression = function(p: Promise) { return p; }; +const nonAsyncPromiseFunctionExpression = function(p: Promise) { + return p; +}; -function nonAsyncPromiseFunctionDeclaration(p: Promise) { return p; } +function nonAsyncPromiseFunctionDeclaration(p: Promise) { + return p; +} const nonAsyncPromiseArrowFunction = (p: Promise) => p; @@ -360,7 +402,7 @@ class Test { return p; } } -`, + `, options: [ { checkMethodDeclarations: false, @@ -372,21 +414,21 @@ class Test { messageId, }, { - line: 4, + line: 6, messageId, }, { - line: 6, + line: 10, messageId, }, ], }, { code: ` -class PromiseType { } +class PromiseType {} const returnAllowedType = () => new PromiseType(); -`, + `, options: [ { allowedPromiseNames: ['PromiseType'], @@ -403,7 +445,9 @@ const returnAllowedType = () => new PromiseType(); code: ` interface SPromise extends Promise {} function foo(): Promise | SPromise { - return Math.random() > 0.5 ? Promise.resolve('value') : Promise.resolve(false); + return Math.random() > 0.5 + ? Promise.resolve('value') + : Promise.resolve(false); } `, options: [ From 8dd893c01de042fc3b00b8e5faf4991b94e400a8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 21:52:52 -0700 Subject: [PATCH 21/92] chore: prefer-regexp-exec --- .../tests/rules/prefer-regexp-exec.test.ts | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts index 045c36206ee7..fc97766f9f7f 100644 --- a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts @@ -13,30 +13,30 @@ const ruleTester = new RuleTester({ ruleTester.run('prefer-regexp-exec', rule, { valid: [ - '"something".match();', - '"something".match(/thing/g);', + "'something'.match();", + "'something'.match(/thing/g);", ` -const text = "something"; +const text = 'something'; const search = /thing/g; text.match(search); -`, + `, ` -const match = (s: RegExp) => "something"; +const match = (s: RegExp) => 'something'; match(/thing/); -`, + `, ` -const a = {match : (s: RegExp) => "something"}; +const a = { match: (s: RegExp) => 'something' }; a.match(/thing/); -`, + `, ` function f(s: string | string[]) { s.match(/e/); } -`, + `, ], invalid: [ { - code: '"something".match(/thing/);', + code: "'something'.match(/thing/);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -47,10 +47,10 @@ function f(s: string | string[]) { }, { code: ` -const text = "something"; +const text = 'something'; const search = /thing/; text.match(search); -`, + `, errors: [ { messageId: 'regExpExecOverStringMatch', @@ -60,7 +60,7 @@ text.match(search); ], }, { - code: '"212".match(2);', + code: "'212'.match(2);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -70,7 +70,7 @@ text.match(search); ], }, { - code: '"212".match(+2);', + code: "'212'.match(+2);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -80,7 +80,7 @@ text.match(search); ], }, { - code: '"oNaNo".match(NaN);', + code: "'oNaNo'.match(NaN);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -91,7 +91,7 @@ text.match(search); }, { code: - '"Infinity contains -Infinity and +Infinity in JavaScript.".match(Infinity);', + "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(Infinity);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -102,7 +102,7 @@ text.match(search); }, { code: - '"Infinity contains -Infinity and +Infinity in JavaScript.".match(+Infinity);', + "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(+Infinity);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -113,7 +113,7 @@ text.match(search); }, { code: - '"Infinity contains -Infinity and +Infinity in JavaScript.".match(-Infinity);', + "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(-Infinity);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -123,7 +123,7 @@ text.match(search); ], }, { - code: '"void and null".match(null);', + code: "'void and null'.match(null);", errors: [ { messageId: 'regExpExecOverStringMatch', @@ -137,7 +137,7 @@ text.match(search); function f(s: 'a' | 'b') { s.match('a'); } -`, + `, errors: [ { messageId: 'regExpExecOverStringMatch', @@ -148,11 +148,11 @@ function f(s: 'a' | 'b') { }, { code: ` -type SafeString = string & {__HTML_ESCAPED__: void} +type SafeString = string & { __HTML_ESCAPED__: void }; function f(s: SafeString) { s.match(/thing/); } -`, + `, errors: [ { messageId: 'regExpExecOverStringMatch', @@ -163,10 +163,10 @@ function f(s: SafeString) { }, { code: ` -function f(s: T) { +function f(s: T) { s.match(/thing/); } - `, + `, errors: [ { messageId: 'regExpExecOverStringMatch', From d9b0e8458ad20fd9ccd1fa378d38763b7b589c30 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 21:59:42 -0700 Subject: [PATCH 22/92] chore: prefer-readonly --- .../tests/rules/prefer-readonly.test.ts | 734 ++++++++++-------- 1 file changed, 420 insertions(+), 314 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts b/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts index 256e38360d9e..43ecdb03fe81 100644 --- a/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-readonly.test.ts @@ -13,217 +13,275 @@ const ruleTester = new RuleTester({ ruleTester.run('prefer-readonly', rule, { valid: [ - 'function ignore() { }', - 'const ignore = function () { }', - 'const ignore = () => { }', - `const container = { member: true }; - container.member;`, - `const container = { member: 1 }; - +container.member;`, - `const container = { member: 1 }; - ++container.member;`, - `const container = { member: 1 }; - container.member++;`, - `const container = { member: 1 }; - -container.member;`, - `const container = { member: 1 }; - --container.member;`, - `const container = { member: 1 }; - container.member--;`, - 'class TestEmpty { }', - `class TestReadonlyStatic { - private static readonly correctlyReadonlyStatic = 7; - }`, - `class TestModifiableStatic { - private static correctlyModifiableStatic = 7; - - public constructor() { - TestModifiableStatic.correctlyModifiableStatic += 1; + 'function ignore() {}', + 'const ignore = function() {};', + 'const ignore = () => {};', + ` + const container = { member: true }; + container.member; + `, + ` + const container = { member: 1 }; + +container.member; + `, + ` + const container = { member: 1 }; + ++container.member; + `, + ` + const container = { member: 1 }; + container.member++; + `, + ` + const container = { member: 1 }; + -container.member; + `, + ` + const container = { member: 1 }; + --container.member; + `, + ` + const container = { member: 1 }; + container.member--; + `, + 'class TestEmpty {}', + ` + class TestReadonlyStatic { + private static readonly correctlyReadonlyStatic = 7; } - }`, - `class TestModifiableByParameterProperty { - private static readonly correctlyModifiableByParameterProperty = 7; - - public constructor( - public correctlyModifiablePublicParameter: number = (() => { - return TestModifiableStatic.correctlyModifiableByParameterProperty += 1; - })() - ) { } - }`, - `class TestReadonlyInline { - private readonly correctlyReadonlyInline = 7; - }`, - `class TestReadonlyDelayed { - private readonly correctlyReadonlyDelayed = 7; - - public constructor() { - this.correctlyReadonlyDelayed += 1; + `, + ` + class TestModifiableStatic { + private static correctlyModifiableStatic = 7; + + public constructor() { + TestModifiableStatic.correctlyModifiableStatic += 1; + } } - }`, - `class TestModifiableInline { - private correctlyModifiableInline = 7; + `, + ` + class TestModifiableByParameterProperty { + private static readonly correctlyModifiableByParameterProperty = 7; - public mutate() { - this.correctlyModifiableInline += 1; + public constructor( + public correctlyModifiablePublicParameter: number = (() => { + return (TestModifiableStatic.correctlyModifiableByParameterProperty += 1); + })(), + ) {} + } + `, + ` + class TestReadonlyInline { + private readonly correctlyReadonlyInline = 7; + } + `, + ` + class TestReadonlyDelayed { + private readonly correctlyReadonlyDelayed = 7; - return class { - private correctlyModifiableInline = 7; + public constructor() { + this.correctlyReadonlyDelayed += 1; + } + } + `, + ` + class TestModifiableInline { + private correctlyModifiableInline = 7; - mutate() { - this.correctlyModifiableInline += 1; - } - }; + public mutate() { + this.correctlyModifiableInline += 1; + + return class { + private correctlyModifiableInline = 7; + + mutate() { + this.correctlyModifiableInline += 1; + } + }; + } } - }`, - `class TestModifiableDelayed { - private correctlyModifiableDelayed = 7; + `, + ` + class TestModifiableDelayed { + private correctlyModifiableDelayed = 7; - public mutate() { - this.correctlyModifiableDelayed += 1; + public mutate() { + this.correctlyModifiableDelayed += 1; + } } - }`, - `class TestModifiableDeleted { - private correctlyModifiableDeleted = 7; + `, + ` + class TestModifiableDeleted { + private correctlyModifiableDeleted = 7; - public mutate() { - delete this.correctlyModifiableDeleted; + public mutate() { + delete this.correctlyModifiableDeleted; + } } - }`, - `class TestModifiableWithinConstructor { - private correctlyModifiableWithinConstructor = 7; - - public constructor() { - (() => { - this.correctlyModifiableWithinConstructor += 1; - })(); + `, + ` + class TestModifiableWithinConstructor { + private correctlyModifiableWithinConstructor = 7; + + public constructor() { + (() => { + this.correctlyModifiableWithinConstructor += 1; + })(); + } } - }`, - `class TestModifiableWithinConstructorArrowFunction { - private correctlyModifiableWithinConstructorArrowFunction = 7; - - public constructor() { - (() => { - this.correctlyModifiableWithinConstructorArrowFunction += 1; - })(); + `, + ` + class TestModifiableWithinConstructorArrowFunction { + private correctlyModifiableWithinConstructorArrowFunction = 7; + + public constructor() { + (() => { + this.correctlyModifiableWithinConstructorArrowFunction += 1; + })(); + } } - }`, - `class TestModifiableWithinConstructorInFunctionExpression { - private correctlyModifiableWithinConstructorInFunctionExpression = 7; + `, + ` + class TestModifiableWithinConstructorInFunctionExpression { + private correctlyModifiableWithinConstructorInFunctionExpression = 7; - public constructor() { - const self = this; + public constructor() { + const self = this; - (() => { - self.correctlyModifiableWithinConstructorInFunctionExpression += 1; - })(); + (() => { + self.correctlyModifiableWithinConstructorInFunctionExpression += 1; + })(); + } } - }`, - `class TestModifiableWithinConstructorInGetAccessor { - private correctlyModifiableWithinConstructorInGetAccessor = 7; + `, + ` + class TestModifiableWithinConstructorInGetAccessor { + private correctlyModifiableWithinConstructorInGetAccessor = 7; - public constructor() { - const self = this; + public constructor() { + const self = this; - const confusingObject = { - get accessor() { - return self.correctlyModifiableWithinConstructorInGetAccessor += 1; - }, - }; + const confusingObject = { + get accessor() { + return (self.correctlyModifiableWithinConstructorInGetAccessor += 1); + }, + }; + } } - }`, - `class TestModifiableWithinConstructorInMethodDeclaration { - private correctlyModifiableWithinConstructorInMethodDeclaration = 7; + `, + ` + class TestModifiableWithinConstructorInMethodDeclaration { + private correctlyModifiableWithinConstructorInMethodDeclaration = 7; - public constructor() { - const self = this; + public constructor() { + const self = this; - const confusingObject = { - methodDeclaration() { - self.correctlyModifiableWithinConstructorInMethodDeclaration = 7; - } - }; + const confusingObject = { + methodDeclaration() { + self.correctlyModifiableWithinConstructorInMethodDeclaration = 7; + }, + }; + } } - }`, - `class TestModifiableWithinConstructorInSetAccessor { - private correctlyModifiableWithinConstructorInSetAccessor = 7; + `, + ` + class TestModifiableWithinConstructorInSetAccessor { + private correctlyModifiableWithinConstructorInSetAccessor = 7; - public constructor() { - const self = this; + public constructor() { + const self = this; - const confusingObject = { - set accessor(value: number) { - self.correctlyModifiableWithinConstructorInSetAccessor += value; - }, - }; + const confusingObject = { + set accessor(value: number) { + self.correctlyModifiableWithinConstructorInSetAccessor += value; + }, + }; + } } - }`, - `class TestModifiablePostDecremented { - private correctlyModifiablePostDecremented = 7; + `, + ` + class TestModifiablePostDecremented { + private correctlyModifiablePostDecremented = 7; - public mutate() { - this.correctlyModifiablePostDecremented -= 1; + public mutate() { + this.correctlyModifiablePostDecremented -= 1; + } } - }`, - `class TestyModifiablePostIncremented { - private correctlyModifiablePostIncremented = 7; + `, + ` + class TestyModifiablePostIncremented { + private correctlyModifiablePostIncremented = 7; - public mutate() { - this.correctlyModifiablePostIncremented += 1; + public mutate() { + this.correctlyModifiablePostIncremented += 1; + } } - }`, - `class TestModifiablePreDecremented { - private correctlyModifiablePreDecremented = 7; + `, + ` + class TestModifiablePreDecremented { + private correctlyModifiablePreDecremented = 7; - public mutate() { - --this.correctlyModifiablePreDecremented; + public mutate() { + --this.correctlyModifiablePreDecremented; + } } - }`, - `class TestModifiablePreIncremented { - private correctlyModifiablePreIncremented = 7; + `, + ` + class TestModifiablePreIncremented { + private correctlyModifiablePreIncremented = 7; - public mutate() { - ++this.correctlyModifiablePreIncremented; + public mutate() { + ++this.correctlyModifiablePreIncremented; + } + } + `, + ` + class TestProtectedModifiable { + protected protectedModifiable = 7; + } + `, + ` + class TestPublicModifiable { + public publicModifiable = 7; + } + `, + ` + class TestReadonlyParameter { + public constructor(private readonly correctlyReadonlyParameter = 7) {} } - }`, - `class TestProtectedModifiable { - protected protectedModifiable = 7; - }`, - `class TestPublicModifiable { - public publicModifiable = 7; - }`, - `class TestReadonlyParameter { - public constructor( - private readonly correctlyReadonlyParameter = 7, - ) { } - }`, - `class TestCorrectlyModifiableParameter { - public constructor( - private correctlyModifiableParameter = 7, - ) { } - - public mutate() { - this.correctlyModifiableParameter += 1; + `, + ` + class TestCorrectlyModifiableParameter { + public constructor(private correctlyModifiableParameter = 7) {} + + public mutate() { + this.correctlyModifiableParameter += 1; + } } - }`, + `, { - code: `class TestCorrectlyNonInlineLambdas { - private correctlyNonInlineLambda = 7; - }`, + code: ` + class TestCorrectlyNonInlineLambdas { + private correctlyNonInlineLambda = 7; + } + `, options: [ { onlyInlineLambdas: true, }, ], }, - `class TestComputedParameter { - public mutate() { - this['computed'] = 1; + ` + class TestComputedParameter { + public mutate() { + this['computed'] = 1; + } } - }`, + `, { code: ` class Foo { - private value: number = 0 + private value: number = 0; bar(newValue: { value: number }) { ({ value: this.value } = newValue); @@ -247,7 +305,7 @@ class Foo { { code: ` class Foo { - private value: number[] = [] + private value: number[] = []; bar(newValue: number[]) { [...this.value] = newValue; @@ -271,9 +329,11 @@ class Foo { ], invalid: [ { - code: `class TestIncorrectlyModifiableStatic { - private static incorrectlyModifiableStatic = 7; - }`, + code: ` + class TestIncorrectlyModifiableStatic { + private static incorrectlyModifiableStatic = 7; + } + `, errors: [ { data: { @@ -282,14 +342,18 @@ class Foo { messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableStatic { - private static readonly incorrectlyModifiableStatic = 7; - }`, + output: ` + class TestIncorrectlyModifiableStatic { + private static readonly incorrectlyModifiableStatic = 7; + } + `, }, { - code: `class TestIncorrectlyModifiableStaticArrow { - private static incorrectlyModifiableStaticArrow = () => 7; - }`, + code: ` + class TestIncorrectlyModifiableStaticArrow { + private static incorrectlyModifiableStaticArrow = () => 7; + } + `, errors: [ { data: { @@ -298,54 +362,62 @@ class Foo { messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableStaticArrow { - private static readonly incorrectlyModifiableStaticArrow = () => 7; - }`, + output: ` + class TestIncorrectlyModifiableStaticArrow { + private static readonly incorrectlyModifiableStaticArrow = () => 7; + } + `, }, { - code: `class TestIncorrectlyModifiableInline { - private incorrectlyModifiableInline = 7; + code: ` + class TestIncorrectlyModifiableInline { + private incorrectlyModifiableInline = 7; - public createConfusingChildClass() { - return class { + public createConfusingChildClass() { + return class { private incorrectlyModifiableInline = 7; + }; } } - }`, + `, errors: [ { data: { name: 'incorrectlyModifiableInline', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, { data: { name: 'incorrectlyModifiableInline', }, - line: 6, + line: 7, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableInline { - private readonly incorrectlyModifiableInline = 7; + output: ` + class TestIncorrectlyModifiableInline { + private readonly incorrectlyModifiableInline = 7; - public createConfusingChildClass() { - return class { + public createConfusingChildClass() { + return class { private readonly incorrectlyModifiableInline = 7; + }; } } - }`, + `, }, { - code: `class TestIncorrectlyModifiableDelayed { - private incorrectlyModifiableDelayed = 7; + code: ` + class TestIncorrectlyModifiableDelayed { + private incorrectlyModifiableDelayed = 7; - public constructor() { - this.incorrectlyModifiableDelayed = 7; + public constructor() { + this.incorrectlyModifiableDelayed = 7; + } } - }`, + `, errors: [ { data: { @@ -354,190 +426,216 @@ class Foo { messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableDelayed { - private readonly incorrectlyModifiableDelayed = 7; + output: ` + class TestIncorrectlyModifiableDelayed { + private readonly incorrectlyModifiableDelayed = 7; - public constructor() { - this.incorrectlyModifiableDelayed = 7; + public constructor() { + this.incorrectlyModifiableDelayed = 7; + } } - }`, + `, }, { - code: `class TestChildClassExpressionModifiable { - private childClassExpressionModifiable = 7; + code: ` + class TestChildClassExpressionModifiable { + private childClassExpressionModifiable = 7; - public createConfusingChildClass() { - return class { - private childClassExpressionModifiable = 7; + public createConfusingChildClass() { + return class { + private childClassExpressionModifiable = 7; - mutate() { - this.childClassExpressionModifiable += 1; - } + mutate() { + this.childClassExpressionModifiable += 1; + } + }; } } - }`, + `, errors: [ { data: { name: 'childClassExpressionModifiable', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestChildClassExpressionModifiable { - private readonly childClassExpressionModifiable = 7; - - public createConfusingChildClass() { - return class { - private childClassExpressionModifiable = 7; - - mutate() { - this.childClassExpressionModifiable += 1; - } + output: ` + class TestChildClassExpressionModifiable { + private readonly childClassExpressionModifiable = 7; + + public createConfusingChildClass() { + return class { + private childClassExpressionModifiable = 7; + + mutate() { + this.childClassExpressionModifiable += 1; + } + }; } } - }`, + `, }, { - code: `class TestIncorrectlyModifiablePostMinus { - private incorrectlyModifiablePostMinus = 7; + code: ` + class TestIncorrectlyModifiablePostMinus { + private incorrectlyModifiablePostMinus = 7; - public mutate() { - this.incorrectlyModifiablePostMinus - 1; + public mutate() { + this.incorrectlyModifiablePostMinus - 1; + } } - }`, + `, errors: [ { data: { name: 'incorrectlyModifiablePostMinus', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiablePostMinus { - private readonly incorrectlyModifiablePostMinus = 7; + output: ` + class TestIncorrectlyModifiablePostMinus { + private readonly incorrectlyModifiablePostMinus = 7; - public mutate() { - this.incorrectlyModifiablePostMinus - 1; + public mutate() { + this.incorrectlyModifiablePostMinus - 1; + } } - }`, + `, }, { - code: `class TestIncorrectlyModifiablePostPlus { - private incorrectlyModifiablePostPlus = 7; + code: ` + class TestIncorrectlyModifiablePostPlus { + private incorrectlyModifiablePostPlus = 7; - public mutate() { - this.incorrectlyModifiablePostPlus + 1; + public mutate() { + this.incorrectlyModifiablePostPlus + 1; + } } - }`, + `, errors: [ { data: { name: 'incorrectlyModifiablePostPlus', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiablePostPlus { - private readonly incorrectlyModifiablePostPlus = 7; + output: ` + class TestIncorrectlyModifiablePostPlus { + private readonly incorrectlyModifiablePostPlus = 7; - public mutate() { - this.incorrectlyModifiablePostPlus + 1; + public mutate() { + this.incorrectlyModifiablePostPlus + 1; + } } - }`, + `, }, { - code: `class TestIncorrectlyModifiablePreMinus { - private incorrectlyModifiablePreMinus = 7; + code: ` + class TestIncorrectlyModifiablePreMinus { + private incorrectlyModifiablePreMinus = 7; - public mutate() { - -this.incorrectlyModifiablePreMinus; + public mutate() { + -this.incorrectlyModifiablePreMinus; + } } - }`, + `, errors: [ { data: { name: 'incorrectlyModifiablePreMinus', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiablePreMinus { - private readonly incorrectlyModifiablePreMinus = 7; + output: ` + class TestIncorrectlyModifiablePreMinus { + private readonly incorrectlyModifiablePreMinus = 7; - public mutate() { - -this.incorrectlyModifiablePreMinus; + public mutate() { + -this.incorrectlyModifiablePreMinus; + } } - }`, + `, }, { - code: `class TestIncorrectlyModifiablePrePlus { - private incorrectlyModifiablePrePlus = 7; + code: ` + class TestIncorrectlyModifiablePrePlus { + private incorrectlyModifiablePrePlus = 7; - public mutate() { - +this.incorrectlyModifiablePrePlus; + public mutate() { + +this.incorrectlyModifiablePrePlus; + } } - }`, + `, errors: [ { data: { name: 'incorrectlyModifiablePrePlus', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiablePrePlus { - private readonly incorrectlyModifiablePrePlus = 7; + output: ` + class TestIncorrectlyModifiablePrePlus { + private readonly incorrectlyModifiablePrePlus = 7; - public mutate() { - +this.incorrectlyModifiablePrePlus; + public mutate() { + +this.incorrectlyModifiablePrePlus; + } } - }`, + `, }, { - code: `class TestOverlappingClassVariable { - private overlappingClassVariable = 7; + code: ` + class TestOverlappingClassVariable { + private overlappingClassVariable = 7; - public workWithSimilarClass(other: SimilarClass) { - other.overlappingClassVariable = 7; + public workWithSimilarClass(other: SimilarClass) { + other.overlappingClassVariable = 7; + } } - } - class SimilarClass { - public overlappingClassVariable = 7; - }`, + class SimilarClass { + public overlappingClassVariable = 7; + } + `, errors: [ { data: { name: 'overlappingClassVariable', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], - output: `class TestOverlappingClassVariable { - private readonly overlappingClassVariable = 7; + output: ` + class TestOverlappingClassVariable { + private readonly overlappingClassVariable = 7; - public workWithSimilarClass(other: SimilarClass) { - other.overlappingClassVariable = 7; + public workWithSimilarClass(other: SimilarClass) { + other.overlappingClassVariable = 7; + } } - } - class SimilarClass { - public overlappingClassVariable = 7; - }`, + class SimilarClass { + public overlappingClassVariable = 7; + } + `, }, { - code: `class TestIncorrectlyModifiableParameter { - public constructor( - private incorrectlyModifiableParameter = 7, - ) { } - }`, + code: ` + class TestIncorrectlyModifiableParameter { + public constructor(private incorrectlyModifiableParameter = 7) {} + } + `, errors: [ { data: { @@ -547,45 +645,51 @@ class Foo { messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableParameter { - public constructor( - private readonly incorrectlyModifiableParameter = 7, - ) { } - }`, + output: ` + class TestIncorrectlyModifiableParameter { + public constructor(private readonly incorrectlyModifiableParameter = 7) {} + } + `, }, { - code: `class TestIncorrectlyModifiableParameter { - public constructor( - public ignore: boolean, - private incorrectlyModifiableParameter = 7, - ) { } - }`, + code: ` + class TestIncorrectlyModifiableParameter { + public constructor( + public ignore: boolean, + private incorrectlyModifiableParameter = 7, + ) {} + } + `, errors: [ { data: { name: 'incorrectlyModifiableParameter', }, - line: 4, + line: 5, messageId: 'preferReadonly', }, ], - output: `class TestIncorrectlyModifiableParameter { - public constructor( - public ignore: boolean, - private readonly incorrectlyModifiableParameter = 7, - ) { } - }`, + output: ` + class TestIncorrectlyModifiableParameter { + public constructor( + public ignore: boolean, + private readonly incorrectlyModifiableParameter = 7, + ) {} + } + `, }, { - code: `class TestCorrectlyNonInlineLambdas { - private incorrectlyInlineLambda = () => 7; - }`, + code: ` + class TestCorrectlyNonInlineLambdas { + private incorrectlyInlineLambda = () => 7; + } + `, errors: [ { data: { name: 'incorrectlyInlineLambda', }, - line: 2, + line: 3, messageId: 'preferReadonly', }, ], @@ -594,9 +698,11 @@ class Foo { onlyInlineLambdas: true, }, ], - output: `class TestCorrectlyNonInlineLambdas { - private readonly incorrectlyInlineLambda = () => 7; - }`, + output: ` + class TestCorrectlyNonInlineLambdas { + private readonly incorrectlyInlineLambda = () => 7; + } + `, }, ], }); From b9ee7d704a50a92c2a4062082ec925c3220f1ff8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:09:43 -0700 Subject: [PATCH 23/92] chore: prefer-readonly-parameter-types --- .../prefer-readonly-parameter-types.test.ts | 129 ++++++++++-------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-readonly-parameter-types.test.ts b/packages/eslint-plugin/tests/rules/prefer-readonly-parameter-types.test.ts index 32caf683c77b..3ca6c0201fc1 100644 --- a/packages/eslint-plugin/tests/rules/prefer-readonly-parameter-types.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-readonly-parameter-types.test.ts @@ -72,7 +72,10 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { function foo(arg: typeof symb) {} `, ` - enum Enum { a, b } + enum Enum { + a, + b, + } function foo(arg: Enum) {} `, @@ -97,19 +100,15 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { ` function foo(arg: { readonly foo: { - readonly bar: string - } + readonly bar: string; + }; }) {} `, ` - function foo(arg: { - readonly [k: string]: string, - }) {} + function foo(arg: { readonly [k: string]: string }) {} `, ` - function foo(arg: { - readonly [k: number]: string, - }) {} + function foo(arg: { readonly [k: number]: string }) {} `, ` interface Empty {} @@ -120,13 +119,13 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { ...weirdIntersections.map(code => code), ` interface Test extends ReadonlyArray { - readonly property: boolean + readonly property: boolean; } function foo(arg: Readonly) {} `, ` - type Test = (readonly string[]) & { - readonly property: boolean + type Test = readonly string[] & { + readonly property: boolean; }; function foo(arg: Readonly) {} `, @@ -193,12 +192,24 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { }, // type functions - 'interface Foo { (arg: readonly string[]): void }', // TSCallSignatureDeclaration - 'interface Foo { new (arg: readonly string[]): void }', // TSConstructSignatureDeclaration - 'const x = { foo(arg: readonly string[]): void }', // TSEmptyBodyFunctionExpression + ` + interface Foo { + (arg: readonly string[]): void; + } + `, // TSCallSignatureDeclaration + ` + interface Foo { + new (arg: readonly string[]): void; + } + `, // TSConstructSignatureDeclaration + 'const x = { foo(arg: readonly string[]): void; };', // TSEmptyBodyFunctionExpression 'function foo(arg: readonly string[]);', // TSDeclareFunction 'type Foo = (arg: readonly string[]) => void;', // TSFunctionType - 'interface Foo { foo(arg: readonly string[]): void }', // TSMethodSignature + ` + interface Foo { + foo(arg: readonly string[]): void; + } + `, // TSMethodSignature // https://github.com/typescript-eslint/typescript-eslint/issues/1665 // directly recursive @@ -248,12 +259,12 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { }), // nested arrays { - code: 'function foo(arg: readonly (string[])[]) {}', + code: 'function foo(arg: readonly string[][]) {}', errors: [ { messageId: 'shouldBeReadonly', column: 14, - endColumn: 40, + endColumn: 38, }, ], }, @@ -295,8 +306,8 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { code: ` function foo(arg: { readonly foo: { - bar: string - } + bar: string; + }; }) {} `, errors: [ @@ -312,33 +323,29 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { // object index signatures { code: ` - function foo(arg: { - [key: string]: string - }) {} + function foo(arg: { [key: string]: string }) {} `, errors: [ { messageId: 'shouldBeReadonly', line: 2, column: 22, - endLine: 4, - endColumn: 10, + endLine: 2, + endColumn: 52, }, ], }, { code: ` - function foo(arg: { - [key: number]: string - }) {} + function foo(arg: { [key: number]: string }) {} `, errors: [ { messageId: 'shouldBeReadonly', line: 2, column: 22, - endLine: 4, - endColumn: 10, + endLine: 2, + endColumn: 52, }, ], }, @@ -356,7 +363,7 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { { code: ` interface Test extends Array { - readonly property: boolean + readonly property: boolean; } function foo(arg: Test) {} `, @@ -373,7 +380,7 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { { code: ` interface Test extends Array { - property: boolean + property: boolean; } function foo(arg: Test) {} `, @@ -393,10 +400,10 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { code: ` class Foo { constructor( - private arg1: string[], - public arg2: string[], + private arg1: string[], + public arg2: string[], protected arg3: string[], - readonly arg4: string[], + readonly arg4: string[], ) {} } `, @@ -409,16 +416,16 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { { messageId: 'shouldBeReadonly', line: 4, - column: 23, + column: 21, endLine: 4, - endColumn: 37, + endColumn: 35, }, { messageId: 'shouldBeReadonly', line: 5, - column: 23, + column: 20, endLine: 5, - endColumn: 37, + endColumn: 34, }, { messageId: 'shouldBeReadonly', @@ -430,9 +437,9 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { { messageId: 'shouldBeReadonly', line: 7, - column: 23, + column: 22, endLine: 7, - endColumn: 37, + endColumn: 36, }, ], }, @@ -440,10 +447,10 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { code: ` class Foo { constructor( - private arg1: readonly string[], - public arg2: readonly string[], + private arg1: readonly string[], + public arg2: readonly string[], protected arg3: readonly string[], - readonly arg4: readonly string[], + readonly arg4: readonly string[], arg5: string[], ) {} } @@ -467,29 +474,37 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { // type functions { // TSCallSignatureDeclaration - code: 'interface Foo { (arg: string[]): void }', + code: ` + interface Foo { + (arg: string[]): void; + } + `, errors: [ { messageId: 'shouldBeReadonly', - column: 18, - endColumn: 31, + column: 12, + endColumn: 25, }, ], }, { // TSConstructSignatureDeclaration - code: 'interface Foo { new (arg: string[]): void }', + code: ` + interface Foo { + new (arg: string[]): void; + } + `, errors: [ { messageId: 'shouldBeReadonly', - column: 22, - endColumn: 35, + column: 16, + endColumn: 29, }, ], }, { // TSEmptyBodyFunctionExpression - code: 'const x = { foo(arg: string[]): void }', + code: 'const x = { foo(arg: string[]): void; };', errors: [ { messageId: 'shouldBeReadonly', @@ -511,7 +526,7 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { }, { // TSFunctionType - code: 'type Foo = (arg: string[]) => void', + code: 'type Foo = (arg: string[]) => void;', errors: [ { messageId: 'shouldBeReadonly', @@ -522,12 +537,16 @@ ruleTester.run('prefer-readonly-parameter-types', rule, { }, { // TSMethodSignature - code: 'interface Foo { foo(arg: string[]): void }', + code: ` + interface Foo { + foo(arg: string[]): void; + } + `, errors: [ { messageId: 'shouldBeReadonly', - column: 21, - endColumn: 34, + column: 15, + endColumn: 28, }, ], }, From 19a7d5bae5b4454da788a279f4200e18721719a8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:13:34 -0700 Subject: [PATCH 24/92] chore: prefer-optional-chain --- .../tests/rules/prefer-optional-chain.test.ts | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts b/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts index ccd3e992cf01..eed61d687e42 100644 --- a/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/prefer-optional-chain'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; import { TSESLint } from '@typescript-eslint/experimental-utils'; import { InferMessageIdsTypeFromRule, @@ -116,23 +116,23 @@ const baseCases = [ ruleTester.run('prefer-optional-chain', rule, { valid: [ - 'foo && bar', - 'foo && foo', - 'foo || bar', - 'foo ?? bar', - 'foo || foo.bar', - 'foo ?? foo.bar', - "file !== 'index.ts' && file.endsWith('.ts')", - 'nextToken && sourceCode.isSpaceBetweenTokens(prevToken, nextToken)', - 'result && this.options.shouldPreserveNodeMaps', - 'foo && fooBar.baz', - 'match && match$1 !== undefined', - 'foo !== null && foo !== undefined', - 'x["y"] !== undefined && x["y"] !== null', + 'foo && bar;', + 'foo && foo;', + 'foo || bar;', + 'foo ?? bar;', + 'foo || foo.bar;', + 'foo ?? foo.bar;', + "file !== 'index.ts' && file.endsWith('.ts');", + 'nextToken && sourceCode.isSpaceBetweenTokens(prevToken, nextToken);', + 'result && this.options.shouldPreserveNodeMaps;', + 'foo && fooBar.baz;', + 'match && match$1 !== undefined;', + 'foo !== null && foo !== undefined;', + "x['y'] !== undefined && x['y'] !== null;", // currently do not handle complex computed properties - 'foo && foo[bar as string] && foo[bar as string].baz', - 'foo && foo[1 + 2] && foo[1 + 2].baz', - 'foo && foo[typeof bar] && foo[typeof bar].baz', + 'foo && foo[bar as string] && foo[bar as string].baz;', + 'foo && foo[1 + 2] && foo[1 + 2].baz;', + 'foo && foo[typeof bar] && foo[typeof bar].baz;', ], invalid: [ ...baseCases, @@ -176,8 +176,8 @@ ruleTester.run('prefer-optional-chain', rule, { { // case with inconsistent checks code: - 'foo && foo.bar != null && foo.bar.baz !== undefined && foo.bar.baz.buzz', - output: 'foo?.bar?.baz?.buzz', + 'foo && foo.bar != null && foo.bar.baz !== undefined && foo.bar.baz.buzz;', + output: 'foo?.bar?.baz?.buzz;', errors: [ { messageId: 'preferOptionalChain', @@ -186,8 +186,8 @@ ruleTester.run('prefer-optional-chain', rule, { }, // ensure essential whitespace isn't removed { - code: 'foo && foo.bar(baz => ());', - output: 'foo?.bar(baz => ());', + code: 'foo && foo.bar(baz => );', + output: 'foo?.bar(baz => );', errors: [ { messageId: 'preferOptionalChain', @@ -209,8 +209,8 @@ ruleTester.run('prefer-optional-chain', rule, { ], }, { - code: 'foo && foo["some long string"] && foo["some long string"].baz', - output: 'foo?.["some long string"]?.baz', + code: noFormat`foo && foo["some long string"] && foo["some long string"].baz`, + output: noFormat`foo?.["some long string"]?.baz`, errors: [ { messageId: 'preferOptionalChain', @@ -218,8 +218,8 @@ ruleTester.run('prefer-optional-chain', rule, { ], }, { - code: 'foo && foo[`some long string`] && foo[`some long string`].baz', - output: 'foo?.[`some long string`]?.baz', + code: noFormat`foo && foo[\`some long string\`] && foo[\`some long string\`].baz`, + output: noFormat`foo?.[\`some long string\`]?.baz`, errors: [ { messageId: 'preferOptionalChain', @@ -227,8 +227,8 @@ ruleTester.run('prefer-optional-chain', rule, { ], }, { - code: "foo && foo['some long string'] && foo['some long string'].baz", - output: "foo?.['some long string']?.baz", + code: "foo && foo['some long string'] && foo['some long string'].baz;", + output: "foo?.['some long string']?.baz;", errors: [ { messageId: 'preferOptionalChain', @@ -237,12 +237,12 @@ ruleTester.run('prefer-optional-chain', rule, { }, // should preserve comments in a call expression { - code: ` + code: noFormat` foo && foo.bar(/* comment */a, // comment2 b, ); `, - output: ` + output: noFormat` foo?.bar(/* comment */a, // comment2 b, ); @@ -255,8 +255,8 @@ ruleTester.run('prefer-optional-chain', rule, { }, // ensure binary expressions that are the last expression do not get removed { - code: 'foo && foo.bar != null', - output: 'foo?.bar != null', + code: 'foo && foo.bar != null;', + output: 'foo?.bar != null;', errors: [ { messageId: 'preferOptionalChain', @@ -264,8 +264,8 @@ ruleTester.run('prefer-optional-chain', rule, { ], }, { - code: 'foo && foo.bar != undefined', - output: 'foo?.bar != undefined', + code: 'foo && foo.bar != undefined;', + output: 'foo?.bar != undefined;', errors: [ { messageId: 'preferOptionalChain', @@ -273,8 +273,8 @@ ruleTester.run('prefer-optional-chain', rule, { ], }, { - code: 'foo && foo.bar != null && baz', - output: 'foo?.bar != null && baz', + code: 'foo && foo.bar != null && baz;', + output: 'foo?.bar != null && baz;', errors: [ { messageId: 'preferOptionalChain', @@ -283,8 +283,8 @@ ruleTester.run('prefer-optional-chain', rule, { }, // other weird cases { - code: 'foo && foo?.()', - output: 'foo?.()', + code: 'foo && foo?.();', + output: 'foo?.();', errors: [ { messageId: 'preferOptionalChain', From b46c65d87d91dd3ab6b68184e28e09c60a5f5004 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:18:53 -0700 Subject: [PATCH 25/92] chore: prefer-namespace-keyword --- .../rules/prefer-namespace-keyword.test.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-namespace-keyword.test.ts b/packages/eslint-plugin/tests/rules/prefer-namespace-keyword.test.ts index 47ebc5fd5e9f..b512cea67571 100644 --- a/packages/eslint-plugin/tests/rules/prefer-namespace-keyword.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-namespace-keyword.test.ts @@ -7,16 +7,16 @@ const ruleTester = new RuleTester({ ruleTester.run('prefer-namespace-keyword', rule, { valid: [ - "declare module 'foo'", - "declare module 'foo' { }", - 'namespace foo { }', - 'declare namespace foo { }', - 'declare global { }', + "declare module 'foo';", + "declare module 'foo' {}", + 'namespace foo {}', + 'declare namespace foo {}', + 'declare global {}', ], invalid: [ { - code: 'module foo { }', - output: 'namespace foo { }', + code: 'module foo {}', + output: 'namespace foo {}', errors: [ { messageId: 'useNamespace', @@ -26,8 +26,8 @@ ruleTester.run('prefer-namespace-keyword', rule, { ], }, { - code: 'declare module foo { }', - output: 'declare namespace foo { }', + code: 'declare module foo {}', + output: 'declare namespace foo {}', errors: [ { messageId: 'useNamespace', @@ -39,14 +39,14 @@ ruleTester.run('prefer-namespace-keyword', rule, { { code: ` declare module foo { - declare module bar { } + declare module bar {} } - `, + `, output: ` declare namespace foo { - declare namespace bar { } + declare namespace bar {} } - `, + `, errors: [ { messageId: 'useNamespace', @@ -56,7 +56,7 @@ declare namespace foo { { messageId: 'useNamespace', line: 3, - column: 5, + column: 3, }, ], }, From ab27ad5d02030289a1b730fbedeaee4c9f028536 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:20:12 -0700 Subject: [PATCH 26/92] chore: prefer-function-type --- .../tests/rules/prefer-function-type.test.ts | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-function-type.test.ts b/packages/eslint-plugin/tests/rules/prefer-function-type.test.ts index b4c6b4a3de85..257bd9e20d85 100644 --- a/packages/eslint-plugin/tests/rules/prefer-function-type.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-function-type.test.ts @@ -14,30 +14,35 @@ ruleTester.run('prefer-function-type', rule, { interface Foo { (): void; bar: number; -}`, +} + `, ` type Foo = { (): void; bar: number; -}`, +}; + `, ` -function foo(bar: { (): string, baz: number }): string { +function foo(bar: { (): string; baz: number }): string { return bar(); -}`, +} + `, ` interface Foo { bar: string; } interface Bar extends Foo { (): void; -}`, +} + `, ` interface Foo { bar: string; } interface Bar extends Function, Foo { (): void; -}`, +} + `, ], invalid: [ @@ -45,7 +50,8 @@ interface Bar extends Function, Foo { code: ` interface Foo { (): string; -}`, +} + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -53,13 +59,15 @@ interface Foo { }, ], output: ` -type Foo = () => string;`, +type Foo = () => string; + `, }, { code: ` type Foo = { (): string; -}`, +}; + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -67,13 +75,15 @@ type Foo = { }, ], output: ` -type Foo = () => string`, +type Foo = () => string; + `, }, { code: ` function foo(bar: { (s: string): number }): number { - return bar("hello"); -}`, + return bar('hello'); +} + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -82,14 +92,16 @@ function foo(bar: { (s: string): number }): number { ], output: ` function foo(bar: (s: string) => number): number { - return bar("hello"); -}`, + return bar('hello'); +} + `, }, { code: ` function foo(bar: { (s: string): number } | undefined): number { - return bar("hello"); -}`, + return bar('hello'); +} + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -98,14 +110,16 @@ function foo(bar: { (s: string): number } | undefined): number { ], output: ` function foo(bar: ((s: string) => number) | undefined): number { - return bar("hello"); -}`, + return bar('hello'); +} + `, }, { code: ` interface Foo extends Function { (): void; -}`, +} + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -113,13 +127,15 @@ interface Foo extends Function { }, ], output: ` -type Foo = () => void;`, +type Foo = () => void; + `, }, { code: ` interface Foo { (bar: T): string; -}`, +} + `, errors: [ { messageId: 'functionTypeOverCallableType', @@ -127,7 +143,8 @@ interface Foo { }, ], output: ` -type Foo = (bar: T) => string;`, +type Foo = (bar: T) => string; + `, }, ], }); From 107d14dea42fdce29b81165949f81ad424952a48 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:21:23 -0700 Subject: [PATCH 27/92] chore: prefer-for-of --- .../tests/rules/prefer-for-of.test.ts | 156 +++++++++--------- 1 file changed, 77 insertions(+), 79 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts index 0e22722afb43..d2d7319b3b1d 100644 --- a/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-for-of.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/prefer-for-of'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -11,42 +11,42 @@ ruleTester.run('prefer-for-of', rule, { for (let i = 0; i < arr1.length; i++) { const x = arr1[i] === arr2[i]; } - `, + `, ` for (let i = 0; i < arr.length; i++) { arr[i] = 0; } - `, + `, ` for (var c = 0; c < arr.length; c++) { doMath(c); } - `, + `, ` for (var d = 0; d < arr.length; d++) doMath(d); -`, + `, ` for (var e = 0; e < arr.length; e++) { - if(e > 5) { + if (e > 5) { doMath(e); } console.log(arr[e]); } -`, + `, ` for (var f = 0; f <= 40; f++) { doMath(f); } -`, + `, ` for (var g = 0; g <= 40; g++) doMath(g); -`, + `, ` -for(var h=0, len=arr.length; h < len; h++) {} -`, +for (var h = 0, len = arr.length; h < len; h++) {} + `, ` -for(var i=0, len=arr.length; i < len; i++) arr[i]; -`, +for (var i = 0, len = arr.length; i < len; i++) arr[i]; + `, ` var m = 0; for (;;) { @@ -54,107 +54,106 @@ for (;;) { console.log(m); m++; } -`, + `, ` var n = 0; for (; n < 9; n++) { console.log(n); } -`, + `, ` var o = 0; for (; o < arr.length; o++) { console.log(arr[o]); } -`, + `, ` -for(; x < arr.length; x++) {} -`, +for (; x < arr.length; x++) {} + `, ` -for(let x = 0;; x++) {} -`, +for (let x = 0; ; x++) {} + `, ` -for(let x = 0; x < arr.length;) {} -`, +for (let x = 0; x < arr.length; ) {} + `, ` -for(let x = 0; NOTX < arr.length; x++) {} -`, +for (let x = 0; NOTX < arr.length; x++) {} + `, ` -for(let x = 0; x < arr.length; NOTX++) {} -`, +for (let x = 0; x < arr.length; NOTX++) {} + `, ` -for(let NOTX = 0; x < arr.length; x++) {} -`, +for (let NOTX = 0; x < arr.length; x++) {} + `, ` -for(let x = 0; x < arr.length; x--) {} -`, +for (let x = 0; x < arr.length; x--) {} + `, ` -for(let x = 0; x <= arr.length; x++) {} -`, +for (let x = 0; x <= arr.length; x++) {} + `, ` -for(let x = 1; x < arr.length; x++) {} - -`, +for (let x = 1; x < arr.length; x++) {} + `, ` -for(let x = 0; x < arr.length(); x++) {} -`, +for (let x = 0; x < arr.length(); x++) {} + `, ` -for(let x = 0; x < arr.length; x+=11) {} -`, +for (let x = 0; x < arr.length; x += 11) {} + `, ` -for(let x = arr.length; x > 1; x-=1) {} -`, +for (let x = arr.length; x > 1; x -= 1) {} + `, ` -for(let x = 0; x < arr.length; x*=2) {} -`, +for (let x = 0; x < arr.length; x *= 2) {} + `, ` -for(let x = 0; x < arr.length; x=x+11) {} -`, +for (let x = 0; x < arr.length; x = x + 11) {} + `, ` -for(let x = 0; x < arr.length; x++) { +for (let x = 0; x < arr.length; x++) { x++; } -`, + `, ` -for(let x = 0; true; x++) {} -`, +for (let x = 0; true; x++) {} + `, ` for (var q in obj) { if (obj.hasOwnProperty(q)) { console.log(q); } } -`, + `, ` for (var r of arr) { console.log(r); } -`, + `, ` for (let x = 0; x < arr.length; x++) { let y = arr[x + 1]; } -`, + `, ` for (let i = 0; i < arr.length; i++) { delete arr[i]; } -`, + `, ` for (let i = 0; i < arr.length; i++) { [arr[i]] = [1]; } -`, + `, ` for (let i = 0; i < arr.length; i++) { [...arr[i]] = [1]; } -`, - ` + `, + noFormat` for (let i = 0; i < arr.length; i++) { ({ foo: arr[i] }) = { foo: 0 }; } -`, + `, ` for (let i = 0; i < arr1?.length; i++) { const x = arr1[i] === arr2[i]; @@ -188,7 +187,7 @@ for (var d = 0; d < arr.length; d++) doMath?.(d); for (var a = 0; a < obj.arr.length; a++) { console.log(obj.arr[a]); } - `, + `, errors: [ { messageId: 'preferForOf', @@ -198,7 +197,7 @@ for (var a = 0; a < obj.arr.length; a++) { { code: ` for (var b = 0; b < arr.length; b++) console.log(arr[b]); - `, + `, errors: [ { messageId: 'preferForOf', @@ -220,7 +219,7 @@ for (let a = 0; a < arr.length; a++) { { code: ` for (var b = 0; b < arr.length; b++) console?.log(arr[b]); - `, + `, errors: [ { messageId: 'preferForOf', @@ -232,7 +231,7 @@ for (var b = 0; b < arr.length; b++) console?.log(arr[b]); for (let a = 0; a < arr.length; a++) { console?.log(arr[a]); } - `, + `, errors: [ { messageId: 'preferForOf', @@ -244,7 +243,7 @@ for (let a = 0; a < arr.length; a++) { for (let a = 0; a < arr.length; ++a) { arr[a].whatever(); } - `, + `, errors: [ { messageId: 'preferForOf', @@ -253,8 +252,8 @@ for (let a = 0; a < arr.length; ++a) { }, { code: ` -for(let x = 0; x < arr.length; x++) {} - `, +for (let x = 0; x < arr.length; x++) {} + `, errors: [ { messageId: 'preferForOf', @@ -263,8 +262,8 @@ for(let x = 0; x < arr.length; x++) {} }, { code: ` -for(let x = 0; x < arr.length; x+=1) {} - `, +for (let x = 0; x < arr.length; x += 1) {} + `, errors: [ { messageId: 'preferForOf', @@ -273,8 +272,8 @@ for(let x = 0; x < arr.length; x+=1) {} }, { code: ` -for(let x = 0; x < arr.length; x=x+1) {} - `, +for (let x = 0; x < arr.length; x = x + 1) {} + `, errors: [ { messageId: 'preferForOf', @@ -283,8 +282,8 @@ for(let x = 0; x < arr.length; x=x+1) {} }, { code: ` -for(let x = 0; x < arr.length; x=1+x) {} - `, +for (let x = 0; x < arr.length; x = 1 + x) {} + `, errors: [ { messageId: 'preferForOf', @@ -294,10 +293,9 @@ for(let x = 0; x < arr.length; x=1+x) {} { code: ` for (let shadow = 0; shadow < arr.length; shadow++) { - for (let shadow = 0; shadow < arr.length; shadow++) { - } + for (let shadow = 0; shadow < arr.length; shadow++) {} } - `, + `, errors: [ { messageId: 'preferForOf', @@ -312,7 +310,7 @@ for (let shadow = 0; shadow < arr.length; shadow++) { for (let i = 0; i < arr.length; i++) { obj[arr[i]] = 1; } - `, + `, errors: [ { messageId: 'preferForOf', @@ -324,7 +322,7 @@ for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) { delete obj[arr[i]]; } - `, + `, errors: [ { messageId: 'preferForOf', @@ -336,7 +334,7 @@ for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) { [obj[arr[i]]] = [1]; } - `, + `, errors: [ { messageId: 'preferForOf', @@ -348,7 +346,7 @@ for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) { [...obj[arr[i]]] = [1]; } - `, + `, errors: [ { messageId: 'preferForOf', @@ -358,9 +356,9 @@ for (let i = 0; i < arr.length; i++) { { code: ` for (let i = 0; i < arr.length; i++) { - ({ foo: obj[arr[i]] }) = { foo: 1 }; + ({ foo: obj[arr[i]] } = { foo: 1 }); } - `, + `, errors: [ { messageId: 'preferForOf', From 7df018b938159658913747c43844f3a05ac1c142 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:22:12 -0700 Subject: [PATCH 28/92] chore: prefer-as-const --- .../tests/rules/prefer-as-const.test.ts | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts b/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts index c18ac5d7f14b..d5c15474f698 100644 --- a/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts @@ -7,12 +7,12 @@ const ruleTester = new RuleTester({ ruleTester.run('prefer-as-const', rule, { valid: [ - "let foo = 'baz' as const", - 'let foo = 1 as const', - "let foo = { bar: 'baz' as const }", - 'let foo = { bar: 1 as const }', - "let foo = { bar: 'baz' }", - 'let foo = { bar: 2 }', + "let foo = 'baz' as const;", + 'let foo = 1 as const;', + "let foo = { bar: 'baz' as const };", + 'let foo = { bar: 1 as const };', + "let foo = { bar: 'baz' };", + 'let foo = { bar: 2 };', "let foo = 'bar';", "let foo = 'bar';", "let foo = 'bar' as string;", @@ -23,16 +23,24 @@ ruleTester.run('prefer-as-const', rule, { 'let foo: number = 1;', "let foo: 'bar' = baz;", "let foo = 'bar';", - 'class foo { bar: "baz" = "baz" }', - 'class foo { bar = "baz" }', - "let foo: 'bar'", - 'let foo = { bar }', - "let foo: 'baz' = 'baz' as const", + ` + class foo { + bar: 'baz' = 'baz'; + } + `, + ` + class foo { + bar = 'baz'; + } + `, + "let foo: 'bar';", + 'let foo = { bar };', + "let foo: 'baz' = 'baz' as const;", ], invalid: [ { - code: "let foo = { bar: 'baz' as 'baz' }", - output: "let foo = { bar: 'baz' as const }", + code: "let foo = { bar: 'baz' as 'baz' };", + output: "let foo = { bar: 'baz' as const };", errors: [ { messageId: 'preferConstAssertion', @@ -42,8 +50,8 @@ ruleTester.run('prefer-as-const', rule, { ], }, { - code: 'let foo = { bar: 1 as 1 }', - output: 'let foo = { bar: 1 as const }', + code: 'let foo = { bar: 1 as 1 };', + output: 'let foo = { bar: 1 as const };', errors: [ { messageId: 'preferConstAssertion', From ec6f5f69ba347b5b99fbee53fd7439a73eee3ae5 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:22:42 -0700 Subject: [PATCH 29/92] chore: no-var-requires --- .../tests/rules/no-var-requires.test.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-var-requires.test.ts b/packages/eslint-plugin/tests/rules/no-var-requires.test.ts index 4433a634c7b4..25d53d9ab941 100644 --- a/packages/eslint-plugin/tests/rules/no-var-requires.test.ts +++ b/packages/eslint-plugin/tests/rules/no-var-requires.test.ts @@ -6,10 +6,14 @@ const ruleTester = new RuleTester({ }); ruleTester.run('no-var-requires', rule, { - valid: ["import foo = require('foo')", "require('foo')", "require?.('foo')"], + valid: [ + "import foo = require('foo');", + "require('foo');", + "require?.('foo');", + ], invalid: [ { - code: "var foo = require('foo')", + code: "var foo = require('foo');", errors: [ { messageId: 'noVarReqs', @@ -19,7 +23,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "const foo = require('foo')", + code: "const foo = require('foo');", errors: [ { messageId: 'noVarReqs', @@ -29,7 +33,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "let foo = require('foo')", + code: "let foo = require('foo');", errors: [ { messageId: 'noVarReqs', @@ -39,7 +43,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "let foo = trick(require('foo'))", + code: "let foo = trick(require('foo'));", errors: [ { messageId: 'noVarReqs', @@ -49,7 +53,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "var foo = require?.('foo')", + code: "var foo = require?.('foo');", errors: [ { messageId: 'noVarReqs', @@ -59,7 +63,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "const foo = require?.('foo')", + code: "const foo = require?.('foo');", errors: [ { messageId: 'noVarReqs', @@ -69,7 +73,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "let foo = require?.('foo')", + code: "let foo = require?.('foo');", errors: [ { messageId: 'noVarReqs', @@ -79,7 +83,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "let foo = trick(require?.('foo'))", + code: "let foo = trick(require?.('foo'));", errors: [ { messageId: 'noVarReqs', @@ -89,7 +93,7 @@ ruleTester.run('no-var-requires', rule, { ], }, { - code: "let foo = trick?.(require('foo'))", + code: "let foo = trick?.(require('foo'));", errors: [ { messageId: 'noVarReqs', From 4b6cbe5c2543722508ee956aaa8be85b5b1a9b9c Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:24:07 -0700 Subject: [PATCH 30/92] chore: no-useless-constructor --- .../rules/no-useless-constructor.test.ts | 292 +++++++++++++++--- 1 file changed, 246 insertions(+), 46 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts b/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts index 49b362f4d9f7..412aab3a1f1d 100644 --- a/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts +++ b/packages/eslint-plugin/tests/rules/no-useless-constructor.test.ts @@ -19,83 +19,283 @@ const error: any = { ruleTester.run('no-useless-constructor', rule, { valid: [ - 'class A { }', - 'class A { constructor(){ doSomething(); } }', - 'class A extends B { constructor(){} }', - "class A extends B { constructor(){ super('foo'); } }", - 'class A extends B { constructor(foo, bar){ super(foo, bar, 1); } }', - 'class A extends B { constructor(){ super(); doSomething(); } }', - 'class A extends B { constructor(...args){ super(...args); doSomething(); } }', - 'class A { dummyMethod(){ doSomething(); } }', - 'class A extends B.C { constructor() { super(foo); } }', - 'class A extends B.C { constructor([a, b, c]) { super(...arguments); } }', - 'class A extends B.C { constructor(a = f()) { super(...arguments); } }', - 'class A extends B { constructor(a, b, c) { super(a, b); } }', - 'class A extends B { constructor(foo, bar){ super(foo); } }', - 'class A extends B { constructor(test) { super(); } }', - 'class A extends B { constructor() { foo; } }', - 'class A extends B { constructor(foo, bar) { super(bar); } }', + 'class A {}', + ` +class A { + constructor() { + doSomething(); + } +} + `, + ` +class A extends B { + constructor() {} +} + `, + ` +class A extends B { + constructor() { + super('foo'); + } +} + `, + ` +class A extends B { + constructor(foo, bar) { + super(foo, bar, 1); + } +} + `, + ` +class A extends B { + constructor() { + super(); + doSomething(); + } +} + `, + ` +class A extends B { + constructor(...args) { + super(...args); + doSomething(); + } +} + `, + ` +class A { + dummyMethod() { + doSomething(); + } +} + `, + ` +class A extends B.C { + constructor() { + super(foo); + } +} + `, + ` +class A extends B.C { + constructor([a, b, c]) { + super(...arguments); + } +} + `, + ` +class A extends B.C { + constructor(a = f()) { + super(...arguments); + } +} + `, + ` +class A extends B { + constructor(a, b, c) { + super(a, b); + } +} + `, + ` +class A extends B { + constructor(foo, bar) { + super(foo); + } +} + `, + ` +class A extends B { + constructor(test) { + super(); + } +} + `, + ` +class A extends B { + constructor() { + foo; + } +} + `, + ` +class A extends B { + constructor(foo, bar) { + super(bar); + } +} + `, // https://github.com/typescript-eslint/typescript-eslint/issues/15 - 'declare class A { constructor(); }', - 'class A { constructor(); }', - 'abstract class A { constructor(); }', - 'abstract class A { abstract constructor(); }', + ` +declare class A { + constructor(); +} + `, + ` +class A { + constructor(); +} + `, + ` +abstract class A { + constructor(); +} + `, + ` +abstract class A { + abstract constructor(); +} + `, // https://github.com/typescript-eslint/typescript-eslint/issues/48 - 'class A { constructor(private name: string) {} }', - 'class A { constructor(public name: string) {} }', - 'class A { constructor(protected name: string) {} }', + ` +class A { + constructor(private name: string) {} +} + `, + ` +class A { + constructor(public name: string) {} +} + `, + ` +class A { + constructor(protected name: string) {} +} + `, // https://github.com/typescript-eslint/typescript-eslint/pull/167#discussion_r252638401 - 'class A { private constructor() {} }', - 'class A { protected constructor() {} }', - 'class A extends B { public constructor() {} }', - 'class A extends B { protected constructor(foo, bar) { super(bar); } }', - 'class A extends B { private constructor(foo, bar) { super(bar); } }', - 'class A extends B { public constructor(foo){ super(foo); } }', - 'class A extends B { public constructor(foo){} }', + ` +class A { + private constructor() {} +} + `, + ` +class A { + protected constructor() {} +} + `, + ` +class A extends B { + public constructor() {} +} + `, + ` +class A extends B { + protected constructor(foo, bar) { + super(bar); + } +} + `, + ` +class A extends B { + private constructor(foo, bar) { + super(bar); + } +} + `, + ` +class A extends B { + public constructor(foo) { + super(foo); + } +} + `, + ` +class A extends B { + public constructor(foo) {} +} + `, // type definition / overload - 'class A { constructor(foo); }', + ` +class A { + constructor(foo); +} + `, ], invalid: [ { - code: 'class A { constructor(){} }', + code: ` +class A { + constructor() {} +} + `, errors: [error], }, { - code: "class A { 'constructor'(){} }", + code: ` +class A extends B { + constructor() { + super(); + } +} + `, errors: [error], }, { - code: 'class A extends B { constructor() { super(); } }', + code: ` +class A extends B { + constructor(foo) { + super(foo); + } +} + `, errors: [error], }, { - code: 'class A extends B { constructor(foo){ super(foo); } }', + code: ` +class A extends B { + constructor(foo, bar) { + super(foo, bar); + } +} + `, errors: [error], }, { - code: 'class A extends B { constructor(foo, bar){ super(foo, bar); } }', + code: ` +class A extends B { + constructor(...args) { + super(...args); + } +} + `, errors: [error], }, { - code: 'class A extends B { constructor(...args){ super(...args); } }', + code: ` +class A extends B.C { + constructor() { + super(...arguments); + } +} + `, errors: [error], }, { - code: 'class A extends B.C { constructor() { super(...arguments); } }', + code: ` +class A extends B { + constructor(a, b, ...c) { + super(...arguments); + } +} + `, errors: [error], }, { - code: - 'class A extends B { constructor(a, b, ...c) { super(...arguments); } }', + code: ` +class A extends B { + constructor(a, b, ...c) { + super(a, b, ...c); + } +} + `, errors: [error], }, { - code: - 'class A extends B { constructor(a, b, ...c) { super(a, b, ...c); } }', - errors: [error], - }, - { - code: 'class A { public constructor() {} }', + code: ` +class A { + public constructor() {} +} + `, errors: [error], }, ], From cc6a199a664b9e597122914bb2cca0e7a2e4666f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:49:47 -0700 Subject: [PATCH 31/92] chore: no-use-before-define --- .../tests/rules/no-use-before-define.test.ts | 324 ++++++++++-------- 1 file changed, 180 insertions(+), 144 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts b/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts index 70d91022816d..dbbfcd61f5a4 100644 --- a/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts +++ b/packages/eslint-plugin/tests/rules/no-use-before-define.test.ts @@ -10,21 +10,44 @@ const parserOptions = { ecmaVersion: 6 as const }; ruleTester.run('no-use-before-define', rule, { valid: [ - 'type foo = 1; const x: foo = 1;', - 'type foo = 1; type bar = foo;', + ` +type foo = 1; +const x: foo = 1; + `, + ` +type foo = 1; +type bar = foo; + `, ` interface Foo {} const x: Foo = {}; `, - 'var a=10; alert(a);', - 'function b(a) { alert(a); }', + ` +var a = 10; +alert(a); + `, + ` +function b(a) { + alert(a); +} + `, 'Object.hasOwnProperty.call(a);', - 'function a() { alert(arguments);}', - 'declare function a()', - 'declare class a { foo() }', - `const updatedAt = data?.updatedAt;`, ` -function f() { return function t() {}; } +function a() { + alert(arguments); +} + `, + 'declare function a();', + ` +declare class a { + foo(); +} + `, + 'const updatedAt = data?.updatedAt;', + ` +function f() { + return function t() {}; +} f()?.(); `, ` @@ -34,94 +57,106 @@ alert(a?.b); { code: ` a(); -function a() { alert(arguments); } - `, +function a() { + alert(arguments); +} + `, options: ['nofunc'], }, { - code: '(() => { var a = 42; alert(a); })();', + code: ` +(() => { + var a = 42; + alert(a); +})(); + `, parserOptions, }, ` a(); try { - throw new Error() + throw new Error(); } catch (a) {} - `, + `, { code: ` class A {} new A(); - `, + `, parserOptions, }, - 'var a = 0, b = a;', - { code: 'var {a = 0, b = a} = {};', parserOptions }, + ` +var a = 0, + b = a; + `, + { code: 'var { a = 0, b = a } = {};', parserOptions }, { code: 'var [a = 0, b = a] = {};', parserOptions }, ` function foo() { - foo(); + foo(); } - `, + `, ` var foo = function() { - foo(); + foo(); }; - `, + `, ` var a; -for (a in a) {} - `, +for (a in a) { +} + `, { code: ` var a; -for (a of a) {} - `, +for (a of a) { +} + `, parserOptions, }, // Block-level bindings { code: ` -"use strict"; +'use strict'; a(); { - function a() {} + function a() {} } - `, + `, parserOptions, }, { code: ` -"use strict"; +'use strict'; { - a(); - function a() {} + a(); + function a() {} } - `, + `, options: ['nofunc'], parserOptions, }, { code: ` switch (foo) { - case 1: { - a(); - } - default: { - let a; - } + case 1: { + a(); + } + default: { + let a; + } } - `, + `, parserOptions, }, { code: ` a(); { - let a = function () {}; + let a = function() {}; } - `, + `, parserOptions, }, @@ -130,29 +165,29 @@ a(); code: ` a(); function a() { - alert(arguments); + alert(arguments); } - `, + `, options: [{ functions: false }], }, { code: ` -"use strict"; +'use strict'; { - a(); - function a() {} + a(); + function a() {} } - `, + `, options: [{ functions: false }], parserOptions, }, { code: ` function foo() { - new A(); + new A(); } -class A {}; - `, +class A {} + `, options: [{ classes: false }], parserOptions, }, @@ -161,17 +196,17 @@ class A {}; { code: ` function foo() { - bar; + bar; } var bar; - `, + `, options: [{ variables: false }], }, { code: ` var foo = () => bar; var bar; - `, + `, options: [{ variables: false }], parserOptions, }, @@ -180,8 +215,8 @@ var bar; { code: ` var x: Foo = 2; -type Foo = string | number - `, +type Foo = string | number; + `, options: [{ typedefs: false }], }, @@ -191,7 +226,7 @@ type Foo = string | number var alias = Test; class Test {} - `, + `, parserOptions, options: [{ classes: false }], }, @@ -200,7 +235,7 @@ class Test {} var alias = Test; export class Test {} - `, + `, parserOptions: { ecmaVersion: 6, sourceType: 'module' }, options: [{ classes: false }], }, @@ -208,9 +243,9 @@ export class Test {} { code: ` interface ITest { - first : boolean; - second : string; - third : boolean; + first: boolean; + second: string; + third: boolean; } let first = () => console.log('first'); @@ -220,23 +255,23 @@ export let second = () => console.log('second'); export namespace Third { export let third = () => console.log('third'); } - `, + `, parserOptions: { ecmaVersion: 6, sourceType: 'module' }, }, // https://github.com/eslint/typescript-eslint-parser/issues/550 ` function test(file: Blob) { const slice: typeof file.slice = - file.slice || (file as any).webkitSlice || (file as any).mozSlice - return slice + file.slice || (file as any).webkitSlice || (file as any).mozSlice; + return slice; } `, // https://github.com/eslint/typescript-eslint-parser/issues/435 ` interface Foo { - bar: string + bar: string; } -const bar = 'blah' +const bar = 'blah'; `, { code: ` @@ -279,8 +314,8 @@ enum Foo { { code: ` a++; -var a=19; - `, +var a = 19; + `, parserOptions: { sourceType: 'module' }, errors: [ { @@ -293,8 +328,8 @@ var a=19; { code: ` a++; -var a=19; - `, +var a = 19; + `, parserOptions, errors: [ { @@ -307,8 +342,8 @@ var a=19; { code: ` a++; -var a=19; - `, +var a = 19; + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -320,8 +355,8 @@ var a=19; { code: ` a(); -var a=function() {}; - `, +var a = function() {}; + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -333,8 +368,8 @@ var a=function() {}; { code: ` alert(a[1]); -var a=[1,3]; - `, +var a = [1, 3]; + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -347,11 +382,11 @@ var a=[1,3]; code: ` a(); function a() { - alert(b); - var b=10; - a(); + alert(b); + var b = 10; + a(); } - `, + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -368,9 +403,8 @@ function a() { { code: ` a(); -var a=function() {}; - - `, +var a = function() {}; + `, options: ['nofunc'], errors: [ { @@ -382,13 +416,11 @@ var a=function() {}; }, { code: ` -( - () => { - alert(a); - var a = 42; - } -)(); - `, +(() => { + alert(a); + var a = 42; +})(); + `, parserOptions, errors: [ { @@ -400,11 +432,9 @@ var a=function() {}; }, { code: ` -( - () => a() -)(); -function a() { } - `, +(() => a())(); +function a() {} + `, parserOptions, errors: [ { @@ -416,12 +446,12 @@ function a() { } }, { code: ` -"use strict"; +'use strict'; a(); { - function a() {} + function a() {} } - `, + `, parser: require.resolve('espree'), errors: [ { @@ -435,11 +465,11 @@ a(); code: ` a(); try { - throw new Error() + throw new Error(); } catch (foo) { - var a; + var a; } - `, + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -452,7 +482,7 @@ try { code: ` var f = () => a; var a; - `, + `, parserOptions, errors: [ { @@ -465,8 +495,8 @@ var a; { code: ` new A(); -class A {}; - `, +class A {} + `, parserOptions, errors: [ { @@ -479,10 +509,10 @@ class A {}; { code: ` function foo() { - new A(); + new A(); } -class A {}; - `, +class A {} + `, parserOptions, errors: [ { @@ -496,7 +526,7 @@ class A {}; code: ` new A(); var A = class {}; - `, + `, parserOptions, errors: [ { @@ -509,10 +539,10 @@ var A = class {}; { code: ` function foo() { - new A(); + new A(); } var A = class {}; - `, + `, parserOptions, errors: [ { @@ -528,9 +558,9 @@ var A = class {}; code: ` a++; { - var a; + var a; } - `, + `, parserOptions, errors: [ { @@ -542,12 +572,12 @@ a++; }, { code: ` -"use strict"; +'use strict'; { - a(); - function a() {} + a(); + function a() {} } - `, + `, parserOptions, errors: [ { @@ -560,10 +590,10 @@ a++; { code: ` { - a; - let a = 1 + a; + let a = 1; } - `, + `, parserOptions, errors: [ { @@ -576,12 +606,12 @@ a++; { code: ` switch (foo) { - case 1: - a(); - default: - let a; + case 1: + a(); + default: + let a; } - `, + `, parserOptions, errors: [ { @@ -594,12 +624,12 @@ switch (foo) { { code: ` if (true) { - function foo() { - a; - } - let a; + function foo() { + a; + } + let a; } - `, + `, parserOptions, errors: [ { @@ -614,8 +644,8 @@ if (true) { { code: ` a(); -var a=function() {}; - `, +var a = function() {}; + `, options: [{ functions: false, classes: false }], errors: [ { @@ -629,7 +659,7 @@ var a=function() {}; code: ` new A(); var A = class {}; - `, + `, options: [{ classes: false }], parserOptions, errors: [ @@ -643,10 +673,10 @@ var A = class {}; { code: ` function foo() { - new A(); + new A(); } var A = class {}; - `, + `, options: [{ classes: false }], parserOptions, errors: [ @@ -703,7 +733,7 @@ var A = class {}; ], }, { - code: 'var {a = a} = [];', + code: 'var { a = a } = [];', parserOptions, errors: [ { @@ -725,7 +755,7 @@ var A = class {}; ], }, { - code: 'var {b = a, a} = {};', + code: 'var { b = a, a } = {};', parserOptions, errors: [ { @@ -747,7 +777,7 @@ var A = class {}; ], }, { - code: 'var {a = 0} = a;', + code: 'var { a = 0 } = a;', parserOptions, errors: [ { @@ -769,7 +799,10 @@ var A = class {}; ], }, { - code: 'for (var a in a) {}', + code: ` +for (var a in a) { +} + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -779,7 +812,10 @@ var A = class {}; ], }, { - code: 'for (var a of a) {}', + code: ` +for (var a of a) { +} + `, parserOptions, errors: [ { @@ -794,8 +830,8 @@ var A = class {}; { code: ` function foo() { - bar; - var bar = 1; + bar; + var bar = 1; } var bar; `, @@ -864,7 +900,7 @@ enum Foo { code: ` f(); function f() {} - `, + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -876,7 +912,7 @@ function f() {} code: ` alert(a); var a = 10; - `, + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -890,7 +926,7 @@ f()?.(); function f() { return function t() {}; } - `, + `, errors: [ { messageId: 'noUseBeforeDefine', @@ -902,7 +938,7 @@ function f() { code: ` alert(a?.b); var a = { b: 5 }; - `, + `, errors: [ { messageId: 'noUseBeforeDefine', From c4c1073d6c32e149b44ae40303e06aa758d7d534 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:50:21 -0700 Subject: [PATCH 32/92] chore: no-unused-vars --- .../tests/rules/no-unused-vars.test.ts | 476 +++++++++--------- 1 file changed, 251 insertions(+), 225 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts index 45bb8c80db15..f59cb5884252 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts @@ -24,426 +24,439 @@ ruleTester.run('no-unused-vars', rule, { import { ClassDecoratorFactory } from 'decorators'; @ClassDecoratorFactory() export class Foo {} - `, + `, ` import { ClassDecorator } from 'decorators'; @ClassDecorator export class Foo {} - `, + `, ` import { AccessorDecoratorFactory } from 'decorators'; export class Foo { - @AccessorDecoratorFactory(true) - get bar() {} + @AccessorDecoratorFactory(true) + get bar() {} } - `, + `, ` import { AccessorDecorator } from 'decorators'; export class Foo { - @AccessorDecorator - set bar() {} + @AccessorDecorator + set bar() {} } - `, + `, ` import { MethodDecoratorFactory } from 'decorators'; export class Foo { - @MethodDecoratorFactory(false) - bar() {} + @MethodDecoratorFactory(false) + bar() {} } - `, + `, ` import { MethodDecorator } from 'decorators'; export class Foo { - @MethodDecorator - static bar() {} + @MethodDecorator + static bar() {} } - `, + `, ` import { ConstructorParameterDecoratorFactory } from 'decorators'; export class Service { - constructor(@ConstructorParameterDecoratorFactory(APP_CONFIG) config: AppConfig) { - this.title = config.title; - } + constructor( + @ConstructorParameterDecoratorFactory(APP_CONFIG) config: AppConfig, + ) { + this.title = config.title; + } } - `, + `, ` import { ConstructorParameterDecorator } from 'decorators'; export class Foo { - constructor(@ConstructorParameterDecorator bar) { - this.bar = bar; - } + constructor(@ConstructorParameterDecorator bar) { + this.bar = bar; + } } - `, + `, ` import { ParameterDecoratorFactory } from 'decorators'; export class Qux { - bar(@ParameterDecoratorFactory(true) baz: number) { - console.log(baz); - } + bar(@ParameterDecoratorFactory(true) baz: number) { + console.log(baz); + } } - `, + `, ` import { ParameterDecorator } from 'decorators'; export class Foo { - static greet(@ParameterDecorator name: string) { - return name; - } + static greet(@ParameterDecorator name: string) { + return name; + } } - `, + `, ` import { Input, Output, EventEmitter } from 'decorators'; export class SomeComponent { - @Input() data; - @Output() - click = new EventEmitter(); + @Input() data; + @Output() + click = new EventEmitter(); } - `, + `, ` import { configurable } from 'decorators'; export class A { - @configurable(true) static prop1; + @configurable(true) static prop1; - @configurable(false) - static prop2; + @configurable(false) + static prop2; } - `, + `, ` import { foo, bar } from 'decorators'; export class B { - @foo x; + @foo x; - @bar - y; + @bar + y; } - `, + `, ` interface Base {} class Thing implements Base {} -new Thing() - `, +new Thing(); + `, ` interface Base {} -const a: Base = {} +const a: Base = {}; console.log(a); - `, + `, ` -import { Foo } from 'foo' +import { Foo } from 'foo'; function bar() {} -bar() - `, +bar(); + `, ` -import { Foo } from 'foo' -const bar = function () {} -bar() - `, +import { Foo } from 'foo'; +const bar = function() {}; +bar(); + `, ` -import { Foo } from 'foo' -const bar = () => {} -bar() - `, +import { Foo } from 'foo'; +const bar = () => {}; +bar(); + `, ` -import { Foo } from 'foo' -(() => {})() - `, +import { Foo } from 'foo'; +(() => {})(); + `, ` import { Nullable } from 'nullable'; const a: Nullable = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; const a: Nullable = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Nullable | undefined = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Nullable & undefined = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; const a: Nullable = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; const a: Nullable> = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Array = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Nullable[] = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Array = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; const a: Array> = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; const a: Array> = 'hello'; console.log(a); - `, + `, ` import { Nullable } from 'nullable'; import { Component } from 'react'; -class Foo implements Component{}; +class Foo implements Component {} new Foo(); - `, + `, ` import { Nullable } from 'nullable'; import { Component } from 'react'; -class Foo extends Component{} +class Foo extends Component {} new Foo(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Component } from 'react'; -class Foo extends Component, {}>{} +class Foo extends Component, {}> {} new Foo(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Component } from 'react'; -class Foo implements Component, {}>{} +class Foo implements Component, {}> {} new Foo(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Component, Component2 } from 'react'; -class Foo implements Component, {}>, Component2{} +class Foo implements Component, {}>, Component2 {} new Foo(); - `, + `, ` import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do = (a: Nullable) => { console.log(a); } + do = (a: Nullable) => { + console.log(a); + }; } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do(a: Nullable) { console.log(a); } + do(a: Nullable) { + console.log(a); + } } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do(): Nullable { return null; } + do(): Nullable { + return null; + } } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { Another } from 'some'; interface A { - do(a: Nullable); + do(a: Nullable); } - `, + `, ` import { Nullable } from 'nullable'; import { Another } from 'some'; interface A { - other: Nullable; + other: Nullable; } - `, + `, ` import { Nullable } from 'nullable'; -function foo(a: Nullable) { console.log(a); } +function foo(a: Nullable) { + console.log(a); +} foo(); - `, + `, ` import { Nullable } from 'nullable'; -function foo(): Nullable { return null; } +function foo(): Nullable { + return null; +} foo(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; class A extends Nullable { - other: Nullable; + other: Nullable; } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; class A extends Nullable { - do(a: Nullable){ console.log(a); } + do(a: Nullable) { + console.log(a); + } } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; interface A extends Nullable { - other: Nullable; + other: Nullable; } - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; interface A extends Nullable { - do(a: Nullable); + do(a: Nullable); } - `, + `, ` import { Foo } from './types'; class Bar {} -new Bar() - `, +new Bar(); + `, ` import { Foo, Bar } from './types'; class Baz {} -new Baz() - `, +new Baz(); + `, ` import { Foo } from './types'; class Bar {} -new Bar() - `, +new Bar(); + `, ` import { Foo } from './types'; class Foo {} -new Foo() - `, +new Foo(); + `, ` import { Foo } from './types'; class Foo {} -new Foo() - `, +new Foo(); + `, ` import { Foo } from './types'; class Foo {} -new Foo() - `, +new Foo(); + `, ` -type Foo = "a" | "b" | "c" -type Bar = number +type Foo = 'a' | 'b' | 'c'; +type Bar = number; export const map: { [name in Foo]: Bar } = { - a: 1, - b: 2, - c: 3 -} - `, + a: 1, + b: 2, + c: 3, +}; + `, ` import { Nullable } from 'nullable'; class A { - bar: T + bar: T; } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; -function foo() { -} +function foo() {} foo(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; class A { - bar: T; + bar: T; } new A(); - `, + `, ` import { Nullable } from 'nullable'; import { SomeOther } from 'other'; interface A { - bar: T; + bar: T; } export const a: A = { - foo: "bar" + foo: 'bar', }; - `, + `, // https://github.com/bradzacher/eslint-plugin-typescript/issues/150 ` export class App { - constructor(private logger: Logger) { - console.log(this.logger); - } + constructor(private logger: Logger) { + console.log(this.logger); + } } - `, + `, ` export class App { - constructor(bar: string); - constructor(private logger: Logger) { - console.log(this.logger); - } + constructor(bar: string); + constructor(private logger: Logger) { + console.log(this.logger); + } } - `, + `, ` export class App { - constructor(baz: string, private logger: Logger) { - console.log(baz); - console.log(this.logger); - } + constructor(baz: string, private logger: Logger) { + console.log(baz); + console.log(this.logger); + } } - `, + `, ` export class App { - constructor(baz: string, private logger: Logger, private bar: () => void) { - console.log(this.logger); - this.bar(); - } + constructor(baz: string, private logger: Logger, private bar: () => void) { + console.log(this.logger); + this.bar(); + } } - `, + `, ` export class App { - constructor(private logger: Logger) {} - meth() { - console.log(this.logger); - } + constructor(private logger: Logger) {} + meth() { + console.log(this.logger); + } } - `, + `, // https://github.com/bradzacher/eslint-plugin-typescript/issues/126 ` import { Component, Vue } from 'vue-property-decorator'; @@ -451,96 +464,99 @@ import HelloWorld from './components/HelloWorld.vue'; @Component({ components: { - HelloWorld - } + HelloWorld, + }, }) export default class App extends Vue {} - `, + `, // https://github.com/bradzacher/eslint-plugin-typescript/issues/189 ` -import firebase, {User} from 'firebase/app' +import firebase, { User } from 'firebase/app'; // initialize firebase project -firebase.initializeApp({ -}) +firebase.initializeApp({}); export function authenticated(cb: (user: User | null) => void): void { - firebase.auth().onAuthStateChanged(user => cb(user)) + firebase.auth().onAuthStateChanged(user => cb(user)); } - `, + `, // https://github.com/bradzacher/eslint-plugin-typescript/issues/33 ` import { Foo } from './types'; export class Bar {} - `, + `, ` import webpack from 'webpack'; export default function webpackLoader(this: webpack.loader.LoaderContext) {} - `, + `, ` import execa, { Options as ExecaOptions } from 'execa'; export function foo(options: ExecaOptions): execa { - options() + options(); } - `, + `, ` import { Foo, Bar } from './types'; export class Baz {} - `, + `, ` // warning 'B' is defined but never used -export const a: Array<{b: B}> = [] - `, +export const a: Array<{ b: B }> = []; + `, ` export enum FormFieldIds { - PHONE = 'phone', - EMAIL = 'email', + PHONE = 'phone', + EMAIL = 'email', } - `, + `, ` enum FormFieldIds { - PHONE = 'phone', - EMAIL = 'email', + PHONE = 'phone', + EMAIL = 'email', } interface IFoo { - fieldName: FormFieldIds, + fieldName: FormFieldIds; } - `, + `, ` enum FormFieldIds { - PHONE = 'phone', - EMAIL = 'email', + PHONE = 'phone', + EMAIL = 'email', } interface IFoo { - fieldName: FormFieldIds.EMAIL, + fieldName: FormFieldIds.EMAIL; } `, // https://github.com/typescript-eslint/typescript-eslint/issues/25 ` -import * as fastify from 'fastify' -import { Server, IncomingMessage, ServerResponse } from 'http' -const server: fastify.FastifyInstance = fastify({}) -server.get('/ping') +import * as fastify from 'fastify'; +import { Server, IncomingMessage, ServerResponse } from 'http'; +const server: fastify.FastifyInstance< + Server, + IncomingMessage, + ServerResponse +> = fastify({}); +server.get('/ping'); `, // https://github.com/typescript-eslint/typescript-eslint/issues/61 - `declare function foo();`, + 'declare function foo();', // https://github.com/typescript-eslint/typescript-eslint/issues/61 ` declare namespace Foo { - function bar(line: string, index: number | null, tabSize: number): number; - var baz: string; + function bar(line: string, index: number | null, tabSize: number): number; + var baz: string; } `, // https://github.com/typescript-eslint/typescript-eslint/issues/61 ` declare var Foo: { - new (value?: any): Object, - foo(): string -} + new (value?: any): Object; + foo(): string; +}; `, // https://github.com/typescript-eslint/typescript-eslint/issues/106 ` declare class Foo { - constructor(value?: any): Object; - foo(): string; + constructor(value?: any): Object; + foo(): string; } `, ` @@ -602,9 +618,9 @@ import { Dec, TypeA, Class } from 'test'; export default class Foo { constructor( @Dec(Class) - ...prop: TypeA, + ...prop: TypeA ) { - prop() + prop(); } } `, @@ -615,7 +631,7 @@ export default class Foo { code: ` import { ClassDecoratorFactory } from 'decorators'; export class Foo {} - `, + `, errors: error([ { message: "'ClassDecoratorFactory' is defined but never used.", @@ -628,8 +644,8 @@ export class Foo {} code: ` import { Foo, Bar } from 'foo'; function baz() {} -baz() - `, +baz(); + `, errors: error([ { message: "'Foo' is defined but never used.", @@ -643,7 +659,7 @@ baz() import { Nullable } from 'nullable'; const a: string = 'hello'; console.log(a); - `, + `, errors: error([ { message: "'Nullable' is defined but never used.", @@ -658,7 +674,7 @@ import { Nullable } from 'nullable'; import { SomeOther } from 'other'; const a: Nullable = 'hello'; console.log(a); - `, + `, errors: error([ { message: "'SomeOther' is defined but never used.", @@ -673,10 +689,12 @@ console.log(a); import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do = (a: Nullable) => { console.log(a); } + do = (a: Nullable) => { + console.log(a); + }; } new A(); - `, + `, errors: error([ { message: "'Another' is defined but never used.", @@ -690,10 +708,12 @@ new A(); import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do(a: Nullable) { console.log(a); } + do(a: Nullable) { + console.log(a); + } } new A(); - `, + `, errors: error([ { message: "'Another' is defined but never used.", @@ -707,10 +727,12 @@ new A(); import { Nullable } from 'nullable'; import { Another } from 'some'; class A { - do(): Nullable { return null; } + do(): Nullable { + return null; + } } new A(); - `, + `, errors: error([ { message: "'Another' is defined but never used.", @@ -724,9 +746,9 @@ new A(); import { Nullable } from 'nullable'; import { Another } from 'some'; interface A { - do(a: Nullable); + do(a: Nullable); } - `, + `, errors: error([ { message: "'Another' is defined but never used.", @@ -740,9 +762,9 @@ interface A { import { Nullable } from 'nullable'; import { Another } from 'some'; interface A { - other: Nullable; + other: Nullable; } - `, + `, errors: error([ { message: "'Another' is defined but never used.", @@ -754,9 +776,11 @@ interface A { { code: ` import { Nullable } from 'nullable'; -function foo(a: string) { console.log(a); } +function foo(a: string) { + console.log(a); +} foo(); - `, + `, errors: error([ { message: "'Nullable' is defined but never used.", @@ -768,9 +792,11 @@ foo(); { code: ` import { Nullable } from 'nullable'; -function foo(): string | null { return null; } +function foo(): string | null { + return null; +} foo(); - `, + `, errors: error([ { message: "'Nullable' is defined but never used.", @@ -785,10 +811,10 @@ import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; class A extends Nullable { - other: Nullable; + other: Nullable; } new A(); - `, + `, errors: error([ { message: "'SomeOther' is defined but never used.", @@ -803,10 +829,10 @@ import { Nullable } from 'nullable'; import { SomeOther } from 'some'; import { Another } from 'some'; abstract class A extends Nullable { - other: Nullable; + other: Nullable; } new A(); - `, + `, errors: error([ { message: "'SomeOther' is defined but never used.", @@ -818,10 +844,10 @@ new A(); { code: ` enum FormFieldIds { - PHONE = 'phone', - EMAIL = 'email', + PHONE = 'phone', + EMAIL = 'email', } - `, + `, errors: error([ { message: "'FormFieldIds' is defined but never used.", @@ -835,7 +861,7 @@ enum FormFieldIds { import test from 'test'; import baz from 'baz'; export interface Bar extends baz.test {} - `, + `, errors: error([ { message: "'test' is defined but never used.", @@ -849,7 +875,7 @@ export interface Bar extends baz.test {} import test from 'test'; import baz from 'baz'; export interface Bar extends baz().test {} - `, + `, errors: error([ { message: "'test' is defined but never used.", @@ -863,7 +889,7 @@ export interface Bar extends baz().test {} import test from 'test'; import baz from 'baz'; export class Bar implements baz.test {} - `, + `, errors: error([ { message: "'test' is defined but never used.", @@ -877,7 +903,7 @@ export class Bar implements baz.test {} import test from 'test'; import baz from 'baz'; export class Bar implements baz().test {} - `, + `, errors: error([ { message: "'test' is defined but never used.", From 6f9ff08511a9f19274673c862a4e750b25abfc8a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:51:01 -0700 Subject: [PATCH 33/92] chore: no-unused-expressions --- .../tests/rules/no-unused-expressions.test.ts | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts b/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts index feb3a584bcc3..a50d74f6b745 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts @@ -69,19 +69,19 @@ ruleTester.run('no-unused-expressions', rule, { invalid: [ { code: ` -if(0) 0 - `, +if (0) 0; + `, errors: error([ { line: 2, - column: 7, + column: 8, }, ]), }, { code: ` -f(0), {} - `, +f(0), {}; + `, errors: error([ { line: 2, @@ -91,8 +91,8 @@ f(0), {} }, { code: ` -a, b() - `, +a, b(); + `, errors: error([ { line: 2, @@ -102,8 +102,11 @@ a, b() }, { code: ` -a() && function namedFunctionInExpressionContext () {f();} - `, +a() && + function namedFunctionInExpressionContext() { + f(); + }; + `, errors: error([ { line: 2, @@ -113,8 +116,8 @@ a() && function namedFunctionInExpressionContext () {f();} }, { code: ` -a?.b - `, +a?.b; + `, errors: error([ { line: 2, @@ -124,8 +127,8 @@ a?.b }, { code: ` -(a?.b).c - `, +(a?.b).c; + `, errors: error([ { line: 2, @@ -135,8 +138,8 @@ a?.b }, { code: ` -a?.['b'] - `, +a?.['b']; + `, errors: error([ { line: 2, @@ -146,8 +149,8 @@ a?.['b'] }, { code: ` -(a?.['b']).c - `, +(a?.['b']).c; + `, errors: error([ { line: 2, @@ -157,8 +160,8 @@ a?.['b'] }, { code: ` -a?.b()?.c - `, +a?.b()?.c; + `, errors: error([ { line: 2, @@ -168,8 +171,8 @@ a?.b()?.c }, { code: ` -(a?.b()).c - `, +(a?.b()).c; + `, errors: error([ { line: 2, @@ -180,7 +183,7 @@ a?.b()?.c { code: ` one[2]?.[3][4]; - `, + `, errors: error([ { line: 2, @@ -191,7 +194,7 @@ one[2]?.[3][4]; { code: ` one.two?.three.four; - `, + `, errors: error([ { line: 2, From 5455ca3c250047cccf8bd0295fee9bfe8231869e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:52:41 -0700 Subject: [PATCH 34/92] chore: no-untyped-public-signature --- .../rules/no-untyped-public-signature.test.ts | 203 +++++++++--------- 1 file changed, 99 insertions(+), 104 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-untyped-public-signature.test.ts b/packages/eslint-plugin/tests/rules/no-untyped-public-signature.test.ts index 28ee928cb41c..d71b98ce3c10 100644 --- a/packages/eslint-plugin/tests/rules/no-untyped-public-signature.test.ts +++ b/packages/eslint-plugin/tests/rules/no-untyped-public-signature.test.ts @@ -13,71 +13,65 @@ const ruleTester = new RuleTester({ ruleTester.run('no-untyped-public-signature', rule, { valid: [ { - code: `class A { - private a(c) { - } - }`, + code: ` +class A { + private a(c) {} +} + `, }, { - code: `class A { - private async a(c) { - } - }`, + code: ` +class A { + private async a(c) {} +} + `, }, { code: ` - class A { - public b(c: string):void { - - } - }`, +class A { + public b(c: string): void {} +} + `, }, { code: ` - class A { - public b(...c):void { - - } - }`, +class A { + public b(...c): void {} +} + `, }, { code: ` - class A { - b(c):void { - - } - }`, +class A { + b(c): void {} +} + `, options: [{ ignoredMethods: ['b'] }], }, { code: ` - class A { - ['b'](c):void { - - } - }`, +class A { + ['b'](c): void {} +} + `, options: [{ ignoredMethods: ['b'] }], }, { code: ` - class A { - [\`b\`](c):void { - - } - }`, +class A { + [\`b\`](c): void {} +} + `, options: [{ ignoredMethods: ['b'] }], }, { code: ` - class A { - b(...c):void { +class A { + b(...c): void {} - } - - d(c):void { - - } - }`, + d(c): void {} +} + `, options: [{ ignoredMethods: ['b', 'd'] }], }, // https://github.com/typescript-eslint/typescript-eslint/issues/1229 @@ -133,54 +127,56 @@ class Foo { invalid: [ //untyped parameter { - code: `class A { - public b(c):void { - - } - }`, + code: ` +class A { + public b(c): void {} +} + `, errors: [{ messageId: 'untypedParameter' }], }, //untyped parameter (any) { - code: `class A { - public b(c: any):void { - - } - }`, + code: ` +class A { + public b(c: any): void {} +} + `, errors: [{ messageId: 'untypedParameter' }], }, //implicit public method { - code: `class A { - b(c):void { - - } - }`, + code: ` +class A { + b(c): void {} +} + `, errors: [{ messageId: 'untypedParameter' }], }, //implicit async public method { - code: `class A { - async a(c): void { - } - }`, + code: ` +class A { + async a(c): void {} +} + `, errors: [{ messageId: 'untypedParameter' }], }, //no return type { - code: `class A { - public a(c: number) { - } - }`, + code: ` +class A { + public a(c: number) {} +} + `, errors: [{ messageId: 'noReturnType' }], }, //no return type + untyped parameter { - code: `class A { - public b(c) { - - } - }`, + code: ` +class A { + public b(c) {} +} + `, errors: [ { messageId: 'untypedParameter' }, { messageId: 'noReturnType' }, @@ -188,71 +184,70 @@ class Foo { }, //any return type { - code: `class A { - public b(c: number): any { - - } - }`, + code: ` +class A { + public b(c: number): any {} +} + `, errors: [{ messageId: 'noReturnType' }], }, //with ignored methods { - code: `class A { - public b(c: number): any { - - } + code: ` +class A { + public b(c: number): any {} - c() { - } - }`, + c() {} +} + `, options: [{ ignoredMethods: ['c'] }], errors: [{ messageId: 'noReturnType' }], }, { code: ` - let c = 'd'; - class A { - [methodName]() { - } - }`, +let c = 'd'; +class A { + [methodName]() {} +} + `, options: [{ ignoredMethods: ['methodName'] }], errors: [{ messageId: 'noReturnType' }], }, { code: ` - class A { - [1]() { - } - }`, +class A { + [1]() {} +} + `, options: [{ ignoredMethods: ['1'] }], errors: [{ messageId: 'noReturnType' }], }, { code: ` - let c = 'C'; - class A { - [\`methodName\${c}\`]() { - } - }`, +let c = 'C'; +class A { + [\`methodName\${c}\`]() {} +} + `, options: [{ ignoredMethods: ['methodNameC', 'methodNamec'] }], errors: [{ messageId: 'noReturnType' }], }, { code: ` - let c = '1'; - class A { - [(c as number)]() { - } - }`, +let c = '1'; +class A { + [c as number]() {} +} + `, options: [{ ignoredMethods: ['1'] }], errors: [{ messageId: 'noReturnType' }], }, { code: ` - class A { - abstract c() { - } - }`, +class A { + abstract c() {} +} + `, errors: [{ messageId: 'noReturnType' }], }, // https://github.com/typescript-eslint/typescript-eslint/issues/1229 From 8a63f6c187ce7661bdc9aab859d0c42f281c58a7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:54:59 -0700 Subject: [PATCH 35/92] chore: no-unsafe-return --- .../tests/rules/no-unsafe-return.test.ts | 85 ++++++++++++++----- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts index 0fdcaddf36b6..b0c1ead90076 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts @@ -3,6 +3,7 @@ import { RuleTester, batchedSingleLineTests, getFixturesRootDir, + noFormat, } from '../RuleTester'; const ruleTester = new RuleTester({ @@ -15,29 +16,65 @@ const ruleTester = new RuleTester({ ruleTester.run('no-unsafe-return', rule, { valid: [ - 'function foo() { return; }', - 'function foo() { return 1; }', - 'function foo() { return ""; }', - 'function foo() { return true; }', + ` +function foo() { + return; +} + `, + ` +function foo() { + return 1; +} + `, + ` +function foo() { + return ''; +} + `, + ` +function foo() { + return true; +} + `, // this actually types as `never[]` - 'function foo() { return []; }', + ` +function foo() { + return []; +} + `, // explicit any generic return type is allowed, if you want to be unsafe like that - 'function foo(): Set { return new Set(); }', + ` +function foo(): Set { + return new Set(); +} + `, // TODO - this should error, but it's hard to detect, as the type references are different - 'function foo(): ReadonlySet { return new Set(); }', - 'function foo(): Set { return new Set([1]); }', + ` +function foo(): ReadonlySet { + return new Set(); +} + `, + ` +function foo(): Set { + return new Set([1]); +} + `, ` type Foo = { prop: T }; - function foo(): Foo { return ({ prop: 1 } as Foo)} + function foo(): Foo { + return { prop: 1 } as Foo; + } `, ` type Foo = { prop: any }; - function foo(): Foo { return { prop: '' } as Foo; } + function foo(): Foo { + return { prop: '' } as Foo; + } `, ], invalid: [ ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo() { return (1 as any); } function foo() { return Object.create(null); } const foo = () => { return (1 as any) }; @@ -79,7 +116,7 @@ const foo = () => Object.create(null); ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo() { return ([] as any[]); } function foo() { return ([] as Array); } function foo() { return ([] as readonly any[]); } @@ -139,7 +176,7 @@ const foo = () => ([] as any[]); ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(): Set { return new Set(); } function foo(): Map { return new Map(); } function foo(): Set { return new Set(); } @@ -182,9 +219,11 @@ function foo(): Set>> { return new Set>>(); } }), { code: ` - type Fn = () => Set; - const foo1: Fn = () => new Set(); - const foo2: Fn = function test() { return new Set() }; +type Fn = () => Set; +const foo1: Fn = () => new Set(); +const foo2: Fn = function test() { + return new Set(); +}; `, errors: [ { @@ -197,7 +236,7 @@ function foo(): Set>> { return new Set>>(); } }, { messageId: 'unsafeReturnAssignment', - line: 4, + line: 5, data: { sender: 'Set', receiver: 'Set', @@ -207,10 +246,12 @@ function foo(): Set>> { return new Set>>(); } }, { code: ` - type Fn = () => Set; - function receiver(arg: Fn) {} - receiver(() => new Set()); - receiver(function test() { return new Set() }); +type Fn = () => Set; +function receiver(arg: Fn) {} +receiver(() => new Set()); +receiver(function test() { + return new Set(); +}); `, errors: [ { @@ -223,7 +264,7 @@ function foo(): Set>> { return new Set>>(); } }, { messageId: 'unsafeReturnAssignment', - line: 5, + line: 6, data: { sender: 'Set', receiver: 'Set', From 482d164d9472fe13e34b20442585fb8b243e1998 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:55:31 -0700 Subject: [PATCH 36/92] chore: no-unsafe-member-access --- .../rules/no-unsafe-member-access.test.ts | 67 +++++++++++++++---- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts index 484d4690c168..bf6ff85c121d 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts @@ -3,6 +3,7 @@ import { RuleTester, batchedSingleLineTests, getFixturesRootDir, + noFormat, } from '../RuleTester'; const ruleTester = new RuleTester({ @@ -15,20 +16,60 @@ const ruleTester = new RuleTester({ ruleTester.run('no-unsafe-member-access', rule, { valid: [ - 'function foo(x: { a: number }, y: any) { x[y++] }', - 'function foo(x: { a: number }) { x.a }', - 'function foo(x?: { a: number }) { x?.a }', - 'function foo(x: { a: number }) { x["a"] }', - 'function foo(x?: { a: number }) { x?.["a"] }', - 'function foo(x: { a: number }, y: string) { x[y] }', - 'function foo(x?: { a: number }, y: string) { x?.[y] }', - 'function foo(x: string[]) { x[1] }', - 'function foo(x?: string[]) { x?.[1++] }', - 'function foo(x?: string[]) { x?.[(1 as any)++] }', + ` +function foo(x: { a: number }, y: any) { + x[y++]; +} + `, + ` +function foo(x: { a: number }) { + x.a; +} + `, + ` +function foo(x?: { a: number }) { + x?.a; +} + `, + ` +function foo(x: { a: number }) { + x['a']; +} + `, + ` +function foo(x?: { a: number }) { + x?.['a']; +} + `, + ` +function foo(x: { a: number }, y: string) { + x[y]; +} + `, + ` +function foo(x?: { a: number }, y: string) { + x?.[y]; +} + `, + ` +function foo(x: string[]) { + x[1]; +} + `, + ` +function foo(x?: string[]) { + x?.[1++]; +} + `, + ` +function foo(x?: string[]) { + x?.[(1 as any)++]; +} + `, ], invalid: [ ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: any) { x.a } function foo(x: any) { x.a.b.c.d.e.f.g } function foo(x: { a: any }) { x.a.b.c.d.e.f.g } @@ -64,7 +105,7 @@ function foo(x: { a: any }) { x.a.b.c.d.e.f.g } ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: any) { x['a'] } function foo(x: any) { x['a']['b']['c'] } `, @@ -90,7 +131,7 @@ function foo(x: any) { x['a']['b']['c'] } ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: { a: number }, y: any) { x[y] } function foo(x?: { a: number }, y: any) { x?.[y] } function foo(x: { a: number }, y: any) { x[y += 1] } From 4afb879c47742bd9b579f516d557b817ecfb22c7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:56:07 -0700 Subject: [PATCH 37/92] chore: no-unsafe-call --- .../tests/rules/no-unsafe-call.test.ts | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-call.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-call.test.ts index bc7b6c7d0860..8adb44b9960f 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-call.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-call.test.ts @@ -3,6 +3,7 @@ import { RuleTester, batchedSingleLineTests, getFixturesRootDir, + noFormat, } from '../RuleTester'; const ruleTester = new RuleTester({ @@ -15,12 +16,24 @@ const ruleTester = new RuleTester({ ruleTester.run('no-unsafe-call', rule, { valid: [ - 'function foo(x: () => void) { x() }', - 'function foo(x?: { a: () => void }) { x?.a() }', - 'function foo(x: { a?: () => void }) { x.a?.() }', - 'new Map()', - 'String.raw`foo`', - 'const x = import("./foo");', + ` +function foo(x: () => void) { + x(); +} + `, + ` +function foo(x?: { a: () => void }) { + x?.a(); +} + `, + ` +function foo(x: { a?: () => void }) { + x.a?.(); +} + `, + 'new Map();', + 'String.raw`foo`;', + "const x = import('./foo');", // https://github.com/typescript-eslint/typescript-eslint/issues/1825 ` let foo: any = 23; @@ -29,7 +42,7 @@ ruleTester.run('no-unsafe-call', rule, { ], invalid: [ ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: any) { x() } function foo(x: any) { x?.() } function foo(x: any) { x.a.b.c.d.e.f.g() } @@ -63,7 +76,7 @@ function foo(x: any) { x.a.b.c.d.e.f.g?.() } ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: { a: any }) { x.a() } function foo(x: { a: any }) { x?.a() } function foo(x: { a: any }) { x.a?.() } @@ -90,7 +103,7 @@ function foo(x: { a: any }) { x.a?.() } ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: any) { new x() } function foo(x: { a: any }) { new x.a() } `, @@ -110,7 +123,7 @@ function foo(x: { a: any }) { new x.a() } ], }), ...batchedSingleLineTests({ - code: ` + code: noFormat` function foo(x: any) { x\`foo\` } function foo(x: { tag: any }) { x.tag\`foo\` } `, From ffbc0e210578140f6e616c83a3dceda2b7ae31a6 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:56:48 -0700 Subject: [PATCH 38/92] chore: no-unnecessary-type-assertion --- .../no-unnecessary-type-assertion.test.ts | 151 ++++++++++-------- 1 file changed, 87 insertions(+), 64 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts index 4d1ae880e840..d6febe39ea54 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts @@ -15,27 +15,34 @@ const ruleTester = new RuleTester({ ruleTester.run('no-unnecessary-type-assertion', rule, { valid: [ 'const foo = 3 as number;', - 'const foo = 3;', + 'const foo = 3;', 'const foo = <3>3;', 'const foo = 3 as 3;', ` -type Tuple = [3, "hi", "bye"]; -const foo = ([3, "hi", "bye"]) as Tuple;`, +type Tuple = [3, 'hi', 'bye']; +const foo = [3, 'hi', 'bye'] as Tuple; + `, ` type PossibleTuple = {}; -const foo = ({}) as PossibleTuple;`, +const foo = {} as PossibleTuple; + `, ` -type PossibleTuple = { hello: "hello" }; -const foo = ({ hello: "hello" }) as PossibleTuple;`, +type PossibleTuple = { hello: 'hello' }; +const foo = { hello: 'hello' } as PossibleTuple; + `, + ` +type PossibleTuple = { 0: 'hello'; 5: 'hello' }; +const foo = { 0: 'hello', 5: 'hello' } as PossibleTuple; + `, ` -type PossibleTuple = { 0: "hello", 5: "hello" }; -const foo = ({ 0: "hello", 5: "hello" }) as PossibleTuple;`, - `let bar: number | undefined = x; - let foo: number = bar!;`, +let bar: number | undefined = x; +let foo: number = bar!; + `, { code: ` type Foo = number; -const foo = (3 + 5) as Foo;`, +const foo = (3 + 5) as Foo; + `, options: [{ typesToIgnore: ['Foo'] }], }, { @@ -43,7 +50,7 @@ const foo = (3 + 5) as Foo;`, options: [{ typesToIgnore: ['any'] }], }, { - code: "((Syntax as any).ArrayExpression = 'foo')", + code: "(Syntax as any).ArrayExpression = 'foo';", options: [{ typesToIgnore: ['any'] }], }, { @@ -53,26 +60,27 @@ const foo = (3 + 5) as Foo;`, { code: ` type Foo = number; -const foo = (3 + 5);`, +const foo = (3 + 5); + `, options: [{ typesToIgnore: ['Foo'] }], }, // https://github.com/typescript-eslint/typescript-eslint/issues/453 // the ol' use-before-assign-is-okay-trust-me assertion ` -let bar: number -bar! + 1 +let bar: number; +bar! + 1; `, ` -let bar: undefined | number -bar! + 1 +let bar: undefined | number; +bar! + 1; `, ` -let bar: number, baz: number -bar! + 1 +let bar: number, baz: number; +bar! + 1; `, ` function foo(bar: T) { - return bar! + return bar!; } `, ` @@ -108,23 +116,29 @@ class Mx { `, // https://github.com/typescript-eslint/typescript-eslint/issues/1199 ` -function testFunction(_param: string | undefined): void { /* noop */ } -const value = 'test' as string | null | undefined -testFunction(value!) +function testFunction(_param: string | undefined): void { + /* noop */ +} +const value = 'test' as string | null | undefined; +testFunction(value!); `, ` -function testFunction(_param: string | null): void { /* noop */ } -const value = 'test' as string | null | undefined -testFunction(value!) +function testFunction(_param: string | null): void { + /* noop */ +} +const value = 'test' as string | null | undefined; +testFunction(value!); `, // https://github.com/typescript-eslint/typescript-eslint/issues/982 { code: ` -declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } } +declare namespace JSX { + interface IntrinsicElements { + div: { key?: string | number }; + } +} -function Test(props: { - id?: null | string | number; -}) { +function Test(props: { id?: null | string | number }) { return
; } `, @@ -138,13 +152,13 @@ const c = [...a, ...b] as const; `, }, { - code: `const a = [1, 2] as const;`, + code: 'const a = [1, 2] as const;', }, { - code: `const a = 'a' as const;`, + code: "const a = 'a' as const;", }, { - code: `const a = { foo: 'foo' } as const`, + code: "const a = { foo: 'foo' } as const;", }, { code: ` @@ -154,13 +168,13 @@ const c = [...a, ...b]; `, }, { - code: `const a = [1, 2];`, + code: 'const a = [1, 2];', }, { - code: `const a = 'a';`, + code: "const a = 'a';", }, { - code: `const a = { foo: 'foo' };`, + code: "const a = { foo: 'foo' };", }, ], @@ -168,7 +182,8 @@ const c = [...a, ...b]; { code: ` const foo = 3; -const bar = foo!;`, +const bar = foo!; + `, errors: [ { messageId: 'unnecessaryAssertion', @@ -179,7 +194,8 @@ const bar = foo!;`, }, { code: ` -const foo = (3 + 5) as number;`, +const foo = (3 + 5) as number; + `, errors: [ { messageId: 'unnecessaryAssertion', @@ -190,7 +206,8 @@ const foo = (3 + 5) as number;`, }, { code: ` -const foo = (3 + 5);`, +const foo = (3 + 5); + `, errors: [ { messageId: 'unnecessaryAssertion', @@ -202,7 +219,8 @@ const foo = (3 + 5);`, { code: ` type Foo = number; -const foo = (3 + 5) as Foo;`, +const foo = (3 + 5) as Foo; + `, errors: [ { messageId: 'unnecessaryAssertion', @@ -214,7 +232,8 @@ const foo = (3 + 5) as Foo;`, { code: ` type Foo = number; -const foo = (3 + 5);`, +const foo = (3 + 5); + `, errors: [ { messageId: 'unnecessaryAssertion', @@ -226,12 +245,12 @@ const foo = (3 + 5);`, // https://github.com/typescript-eslint/typescript-eslint/issues/453 { code: ` -let bar: number = 1 -bar! + 1 +let bar: number = 1; +bar! + 1; `, output: ` -let bar: number = 1 -bar + 1 +let bar: number = 1; +bar + 1; `, errors: [ { @@ -243,12 +262,12 @@ bar + 1 { // definite declaration operator code: ` -let bar!: number -bar! + 1 +let bar!: number; +bar! + 1; `, output: ` -let bar!: number -bar + 1 +let bar!: number; +bar + 1; `, errors: [ { @@ -259,14 +278,14 @@ bar + 1 }, { code: ` -let bar: number | undefined +let bar: number | undefined; bar = 1; -bar! + 1 +bar! + 1; `, output: ` -let bar: number | undefined +let bar: number | undefined; bar = 1; -bar + 1 +bar + 1; `, errors: [ { @@ -278,12 +297,12 @@ bar + 1 { code: ` function foo(bar: T) { - return bar! + return bar!; } `, output: ` function foo(bar: T) { - return bar + return bar; } `, errors: [ @@ -375,27 +394,31 @@ class Mx { // https://github.com/typescript-eslint/typescript-eslint/issues/982 { code: ` -declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } } +declare namespace JSX { + interface IntrinsicElements { + div: { key?: string | number }; + } +} -function Test(props: { - id?: string | number; -}) { +function Test(props: { id?: string | number }) { return
; } `, output: ` -declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } } +declare namespace JSX { + interface IntrinsicElements { + div: { key?: string | number }; + } +} -function Test(props: { - id?: string | number; -}) { +function Test(props: { id?: string | number }) { return
; } `, errors: [ { messageId: 'contextuallyUnnecessary', - line: 7, + line: 9, }, ], filename: 'react.tsx', From e02325489bc60d5d472ac493e6e1895850732543 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:57:55 -0700 Subject: [PATCH 39/92] chore: no-unnecessary-type-arguments --- .../no-unnecessary-type-arguments.test.ts | 314 ++++++++++++------ 1 file changed, 204 insertions(+), 110 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-arguments.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-arguments.test.ts index 11ff8344698e..9c642d5f88fd 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-arguments.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-arguments.test.ts @@ -14,187 +14,281 @@ const ruleTester = new RuleTester({ ruleTester.run('no-unnecessary-type-arguments', rule, { valid: [ - `f<>();`, - `f();`, - `expect().toBe<>();`, - `class Foo extends Bar<> {}`, - `class Foo extends Bar {}`, - `class Foo implements Bar<> {}`, - `class Foo implements Bar {}`, - `function f() { } - f();`, - `function f() { } - f();`, - `declare const f: (() => void) | null; - f?.();`, - `declare const f: (() => void) | null; - f?.();`, - `declare const f: any; - f();`, - `declare const f: any; - f();`, - `declare const f: unknown; - f();`, - `declare const f: unknown; - f();`, - `function g() { } - g();`, - `declare const g: any; - g();`, - `declare const g: unknown; - g();`, - `class C { } - new C();`, - `declare const C: any; - new C();`, - `declare const C: unknown; - new C();`, - `class C { } - class D extends C { }`, - `declare const C: any; - class D extends C { }`, - `declare const C: unknown; - class D extends C { }`, - `interface I { } - class Impl implements I { }`, - `class C { } - class D extends C { }`, - `declare const C: any; - class D extends C { }`, - `declare const C: unknown; - class D extends C { }`, - `let a: A`, - `class Foo {} - const foo = new Foo();`, - `type Foo = import('foo').Foo;`, - `class Bar {} - class Foo extends Bar {}`, - `interface Bar {} - class Foo implements Bar {}`, - `class Bar {} - class Foo extends Bar {}`, - `interface Bar {} - class Foo implements Bar {}`, - `import { F } from './missing'; - function bar() {} - bar>()`, + 'f<>();', + 'f();', + 'expect().toBe<>();', + 'class Foo extends Bar<> {}', + 'class Foo extends Bar {}', + 'class Foo implements Bar<> {}', + 'class Foo implements Bar {}', + ` +function f() {} +f(); + `, + ` +function f() {} +f(); + `, + ` +declare const f: (() => void) | null; +f?.(); + `, + ` +declare const f: (() => void) | null; +f?.(); + `, + ` +declare const f: any; +f(); + `, + ` +declare const f: any; +f(); + `, + ` +declare const f: unknown; +f(); + `, + ` +declare const f: unknown; +f(); + `, + ` +function g() {} +g(); + `, + ` +declare const g: any; +g(); + `, + ` +declare const g: unknown; +g(); + `, + ` +class C {} +new C(); + `, + ` +declare const C: any; +new C(); + `, + ` +declare const C: unknown; +new C(); + `, + ` +class C {} +class D extends C {} + `, + ` +declare const C: any; +class D extends C {} + `, + ` +declare const C: unknown; +class D extends C {} + `, + ` +interface I {} +class Impl implements I {} + `, + ` +class C {} +class D extends C {} + `, + ` +declare const C: any; +class D extends C {} + `, + ` +declare const C: unknown; +class D extends C {} + `, + 'let a: A;', + ` +class Foo {} +const foo = new Foo(); + `, + "type Foo = import('foo').Foo;", + ` +class Bar {} +class Foo extends Bar {} + `, + ` +interface Bar {} +class Foo implements Bar {} + `, + ` +class Bar {} +class Foo extends Bar {} + `, + ` +interface Bar {} +class Foo implements Bar {} + `, + ` +import { F } from './missing'; +function bar() {} +bar>(); + `, ], invalid: [ { - code: `function f() { } - f();`, + code: ` +function f() {} +f(); + `, errors: [ { - column: 11, + column: 3, messageId: 'unnecessaryTypeParameter', }, ], - output: `function f() { } - f();`, + output: ` +function f() {} +f(); + `, }, { - code: `function g() { } - g();`, + code: ` +function g() {} +g(); + `, errors: [ { - column: 19, + column: 11, messageId: 'unnecessaryTypeParameter', }, ], - output: `function g() { } - g();`, + output: ` +function g() {} +g(); + `, }, { - code: `class C { } - function h(c: C) { }`, + code: ` +class C {} +function h(c: C) {} + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `class C { } - function h(c: C) { }`, + output: ` +class C {} +function h(c: C) {} + `, }, { - code: `class C { } - new C();`, + code: ` +class C {} +new C(); + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `class C { } - new C();`, + output: ` +class C {} +new C(); + `, }, { - code: `class C { } - class D extends C { }`, + code: ` +class C {} +class D extends C {} + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `class C { } - class D extends C { }`, + output: ` +class C {} +class D extends C {} + `, }, { - code: `interface I { } - class Impl implements I { }`, + code: ` +interface I {} +class Impl implements I {} + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `interface I { } - class Impl implements I { }`, + output: ` +interface I {} +class Impl implements I {} + `, }, { - code: `class Foo {} - const foo = new Foo();`, + code: ` +class Foo {} +const foo = new Foo(); + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `class Foo {} - const foo = new Foo();`, + output: ` +class Foo {} +const foo = new Foo(); + `, }, { - code: `interface Bar {} - class Foo implements Bar {}`, + code: ` +interface Bar {} +class Foo implements Bar {} + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `interface Bar {} - class Foo implements Bar {}`, + output: ` +interface Bar {} +class Foo implements Bar {} + `, }, { - code: `class Bar {} - class Foo extends Bar {}`, + code: ` +class Bar {} +class Foo extends Bar {} + `, errors: [ { messageId: 'unnecessaryTypeParameter', }, ], - output: `class Bar {} - class Foo extends Bar {}`, + output: ` +class Bar {} +class Foo extends Bar {} + `, }, { - code: `import { F } from './missing'; - function bar>() {} - bar>()`, + code: ` +import { F } from './missing'; +function bar>() {} +bar>(); + `, errors: [ { - line: 3, - column: 13, + line: 4, + column: 5, messageId: 'unnecessaryTypeParameter', }, ], - output: `import { F } from './missing'; - function bar>() {} - bar()`, + output: ` +import { F } from './missing'; +function bar>() {} +bar(); + `, }, ], }); From bd22cb50fec12746ca24537af3a452165643b452 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 22:58:19 -0700 Subject: [PATCH 40/92] chore: no-unnecessary-qualifier --- .../rules/no-unnecessary-qualifier.test.ts | 86 ++++++++++++------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-qualifier.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-qualifier.test.ts index 68e0e258383d..a03cdc7d7479 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-qualifier.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-qualifier.test.ts @@ -24,29 +24,33 @@ namespace X { namespace Y { export const x: X.T = 3; -}`, +} + `, ` enum A { - X, - Y + X, + Y, } enum B { - Z = A.X -}`, + Z = A.X, +} + `, ` namespace X { - export type T = number; - namespace Y { - type T = string; - const x: X.T = 0; - } -}`, - `const x: A.B = 3;`, + export type T = number; + namespace Y { + type T = string; + const x: X.T = 0; + } +} + `, + 'const x: A.B = 3;', ` namespace X { const z = X.y; -}`, +} + `, ], invalid: [ @@ -55,7 +59,8 @@ namespace X { namespace A { export type B = number; const x: A.B = 3; -}`, +} + `, errors: [ { messageId, @@ -66,14 +71,16 @@ namespace A { namespace A { export type B = number; const x: B = 3; -}`, +} + `, }, { code: ` namespace A { export const x = 3; export const y = A.x; -}`, +} + `, errors: [ { messageId, @@ -84,7 +91,8 @@ namespace A { namespace A { export const x = 3; export const y = x; -}`, +} + `, }, { code: ` @@ -93,7 +101,8 @@ namespace A { export namespace B { const x: A.T = 3; } -}`, +} + `, errors: [ { messageId, @@ -106,7 +115,8 @@ namespace A { export namespace B { const x: T = 3; } -}`, +} + `, }, { code: ` @@ -115,7 +125,8 @@ namespace A { export type T = number; const x: A.B.T = 3; } -}`, +} + `, errors: [ { messageId, @@ -128,7 +139,8 @@ namespace A { export type T = number; const x: T = 3; } -}`, +} + `, }, { code: ` @@ -137,7 +149,8 @@ namespace A { export const x = 3; const y = A.B.x; } -}`, +} + `, errors: [ { messageId, @@ -150,14 +163,16 @@ namespace A { export const x = 3; const y = x; } -}`, +} + `, }, { code: ` enum A { B, - C = A.B -}`, + C = A.B, +} + `, errors: [ { messageId, @@ -167,17 +182,19 @@ enum A { output: ` enum A { B, - C = B -}`, + C = B, +} + `, }, { code: ` namespace Foo { export enum A { B, - C = Foo.A.B + C = Foo.A.B, } -}`, +} + `, errors: [ { messageId, @@ -188,16 +205,18 @@ namespace Foo { namespace Foo { export enum A { B, - C = B + C = B, } -}`, +} + `, }, { code: ` import * as Foo from './foo'; declare module './foo' { const x: Foo.T = 3; -}`, +} + `, errors: [ { messageId, @@ -208,7 +227,8 @@ declare module './foo' { import * as Foo from './foo'; declare module './foo' { const x: T = 3; -}`, +} + `, }, ], }); From 0bb96bdbadcde32be29569af64f1a9c14d2bc85e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:06:52 -0700 Subject: [PATCH 41/92] chore: no-unnecessary-conditionals --- .../rules/no-unnecessary-condition.test.ts | 314 ++++++++++-------- 1 file changed, 180 insertions(+), 134 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index f3adbc3a8495..42f9e288b234 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -6,7 +6,7 @@ import rule, { Options, MessageId, } from '../../src/rules/no-unnecessary-condition'; -import { RuleTester, getFixturesRootDir } from '../RuleTester'; +import { RuleTester, getFixturesRootDir, noFormat } from '../RuleTester'; const rootPath = getFixturesRootDir(); @@ -49,10 +49,14 @@ declare const b1: boolean; declare const b2: boolean; const t1 = b1 && b2; const t2 = b1 || b2; -if(b1 && b2) {} -while(b1 && b2) {} -for (let i = 0; (b1 && b2); i++) { break; } -const t1 = (b1 && b2) ? 'yes' : 'no'`, +if (b1 && b2) { +} +while (b1 && b2) {} +for (let i = 0; b1 && b2; i++) { + break; +} +const t1 = b1 && b2 ? 'yes' : 'no'; + `, necessaryConditionTest('false | 5'), // Truthy literal and falsy literal necessaryConditionTest('boolean | "foo"'), // boolean and truthy literal necessaryConditionTest('0 | boolean'), // boolean and falsy literal @@ -69,56 +73,60 @@ const t1 = (b1 && b2) ? 'yes' : 'no'`, // Generic type params ` function test(t: T) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, ` // Naked type param function test(t: T) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, ` // Naked type param in union function test(t: T | []) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, // Boolean expressions ` function test(a: string) { - return a === "a" -}`, + return a === 'a'; +} + `, /** * Predicate functions **/ // valid, with the flag off ` -[1,3,5].filter(() => true); -[1,2,3].find(() => false); +[1, 3, 5].filter(() => true); +[1, 2, 3].find(() => false); function truthy() { return []; } function falsy() {} -[1,3,5].filter(truthy); -[1,2,3].find(falsy); -`, +[1, 3, 5].filter(truthy); +[1, 2, 3].find(falsy); + `, { options: [{ checkArrayPredicates: true }], code: ` // with literal arrow function -[0,1,2].filter(x => x); +[0, 1, 2].filter(x => x); // filter with named function function length(x: string) { return x.length; } -["a", "b", ""].filter(length); +['a', 'b', ''].filter(length); // with non-literal array function nonEmptyStrings(x: string[]) { return x.filter(length); } -`, + `, }, // Ignores non-array methods of the same name { @@ -130,131 +138,145 @@ const notArray = { }; notArray.filter(() => true); notArray.find(() => true); -`, + `, }, // Nullish coalescing operator ` function test(a: string | null) { - return a ?? "default"; -}`, + return a ?? 'default'; +} + `, ` function test(a: string | undefined) { - return a ?? "default"; -}`, + return a ?? 'default'; +} + `, ` function test(a: string | null | undefined) { - return a ?? "default"; -}`, + return a ?? 'default'; +} + `, ` function test(a: unknown) { - return a ?? "default"; -}`, + return a ?? 'default'; +} + `, // Indexing cases ` declare const arr: object[]; -if(arr[42]) {} // looks unnecessary from the types, but isn't +if (arr[42]) { +} // looks unnecessary from the types, but isn't const tuple = [{}] as [object]; declare const n: number; -if(tuple[n]) {} -`, +if (tuple[n]) { +} + `, // Optional-chaining indexing ` -declare const arr: Array<{value: string} & (() => void)>; -if(arr[42]?.value) {} +declare const arr: Array<{ value: string } & (() => void)>; +if (arr[42]?.value) { +} arr[41]?.(); // An array access can "infect" deeper into the chain -declare const arr2: Array<{x: {y: {z: object}}}>; +declare const arr2: Array<{ x: { y: { z: object } } }>; arr2[42]?.x?.y?.z; -const tuple = ["foo"] as const; +const tuple = ['foo'] as const; declare const n: number; tuple[n]?.toUpperCase(); `, - `if(arr?.[42]) {}`, + ` +if (arr?.[42]) { +} + `, ` declare const returnsArr: undefined | (() => string[]); -if(returnsArr?.()[42]) {} -returnsArr?.()[42]?.toUpperCase()`, +if (returnsArr?.()[42]) { +} +returnsArr?.()[42]?.toUpperCase(); + `, // nullish + array index ` declare const arr: string[][]; arr[x] ?? []; -`, + `, // Supports ignoring the RHS { code: ` declare const b1: boolean; declare const b2: true; -if(b1 && b2) {}`, +if (b1 && b2) { +} + `, options: [{ ignoreRhs: true }], }, { code: ` -while(true) {} -for (;true;) {} -do {} while(true) +while (true) {} +for (; true; ) {} +do {} while (true); `, options: [{ allowConstantLoopConditions: true }], }, ` let foo: undefined | { bar: true }; foo?.bar; -`, + `, ` let foo: null | { bar: true }; foo?.bar; -`, + `, ` let foo: undefined; foo?.bar; -`, + `, ` let foo: undefined; foo?.bar.baz; -`, + `, ` let foo: null; foo?.bar; -`, + `, ` let anyValue: any; anyValue?.foo; -`, + `, ` let unknownValue: unknown; unknownValue?.foo; -`, + `, ` let foo: undefined | (() => {}); foo?.(); -`, + `, ` let foo: null | (() => {}); foo?.(); -`, + `, ` let foo: undefined; foo?.(); -`, + `, ` let foo: undefined; foo?.().bar; -`, + `, ` let foo: null; foo?.(); -`, + `, ` let anyValue: any; anyValue?.(); -`, + `, ` let unknownValue: unknown; unknownValue?.(); -`, + `, 'const foo = [1, 2, 3][0];', ], invalid: [ @@ -265,17 +287,21 @@ const b1 = true; declare const b2: boolean; const t1 = b1 && b2; const t2 = b1 || b2; -if(b1 && b2) {} -while(b1 && b2) {} -for (let i = 0; (b1 && b2); i++) { break; } -const t1 = (b1 && b2) ? 'yes' : 'no'`, +if (b1 && b2) { +} +while (b1 && b2) {} +for (let i = 0; b1 && b2; i++) { + break; +} +const t1 = b1 && b2 ? 'yes' : 'no'; + `, errors: [ ruleError(4, 12, 'alwaysTruthy'), ruleError(5, 12, 'alwaysTruthy'), - ruleError(6, 4, 'alwaysTruthy'), - ruleError(7, 7, 'alwaysTruthy'), - ruleError(8, 18, 'alwaysTruthy'), - ruleError(9, 13, 'alwaysTruthy'), + ruleError(6, 5, 'alwaysTruthy'), + ruleError(8, 8, 'alwaysTruthy'), + ruleError(9, 17, 'alwaysTruthy'), + ruleError(12, 12, 'alwaysTruthy'), ], }, // Ensure that it's complaining about the right things @@ -292,50 +318,56 @@ const t1 = (b1 && b2) ? 'yes' : 'no'`, { code: ` function test(t: T) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, errors: [ruleError(3, 10, 'alwaysTruthy')], }, { code: ` function test(t: T) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, errors: [ruleError(3, 10, 'alwaysFalsy')], }, { code: ` function test(t: T) { - return t ? 'yes' : 'no' -}`, + return t ? 'yes' : 'no'; +} + `, errors: [ruleError(3, 10, 'alwaysTruthy')], }, // Boolean expressions { code: ` -function test(a: "a") { - return a === "a" -}`, +function test(a: 'a') { + return a === 'a'; +} + `, errors: [ruleError(3, 10, 'literalBooleanExpression')], }, { code: ` const y = 1; -if (y === 0) {} -`, +if (y === 0) { +} + `, errors: [ruleError(3, 5, 'literalBooleanExpression')], }, { code: ` enum Foo { a = 1, - b = 2 + b = 2, } const x = Foo.a; -if (x === Foo.a) {} -`, +if (x === Foo.a) { +} + `, errors: [ruleError(8, 5, 'literalBooleanExpression')], }, // Nullish coalescing operator @@ -343,28 +375,32 @@ if (x === Foo.a) {} code: ` function test(a: string) { return a ?? 'default'; -}`, +} + `, errors: [ruleError(3, 10, 'neverNullish')], }, { code: ` function test(a: string | false) { return a ?? 'default'; -}`, +} + `, errors: [ruleError(3, 10, 'neverNullish')], }, { code: ` function test(a: null) { return a ?? 'default'; -}`, +} + `, errors: [ruleError(3, 10, 'alwaysNullish')], }, { code: ` function test(a: never) { return a ?? 'default'; -}`, +} + `, errors: [ruleError(3, 10, 'never')], }, @@ -372,8 +408,10 @@ function test(a: never) { { options: [{ checkArrayPredicates: true }], code: ` -[1,3,5].filter(() => true); -[1,2,3].find(() => { return false; }); +[1, 3, 5].filter(() => true); +[1, 2, 3].find(() => { + return false; +}); // with non-literal array function nothing(x: string[]) { @@ -387,13 +425,13 @@ function nothing2(x: readonly string[]) { function nothing3(x: [string, string]) { return x.filter(() => false); } -`, + `, errors: [ - ruleError(2, 22, 'alwaysTruthy'), - ruleError(3, 29, 'alwaysFalsy'), - ruleError(7, 25, 'alwaysFalsy'), - ruleError(11, 25, 'alwaysFalsy'), - ruleError(15, 25, 'alwaysFalsy'), + ruleError(2, 24, 'alwaysTruthy'), + ruleError(4, 10, 'alwaysFalsy'), + ruleError(9, 25, 'alwaysFalsy'), + ruleError(13, 25, 'alwaysFalsy'), + ruleError(17, 25, 'alwaysFalsy'), ], }, // Indexing cases @@ -402,30 +440,34 @@ function nothing3(x: [string, string]) { // the potential for undefined in its types code: ` declare const dict: Record; -if(dict["mightNotExist"]) {} -`, - errors: [ruleError(3, 4, 'alwaysTruthy')], +if (dict['mightNotExist']) { +} + `, + errors: [ruleError(3, 5, 'alwaysTruthy')], }, { // Should still check tuples when accessed with literal numbers, since they don't have // unsound index signatures code: ` -const x = [{}] as [{foo: string}]; -if(x[0]) {} -if(x[0]?.foo) {} -`, +const x = [{}] as [{ foo: string }]; +if (x[0]) { +} +if (x[0]?.foo) { +} + `, errors: [ - ruleError(3, 4, 'alwaysTruthy'), - ruleError(4, 8, 'neverOptionalChain'), + ruleError(3, 5, 'alwaysTruthy'), + ruleError(5, 9, 'neverOptionalChain'), ], }, { // Shouldn't mistake this for an array indexing case code: ` declare const arr: object[]; -if(arr.filter) {} -`, - errors: [ruleError(3, 4, 'alwaysTruthy')], +if (arr.filter) { +} + `, + errors: [ruleError(3, 5, 'alwaysTruthy')], }, { options: [{ checkArrayPredicates: true }], @@ -434,12 +476,12 @@ function truthy() { return []; } function falsy() {} -[1,3,5].filter(truthy); -[1,2,3].find(falsy); -`, +[1, 3, 5].filter(truthy); +[1, 2, 3].find(falsy); + `, errors: [ - ruleError(6, 16, 'alwaysTruthyFunc'), - ruleError(7, 14, 'alwaysFalsyFunc'), + ruleError(6, 18, 'alwaysTruthyFunc'), + ruleError(7, 16, 'alwaysFalsyFunc'), ], }, // Supports generics @@ -464,34 +506,38 @@ const b1 = true; const b2 = false; const t1 = b1 && b2; const t2 = b1 || b2; -if(b1 && b2) {} -while(b1 && b2) {} -for (let i = 0; (b1 && b2); i++) { break; } -const t1 = (b1 && b2) ? 'yes' : 'no'`, +if (b1 && b2) { +} +while (b1 && b2) {} +for (let i = 0; b1 && b2; i++) { + break; +} +const t1 = b1 && b2 ? 'yes' : 'no'; + `, errors: [ ruleError(4, 12, 'alwaysTruthy'), ruleError(5, 12, 'alwaysTruthy'), - ruleError(6, 4, 'alwaysTruthy'), - ruleError(7, 7, 'alwaysTruthy'), - ruleError(8, 18, 'alwaysTruthy'), - ruleError(9, 13, 'alwaysTruthy'), + ruleError(6, 5, 'alwaysTruthy'), + ruleError(8, 8, 'alwaysTruthy'), + ruleError(9, 17, 'alwaysTruthy'), + ruleError(12, 12, 'alwaysTruthy'), ], }, { code: ` -while(true) {} -for (;true;) {} -do {} while(true) +while (true) {} +for (; true; ) {} +do {} while (true); `, options: [{ allowConstantLoopConditions: false }], errors: [ - ruleError(2, 7, 'alwaysTruthy'), - ruleError(3, 7, 'alwaysTruthy'), - ruleError(4, 13, 'alwaysTruthy'), + ruleError(2, 8, 'alwaysTruthy'), + ruleError(3, 8, 'alwaysTruthy'), + ruleError(4, 14, 'alwaysTruthy'), ], }, { - code: ` + code: noFormat` let foo = { bar: true }; foo?.bar; foo ?. bar; @@ -499,8 +545,8 @@ foo ?. bar; foo ?. bar; -`, - output: ` + `, + output: noFormat` let foo = { bar: true }; foo.bar; foo . bar; @@ -508,7 +554,7 @@ foo . bar; foo . bar; -`, + `, errors: [ { messageId: 'neverOptionalChain', @@ -541,7 +587,7 @@ foo ], }, { - code: ` + code: noFormat` let foo = () => {}; foo?.(); foo ?. (); @@ -549,7 +595,7 @@ foo ?. (); foo ?. (); -`, + `, output: ` let foo = () => {}; foo(); @@ -558,7 +604,7 @@ foo${' '} (); foo (); -`, + `, errors: [ { messageId: 'neverOptionalChain', @@ -591,7 +637,7 @@ foo ], }, { - code: ` + code: noFormat` let foo = () => {}; foo?.(bar); foo ?. (bar); @@ -599,7 +645,7 @@ foo ?. (bar); foo ?. (bar); -`, + `, output: ` let foo = () => {}; foo(bar); @@ -608,7 +654,7 @@ foo${' '} (bar); foo (bar); -`, + `, errors: [ { messageId: 'neverOptionalChain', From 2245c5ee887a16c6d0047d7806f0fb7b7cd56eea Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:07:18 -0700 Subject: [PATCH 42/92] chore: no-unnecessary-boolean-literal-compare --- ...nnecessary-boolean-literal-compare.test.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts index 1c6cfebeeccc..6480d5489062 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.ts @@ -11,7 +11,7 @@ const ruleTester = new RuleTester({ }, }); -ruleTester.run('boolean-literal-compare', rule, { +ruleTester.run('no-unnecessary-boolean-literal-compare', rule, { valid: [ ` declare const varAny: any; @@ -49,33 +49,34 @@ ruleTester.run('boolean-literal-compare', rule, { declare const varBooleanOrUndefined: boolean | undefined; varBooleanOrUndefined === true; `, - `'false' === true;`, - `'true' === false;`, + "'false' === true;", + "'true' === false;", ], invalid: [ { - code: `true === true`, + code: 'true === true;', errors: [ { messageId: 'direct', }, ], - output: `true`, + output: 'true;', }, { - code: `false !== true`, + code: 'false !== true;', errors: [ { messageId: 'negated', }, ], - output: `!false`, + output: '!false;', }, { code: ` declare const varBoolean: boolean; - if (varBoolean !== false) { } + if (varBoolean !== false) { + } `, errors: [ { @@ -84,13 +85,15 @@ ruleTester.run('boolean-literal-compare', rule, { ], output: ` declare const varBoolean: boolean; - if (varBoolean) { } + if (varBoolean) { + } `, }, { code: ` declare const varTrue: true; - if (varTrue !== true) { } + if (varTrue !== true) { + } `, errors: [ { @@ -99,7 +102,8 @@ ruleTester.run('boolean-literal-compare', rule, { ], output: ` declare const varTrue: true; - if (!varTrue) { } + if (!varTrue) { + } `, }, ], From 5c8781be7d3e6d03bb3b648c63e8cdac6015b800 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:16:22 -0700 Subject: [PATCH 43/92] chore: no-type-alias --- .../tests/rules/no-type-alias.test.ts | 384 ++++++++++-------- 1 file changed, 205 insertions(+), 179 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index 9d6b0cd0f97f..29c4f85b2f4e 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -64,11 +64,11 @@ ruleTester.run('no-type-alias', rule, { options: [{ allowAliases: 'in-intersections' }], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'always' }], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'in-unions-and-intersections' }], }, { @@ -124,11 +124,11 @@ ruleTester.run('no-type-alias', rule, { options: [{ allowAliases: 'in-intersections' }], }, { - code: 'type Foo = 1 | 2 & 3;', + code: 'type Foo = 1 | (2 & 3);', options: [{ allowAliases: 'always' }], }, { - code: 'type Foo = 1 | 2 & 3;', + code: 'type Foo = 1 | (2 & 3);', options: [{ allowAliases: 'in-unions-and-intersections' }], }, { @@ -163,42 +163,42 @@ ruleTester.run('no-type-alias', rule, { code: ` interface Bar {} type Foo = Bar | string; - `, + `, options: [{ allowAliases: 'always' }], }, { code: ` interface Bar {} type Foo = Bar | string; - `, + `, options: [{ allowAliases: 'in-unions-and-intersections' }], }, { code: ` interface Bar {} type Foo = Bar | string; - `, + `, options: [{ allowAliases: 'in-unions' }], }, { code: ` interface Bar {} type Foo = Bar & string; - `, + `, options: [{ allowAliases: 'always' }], }, { code: ` interface Bar {} type Foo = Bar & string; - `, + `, options: [{ allowAliases: 'in-unions-and-intersections' }], }, { code: ` interface Bar {} type Foo = Bar & string; - `, + `, options: [{ allowAliases: 'in-intersections' }], }, { @@ -292,74 +292,91 @@ type Foo = Bar & string; { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, options: [{ allowMappedTypes: 'always' }], }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'always' }], }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-unions-and-intersections' }], }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-unions' }], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'always' }], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-unions-and-intersections' }], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-intersections' }], }, { - code: - 'export type ClassValue = string | number | ClassDictionary | ClassArray | undefined | null | false;', + code: ` +export type ClassValue = + | string + | number + | ClassDictionary + | ClassArray + | undefined + | null + | false; + `, options: [ { allowAliases: 'in-unions-and-intersections', @@ -378,7 +395,7 @@ type Foo = { options: [{ allowAliases: 'in-unions' }], }, { - code: 'type Foo = keyof [string]', + code: 'type Foo = keyof [string];', options: [{ allowTupleTypes: 'always' }], }, { @@ -395,7 +412,7 @@ type Foo = { }, { code: - 'type Foo = [string] & [number, number] | [number, number, number];', + 'type Foo = ([string] & [number, number]) | [number, number, number];', options: [{ allowTupleTypes: 'in-unions-and-intersections' }], }, { @@ -416,7 +433,7 @@ type Foo = { }, { code: - 'type Foo = [string] & [number, number] | readonly [number, number, number];', + 'type Foo = ([string] & [number, number]) | readonly [number, number, number];', options: [{ allowTupleTypes: 'in-unions-and-intersections' }], }, { @@ -437,7 +454,7 @@ type Foo = { }, { code: - 'type Foo = [string] & [number, number] | keyof [number, number, number];', + 'type Foo = ([string] & [number, number]) | keyof [number, number, number];', options: [{ allowTupleTypes: 'in-unions-and-intersections' }], }, { @@ -451,7 +468,7 @@ type Foo = { ], invalid: [ { - code: "type Foo = 'a'", + code: "type Foo = 'a';", errors: [ { messageId: 'noTypeAlias', @@ -464,7 +481,7 @@ type Foo = { ], }, { - code: "type Foo = 'a'", + code: "type Foo = 'a';", options: [{ allowAliases: 'never' }], errors: [ { @@ -1154,7 +1171,7 @@ type Foo = { ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", errors: [ { messageId: 'noCompositionAlias', @@ -1172,7 +1189,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1181,12 +1198,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowLiterals: 'in-intersections' }], errors: [ { @@ -1205,7 +1222,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1214,12 +1231,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'never' }], errors: [ { @@ -1238,7 +1255,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1247,12 +1264,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'never', allowLiterals: 'in-intersections' }], errors: [ { @@ -1271,7 +1288,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1280,12 +1297,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'in-unions' }], errors: [ { @@ -1295,7 +1312,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1304,12 +1321,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [ { allowAliases: 'in-unions', @@ -1324,7 +1341,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 18, + column: 19, }, { messageId: 'noCompositionAlias', @@ -1333,12 +1350,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 24, + column: 25, }, ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [{ allowAliases: 'in-intersections' }], errors: [ { @@ -1353,7 +1370,7 @@ type Foo = { ], }, { - code: "type Foo = 'a' | 'b' & 'c';", + code: "type Foo = 'a' | ('b' & 'c');", options: [ { allowAliases: 'in-intersections', @@ -1792,7 +1809,7 @@ type Foo = { ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', errors: [ { messageId: 'noCompositionAlias', @@ -1810,7 +1827,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1819,12 +1836,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowLiterals: 'in-unions' }], errors: [ { @@ -1843,7 +1860,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1852,12 +1869,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowAliases: 'never' }], errors: [ { @@ -1876,7 +1893,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1885,12 +1902,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowAliases: 'never', allowLiterals: 'in-unions' }], errors: [ { @@ -1909,7 +1926,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1918,12 +1935,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowAliases: 'in-unions' }], errors: [ { @@ -1933,7 +1950,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1942,12 +1959,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowAliases: 'in-unions', allowLiterals: 'in-unions' }], errors: [ { @@ -1957,7 +1974,7 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -1966,12 +1983,12 @@ type Foo = { compositionType: 'intersection', }, line: 1, - column: 32, + column: 33, }, ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [{ allowAliases: 'in-intersections' }], errors: [ { @@ -1986,7 +2003,7 @@ type Foo = { ], }, { - code: 'type Foo = string | string[] & number;', + code: 'type Foo = string | (string[] & number);', options: [ { allowAliases: 'in-intersections', @@ -2009,7 +2026,7 @@ type Foo = { code: ` interface Bar {} type Foo = Bar; - `, + `, errors: [ { messageId: 'noTypeAlias', @@ -2025,7 +2042,7 @@ type Foo = Bar; code: ` interface Bar {} type Foo = Bar; - `, + `, options: [{ allowAliases: 'never' }], errors: [ { @@ -2042,7 +2059,7 @@ type Foo = Bar; code: ` interface Bar {} type Foo = Bar; - `, + `, options: [{ allowAliases: 'in-unions' }], errors: [ { @@ -2059,7 +2076,7 @@ type Foo = Bar; code: ` interface Bar {} type Foo = Bar; - `, + `, options: [{ allowAliases: 'in-intersections' }], errors: [ { @@ -2076,7 +2093,7 @@ type Foo = Bar; code: ` interface Bar {} type Foo = Bar; - `, + `, options: [{ allowAliases: 'in-unions-and-intersections' }], errors: [ { @@ -2093,7 +2110,7 @@ type Foo = Bar; code: ` interface Bar {} type Foo = Bar | {}; - `, + `, errors: [ { messageId: 'noCompositionAlias', @@ -2119,7 +2136,7 @@ type Foo = Bar | {}; code: ` interface Bar {} type Foo = Bar | {}; - `, + `, options: [{ allowAliases: 'never' }], errors: [ { @@ -2146,7 +2163,7 @@ type Foo = Bar | {}; code: ` interface Bar {} type Foo = Bar | {}; - `, + `, options: [{ allowAliases: 'in-unions' }], errors: [ { @@ -2164,7 +2181,7 @@ type Foo = Bar | {}; code: ` interface Bar {} type Foo = Bar | {}; - `, + `, options: [{ allowAliases: 'in-intersections' }], errors: [ { @@ -2191,7 +2208,7 @@ type Foo = Bar | {}; code: ` interface Bar {} type Foo = Bar | {}; - `, + `, options: [{ allowAliases: 'in-unions-and-intersections' }], errors: [ { @@ -2209,7 +2226,7 @@ type Foo = Bar | {}; code: ` interface Bar {} type Foo = Bar & {}; - `, + `, errors: [ { messageId: 'noCompositionAlias', @@ -2235,7 +2252,7 @@ type Foo = Bar & {}; code: ` interface Bar {} type Foo = Bar & {}; - `, + `, options: [{ allowAliases: 'never' }], errors: [ { @@ -2262,7 +2279,7 @@ type Foo = Bar & {}; code: ` interface Bar {} type Foo = Bar & {}; - `, + `, options: [{ allowAliases: 'in-unions' }], errors: [ { @@ -2289,7 +2306,7 @@ type Foo = Bar & {}; code: ` interface Bar {} type Foo = Bar & {}; - `, + `, options: [{ allowAliases: 'in-intersections' }], errors: [ { @@ -2307,7 +2324,7 @@ type Foo = Bar & {}; code: ` interface Bar {} type Foo = Bar & {}; - `, + `, options: [{ allowAliases: 'in-unions-and-intersections' }], errors: [ { @@ -2645,7 +2662,7 @@ type Foo = Bar & {}; ], }, { - code: "type Foo = string & {} | 'a' | 1;", + code: "type Foo = (string & {}) | 'a' | 1;", options: [{ allowAliases: 'in-unions', allowLiterals: 'in-unions' }], errors: [ { @@ -2655,7 +2672,7 @@ type Foo = Bar & {}; compositionType: 'intersection', }, line: 1, - column: 12, + column: 13, }, { messageId: 'noCompositionAlias', @@ -2664,12 +2681,12 @@ type Foo = Bar & {}; compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, ], }, { - code: "type Foo = string & {} | 'a' | 1;", + code: "type Foo = (string & {}) | 'a' | 1;", options: [ { allowAliases: 'in-intersections', @@ -2684,7 +2701,7 @@ type Foo = Bar & {}; compositionType: 'intersection', }, line: 1, - column: 21, + column: 22, }, { messageId: 'noCompositionAlias', @@ -2693,7 +2710,7 @@ type Foo = Bar & {}; compositionType: 'union', }, line: 1, - column: 26, + column: 28, }, { messageId: 'noCompositionAlias', @@ -2702,16 +2719,16 @@ type Foo = Bar & {}; compositionType: 'union', }, line: 1, - column: 32, + column: 34, }, ], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, errors: [ { messageId: 'noTypeAlias', @@ -2726,9 +2743,9 @@ type Foo = { { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, options: [{ allowMappedTypes: 'never' }], errors: [ { @@ -2744,9 +2761,9 @@ type Foo = { { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, options: [{ allowMappedTypes: 'in-unions' }], errors: [ { @@ -2762,9 +2779,9 @@ type Foo = { { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, options: [{ allowMappedTypes: 'in-intersections' }], errors: [ { @@ -2780,9 +2797,9 @@ type Foo = { { code: ` type Foo = { - readonly [P in keyof T] : T[P] + readonly [P in keyof T]: T[P]; }; - `, + `, options: [{ allowMappedTypes: 'in-unions-and-intersections' }], errors: [ { @@ -2797,12 +2814,14 @@ type Foo = { }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, errors: [ { messageId: 'noCompositionAlias', @@ -2810,8 +2829,8 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 2, - column: 15, + line: 3, + column: 5, }, { messageId: 'noCompositionAlias', @@ -2819,19 +2838,21 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 4, + line: 6, column: 5, }, ], }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'never' }], errors: [ { @@ -2840,8 +2861,8 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 2, - column: 15, + line: 3, + column: 5, }, { messageId: 'noCompositionAlias', @@ -2849,19 +2870,21 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 4, + line: 6, column: 5, }, ], }, { code: ` -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof T] : T[P] -}; - `, +type Foo = + | { + readonly [P in keyof T]: T[P]; + } + | { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-intersections' }], errors: [ { @@ -2870,8 +2893,8 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 2, - column: 15, + line: 3, + column: 5, }, { messageId: 'noCompositionAlias', @@ -2879,7 +2902,7 @@ type Foo = { typeName: 'Mapped types', compositionType: 'union', }, - line: 4, + line: 6, column: 5, }, ], @@ -2887,11 +2910,12 @@ type Foo = { { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, errors: [ { messageId: 'noCompositionAlias', @@ -2908,19 +2932,20 @@ type Foo = { typeName: 'Mapped types', compositionType: 'intersection', }, - line: 4, - column: 5, + line: 5, + column: 3, }, ], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'never' }], errors: [ { @@ -2938,19 +2963,20 @@ type Foo = { typeName: 'Mapped types', compositionType: 'intersection', }, - line: 4, - column: 5, + line: 5, + column: 3, }, ], }, { code: ` type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof T] : T[P] -}; - `, + readonly [P in keyof T]: T[P]; +} & + { + readonly [P in keyof T]: T[P]; + }; + `, options: [{ allowMappedTypes: 'in-unions' }], errors: [ { @@ -2968,8 +2994,8 @@ type Foo = { typeName: 'Mapped types', compositionType: 'intersection', }, - line: 4, - column: 5, + line: 5, + column: 3, }, ], }, @@ -2988,7 +3014,7 @@ type Foo = { ], }, { - code: 'type Foo = [number] | [number, number]', + code: 'type Foo = [number] | [number, number];', options: [{ allowTupleTypes: 'never' }], errors: [ { @@ -3012,7 +3038,7 @@ type Foo = { ], }, { - code: 'type Foo = [number] & [number, number]', + code: 'type Foo = [number] & [number, number];', options: [{ allowTupleTypes: 'in-unions' }], errors: [ { @@ -3036,7 +3062,7 @@ type Foo = { ], }, { - code: 'type Foo = [number] | [number, number]', + code: 'type Foo = [number] | [number, number];', options: [{ allowTupleTypes: 'in-intersections' }], errors: [ { @@ -3087,7 +3113,7 @@ type Foo = { ], }, { - code: 'type Foo = readonly [number] | keyof [number, number]', + code: 'type Foo = readonly [number] | keyof [number, number];', options: [{ allowTupleTypes: 'never' }], errors: [ { @@ -3111,7 +3137,7 @@ type Foo = { ], }, { - code: 'type Foo = keyof [number] & [number, number]', + code: 'type Foo = keyof [number] & [number, number];', options: [{ allowTupleTypes: 'in-unions' }], errors: [ { @@ -3135,7 +3161,7 @@ type Foo = { ], }, { - code: 'type Foo = [number] | readonly [number, number]', + code: 'type Foo = [number] | readonly [number, number];', options: [{ allowTupleTypes: 'in-intersections' }], errors: [ { From e5451969dfaac5ce3d10ad379c646e2798e9dbf9 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:17:33 -0700 Subject: [PATCH 44/92] chore: no-throw-literal --- .../tests/rules/no-throw-literal.test.ts | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts index 451e88262a31..1367c6a8f695 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -14,8 +14,8 @@ const ruleTester = new RuleTester({ ruleTester.run('no-throw-literal', rule, { valid: [ 'throw new Error();', - 'throw new Error("error");', - 'throw Error("error");', + "throw new Error('error');", + "throw Error('error');", ` const e = new Error(); throw e; @@ -35,27 +35,27 @@ throw foo(); `, ` const foo = { - bar: new Error() -} + bar: new Error(), +}; throw foo.bar; `, ` const foo = { - bar: new Error() -} + bar: new Error(), +}; throw foo['bar']; `, ` const foo = { - bar: new Error() -} + bar: new Error(), +}; const bar = 'bar'; throw foo[bar]; `, ` -class CustomError extends Error {}; +class CustomError extends Error {} throw new CustomError(); `, ` @@ -63,14 +63,23 @@ class CustomError1 extends Error {} class CustomError2 extends CustomError1 {} throw new CustomError(); `, - 'throw foo = new Error();', - 'throw 1, 2, new Error();', - 'throw "literal" && new Error();', - 'throw new Error() || "literal"', - 'throw foo ? new Error() : "literal";', - 'throw foo ? "literal" : new Error();', - 'function* foo() { let index = 0; throw yield index++; }', - 'async function foo() { throw await bar; }', + 'throw (foo = new Error());', + 'throw (1, 2, new Error());', + "throw 'literal' && new Error();", + "throw new Error() || 'literal';", + "throw foo ? new Error() : 'literal';", + "throw foo ? 'literal' : new Error();", + ` +function* foo() { + let index = 0; + throw yield index++; +} + `, + ` +async function foo() { + throw await bar; +} + `, ` import { Error } from './missing'; throw Error; @@ -98,7 +107,7 @@ throw new CustomError(); ], }, { - code: 'throw new String("");', + code: "throw new String('');", errors: [ { messageId: 'object', @@ -106,7 +115,7 @@ throw new CustomError(); ], }, { - code: 'throw "error";', + code: "throw 'error';", errors: [ { messageId: 'object', @@ -146,7 +155,7 @@ throw new CustomError(); ], }, { - code: 'throw "a" + "b";', + code: "throw 'a' + 'b';", errors: [ { messageId: 'object', @@ -165,7 +174,7 @@ throw a + 'b'; ], }, { - code: 'throw foo = "error";', + code: "throw (foo = 'error');", errors: [ { messageId: 'object', @@ -173,7 +182,7 @@ throw a + 'b'; ], }, { - code: 'throw new Error(), 1, 2, 3;', + code: 'throw (new Error(), 1, 2, 3);', errors: [ { messageId: 'object', @@ -181,7 +190,7 @@ throw a + 'b'; ], }, { - code: 'throw "literal" && "not an Error";', + code: "throw 'literal' && 'not an Error';", errors: [ { messageId: 'object', @@ -189,7 +198,7 @@ throw a + 'b'; ], }, { - code: 'throw foo ? "not an Error" : "literal";', + code: "throw foo ? 'not an Error' : 'literal';", errors: [ { messageId: 'object', @@ -197,7 +206,7 @@ throw a + 'b'; ], }, { - code: 'throw `${err}`', + code: 'throw `${err}`;', errors: [ { messageId: 'object', @@ -217,8 +226,7 @@ throw err; }, { code: ` -function foo(msg) { -} +function foo(msg) {} throw foo('error'); `, errors: [ @@ -230,7 +238,7 @@ throw foo('error'); { code: ` const foo = { - msg: 'error' + msg: 'error', }; throw foo.msg; `, @@ -243,7 +251,7 @@ throw foo.msg; { code: ` const foo = { - msg: undefined + msg: undefined, }; throw foo.msg; `, From bc3f6974ea2ca4f8fdb4589c2d8cbe27bc3b5cd7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:17:54 -0700 Subject: [PATCH 45/92] chore: no-this-alias --- .../tests/rules/no-this-alias.test.ts | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-this-alias.test.ts b/packages/eslint-plugin/tests/rules/no-this-alias.test.ts index 0cbd35dae19f..43806a67fd9c 100644 --- a/packages/eslint-plugin/tests/rules/no-this-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-this-alias.test.ts @@ -29,7 +29,7 @@ const { length } = this; const { length, toString } = this; const [foo] = this; const [foo, bar] = this; -`, + `, options: [ { allowDestructuring: true, @@ -47,9 +47,9 @@ const [foo, bar] = this; // https://github.com/bradzacher/eslint-plugin-typescript/issues/281 ` declare module 'foo' { - declare const aVar: string + declare const aVar: string; } - `, + `, ], invalid: [ @@ -80,35 +80,35 @@ declare module 'foo' { var unscoped = this; function testFunction() { - let inFunction = this; + let inFunction = this; } const testLambda = () => { - const inLambda = this; + const inLambda = this; }; -`, + `, errors: [idError, idError, idError], }, { code: ` class TestClass { - constructor() { - const inConstructor = this; - const asThis: this = this; + constructor() { + const inConstructor = this; + const asThis: this = this; - const asString = "this"; - const asArray = [this]; - const asArrayString = ["this"]; - } + const asString = 'this'; + const asArray = [this]; + const asArrayString = ['this']; + } - public act(scope: this = this) { - const inMemberFunction = this; - const { act } = this; - const { act, constructor } = this; - const [foo] = this; - const [foo, bar] = this; - } + public act(scope: this = this) { + const inMemberFunction = this; + const { act } = this; + const { act, constructor } = this; + const [foo] = this; + const [foo, bar] = this; + } } -`, + `, options: [ { allowDestructuring: false, From 8ab07dab3329e86ad72975603e579ae3080fa5e2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:19:00 -0700 Subject: [PATCH 46/92] chore: no-require-imports --- .../tests/rules/no-require-imports.test.ts | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index ef49e4df1bf4..606e4c47ce22 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -10,17 +10,17 @@ const ruleTester = new RuleTester({ ruleTester.run('no-require-imports', rule, { valid: [ - "import {l} from 'lib'", - "var lib3 = load('not_an_import')", - 'var lib4 = lib2.subImport', - 'var lib7 = 700', - 'import lib9 = lib2.anotherSubImport', - "import lib10 from 'lib10'", - "var lib3 = load?.('not_an_import')", + "import { l } from 'lib';", + "var lib3 = load('not_an_import');", + 'var lib4 = lib2.subImport;', + 'var lib7 = 700;', + 'import lib9 = lib2.anotherSubImport;', + "import lib10 from 'lib10';", + "var lib3 = load?.('not_an_import');", ], invalid: [ { - code: "var lib = require('lib')", + code: "var lib = require('lib');", errors: [ { messageId: 'noRequireImports', @@ -30,7 +30,7 @@ ruleTester.run('no-require-imports', rule, { ], }, { - code: "let lib2 = require('lib2')", + code: "let lib2 = require('lib2');", errors: [ { messageId: 'noRequireImports', @@ -40,22 +40,25 @@ ruleTester.run('no-require-imports', rule, { ], }, { - code: "var lib5 = require('lib5'), lib6 = require('lib6')", + code: ` +var lib5 = require('lib5'), + lib6 = require('lib6'); + `, errors: [ { messageId: 'noRequireImports', - line: 1, + line: 2, column: 12, }, { messageId: 'noRequireImports', - line: 1, - column: 36, + line: 3, + column: 10, }, ], }, { - code: "import lib8 = require('lib8')", + code: "import lib8 = require('lib8');", errors: [ { messageId: 'noRequireImports', @@ -65,7 +68,7 @@ ruleTester.run('no-require-imports', rule, { ], }, { - code: "var lib = require?.('lib')", + code: "var lib = require?.('lib');", errors: [ { messageId: 'noRequireImports', @@ -75,7 +78,7 @@ ruleTester.run('no-require-imports', rule, { ], }, { - code: "let lib2 = require?.('lib2')", + code: "let lib2 = require?.('lib2');", errors: [ { messageId: 'noRequireImports', @@ -85,17 +88,20 @@ ruleTester.run('no-require-imports', rule, { ], }, { - code: "var lib5 = require?.('lib5'), lib6 = require?.('lib6')", + code: ` +var lib5 = require?.('lib5'), + lib6 = require?.('lib6'); + `, errors: [ { messageId: 'noRequireImports', - line: 1, + line: 2, column: 12, }, { messageId: 'noRequireImports', - line: 1, - column: 38, + line: 3, + column: 10, }, ], }, From 9a49443117be3b3d18b7c54f69bc5a3f99050e8a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:20:35 -0700 Subject: [PATCH 47/92] chore: no-parameter-properties --- .../rules/no-parameter-properties.test.ts | 264 +++++++++--------- 1 file changed, 132 insertions(+), 132 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-parameter-properties.test.ts b/packages/eslint-plugin/tests/rules/no-parameter-properties.test.ts index 2a2292d58612..f4f7fb03d2dd 100644 --- a/packages/eslint-plugin/tests/rules/no-parameter-properties.test.ts +++ b/packages/eslint-plugin/tests/rules/no-parameter-properties.test.ts @@ -9,107 +9,107 @@ ruleTester.run('no-parameter-properties', rule, { valid: [ ` class Foo { - constructor(name: string) {} + constructor(name: string) {} } - `, + `, ` class Foo { - constructor(...name: string[]) {} + constructor(...name: string[]) {} } - `, + `, ` class Foo { - constructor(name: string, age: number) {} + constructor(name: string, age: number) {} } - `, + `, ` class Foo { - constructor(name: string); - constructor(name: string, age?: number) {} + constructor(name: string); + constructor(name: string, age?: number) {} } - `, + `, { code: ` class Foo { - constructor(readonly name: string) { } + constructor(readonly name: string) {} } - `, + `, options: [{ allows: ['readonly'] }], }, { code: ` class Foo { - constructor(private name: string) { } + constructor(private name: string) {} } - `, + `, options: [{ allows: ['private'] }], }, { code: ` class Foo { - constructor(protected name: string) { } + constructor(protected name: string) {} } - `, + `, options: [{ allows: ['protected'] }], }, { code: ` class Foo { - constructor(public name: string) { } + constructor(public name: string) {} } - `, + `, options: [{ allows: ['public'] }], }, { code: ` class Foo { - constructor(private readonly name: string) { } + constructor(private readonly name: string) {} } - `, + `, options: [{ allows: ['private readonly'] }], }, { code: ` class Foo { - constructor(protected readonly name: string) { } + constructor(protected readonly name: string) {} } - `, + `, options: [{ allows: ['protected readonly'] }], }, { code: ` class Foo { - constructor(public readonly name: string) { } + constructor(public readonly name: string) {} } - `, + `, options: [{ allows: ['public readonly'] }], }, { code: ` class Foo { - constructor(readonly name: string, private age: number) { } + constructor(readonly name: string, private age: number) {} } - `, + `, options: [{ allows: ['readonly', 'private'] }], }, { code: ` class Foo { - constructor(public readonly name: string, private age: number) { } + constructor(public readonly name: string, private age: number) {} } - `, + `, options: [{ allows: ['public readonly', 'private'] }], }, // Semantically invalid test case ` class Foo { - constructor(private ...name: string[]) {} + constructor(private ...name: string[]) {} } `, // Semantically invalid test case ` class Foo { - constructor(private [test]: [string]) {} + constructor(private [test]: [string]) {} } `, ], @@ -117,9 +117,9 @@ class Foo { { code: ` class Foo { - constructor(readonly name: string) {} + constructor(readonly name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -127,16 +127,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string) {} + constructor(private name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -144,16 +144,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected name: string) {} + constructor(protected name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -161,16 +161,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public name: string) {} + constructor(public name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -178,16 +178,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private readonly name: string) {} + constructor(private readonly name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -195,16 +195,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected readonly name: string) {} + constructor(protected readonly name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -212,16 +212,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public readonly name: string) {} + constructor(public readonly name: string) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -229,16 +229,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public name: string, age: number) {} + constructor(public name: string, age: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -246,16 +246,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string, private age: number) {} + constructor(private name: string, private age: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -263,7 +263,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -271,16 +271,16 @@ class Foo { parameter: 'age', }, line: 3, - column: 39, + column: 37, }, ], }, { code: ` class Foo { - constructor(protected name: string, protected age: number) {} + constructor(protected name: string, protected age: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -288,7 +288,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -296,16 +296,16 @@ class Foo { parameter: 'age', }, line: 3, - column: 41, + column: 39, }, ], }, { code: ` class Foo { - constructor(public name: string, public age: number) {} + constructor(public name: string, public age: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -313,7 +313,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -321,17 +321,17 @@ class Foo { parameter: 'age', }, line: 3, - column: 38, + column: 36, }, ], }, { code: ` class Foo { - constructor(name: string); - constructor(private name: string, age?: number) {} + constructor(name: string); + constructor(private name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -339,17 +339,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string); - constructor(private name: string, age?: number) {} + constructor(private name: string); + constructor(private name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -357,7 +357,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -365,17 +365,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string); - constructor(private name: string, private age?: number) {} + constructor(private name: string); + constructor(private name: string, private age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -383,7 +383,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -391,7 +391,7 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -399,17 +399,17 @@ class Foo { parameter: 'age', }, line: 4, - column: 39, + column: 37, }, ], }, { code: ` class Foo { - constructor(name: string); - constructor(protected name: string, age?: number) {} + constructor(name: string); + constructor(protected name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -417,17 +417,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected name: string); - constructor(protected name: string, age?: number) {} + constructor(protected name: string); + constructor(protected name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -435,7 +435,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -443,17 +443,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected name: string); - constructor(protected name: string, protected age?: number) {} + constructor(protected name: string); + constructor(protected name: string, protected age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -461,7 +461,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -469,7 +469,7 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -477,17 +477,17 @@ class Foo { parameter: 'age', }, line: 4, - column: 41, + column: 39, }, ], }, { code: ` class Foo { - constructor(name: string); - constructor(public name: string, age?: number) {} + constructor(name: string); + constructor(public name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -495,17 +495,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public name: string); - constructor(public name: string, age?: number) {} + constructor(public name: string); + constructor(public name: string, age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -513,7 +513,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -521,17 +521,17 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public name: string); - constructor(public name: string, public age?: number) {} + constructor(public name: string); + constructor(public name: string, public age?: number) {} } - `, + `, errors: [ { messageId: 'noParamProp', @@ -539,7 +539,7 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -547,7 +547,7 @@ class Foo { parameter: 'name', }, line: 4, - column: 17, + column: 15, }, { messageId: 'noParamProp', @@ -555,7 +555,7 @@ class Foo { parameter: 'age', }, line: 4, - column: 38, + column: 36, }, ], }, @@ -563,9 +563,9 @@ class Foo { { code: ` class Foo { - constructor(readonly name: string) {} + constructor(readonly name: string) {} } - `, + `, options: [{ allows: ['private'] }], errors: [ { @@ -574,16 +574,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string) {} + constructor(private name: string) {} } - `, + `, options: [{ allows: ['readonly'] }], errors: [ { @@ -592,16 +592,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected name: string) {} + constructor(protected name: string) {} } - `, + `, options: [ { allows: ['readonly', 'private', 'public', 'protected readonly'], @@ -614,16 +614,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(public name: string) {} + constructor(public name: string) {} } - `, + `, options: [ { allows: [ @@ -642,16 +642,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private readonly name: string) {} + constructor(private readonly name: string) {} } - `, + `, options: [{ allows: ['readonly', 'private'] }], errors: [ { @@ -660,16 +660,16 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(protected readonly name: string) {} + constructor(protected readonly name: string) {} } - `, + `, options: [ { allows: [ @@ -687,17 +687,17 @@ class Foo { parameter: 'name', }, line: 3, - column: 17, + column: 15, }, ], }, { code: ` class Foo { - constructor(private name: string); - constructor(private name: string, protected age?: number) {} + constructor(private name: string); + constructor(private name: string, protected age?: number) {} } - `, + `, options: [{ allows: ['private'] }], errors: [ { @@ -706,7 +706,7 @@ class Foo { parameter: 'age', }, line: 4, - column: 39, + column: 37, }, ], }, From 9c3eeefd4abeb3b0ee2586435a49acebbec72314 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:21:09 -0700 Subject: [PATCH 48/92] chore: no-non-null-assertion --- .../tests/rules/no-non-null-assertion.test.ts | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-non-null-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-non-null-assertion.test.ts index 3bced219288a..e9fae587f556 100644 --- a/packages/eslint-plugin/tests/rules/no-non-null-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-non-null-assertion.test.ts @@ -8,16 +8,16 @@ const ruleTester = new RuleTester({ ruleTester.run('no-non-null-assertion', rule, { valid: [ // - 'x', - 'x.y', - 'x.y.z', - 'x?.y.z', - 'x?.y?.z', - '!x', + 'x;', + 'x.y;', + 'x.y.z;', + 'x?.y.z;', + 'x?.y?.z;', + '!x;', ], invalid: [ { - code: 'x!', + code: 'x!;', errors: [ { messageId: 'noNonNull', @@ -28,7 +28,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: 'x!.y', + code: 'x!.y;', errors: [ { messageId: 'noNonNull', @@ -37,14 +37,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.y', + output: 'x?.y;', }, ], }, ], }, { - code: 'x.y!', + code: 'x.y!;', errors: [ { messageId: 'noNonNull', @@ -55,7 +55,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: '!x!.y', + code: '!x!.y;', errors: [ { messageId: 'noNonNull', @@ -64,14 +64,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: '!x?.y', + output: '!x?.y;', }, ], }, ], }, { - code: 'x!.y?.z', + code: 'x!.y?.z;', errors: [ { messageId: 'noNonNull', @@ -80,14 +80,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.y?.z', + output: 'x?.y?.z;', }, ], }, ], }, { - code: 'x![y]', + code: 'x![y];', errors: [ { messageId: 'noNonNull', @@ -96,14 +96,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.[y]', + output: 'x?.[y];', }, ], }, ], }, { - code: 'x![y]?.z', + code: 'x![y]?.z;', errors: [ { messageId: 'noNonNull', @@ -112,14 +112,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.[y]?.z', + output: 'x?.[y]?.z;', }, ], }, ], }, { - code: 'x.y.z!()', + code: 'x.y.z!();', errors: [ { messageId: 'noNonNull', @@ -128,14 +128,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x.y.z?.()', + output: 'x.y.z?.();', }, ], }, ], }, { - code: 'x.y?.z!()', + code: 'x.y?.z!();', errors: [ { messageId: 'noNonNull', @@ -144,7 +144,7 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x.y?.z?.()', + output: 'x.y?.z?.();', }, ], }, @@ -152,7 +152,7 @@ ruleTester.run('no-non-null-assertion', rule, { }, // some weirder cases that are stupid but valid { - code: 'x!!!', + code: 'x!!!;', errors: [ { messageId: 'noNonNull', @@ -178,7 +178,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: 'x!!.y', + code: 'x!!.y;', errors: [ { messageId: 'noNonNull', @@ -188,7 +188,7 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x!?.y', + output: 'x!?.y;', }, ], }, @@ -202,7 +202,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: 'x.y!!', + code: 'x.y!!;', errors: [ { messageId: 'noNonNull', @@ -221,7 +221,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: 'x.y.z!!()', + code: 'x.y.z!!();', errors: [ { messageId: 'noNonNull', @@ -231,7 +231,7 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x.y.z!?.()', + output: 'x.y.z!?.();', }, ], }, @@ -245,7 +245,7 @@ ruleTester.run('no-non-null-assertion', rule, { ], }, { - code: 'x!?.[y].z', + code: 'x!?.[y].z;', errors: [ { messageId: 'noNonNull', @@ -254,14 +254,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.[y].z', + output: 'x?.[y].z;', }, ], }, ], }, { - code: 'x!?.y.z', + code: 'x!?.y.z;', errors: [ { messageId: 'noNonNull', @@ -270,14 +270,14 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x?.y.z', + output: 'x?.y.z;', }, ], }, ], }, { - code: 'x.y.z!?.()', + code: 'x.y.z!?.();', errors: [ { messageId: 'noNonNull', @@ -286,7 +286,7 @@ ruleTester.run('no-non-null-assertion', rule, { suggestions: [ { messageId: 'suggestOptionalChain', - output: 'x.y.z?.()', + output: 'x.y.z?.();', }, ], }, From 06ee33b251d325fad8a3e6b18e03cdcdd7d566d2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:23:17 -0700 Subject: [PATCH 49/92] chore: no-non-null-asserted-optional-chain --- ...o-non-null-asserted-optional-chain.test.ts | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-non-null-asserted-optional-chain.test.ts b/packages/eslint-plugin/tests/rules/no-non-null-asserted-optional-chain.test.ts index 33e95fa91a99..0a28cbbfc928 100644 --- a/packages/eslint-plugin/tests/rules/no-non-null-asserted-optional-chain.test.ts +++ b/packages/eslint-plugin/tests/rules/no-non-null-asserted-optional-chain.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/no-non-null-asserted-optional-chain'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -7,177 +7,177 @@ const ruleTester = new RuleTester({ ruleTester.run('no-non-null-asserted-optional-chain', rule, { valid: [ - 'foo.bar!', - 'foo.bar()!', - 'foo?.bar', - 'foo?.bar()', - '(foo?.bar).baz!', - '(foo?.bar()).baz!', + 'foo.bar!;', + 'foo.bar()!;', + 'foo?.bar;', + 'foo?.bar();', + '(foo?.bar).baz!;', + '(foo?.bar()).baz!;', ], invalid: [ { - code: 'foo?.bar!', + code: 'foo?.bar!;', errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo?.bar', + output: 'foo?.bar;', }, ], }, ], }, { - code: 'foo?.["bar"]!', + code: "foo?.['bar']!;", errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo?.["bar"]', + output: "foo?.['bar'];", }, ], }, ], }, { - code: 'foo?.bar()!', + code: 'foo?.bar()!;', errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo?.bar()', + output: 'foo?.bar();', }, ], }, ], }, { - code: 'foo.bar?.()!', + code: 'foo.bar?.()!;', errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo.bar?.()', + output: 'foo.bar?.();', }, ], }, ], }, { - code: 'foo?.bar!()', + code: 'foo?.bar!();', errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo?.bar()', + output: 'foo?.bar();', }, ], }, ], }, { - code: '(foo?.bar)!.baz', + code: noFormat`(foo?.bar)!.baz`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar).baz', + output: noFormat`(foo?.bar).baz`, }, ], }, ], }, { - code: 'foo?.["bar"]!.baz', + code: "foo?.['bar']!.baz;", errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: 'foo?.["bar"].baz', + output: "foo?.['bar'].baz;", }, ], }, ], }, { - code: '(foo?.bar)!().baz', + code: noFormat`(foo?.bar)!().baz`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar)().baz', + output: noFormat`(foo?.bar)().baz`, }, ], }, ], }, { - code: '(foo?.bar)!', + code: noFormat`(foo?.bar)!`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar)', + output: noFormat`(foo?.bar)`, }, ], }, ], }, { - code: '(foo?.bar)!()', + code: noFormat`(foo?.bar)!()`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar)()', + output: noFormat`(foo?.bar)()`, }, ], }, ], }, { - code: '(foo?.bar!)', + code: noFormat`(foo?.bar!)`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar)', + output: noFormat`(foo?.bar)`, }, ], }, ], }, { - code: '(foo?.bar!)()', + code: noFormat`(foo?.bar!)()`, errors: [ { messageId: 'noNonNullOptionalChain', suggestions: [ { messageId: 'suggestRemovingNonNull', - output: '(foo?.bar)()', + output: noFormat`(foo?.bar)()`, }, ], }, From 0b628bcbf1c828bafeca5fd8c64f113ae6caf3ba Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:23:43 -0700 Subject: [PATCH 50/92] chore: no-namespace --- .../tests/rules/no-namespace.test.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-namespace.test.ts b/packages/eslint-plugin/tests/rules/no-namespace.test.ts index 1b9cade4cc69..e175f5895dfc 100644 --- a/packages/eslint-plugin/tests/rules/no-namespace.test.ts +++ b/packages/eslint-plugin/tests/rules/no-namespace.test.ts @@ -7,24 +7,24 @@ const ruleTester = new RuleTester({ ruleTester.run('no-namespace', rule, { valid: [ - 'declare global { }', - "declare module 'foo' { }", + 'declare global {}', + "declare module 'foo' {}", { - code: 'declare module foo { }', + code: 'declare module foo {}', options: [{ allowDeclarations: true }], }, { - code: 'declare namespace foo { }', + code: 'declare namespace foo {}', options: [{ allowDeclarations: true }], }, { filename: 'test.d.ts', - code: 'namespace foo { }', + code: 'namespace foo {}', options: [{ allowDefinitionFiles: true }], }, { filename: 'test.d.ts', - code: 'module foo { }', + code: 'module foo {}', options: [{ allowDefinitionFiles: true }], }, ], @@ -72,7 +72,7 @@ ruleTester.run('no-namespace', rule, { ], }, { - code: 'declare module foo { }', + code: 'declare module foo {}', errors: [ { messageId: 'moduleSyntaxIsPreferred', @@ -82,7 +82,7 @@ ruleTester.run('no-namespace', rule, { ], }, { - code: 'declare namespace foo { }', + code: 'declare namespace foo {}', errors: [ { messageId: 'moduleSyntaxIsPreferred', @@ -115,7 +115,7 @@ ruleTester.run('no-namespace', rule, { }, { filename: 'test.d.ts', - code: 'namespace foo { }', + code: 'namespace foo {}', options: [{ allowDefinitionFiles: false }], errors: [ { @@ -127,7 +127,7 @@ ruleTester.run('no-namespace', rule, { }, { filename: 'test.d.ts', - code: 'module foo { }', + code: 'module foo {}', options: [{ allowDefinitionFiles: false }], errors: [ { @@ -179,7 +179,7 @@ namespace Foo.Bar { interface X {} } } - `, + `, errors: [ { messageId: 'moduleSyntaxIsPreferred', From eed2863e3a50eb83a880f796a6423aa7d7133f62 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:25:12 -0700 Subject: [PATCH 51/92] chore: no-misused-promises --- .../tests/rules/no-misused-promises.test.ts | 178 +++++++++++------- 1 file changed, 108 insertions(+), 70 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts b/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts index fb18a3875e34..cda131067fa6 100644 --- a/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts @@ -14,66 +14,81 @@ const ruleTester = new RuleTester({ ruleTester.run('no-misused-promises', rule, { valid: [ - `if (true) {}`, + ` +if (true) { +} + `, { - code: `if (Promise.resolve()) {}`, + code: ` +if (Promise.resolve()) { +} + `, options: [{ checksConditionals: false }], }, ` -if (true) {} -else if (false) {} -else {} -`, +if (true) { +} else if (false) { +} else { +} + `, { code: ` -if (Promise.resolve()) {} -else if (Promise.resolve()) {} -else {} -`, +if (Promise.resolve()) { +} else if (Promise.resolve()) { +} else { +} + `, options: [{ checksConditionals: false }], }, - `for (;;) {}`, - `for (let i; i < 10; i++) {}`, + 'for (;;) {}', + 'for (let i; i < 10; i++) {}', { - code: `for (let i; Promise.resolve(); i++) {}`, + code: 'for (let i; Promise.resolve(); i++) {}', options: [{ checksConditionals: false }], }, - `do {} while (true);`, + 'do {} while (true);', { - code: `do {} while (Promise.resolve())`, + code: 'do {} while (Promise.resolve());', options: [{ checksConditionals: false }], }, - `while (true) {}`, + 'while (true) {}', { - code: `while (Promise.resolve()) {}`, + code: 'while (Promise.resolve()) {}', options: [{ checksConditionals: false }], }, - `true ? 123 : 456`, + 'true ? 123 : 456;', { - code: `Promise.resolve() ? 123 : 456`, + code: 'Promise.resolve() ? 123 : 456;', options: [{ checksConditionals: false }], }, - `if (!true) {}`, + ` +if (!true) { +} + `, { - code: `if (!Promise.resolve()) {}`, + code: ` +if (!Promise.resolve()) { +} + `, options: [{ checksConditionals: false }], }, - `(await Promise.resolve()) || false`, + '(await Promise.resolve()) || false;', { - code: `Promise.resolve() || false`, + code: 'Promise.resolve() || false;', options: [{ checksConditionals: false }], }, - `(true && await Promise.resolve()) || false`, + '(true && (await Promise.resolve())) || false;', { - code: `(true && Promise.resolve()) || false`, + code: '(true && Promise.resolve()) || false;', options: [{ checksConditionals: false }], }, - `false || (true && Promise.resolve())`, + 'false || (true && Promise.resolve());', ` async function test() { - if (await Promise.resolve()) {} + if (await Promise.resolve()) { + } } -`, + `, ` async function test() { const mixed: Promise | undefined = Promise.resolve(); @@ -81,57 +96,73 @@ async function test() { await mixed; } } -`, - `if (~Promise.resolve()) {}`, + `, + ` +if (~Promise.resolve()) { +} + `, ` interface NotQuiteThenable { then(param: string): void; then(): void; } const value: NotQuiteThenable = { then() {} }; -if (value) {} -`, - `[1, 2, 3].forEach(val => {});`, +if (value) { +} + `, + '[1, 2, 3].forEach(val => {});', { - code: `[1, 2, 3].forEach(async val => {});`, + code: '[1, 2, 3].forEach(async val => {});', options: [{ checksVoidReturn: false }], }, - `new Promise((resolve, reject) => resolve());`, + 'new Promise((resolve, reject) => resolve());', { - code: `new Promise(async (resolve, reject) => resolve());`, + code: 'new Promise(async (resolve, reject) => resolve());', options: [{ checksVoidReturn: false }], }, - `Promise.all(['abc', 'def'].map(async val => { await val; }))`, + ` +Promise.all( + ['abc', 'def'].map(async val => { + await val; + }), +); + `, ` const fn: (arg: () => Promise | void) => void = () => {}; fn(() => Promise.resolve()); `, ` -declare const returnsPromise : (() => Promise) | null; -if (returnsPromise?.()) {} +declare const returnsPromise: (() => Promise) | null; +if (returnsPromise?.()) { +} `, ` -declare const returnsPromise : { call: (() => Promise) } | null; -if (returnsPromise?.call()) {} +declare const returnsPromise: { call: () => Promise } | null; +if (returnsPromise?.call()) { +} `, ], invalid: [ { - code: `if (Promise.resolve()) {}`, + code: ` +if (Promise.resolve()) { +} + `, errors: [ { - line: 1, + line: 2, messageId: 'conditional', }, ], }, { code: ` -if (Promise.resolve()) {} -else if (Promise.resolve()) {} -else {} -`, +if (Promise.resolve()) { +} else if (Promise.resolve()) { +} else { +} + `, errors: [ { line: 2, @@ -144,7 +175,7 @@ else {} ], }, { - code: `for (let i; Promise.resolve(); i++) {}`, + code: 'for (let i; Promise.resolve(); i++) {}', errors: [ { line: 1, @@ -153,7 +184,7 @@ else {} ], }, { - code: `do {} while (Promise.resolve())`, + code: 'do {} while (Promise.resolve());', errors: [ { line: 1, @@ -162,7 +193,7 @@ else {} ], }, { - code: `while (Promise.resolve()) {}`, + code: 'while (Promise.resolve()) {}', errors: [ { line: 1, @@ -171,7 +202,7 @@ else {} ], }, { - code: `Promise.resolve() ? 123 : 456`, + code: 'Promise.resolve() ? 123 : 456;', errors: [ { line: 1, @@ -180,16 +211,19 @@ else {} ], }, { - code: `if (!Promise.resolve()) {}`, + code: ` +if (!Promise.resolve()) { +} + `, errors: [ { - line: 1, + line: 2, messageId: 'conditional', }, ], }, { - code: `Promise.resolve() || false`, + code: 'Promise.resolve() || false;', errors: [ { line: 1, @@ -198,7 +232,7 @@ else {} ], }, { - code: `(true && Promise.resolve()) || false`, + code: '(true && Promise.resolve()) || false;', errors: [ { line: 1, @@ -208,13 +242,13 @@ else {} }, { code: ` -[Promise.resolve(), Promise.reject()].forEach( - async val => { await val; } -); -`, +[Promise.resolve(), Promise.reject()].forEach(async val => { + await val; +}); + `, errors: [ { - line: 3, + line: 2, messageId: 'voidReturn', }, ], @@ -225,7 +259,7 @@ new Promise(async (resolve, reject) => { await Promise.resolve(); resolve(); }); -`, + `, errors: [ { line: 2, @@ -242,7 +276,7 @@ const fnWithCallback = (arg: string, cb: (err: any, res: string) => void) => { fnWithCallback('val', async (err, res) => { await res; }); -`, + `, errors: [ { line: 6, @@ -257,7 +291,7 @@ const fnWithCallback = (arg: string, cb: (err: any, res: string) => void) => { }; fnWithCallback('val', (err, res) => Promise.resolve(res)); -`, + `, errors: [ { line: 6, @@ -278,7 +312,7 @@ fnWithCallback('val', (err, res) => { return Promise.resolve(res); } }); -`, + `, errors: [ { line: 6, @@ -288,22 +322,26 @@ fnWithCallback('val', (err, res) => { }, { code: ` -const fnWithCallback: ((arg: string, cb: (err: any, res: string) => void) => void) | null = (arg, cb) => { +const fnWithCallback: + | ((arg: string, cb: (err: any, res: string) => void) => void) + | null = (arg, cb) => { cb(null, arg); }; fnWithCallback?.('val', (err, res) => Promise.resolve(res)); -`, + `, errors: [ { - line: 6, + line: 8, messageId: 'voidReturn', }, ], }, { code: ` -const fnWithCallback: ((arg: string, cb: (err: any, res: string) => void) => void) | null = (arg, cb) => { +const fnWithCallback: + | ((arg: string, cb: (err: any, res: string) => void) => void) + | null = (arg, cb) => { cb(null, arg); }; @@ -314,10 +352,10 @@ fnWithCallback('val', (err, res) => { return Promise.resolve(res); } }); -`, + `, errors: [ { - line: 6, + line: 8, messageId: 'voidReturn', }, ], From 6ad024bf66d3da65e9e5eabe4ed38f1045e78459 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:26:22 -0700 Subject: [PATCH 52/92] chore: no-misused-new --- .../tests/rules/no-misused-new.test.ts | 117 ++++++++++++------ 1 file changed, 80 insertions(+), 37 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-misused-new.test.ts b/packages/eslint-plugin/tests/rules/no-misused-new.test.ts index 414d86245d5f..527d5d46671f 100644 --- a/packages/eslint-plugin/tests/rules/no-misused-new.test.ts +++ b/packages/eslint-plugin/tests/rules/no-misused-new.test.ts @@ -9,46 +9,89 @@ ruleTester.run('no-misused-new', rule, { valid: [ ` declare abstract class C { - foo () { - } - get new (); - bar(); + foo() {} + get new(); + bar(); } `, - 'class C { constructor(); }', - 'const foo = class { constructor(); }', - 'const foo = class { new(): X }', + ` +class C { + constructor(); +} + `, + ` +const foo = class { + constructor(); +}; + `, + ` +const foo = class { + new(): X; +}; + `, // OK if there's a body - 'class C { new() {} }', - 'class C { constructor() {} }', - 'const foo = class { new() {} }', - 'const foo = class { constructor() {} }', + ` +class C { + new() {} +} + `, + ` +class C { + constructor() {} +} + `, + ` +const foo = class { + new() {} +}; + `, + ` +const foo = class { + constructor() {} +}; + `, // OK if return type is not the interface. - 'interface I { new(): {}; }', + ` +interface I { + new (): {}; +} + `, // 'new' OK in type literal (we don't know the type name) - 'type T = { new(): T; }', - 'export default class { constructor(); }', - 'interface foo { new(): bar; }', - "interface foo { new(): 'x'; }", + 'type T = { new (): T };', + ` +export default class { + constructor(); +} + `, + ` +interface foo { + new (): bar; +} + `, + ` +interface foo { + new (): 'x'; +} + `, ], invalid: [ { code: ` interface I { - new(): I; - constructor(): void; + new (): I; + constructor(): void; } -`, + `, errors: [ { messageId: 'errorMessageInterface', line: 3, - column: 5, + column: 3, }, { messageId: 'errorMessageInterface', line: 4, - column: 5, + column: 3, }, ], }, @@ -56,14 +99,14 @@ interface I { { code: ` interface G { - new(): G; + new (): G; } -`, + `, errors: [ { messageId: 'errorMessageInterface', line: 3, - column: 5, + column: 3, }, ], }, @@ -71,56 +114,56 @@ interface G { { code: ` type T = { - constructor(): void; -} -`, + constructor(): void; +}; + `, errors: [ { messageId: 'errorMessageInterface', line: 3, - column: 5, + column: 3, }, ], }, { code: ` class C { - new(): C; + new(): C; } -`, + `, errors: [ { messageId: 'errorMessageClass', line: 3, - column: 5, + column: 3, }, ], }, { code: ` declare abstract class C { - new(): C; + new(): C; } -`, + `, errors: [ { messageId: 'errorMessageClass', line: 3, - column: 5, + column: 3, }, ], }, { code: ` interface I { - constructor(): ''; + constructor(): ''; } -`, + `, errors: [ { messageId: 'errorMessageInterface', line: 3, - column: 5, + column: 3, }, ], }, From 265742c886d399744fdf33275a64fb42deb34d00 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:27:04 -0700 Subject: [PATCH 53/92] chore: no-magic-numbers --- .../tests/rules/no-magic-numbers.test.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts b/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts index 9c8bdeae10a8..0c220805085f 100644 --- a/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts +++ b/packages/eslint-plugin/tests/rules/no-magic-numbers.test.ts @@ -12,7 +12,7 @@ ruleTester.run('no-magic-numbers', rule, { options: [{ ignoreNumericLiteralTypes: true }], }, { - code: 'type Foo = "bar";', + code: "type Foo = 'bar';", }, { code: 'type Foo = true;', @@ -37,7 +37,7 @@ ruleTester.run('no-magic-numbers', rule, { code: ` enum foo { SECOND = 1000, - NUM = "0123456789", + NUM = '0123456789', NEG = -1, POS = +1, } @@ -141,7 +141,11 @@ class Foo { ], }, { - code: 'interface Foo { bar: 1; }', + code: ` +interface Foo { + bar: 1; +} + `, options: [{ ignoreNumericLiteralTypes: true }], errors: [ { @@ -149,8 +153,8 @@ class Foo { data: { raw: '1', }, - line: 1, - column: 22, + line: 3, + column: 8, }, ], }, @@ -158,7 +162,7 @@ class Foo { code: ` enum foo { SECOND = 1000, - NUM = "0123456789", + NUM = '0123456789', NEG = -1, POS = +1, } From 6dac36d43deb1cbab15a18ca88e587d9ab2954ae Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:28:06 -0700 Subject: [PATCH 54/92] chore: no-inferrable-types --- .../tests/rules/no-inferrable-types.test.ts | 68 +++++++++++++------ 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts index 050e7bee4168..b87e778d5081 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -102,20 +102,30 @@ ruleTester.run('no-inferrable-types', rule, { valid: [ ...validTestCases, - "const fn = (a = 5, b = true, c = 'foo') => {}", - "const fn = function(a = 5, b = true, c = 'foo') {}", + "const fn = (a = 5, b = true, c = 'foo') => {};", + "const fn = function(a = 5, b = true, c = 'foo') {};", "function fn(a = 5, b = true, c = 'foo') {}", 'function fn(a: number, b: boolean, c: string) {}', - "class Foo { a = 5; b = true; c = 'foo'; }", - 'class Foo { readonly a: number = 5; }', + ` +class Foo { + a = 5; + b = true; + c = 'foo'; +} + `, + ` +class Foo { + readonly a: number = 5; +} + `, - 'const a: any = 5', - "const fn = function(a: any = 5, b: any = true, c: any = 'foo') {}", + 'const a: any = 5;', + "const fn = function(a: any = 5, b: any = true, c: any = 'foo') {};", { code: - "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {}", + "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};", options: [{ ignoreParameters: true }], }, { @@ -125,12 +135,17 @@ ruleTester.run('no-inferrable-types', rule, { }, { code: - "const fn = function(a: number = 5, b: boolean = true, c: string = 'foo') {}", + "const fn = function(a: number = 5, b: boolean = true, c: string = 'foo') {};", options: [{ ignoreParameters: true }], }, { - code: - "class Foo { a: number = 5; b: boolean = true; c: string = 'foo'; }", + code: ` +class Foo { + a: number = 5; + b: boolean = true; + c: string = 'foo'; +} + `, options: [{ ignoreProperties: true }], }, { @@ -149,8 +164,8 @@ class Foo { { code: - "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {}", - output: "const fn = (a = 5, b = true, c = 'foo') => {}", + "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};", + output: "const fn = (a = 5, b = true, c = 'foo') => {};", options: [ { ignoreParameters: false, @@ -185,9 +200,20 @@ class Foo { ], }, { - code: - "class Foo { a: number = 5; b: boolean = true; c: string = 'foo'; }", - output: "class Foo { a = 5; b = true; c = 'foo'; }", + code: ` +class Foo { + a: number = 5; + b: boolean = true; + c: string = 'foo'; +} + `, + output: ` +class Foo { + a = 5; + b = true; + c = 'foo'; +} + `, options: [ { ignoreParameters: false, @@ -200,24 +226,24 @@ class Foo { data: { type: 'number', }, - line: 1, - column: 13, + line: 3, + column: 3, }, { messageId: 'noInferrableType', data: { type: 'boolean', }, - line: 1, - column: 28, + line: 4, + column: 3, }, { messageId: 'noInferrableType', data: { type: 'string', }, - line: 1, - column: 47, + line: 5, + column: 3, }, ], }, From 355d17cb7b85fdb5bc8e43a65d9956c3d7a83bdd Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:28:38 -0700 Subject: [PATCH 55/92] chore: no-implied-eval --- .../tests/rules/no-implied-eval.test.ts | 105 +++++++++--------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-implied-eval.test.ts b/packages/eslint-plugin/tests/rules/no-implied-eval.test.ts index b92b61eff6aa..521f838f06f3 100644 --- a/packages/eslint-plugin/tests/rules/no-implied-eval.test.ts +++ b/packages/eslint-plugin/tests/rules/no-implied-eval.test.ts @@ -13,28 +13,28 @@ const ruleTester = new RuleTester({ ruleTester.run('no-implied-eval', rule, { valid: [ - `foo.setImmediate(null);`, - `foo.setInterval(null);`, - `foo.execScript(null);`, - `foo.setTimeout(null);`, - `foo()`, - `(function(){ })()`, + 'foo.setImmediate(null);', + 'foo.setInterval(null);', + 'foo.execScript(null);', + 'foo.setTimeout(null);', + 'foo();', + '(function() {})();', - `setTimeout(() => {}, 0);`, - `window.setTimeout(() => {}, 0);`, - `window['setTimeout'](() => {}, 0);`, + 'setTimeout(() => {}, 0);', + 'window.setTimeout(() => {}, 0);', + "window['setTimeout'](() => {}, 0);", - `setInterval(() => {}, 0);`, - `window.setInterval(() => {}, 0);`, - `window['setInterval'](() => {}, 0);`, + 'setInterval(() => {}, 0);', + 'window.setInterval(() => {}, 0);', + "window['setInterval'](() => {}, 0);", - `setImmediate(() => {});`, - `window.setImmediate(() => {});`, - `window['setImmediate'](() => {});`, + 'setImmediate(() => {});', + 'window.setImmediate(() => {});', + "window['setImmediate'](() => {});", - `execScript(() => {});`, - `window.execScript(() => {});`, - `window['execScript'](() => {});`, + 'execScript(() => {});', + 'window.execScript(() => {});', + "window['execScript'](() => {});", ` const foo = () => {}; @@ -45,7 +45,7 @@ setImmediate(foo); execScript(foo); `, ` -const foo = function () {}; +const foo = function() {}; setTimeout(foo, 0); setInterval(foo, 0); @@ -53,7 +53,7 @@ setImmediate(foo); execScript(foo); `, ` -function foo() {}; +function foo() {} setTimeout(foo, 0); setInterval(foo, 0); @@ -63,7 +63,7 @@ execScript(foo); ` const foo = { fn: () => {}, -} +}; setTimeout(foo.fn, 0); setInterval(foo.fn, 0); @@ -72,8 +72,8 @@ execScript(foo.fn); `, ` const foo = { - fn: function () {}, -} + fn: function() {}, +}; setTimeout(foo.fn, 0); setInterval(foo.fn, 0); @@ -83,7 +83,7 @@ execScript(foo.fn); ` const foo = { fn: function foo() {}, -} +}; setTimeout(foo.fn, 0); setInterval(foo.fn, 0); @@ -93,7 +93,7 @@ execScript(foo.fn); ` const foo = { fn() {}, -} +}; setTimeout(foo.fn, 0); setInterval(foo.fn, 0); @@ -103,7 +103,7 @@ execScript(foo.fn); ` const foo = { fn: () => {}, -} +}; const fn = 'fn'; setTimeout(foo[fn], 0); @@ -114,7 +114,7 @@ execScript(foo[fn]); ` const foo = { fn: () => {}, -} +}; setTimeout(foo['fn'], 0); setInterval(foo['fn'], 0); @@ -122,8 +122,7 @@ setImmediate(foo['fn']); execScript(foo['fn']); `, ` -const foo: () => void = () => { -}; +const foo: () => void = () => {}; setTimeout(foo, 0); setInterval(foo, 0); @@ -131,9 +130,9 @@ setImmediate(foo); execScript(foo); `, ` -const foo: (() => () => void) = () => { +const foo: () => () => void = () => { return () => {}; -} +}; setTimeout(foo(), 0); setInterval(foo(), 0); @@ -141,7 +140,7 @@ setImmediate(foo()); execScript(foo()); `, ` -const foo: (() => () => void) = () => () => {}; +const foo: () => () => void = () => () => {}; setTimeout(foo(), 0); setInterval(foo(), 0); @@ -157,9 +156,9 @@ setImmediate(foo()); execScript(foo()); `, ` -const foo = function foo () { - return function foo() {} -} +const foo = function foo() { + return function foo() {}; +}; setTimeout(foo(), 0); setInterval(foo(), 0); @@ -167,11 +166,11 @@ setImmediate(foo()); execScript(foo()); `, ` -const foo = function () { - return function () { +const foo = function() { + return function() { return ''; - } -} + }; +}; setTimeout(foo(), 0); setInterval(foo(), 0); @@ -179,9 +178,9 @@ setImmediate(foo()); execScript(foo()); `, ` -const foo: (() => () => void) = function foo () { - return function foo() {} -} +const foo: () => () => void = function foo() { + return function foo() {}; +}; setTimeout(foo(), 0); setInterval(foo(), 0); @@ -240,7 +239,7 @@ const fn = (foo: () => void) => { setInterval(foo, 0); setImmediate(foo); execScript(foo); -} +}; `, ], @@ -369,7 +368,7 @@ execScript(foo); }, { code: ` -const foo = function () { +const foo = function() { return 'x + 1'; }; @@ -403,7 +402,7 @@ execScript(foo()); }, { code: ` -const foo = function () { +const foo = function() { return () => 'x + 1'; }; @@ -437,7 +436,7 @@ execScript(foo()()); }, { code: ` -const fn = function () {} +const fn = function() {}; setTimeout(fn + '', 0); setInterval(fn + '', 0); @@ -570,7 +569,7 @@ const fn = (foo: string | any) => { setInterval(foo, 0); setImmediate(foo); execScript(foo); -} +}; `, errors: [ { @@ -653,7 +652,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = Function()`, + code: 'const fn = Function();', errors: [ { messageId: 'noFunctionConstructor', @@ -663,7 +662,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = new Function('a', 'b', 'return a + b');`, + code: "const fn = new Function('a', 'b', 'return a + b');", errors: [ { messageId: 'noFunctionConstructor', @@ -673,7 +672,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = window.Function();`, + code: 'const fn = window.Function();', errors: [ { messageId: 'noFunctionConstructor', @@ -683,7 +682,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = new window.Function();`, + code: 'const fn = new window.Function();', errors: [ { messageId: 'noFunctionConstructor', @@ -693,7 +692,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = window['Function']();`, + code: "const fn = window['Function']();", errors: [ { messageId: 'noFunctionConstructor', @@ -703,7 +702,7 @@ window['execScript'](\`\`); ], }, { - code: `const fn = new window['Function']();`, + code: "const fn = new window['Function']();", errors: [ { messageId: 'noFunctionConstructor', From bb213a5e1cf0ec9ee8aa99497d691374b6a5c359 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:28:59 -0700 Subject: [PATCH 56/92] chore: no-for-in-array --- .../tests/rules/no-for-in-array.test.ts | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts b/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts index 44a40942c9fa..134fd513e877 100644 --- a/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts +++ b/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts @@ -16,20 +16,23 @@ ruleTester.run('no-for-in-array', rule, { valid: [ ` for (const x of [3, 4, 5]) { - console.log(x); -}`, + console.log(x); +} + `, ` for (const x in { a: 1, b: 2, c: 3 }) { - console.log(x); -}`, + console.log(x); +} + `, ], invalid: [ { code: ` for (const x in [3, 4, 5]) { - console.log(x); -}`, + console.log(x); +} + `, errors: [ { messageId: 'forInViolation', @@ -41,8 +44,9 @@ for (const x in [3, 4, 5]) { code: ` const z = [3, 4, 5]; for (const x in z) { - console.log(x); -}`, + console.log(x); +} + `, errors: [ { messageId: 'forInViolation', From 505e41da7bcc88e68f3f9fb9c868794abb71aba8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:30:09 -0700 Subject: [PATCH 57/92] chore: no-floating-promises --- .../tests/rules/no-floating-promises.test.ts | 284 ++++++++++++------ 1 file changed, 184 insertions(+), 100 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index efdbf5e1a48c..c7e688c9852e 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -16,57 +16,89 @@ ruleTester.run('no-floating-promises', rule, { valid: [ ` async function test() { - await Promise.resolve("value"); - Promise.resolve("value").then(() => {}, () => {}); - Promise.resolve("value").then(() => {}).catch(() => {}); - Promise.resolve("value").then(() => {}).catch(() => {}).finally(() => {}); - Promise.resolve("value").catch(() => {}); - Promise.resolve("value").finally(() => {}); - return Promise.resolve("value"); -} -`, + await Promise.resolve('value'); + Promise.resolve('value').then( + () => {}, + () => {}, + ); + Promise.resolve('value') + .then(() => {}) + .catch(() => {}); + Promise.resolve('value') + .then(() => {}) + .catch(() => {}) + .finally(() => {}); + Promise.resolve('value').catch(() => {}); + Promise.resolve('value').finally(() => {}); + return Promise.resolve('value'); +} + `, { options: [{ ignoreVoid: true }], code: ` async function test() { - void Promise.resolve("value"); + void Promise.resolve('value'); } -`, + `, }, ` async function test() { - await Promise.reject(new Error("message")); - Promise.reject(new Error("message")).then(() => {}, () => {}); - Promise.reject(new Error("message")).then(() => {}).catch(() => {}); - Promise.reject(new Error("message")).then(() => {}).catch(() => {}).finally(() => {}); - Promise.reject(new Error("message")).catch(() => {}); - Promise.reject(new Error("message")).finally(() => {}); - return Promise.reject(new Error("message")); -} -`, + await Promise.reject(new Error('message')); + Promise.reject(new Error('message')).then( + () => {}, + () => {}, + ); + Promise.reject(new Error('message')) + .then(() => {}) + .catch(() => {}); + Promise.reject(new Error('message')) + .then(() => {}) + .catch(() => {}) + .finally(() => {}); + Promise.reject(new Error('message')).catch(() => {}); + Promise.reject(new Error('message')).finally(() => {}); + return Promise.reject(new Error('message')); +} + `, ` async function test() { await (async () => true)(); - (async () => true)().then(() => {}, () => {}); - (async () => true)().then(() => {}).catch(() => {}); - (async () => true)().then(() => {}).catch(() => {}).finally(() => {}); + (async () => true)().then( + () => {}, + () => {}, + ); + (async () => true)() + .then(() => {}) + .catch(() => {}); + (async () => true)() + .then(() => {}) + .catch(() => {}) + .finally(() => {}); (async () => true)().catch(() => {}); (async () => true)().finally(() => {}); return (async () => true)(); } -`, + `, ` async function test() { async function returnsPromise() {} await returnsPromise(); - returnsPromise().then(() => {}, () => {}); - returnsPromise().then(() => {}).catch(() => {}); - returnsPromise().then(() => {}).catch(() => {}).finally(() => {}); + returnsPromise().then( + () => {}, + () => {}, + ); + returnsPromise() + .then(() => {}) + .catch(() => {}); + returnsPromise() + .then(() => {}) + .catch(() => {}) + .finally(() => {}); returnsPromise().catch(() => {}); returnsPromise().finally(() => {}); return returnsPromise(); } -`, + `, ` async function test() { const x = Promise.resolve(); @@ -74,83 +106,120 @@ async function test() { y.catch(() => {}); y.finally(() => {}); } -`, + `, ` async function test() { Math.random() > 0.5 ? Promise.resolve().catch(() => {}) : null; } -`, + `, ` async function test() { Promise.resolve().catch(() => {}), 123; Promise.resolve().finally(() => {}), 123; - 123, Promise.resolve().then(() => {}, () => {}); - 123, Promise.resolve().then(() => {}, () => {}), 123; -} -`, + 123, + Promise.resolve().then( + () => {}, + () => {}, + ); + 123, + Promise.resolve().then( + () => {}, + () => {}, + ), + 123; +} + `, ` async function test() { void Promise.resolve().catch(() => {}); } -`, + `, ` async function test() { - Promise.resolve().catch(() => {}) || Promise.resolve().then(() => {}, () => {}); + Promise.resolve().catch(() => {}) || + Promise.resolve().then( + () => {}, + () => {}, + ); } -`, + `, ` async function test() { const promiseValue: Promise; await promiseValue; - promiseValue.then(() => {}, () => {}); + promiseValue.then( + () => {}, + () => {}, + ); promiseValue.then(() => {}).catch(() => {}); - promiseValue.then(() => {}).catch(() => {}).finally(() => {}); + promiseValue + .then(() => {}) + .catch(() => {}) + .finally(() => {}); promiseValue.catch(() => {}); promiseValue.finally(() => {}); return promiseValue; } -`, + `, ` async function test() { const promiseUnion: Promise | number; await promiseUnion; - promiseUnion.then(() => {}, () => {}); + promiseUnion.then( + () => {}, + () => {}, + ); promiseUnion.then(() => {}).catch(() => {}); - promiseUnion.then(() => {}).catch(() => {}).finally(() => {}); + promiseUnion + .then(() => {}) + .catch(() => {}) + .finally(() => {}); promiseUnion.catch(() => {}); promiseValue.finally(() => {}); return promiseUnion; } -`, + `, ` async function test() { const promiseIntersection: Promise & number; await promiseIntersection; - promiseIntersection.then(() => {}, () => {}); + promiseIntersection.then( + () => {}, + () => {}, + ); promiseIntersection.then(() => {}).catch(() => {}); - promiseIntersection.then(() => {}).catch(() => {}).finally(() => {}); + promiseIntersection + .then(() => {}) + .catch(() => {}) + .finally(() => {}); promiseIntersection.catch(() => {}); promiseIntersection.finally(() => {}); return promiseIntersection; } -`, + `, ` async function test() { class CanThen extends Promise {} const canThen: CanThen = Foo.resolve(2); await canThen; - canThen.then(() => {}, () => {}); + canThen.then( + () => {}, + () => {}, + ); canThen.then(() => {}).catch(() => {}); - canThen.then(() => {}).catch(() => {}).finally(() => {}); + canThen + .then(() => {}) + .catch(() => {}) + .finally(() => {}); canThen.catch(() => {}); canThen.finally(() => {}); return canThen; } -`, + `, ` async function test() { await (Math.random() > 0.5 ? numberPromise : 0); @@ -160,12 +229,14 @@ async function test() { const intersectionPromise: Promise & number; await intersectionPromise; } -`, + `, ` async function test() { class Thenable { - then(callback: () => {}): Thenable { return new Thenable(); } - }; + then(callback: () => {}): Thenable { + return new Thenable(); + } + } const thenable = new Thenable(); await thenable; @@ -173,14 +244,14 @@ async function test() { thenable.then(() => {}); return thenable; } -`, + `, ` async function test() { class NonFunctionParamThenable { then(param: string, param2: number): NonFunctionParamThenable { return new NonFunctionParamThenable(); } - }; + } const thenable = new NonFunctionParamThenable(); await thenable; @@ -188,30 +259,32 @@ async function test() { thenable.then('abc', 'def'); return thenable; } -`, + `, ` async function test() { - class NonFunctionThenable { then: number }; + class NonFunctionThenable { + then: number; + } const thenable = new NonFunctionThenable(); thenable; thenable.then; return thenable; } -`, + `, ` async function test() { class CatchableThenable { then(callback: () => {}, callback: () => {}): CatchableThenable { return new CatchableThenable(); } - }; + } const thenable = new CatchableThenable(); - await thenable + await thenable; return thenable; } -`, + `, ` // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/promise-polyfill/index.d.ts // Type definitions for promise-polyfill 6.0 @@ -221,7 +294,7 @@ async function test() { // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped interface PromisePolyfillConstructor extends PromiseConstructor { - _immediateFn?: (handler: (() => void) | string) => void; + _immediateFn?: (handler: (() => void) | string) => void; } declare const PromisePolyfill: PromisePolyfillConstructor; @@ -230,39 +303,50 @@ async function test() { const promise = new PromisePolyfill(() => {}); await promise; - promise.then(() => {}, () => {}); + promise.then( + () => {}, + () => {}, + ); promise.then(() => {}).catch(() => {}); - promise.then(() => {}).catch(() => {}).finally(() => {}); + promise + .then(() => {}) + .catch(() => {}) + .finally(() => {}); promise.catch(() => {}); promise.finally(() => {}); return promise; } -`, + `, // optional chaining ` async function test() { declare const returnsPromise: () => Promise | null; await returnsPromise?.(); - returnsPromise()?.then(() => {}, () => {}); - returnsPromise()?.then(() => {})?.catch(() => {}); + returnsPromise()?.then( + () => {}, + () => {}, + ); + returnsPromise() + ?.then(() => {}) + ?.catch(() => {}); returnsPromise()?.catch(() => {}); returnsPromise()?.finally(() => {}); return returnsPromise(); } - `, + `, ], invalid: [ { code: ` async function test() { - Promise.resolve("value"); - Promise.resolve("value").then(() => {}); - Promise.resolve("value").catch(); - Promise.resolve("value").finally(); + Promise.resolve('value'); + Promise.resolve('value').then(() => {}); + Promise.resolve('value').catch(); + Promise.resolve('value').finally(); } -`, + `, errors: [ { line: 3, @@ -286,9 +370,9 @@ async function test() { options: [{ ignoreVoid: true }], code: ` async function test() { - Promise.resolve("value"); + Promise.resolve('value'); } -`, + `.trimRight(), errors: [ { line: 3, @@ -298,9 +382,9 @@ async function test() { messageId: 'floatingFixVoid', output: ` async function test() { - void Promise.resolve("value"); + void Promise.resolve('value'); } -`, + `.trimRight(), }, ], }, @@ -309,12 +393,12 @@ async function test() { { code: ` async function test() { - Promise.reject(new Error("message")); - Promise.reject(new Error("message")).then(() => {}); - Promise.reject(new Error("message")).catch(); - Promise.reject(new Error("message")).finally(); + Promise.reject(new Error('message')); + Promise.reject(new Error('message')).then(() => {}); + Promise.reject(new Error('message')).catch(); + Promise.reject(new Error('message')).finally(); } -`, + `, errors: [ { line: 3, @@ -342,7 +426,7 @@ async function test() { (async () => true)().catch(); (async () => true)().finally(); } -`, + `, errors: [ { line: 3, @@ -372,7 +456,7 @@ async function test() { returnsPromise().catch(); returnsPromise().finally(); } -`, + `, errors: [ { line: 5, @@ -398,7 +482,7 @@ async function test() { Math.random() > 0.5 ? Promise.resolve() : null; Math.random() > 0.5 ? null : Promise.resolve(); } -`, + `, errors: [ { line: 3, @@ -413,11 +497,11 @@ async function test() { { code: ` async function test() { - Promise.resolve(), 123 - 123, Promise.resolve() - 123, Promise.resolve(), 123 + Promise.resolve(), 123; + 123, Promise.resolve(); + 123, Promise.resolve(), 123; } -`, + `, errors: [ { line: 3, @@ -438,7 +522,7 @@ async function test() { async function test() { void Promise.resolve(); } -`, + `, errors: [ { line: 3, @@ -452,7 +536,7 @@ async function test() { const obj = { foo: Promise.resolve() }; obj.foo; } -`, + `, errors: [ { line: 4, @@ -465,7 +549,7 @@ async function test() { async function test() { new Promise(resolve => resolve()); } -`, + `, errors: [ { line: 3, @@ -483,7 +567,7 @@ async function test() { promiseValue.catch(); promiseValue.finally(); } -`, + `, errors: [ { line: 5, @@ -510,7 +594,7 @@ async function test() { promiseUnion; } -`, + `, errors: [ { line: 5, @@ -524,11 +608,11 @@ async function test() { const promiseIntersection: Promise & number; promiseIntersection; - promiseIntersection.then(() => {}) + promiseIntersection.then(() => {}); promiseIntersection.catch(); promiseIntersection.finally(); } -`, + `, errors: [ { line: 5, @@ -559,7 +643,7 @@ async function test() { canThen.catch(); canThen.finally(); } -`, + `, errors: [ { line: 6, @@ -586,13 +670,13 @@ async function test() { then(callback: () => {}, callback: () => {}): CatchableThenable { return new CatchableThenable(); } - }; + } const thenable = new CatchableThenable(); thenable; thenable.then(() => {}); } -`, + `, errors: [ { line: 10, @@ -614,7 +698,7 @@ async function test() { // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped interface PromisePolyfillConstructor extends PromiseConstructor { - _immediateFn?: (handler: (() => void) | string) => void; + _immediateFn?: (handler: (() => void) | string) => void; } declare const PromisePolyfill: PromisePolyfillConstructor; @@ -626,7 +710,7 @@ async function test() { promise.then(() => {}); promise.catch(); } -`, + `, errors: [ { line: 18, From 24557041cbeccf71aeefd93de75cbee56e30e603 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:30:42 -0700 Subject: [PATCH 58/92] chore: no-extraneous-class --- .../tests/rules/no-extraneous-class.test.ts | 94 ++++++++++--------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts b/packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts index 88d558e711a8..2bb96f196ef1 100644 --- a/packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts @@ -20,26 +20,24 @@ ruleTester.run('no-extraneous-class', rule, { valid: [ ` class Foo { - public prop = 1; - constructor() {} + public prop = 1; + constructor() {} } -`, + `, ` export class CClass extends BaseClass { - public static helper(): void {} - private static privateHelper(): boolean { - return true; - } - constructor() {} + public static helper(): void {} + private static privateHelper(): boolean { + return true; + } + constructor() {} } -`, + `, ` class Foo { - constructor( - public bar: string - ) {} + constructor(public bar: string) {} } -`, + `, { code: 'class Foo {}', options: [{ allowEmpty: true }], @@ -47,24 +45,30 @@ class Foo { { code: ` class Foo { - constructor() {} + constructor() {} } -`, + `, options: [{ allowConstructorOnly: true }], }, { code: ` export class Bar { - public static helper(): void {} - private static privateHelper(): boolean { - return true; - } + public static helper(): void {} + private static privateHelper(): boolean { + return true; + } } -`, + `, options: [{ allowStaticOnly: true }], }, // https://github.com/typescript-eslint/typescript-eslint/issues/170 - 'export default class { hello() { return "I am foo!"; } }', + ` +export default class { + hello() { + return 'I am foo!'; + } +} + `, { code: ` @FooDecorator @@ -82,49 +86,51 @@ class Foo {} { code: ` class Foo { - public prop = 1; - constructor() { - class Bar { - static PROP = 2; - } + public prop = 1; + constructor() { + class Bar { + static PROP = 2; } + } } export class Bar { - public static helper(): void {} - private static privateHelper(): boolean { - return true; - } + public static helper(): void {} + private static privateHelper(): boolean { + return true; + } } -`, + `, errors: [onlyStatic, onlyStatic], }, { code: ` class Foo { - constructor() {} + constructor() {} } -`, + `, errors: [onlyConstructor], }, { code: ` export class AClass { - public static helper(): void {} - private static privateHelper(): boolean { - return true; - } - constructor() { - class nestedClass { - } - } + public static helper(): void {} + private static privateHelper(): boolean { + return true; + } + constructor() { + class nestedClass {} + } } - -`, + `, errors: [onlyStatic, empty], }, { // https://github.com/typescript-eslint/typescript-eslint/issues/170 - code: 'export default class { static hello() {} }', + code: ` +export default class { + static hello() {} +} + `, errors: [ { ...onlyStatic, From 6270d7e21a5e099f4e6387d742474d9099ef0a0a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:31:21 -0700 Subject: [PATCH 59/92] chore: no-extra-semi --- packages/eslint-plugin/tests/rules/no-extra-semi.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts b/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts index c1e07c186816..db3ca1735325 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-semi.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests semis, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/no-extra-semi'; import { RuleTester } from '../RuleTester'; From 3ce1cf3746a7ca2086db979553b6dd48fb457a2e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:32:06 -0700 Subject: [PATCH 60/92] chore: no-extra-parens --- .../eslint-plugin/tests/rules/no-extra-parens.test.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts index 29afab88ec55..03414b9bc490 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests extra parens, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/no-extra-parens'; import { RuleTester, batchedSingleLineTests } from '../RuleTester'; @@ -49,11 +54,11 @@ for (;(a = b);); options: ['all', { returnAssign: false }], }, { - code: `b => (b = 1);`, + code: 'b => (b = 1);', options: ['all', { returnAssign: false }], }, { - code: `b => b ? (c = d) : (c = e);`, + code: 'b => b ? (c = d) : (c = e);', options: ['all', { returnAssign: false }], }, ...batchedSingleLineTests({ From 457f5bf4cfa4c95bd8eedd0265a1174d0a5750ec Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:33:04 -0700 Subject: [PATCH 61/92] chore: no-extra-non-null-assertion --- .../rules/no-extra-non-null-assertion.test.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.test.ts index 51ff2d2e43f2..02da29353bfa 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/no-extra-non-null-assertion'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -17,7 +17,8 @@ const bar = foo!.bar; code: ` function foo(bar: number | undefined) { const bar: number = bar!; -} `, +} + `, }, { code: ` @@ -108,11 +109,11 @@ function foo(bar?: { n: number }) { }, // parentheses { - code: ` + code: noFormat` const foo: { bar: number } | null = null; const bar = (foo!)!.bar; `, - output: ` + output: noFormat` const foo: { bar: number } | null = null; const bar = (foo)!.bar; `, @@ -126,12 +127,12 @@ const bar = (foo)!.bar; ], }, { - code: ` + code: noFormat` function foo(bar?: { n: number }) { return (bar!)?.n; } `, - output: ` + output: noFormat` function foo(bar?: { n: number }) { return (bar)?.n; } @@ -146,12 +147,12 @@ function foo(bar?: { n: number }) { ], }, { - code: ` + code: noFormat` function foo(bar?: { n: number }) { return (bar)!?.n; } `, - output: ` + output: noFormat` function foo(bar?: { n: number }) { return (bar)?.n; } @@ -166,12 +167,12 @@ function foo(bar?: { n: number }) { ], }, { - code: ` + code: noFormat` function foo(bar?: { n: number }) { return (bar!)?.(); } `, - output: ` + output: noFormat` function foo(bar?: { n: number }) { return (bar)?.(); } From 89a23203c49d9de0176f79ccf4a01af5327fe73f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:33:34 -0700 Subject: [PATCH 62/92] chore: no-explicit-any --- .../tests/rules/no-explicit-any.test.ts | 185 ++++++++++-------- 1 file changed, 101 insertions(+), 84 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts b/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts index fa68b09919eb..dde8b938175a 100644 --- a/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts +++ b/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts @@ -11,7 +11,7 @@ const ruleTester = new RuleTester({ ruleTester.run('no-explicit-any', rule, { valid: [ - 'const number: number = 1', + 'const number: number = 1;', 'function greet(): string {}', 'function greet(): Array {}', 'function greet(): string[] {}', @@ -20,119 +20,119 @@ ruleTester.run('no-explicit-any', rule, { 'function greet(param: Array): Array {}', ` class Greeter { - message: string; + message: string; } - `, + `, ` class Greeter { - message: Array; + message: Array; } - `, + `, ` class Greeter { - message: string[]; + message: string[]; } - `, + `, ` class Greeter { - message: Array>; + message: Array>; } - `, + `, ` class Greeter { - message: Array; + message: Array; } - `, + `, ` interface Greeter { - message: string; + message: string; } - `, + `, ` interface Greeter { - message: Array; + message: Array; } - `, + `, ` interface Greeter { - message: string[]; + message: string[]; } - `, + `, ` interface Greeter { - message: Array>; + message: Array>; } - `, + `, ` interface Greeter { - message: Array; + message: Array; } - `, + `, ` type obj = { - message: string; -} - `, + message: string; +}; + `, ` type obj = { - message: Array; -} - `, + message: Array; +}; + `, ` type obj = { - message: string[]; -} - `, + message: string[]; +}; + `, ` type obj = { - message: Array>; -} - `, + message: Array>; +}; + `, ` type obj = { - message: Array; -} - `, + message: Array; +}; + `, ` type obj = { - message: string | number; -} - `, + message: string | number; +}; + `, ` type obj = { - message: string | Array; -} - `, + message: string | Array; +}; + `, ` type obj = { - message: string | string[]; -} - `, + message: string | string[]; +}; + `, ` type obj = { - message: string | Array>; -} - `, + message: string | Array>; +}; + `, ` type obj = { - message: string & number; -} - `, + message: string & number; +}; + `, ` type obj = { - message: string & Array; -} - `, + message: string & Array; +}; + `, ` type obj = { - message: string & string[]; -} - `, + message: string & string[]; +}; + `, ` type obj = { - message: string & Array>; -} - `, + message: string & Array>; +}; + `, // https://github.com/eslint/typescript-eslint-parser/issues/397 { code: ` @@ -143,99 +143,116 @@ type obj = { options: [{ ignoreRestArgs: true }], }, { - code: `function foo1(...args: any[]) {}`, + code: 'function foo1(...args: any[]) {}', options: [{ ignoreRestArgs: true }], }, { - code: `const bar1 = function (...args: any[]) {}`, + code: 'const bar1 = function(...args: any[]) {};', options: [{ ignoreRestArgs: true }], }, { - code: `const baz1 = (...args: any[]) => {}`, + code: 'const baz1 = (...args: any[]) => {};', options: [{ ignoreRestArgs: true }], }, { - code: `function foo2(...args: readonly any[]) {}`, + code: 'function foo2(...args: readonly any[]) {}', options: [{ ignoreRestArgs: true }], }, { - code: `const bar2 = function (...args: readonly any[]) {}`, + code: 'const bar2 = function(...args: readonly any[]) {};', options: [{ ignoreRestArgs: true }], }, { - code: `const baz2 = (...args: readonly any[]) => {}`, + code: 'const baz2 = (...args: readonly any[]) => {};', options: [{ ignoreRestArgs: true }], }, { - code: `function foo3(...args: Array) {}`, + code: 'function foo3(...args: Array) {}', options: [{ ignoreRestArgs: true }], }, { - code: `const bar3 = function (...args: Array) {}`, + code: 'const bar3 = function(...args: Array) {};', options: [{ ignoreRestArgs: true }], }, { - code: `const baz3 = (...args: Array) => {}`, + code: 'const baz3 = (...args: Array) => {};', options: [{ ignoreRestArgs: true }], }, { - code: `function foo4(...args: ReadonlyArray) {}`, + code: 'function foo4(...args: ReadonlyArray) {}', options: [{ ignoreRestArgs: true }], }, { - code: `const bar4 = function (...args: ReadonlyArray) {}`, + code: 'const bar4 = function(...args: ReadonlyArray) {};', options: [{ ignoreRestArgs: true }], }, { - code: `const baz4 = (...args: ReadonlyArray) => {}`, + code: 'const baz4 = (...args: ReadonlyArray) => {};', options: [{ ignoreRestArgs: true }], }, { - code: `interface Qux1 { (...args: any[]): void; }`, + code: ` +interface Qux1 { + (...args: any[]): void; +} + `, options: [{ ignoreRestArgs: true }], }, { - code: `interface Qux2 { (...args: readonly any[]): void; }`, + code: ` +interface Qux2 { + (...args: readonly any[]): void; +} + `, options: [{ ignoreRestArgs: true }], }, { - code: `interface Qux3 { (...args: Array): void; }`, + code: ` +interface Qux3 { + (...args: Array): void; +} + `, options: [{ ignoreRestArgs: true }], }, { - code: `interface Qux4 { (...args: ReadonlyArray): void; }`, + code: ` +interface Qux4 { + (...args: ReadonlyArray): void; +} + `, options: [{ ignoreRestArgs: true }], }, { - code: `function quux1(fn: (...args: any[]) => void): void {}`, + code: 'function quux1(fn: (...args: any[]) => void): void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quux2(fn: (...args: readonly any[]) => void): void {}`, + code: 'function quux2(fn: (...args: readonly any[]) => void): void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quux3(fn: (...args: Array) => void): void {}`, + code: 'function quux3(fn: (...args: Array) => void): void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quux4(fn: (...args: ReadonlyArray) => void): void {}`, + code: + 'function quux4(fn: (...args: ReadonlyArray) => void): void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quuz1(): ((...args: any[]) => void) {}`, + code: 'function quuz1(): (...args: any[]) => void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quuz2(): ((...args: readonly any[]) => void) {}`, + code: 'function quuz2(): (...args: readonly any[]) => void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quuz3(): ((...args: Array) => void) {}`, + code: 'function quuz3(): (...args: Array) => void {}', options: [{ ignoreRestArgs: true }], }, { - code: `function quuz4(): ((...args: ReadonlyArray) => void) {}`, + code: 'function quuz4(): (...args: ReadonlyArray) => void {}', options: [{ ignoreRestArgs: true }], }, ], From 5843fd25c0c6d88419f60e65e15894ba368df265 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:35:43 -0700 Subject: [PATCH 63/92] chore: no-empty-interface --- .../tests/rules/no-empty-interface.test.ts | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-empty-interface.test.ts b/packages/eslint-plugin/tests/rules/no-empty-interface.test.ts index f02d753dee1e..1a69e06f140e 100644 --- a/packages/eslint-plugin/tests/rules/no-empty-interface.test.ts +++ b/packages/eslint-plugin/tests/rules/no-empty-interface.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/no-empty-interface'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -9,29 +9,29 @@ ruleTester.run('no-empty-interface', rule, { valid: [ ` interface Foo { - name: string; + name: string; } - `, + `, ` interface Foo { - name: string; + name: string; } interface Bar { - age: number; + age: number; } // valid because extending multiple interfaces can be used instead of a union type interface Baz extends Foo, Bar {} - `, + `, { code: ` interface Foo { - name: string; + name: string; } interface Bar extends Foo {} - `, + `, options: [{ allowSingleExtends: true }], }, ], @@ -47,7 +47,7 @@ interface Bar extends Foo {} ], }, { - code: 'interface Foo extends {}', + code: noFormat`interface Foo extends {}`, errors: [ { messageId: 'noEmpty', @@ -59,11 +59,11 @@ interface Bar extends Foo {} { code: ` interface Foo { - name: string; + name: string; } interface Bar extends Foo {} - `, + `, options: [{ allowSingleExtends: false }], errors: [ { @@ -75,7 +75,7 @@ interface Bar extends Foo {} }, { code: 'interface Foo extends Array {}', - output: 'type Foo = Array', + output: noFormat`type Foo = Array`, errors: [ { messageId: 'noEmptyWithSuper', @@ -85,8 +85,8 @@ interface Bar extends Foo {} ], }, { - code: 'interface Foo extends Array { }', - output: 'type Foo = Array', + code: 'interface Foo extends Array {}', + output: noFormat`type Foo = Array`, errors: [ { messageId: 'noEmptyWithSuper', @@ -101,13 +101,13 @@ interface Bar { bar: string; } interface Foo extends Array {} -`, - output: ` + `, + output: noFormat` interface Bar { bar: string; } type Foo = Array -`, + `, errors: [ { messageId: 'noEmptyWithSuper', @@ -119,10 +119,12 @@ type Foo = Array { code: ` type R = Record; -interface Foo extends R { };`, - output: ` +interface Foo extends R {} + `, + output: noFormat` type R = Record; -type Foo = R;`, +type Foo = R + `, errors: [ { messageId: 'noEmptyWithSuper', From 7fc5c9767f3f1151c24a3ba603443c57326fad90 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:37:46 -0700 Subject: [PATCH 64/92] chore: no-empty-function --- .../tests/rules/no-empty-function.test.ts | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-empty-function.test.ts b/packages/eslint-plugin/tests/rules/no-empty-function.test.ts index 9b5ad3507d3a..95b8476689a0 100644 --- a/packages/eslint-plugin/tests/rules/no-empty-function.test.ts +++ b/packages/eslint-plugin/tests/rules/no-empty-function.test.ts @@ -59,32 +59,36 @@ function foo() { invalid: [ { - code: `class Person { - constructor(name: string) {} - }`, + code: ` +class Person { + constructor(name: string) {} +} + `, errors: [ { messageId: 'unexpected', data: { name: 'constructor', }, - line: 2, - column: 35, + line: 3, + column: 29, }, ], }, { - code: `class Person { - otherMethod(name: string) {} - }`, + code: ` +class Person { + otherMethod(name: string) {} +} + `, errors: [ { messageId: 'unexpected', data: { name: "method 'otherMethod'", }, - line: 2, - column: 35, + line: 3, + column: 29, }, ], }, @@ -124,8 +128,7 @@ class Foo { }, { code: ` -function foo() { -} +function foo() {} `, errors: [ { From da553db65b6b69a188672ee783f391aa2b7c3326 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:38:05 -0700 Subject: [PATCH 65/92] chore: no-dynamic-delete --- .../tests/rules/no-dynamic-delete.test.ts | 200 +++++++++++------- 1 file changed, 122 insertions(+), 78 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-dynamic-delete.test.ts b/packages/eslint-plugin/tests/rules/no-dynamic-delete.test.ts index 5a88fbff10ca..798971e90f18 100644 --- a/packages/eslint-plugin/tests/rules/no-dynamic-delete.test.ts +++ b/packages/eslint-plugin/tests/rules/no-dynamic-delete.test.ts @@ -13,114 +13,158 @@ const ruleTester = new RuleTester({ ruleTester.run('no-dynamic-delete', rule, { valid: [ - `const container: { [i: string]: 0 } = {}; - delete container.aaa;`, - `const container: { [i: string]: 0 } = {}; - delete container.delete;`, - `const container: { [i: string]: 0 } = {}; - delete container[7];`, - `const container: { [i: string]: 0 } = {}; - delete container[-7];`, - `const container: { [i: string]: 0 } = {}; - delete container[+7];`, - `const container: { [i: string]: 0 } = {}; - delete container['-Infinity'];`, - `const container: { [i: string]: 0 } = {}; - delete container['+Infinity'];`, - `const value = 1; - delete value;`, - `const value = 1; - delete -value;`, + ` +const container: { [i: string]: 0 } = {}; +delete container.aaa; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container.delete; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container[7]; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container[-7]; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container[+7]; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container['-Infinity']; + `, + ` +const container: { [i: string]: 0 } = {}; +delete container['+Infinity']; + `, + ` +const value = 1; +delete value; + `, + ` +const value = 1; +delete -value; + `, ], invalid: [ { - code: `const container: { [i: string]: 0 } = {}; - delete container['aaa'];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container['aaa']; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container.aaa;`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container.aaa; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container [ 'aaa' ] ;`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container['aa' + 'b']; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container .aaa ;`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container['aa' + 'b']; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container['aa' + 'b'];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container['delete']; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container['aa' + 'b'];`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container.delete; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container['delete'];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container[-Infinity]; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container.delete;`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container[-Infinity]; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container[-Infinity];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container[+Infinity]; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container[-Infinity];`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container[+Infinity]; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container[+Infinity];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container[NaN]; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container[+Infinity];`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container[NaN]; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container[NaN];`, + code: ` +const container: { [i: string]: 0 } = {}; +delete container['NaN']; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container[NaN];`, + output: ` +const container: { [i: string]: 0 } = {}; +delete container.NaN; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container['NaN'];`, + code: ` +const container: { [i: string]: 0 } = {}; +const name = 'name'; +delete container[name]; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container.NaN;`, + output: ` +const container: { [i: string]: 0 } = {}; +const name = 'name'; +delete container[name]; + `, }, { - code: `const container: { [i: string]: 0 } = {}; - delete container [ 'NaN' ] ;`, + code: ` +const container: { [i: string]: 0 } = {}; +const getName = () => 'aaa'; +delete container[getName()]; + `, + output: ` +const container: { [i: string]: 0 } = {}; +const getName = () => 'aaa'; +delete container[getName()]; + `, errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - delete container .NaN ;`, }, { - code: `const container: { [i: string]: 0 } = {}; - const name = 'name'; - delete container[name];`, - errors: [{ messageId: 'dynamicDelete' }], - output: `const container: { [i: string]: 0 } = {}; - const name = 'name'; - delete container[name];`, - }, - { - code: `const container: { [i: string]: 0 } = {}; - const getName = () => 'aaa'; - delete container[getName()];`, - output: `const container: { [i: string]: 0 } = {}; - const getName = () => 'aaa'; - delete container[getName()];`, - errors: [{ messageId: 'dynamicDelete' }], - }, - { - code: `const container: { [i: string]: 0 } = {}; - const name = { foo: { bar: 'bar' } }; - delete container[name.foo.bar];`, - output: `const container: { [i: string]: 0 } = {}; - const name = { foo: { bar: 'bar' } }; - delete container[name.foo.bar];`, + code: ` +const container: { [i: string]: 0 } = {}; +const name = { foo: { bar: 'bar' } }; +delete container[name.foo.bar]; + `, + output: ` +const container: { [i: string]: 0 } = {}; +const name = { foo: { bar: 'bar' } }; +delete container[name.foo.bar]; + `, errors: [{ messageId: 'dynamicDelete' }], }, ], From 223dc75f742e933d3cd468e86098efe365c7b1ce Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:40:38 -0700 Subject: [PATCH 66/92] chore: no-dupe-class-members --- .../tests/rules/no-dupe-class-members.test.ts | 153 ++++++++++++++---- 1 file changed, 125 insertions(+), 28 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-dupe-class-members.test.ts b/packages/eslint-plugin/tests/rules/no-dupe-class-members.test.ts index 72430c6e39b7..50cbdba54107 100644 --- a/packages/eslint-plugin/tests/rules/no-dupe-class-members.test.ts +++ b/packages/eslint-plugin/tests/rules/no-dupe-class-members.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/no-dupe-class-members'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -7,16 +7,72 @@ const ruleTester = new RuleTester({ ruleTester.run('no-dupe-class-members', rule, { valid: [ - 'class A { foo() {} bar() {} }', - 'class A { static foo() {} foo() {} }', - 'class A { get foo() {} set foo(value) {} }', - 'class A { static foo() {} get foo() {} set foo(value) {} }', - 'class A { foo() { } } class B { foo() { } }', - 'class A { [foo]() {} foo() {} }', - "class A { 'foo'() {} 'bar'() {} baz() {} }", - "class A { *'foo'() {} *'bar'() {} *baz() {} }", - "class A { get 'foo'() {} get 'bar'() {} get baz() {} }", - 'class A { 1() {} 2() {} }', + ` +class A { + foo() {} + bar() {} +} + `, + ` +class A { + static foo() {} + foo() {} +} + `, + ` +class A { + get foo() {} + set foo(value) {} +} + `, + ` +class A { + static foo() {} + get foo() {} + set foo(value) {} +} + `, + ` +class A { + foo() {} +} +class B { + foo() {} +} + `, + ` +class A { + [foo]() {} + foo() {} +} + `, + ` +class A { + foo() {} + bar() {} + baz() {} +} + `, + ` +class A { + *foo() {} + *bar() {} + *baz() {} +} + `, + ` +class A { + get foo() {} + get bar() {} + get baz() {} +} + `, + ` +class A { + 1() {} + 2() {} +} + `, ` class Foo { foo(a: string): string; @@ -27,52 +83,93 @@ ruleTester.run('no-dupe-class-members', rule, { ], invalid: [ { - code: 'class A { foo() {} foo() {} }', + code: ` +class A { + foo() {} + foo() {} +} + `, errors: [ - { line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: '!class A { foo() {} foo() {} };', + code: ` +!class A { + foo() {} + foo() {} +}; + `, errors: [ - { line: 1, column: 21, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: "class A { 'foo'() {} 'foo'() {} }", + code: noFormat` +class A { + 'foo'() {} + 'foo'() {} +} + `, errors: [ - { line: 1, column: 22, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: 'class A { 10() {} 1e1() {} }', + code: ` +class A { + 10() {} + 1e1() {} +} + `, errors: [ - { line: 1, column: 19, messageId: 'unexpected', data: { name: '10' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: '10' } }, ], }, { - code: 'class A { foo() {} foo() {} foo() {} }', + code: ` +class A { + foo() {} + foo() {} + foo() {} +} + `, errors: [ - { line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, - { line: 1, column: 29, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 5, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: 'class A { static foo() {} static foo() {} }', + code: ` +class A { + static foo() {} + static foo() {} +} + `, errors: [ - { line: 1, column: 27, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: 'class A { foo() {} get foo() {} }', + code: ` +class A { + foo() {} + get foo() {} +} + `, errors: [ - { line: 1, column: 20, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, { - code: 'class A { set foo(value) {} foo() {} }', + code: ` +class A { + set foo(value) {} + foo() {} +} + `, errors: [ - { line: 1, column: 29, messageId: 'unexpected', data: { name: 'foo' } }, + { line: 4, column: 3, messageId: 'unexpected', data: { name: 'foo' } }, ], }, ], From 6a2cedd22b873ae8e456c56bc7e86327c85067e2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:41:14 -0700 Subject: [PATCH 67/92] chore: no-base-to-string --- .../tests/rules/no-base-to-string.test.ts | 114 ++++++++++-------- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts b/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts index 3be0828791df..cd2e4b166a90 100644 --- a/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts +++ b/packages/eslint-plugin/tests/rules/no-base-to-string.test.ts @@ -13,53 +13,67 @@ const ruleTester = new RuleTester({ ruleTester.run('no-base-to-string', rule, { valid: [ - `\`\${""}\``, - `\`\${true}\``, - `\`\${[]}\``, - `\`\${function () {}}\``, - `"" + ""`, - `"" + true`, - `"" + []`, - `true + true`, - `true + ""`, - `true + []`, - `[] + []`, - `[] + true`, - `[] + ""`, - `({}).constructor()`, - `"text".toString()`, - `false.toString()`, - `let value = 1; - value.toString()`, - `let value = 1n; - value.toString()`, - `function someFunction() { } - someFunction.toString();`, - 'unknownObject.toString()', - 'unknownObject.someOtherMethod()', + "`${''}`;", + '`${true}`;', + '`${[]}`;', + '`${function() {}}`;', + "'' + '';", + "'' + true;", + "'' + [];", + 'true + true;', + "true + '';", + 'true + [];', + '[] + [];', + '[] + true;', + "[] + '';", + '({}.constructor());', + "'text'.toString();", + 'false.toString();', + ` +let value = 1; +value.toString(); + `, + ` +let value = 1n; +value.toString(); + `, + ` +function someFunction() {} +someFunction.toString(); + `, + 'unknownObject.toString();', + 'unknownObject.someOtherMethod();', '(() => {}).toString();', - `class CustomToString { toString() { return "Hello, world!"; } } - "" + (new CustomToString());`, - `const literalWithToString = { - toString: () => "Hello, world!", - }; - "" + literalToString;`, - `let _ = {} * {}`, - `let _ = {} / {}`, - `let _ = {} *= {}`, - `let _ = {} /= {}`, - `let _ = {} = {}`, - `let _ = {} == {}`, - `let _ = {} === {}`, - `let _ = {} in {}`, - `let _ = {} & {}`, - `let _ = {} ^ {}`, - `let _ = {} << {}`, - `let _ = {} >> {}`, + ` +class CustomToString { + toString() { + return 'Hello, world!'; + } +} +'' + new CustomToString(); + `, + ` +const literalWithToString = { + toString: () => 'Hello, world!', +}; +'' + literalToString; + `, + 'let _ = {} * {};', + 'let _ = {} / {};', + 'let _ = ({} *= {});', + 'let _ = ({} /= {});', + 'let _ = ({} = {});', + 'let _ = {} == {};', + 'let _ = {} === {};', + 'let _ = {} in {};', + 'let _ = {} & {};', + 'let _ = {} ^ {};', + 'let _ = {} << {};', + 'let _ = {} >> {};', ], invalid: [ { - code: `\`\${{}})\``, + code: '`${{}})`;', errors: [ { data: { @@ -71,7 +85,7 @@ ruleTester.run('no-base-to-string', rule, { ], }, { - code: `({}).toString()`, + code: '({}.toString());', errors: [ { data: { @@ -83,7 +97,7 @@ ruleTester.run('no-base-to-string', rule, { ], }, { - code: `"" + {}`, + code: "'' + {};", errors: [ { data: { @@ -95,7 +109,7 @@ ruleTester.run('no-base-to-string', rule, { ], }, { - code: `"" += {}`, + code: "'' += {};", errors: [ { data: { @@ -108,7 +122,7 @@ ruleTester.run('no-base-to-string', rule, { }, { code: ` - let someObjectOrString = Math.random() ? { a: true } : "text"; + let someObjectOrString = Math.random() ? { a: true } : 'text'; someObjectOrString.toString(); `, errors: [ @@ -123,8 +137,8 @@ ruleTester.run('no-base-to-string', rule, { }, { code: ` - let someObjectOrString = Math.random() ? { a: true } : "text"; - someObjectOrString + ""; + let someObjectOrString = Math.random() ? { a: true } : 'text'; + someObjectOrString + ''; `, errors: [ { @@ -154,7 +168,7 @@ ruleTester.run('no-base-to-string', rule, { { code: ` let someObjectOrObject = Math.random() ? { a: true, b: true } : { a: true }; - someObjectOrObject + ""; + someObjectOrObject + ''; `, errors: [ { From 7519726384e51685d5c9d2984c92199f889b72b2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:41:32 -0700 Subject: [PATCH 68/92] chore: no-array-constructor --- .../tests/rules/no-array-constructor.test.ts | 94 ++++++++----------- 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts index cc6530085ddf..af760d299992 100644 --- a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts +++ b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts @@ -10,38 +10,38 @@ const messageId = 'useLiteral' as const; ruleTester.run('no-array-constructor', rule, { valid: [ - 'new Array(x)', - 'Array(x)', - 'new Array(9)', - 'Array(9)', - 'new foo.Array()', - 'foo.Array()', - 'new Array.foo', - 'Array.foo()', + 'new Array(x);', + 'Array(x);', + 'new Array(9);', + 'Array(9);', + 'new foo.Array();', + 'foo.Array();', + 'new Array.foo();', + 'Array.foo();', // TypeScript - 'new Array(1, 2, 3)', - 'new Array()', - 'Array(1, 2, 3)', - 'Array()', + 'new Array(1, 2, 3);', + 'new Array();', + 'Array(1, 2, 3);', + 'Array();', // optional chain - 'Array?.(x)', - 'Array?.(9)', - 'foo?.Array()', - 'Array?.foo()', - 'foo.Array?.()', - 'Array.foo?.()', - 'Array?.(1, 2, 3)', - 'Array?.()', - 'Array?.(0, 1, 2)', - 'Array?.(x, y)', + 'Array?.(x);', + 'Array?.(9);', + 'foo?.Array();', + 'Array?.foo();', + 'foo.Array?.();', + 'Array.foo?.();', + 'Array?.(1, 2, 3);', + 'Array?.();', + 'Array?.(0, 1, 2);', + 'Array?.(x, y);', ], invalid: [ { - code: 'new Array()', - output: '[]', + code: 'new Array();', + output: '[];', errors: [ { messageId, @@ -50,8 +50,8 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: 'Array()', - output: '[]', + code: 'Array();', + output: '[];', errors: [ { messageId, @@ -60,8 +60,8 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: 'new Array', - output: '[]', + code: 'new Array(x, y);', + output: '[x, y];', errors: [ { messageId, @@ -70,18 +70,8 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: 'new Array(x, y)', - output: '[x, y]', - errors: [ - { - messageId, - type: AST_NODE_TYPES.NewExpression, - }, - ], - }, - { - code: 'Array(x, y)', - output: '[x, y]', + code: 'Array(x, y);', + output: '[x, y];', errors: [ { messageId, @@ -90,8 +80,8 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: 'new Array(0, 1, 2)', - output: '[0, 1, 2]', + code: 'new Array(0, 1, 2);', + output: '[0, 1, 2];', errors: [ { messageId, @@ -100,8 +90,8 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: 'Array(0, 1, 2)', - output: '[0, 1, 2]', + code: 'Array(0, 1, 2);', + output: '[0, 1, 2];', errors: [ { messageId, @@ -110,16 +100,12 @@ ruleTester.run('no-array-constructor', rule, { ], }, { - code: `new Array( - 0, - 1, - 2 - )`, - output: `[ - 0, - 1, - 2 - ]`, + code: ` +new Array(0, 1, 2); + `, + output: ` +[0, 1, 2]; + `, errors: [ { messageId, From 0d43c32eb4c0af0c1f5baf4c06bd897eef7773d8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:42:08 -0700 Subject: [PATCH 69/92] chore: naming-convention --- .../tests/rules/naming-convention.test.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index 0df5f73d4870..b4326c8fe754 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -608,7 +608,7 @@ const cases: Cases = [ ruleTester.run('naming-convention', rule, { valid: [ - `const x = 1;`, // no options shouldn't crash + 'const x = 1;', // no options shouldn't crash ...createValidTestCases(cases), { code: ` @@ -825,10 +825,14 @@ ruleTester.run('naming-convention', rule, { }, { code: ` - declare const function_camelCase1: (() => void); + declare const function_camelCase1: () => void; declare const function_camelCase2: (() => void) | null; declare const function_camelCase3: (() => void) | null | undefined; - declare const function_camelCase4: (() => void) | (() => string) | null | undefined; + declare const function_camelCase4: + | (() => void) + | (() => string) + | null + | undefined; `, options: [ { @@ -847,11 +851,20 @@ ruleTester.run('naming-convention', rule, { declare const array_camelCase2: ReadonlyArray | null; declare const array_camelCase3: number[] | null | undefined; declare const array_camelCase4: readonly number[] | null | undefined; - declare const array_camelCase5: number[] | (number | string)[] | null | undefined; + declare const array_camelCase5: + | number[] + | (number | string)[] + | null + | undefined; declare const array_camelCase6: [] | null | undefined; declare const array_camelCase7: [number] | null | undefined; - declare const array_camelCase8: readonly number[] | Array | [boolean] | null | undefined; + declare const array_camelCase8: + | readonly number[] + | Array + | [boolean] + | null + | undefined; `, options: [ { From 2caf49f3233ab6035af6b112ff737a48e099d51f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:45:49 -0700 Subject: [PATCH 70/92] chore: member-ordering --- packages/eslint-plugin/tests/rules/member-ordering.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index 5307793c3d7f..ddea73e8d1d9 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -1,3 +1,6 @@ +// TODO - migrate this test to the rule +/* eslint-disable @typescript-eslint/internal/plugin-test-formatting */ + import rule from '../../src/rules/member-ordering'; import { RuleTester } from '../RuleTester'; From 7ebfafe0b96501b610623fe244f797c1a921f382 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:49:33 -0700 Subject: [PATCH 71/92] chore: member-naming --- .../tests/rules/member-naming.test.ts | 186 +++++++++++------- 1 file changed, 118 insertions(+), 68 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/member-naming.test.ts b/packages/eslint-plugin/tests/rules/member-naming.test.ts index 851d70ea706c..e3befcffbe1a 100644 --- a/packages/eslint-plugin/tests/rules/member-naming.test.ts +++ b/packages/eslint-plugin/tests/rules/member-naming.test.ts @@ -8,42 +8,72 @@ const ruleTester = new RuleTester({ ruleTester.run('member-naming', rule, { valid: [ { - code: `class Class { _fooBar() {} }`, + code: ` +class Class { + _fooBar() {} +} + `, options: [{ public: '^_' }], }, { - code: `class Class { private constructor(); _fooBar() {} }`, + code: ` +class Class { + private constructor(); + _fooBar() {} +} + `, options: [{ private: '^_' }], }, { - code: `class Class { constructor() {}; _fooBar() {} }`, + code: ` +class Class { + constructor() {} + _fooBar() {} +} + `, options: [{ public: '^_' }], }, { - code: `class Class { public _fooBar() {} }`, + code: ` +class Class { + public _fooBar() {} +} + `, options: [{ public: '^_' }], }, { - code: `class Class { protected _fooBar() {} }`, + code: ` +class Class { + protected _fooBar() {} +} + `, options: [{ protected: '^_' }], }, { - code: `class Class { private _fooBar() {} }`, + code: ` +class Class { + private _fooBar() {} +} + `, options: [{ private: '^_' }], }, { - code: `class Class { protected fooBar() {} }`, + code: ` +class Class { + protected fooBar() {} +} + `, options: [{ private: '^_' }], }, { code: ` class Class { - pubOne() {} - public pubTwo() {} - protected protThree() {} - private privFour() {} + pubOne() {} + public pubTwo() {} + protected protThree() {} + private privFour() {} } - `, + `, options: [ { public: '^pub[A-Z]', @@ -55,12 +85,12 @@ class Class { { code: ` class Class { - pubOne: string; - public pubTwo: string; - protected protThree: string; - private privFour: string; + pubOne: string; + public pubTwo: string; + protected protThree: string; + private privFour: string; } - `, + `, options: [ { public: '^pub[A-Z]', @@ -72,12 +102,12 @@ class Class { { code: ` class Class { - pubOne = true; - public pubTwo = true; - protected protThree = true; - private privFour = true; + pubOne = true; + public pubTwo = true; + protected protThree = true; + private privFour = true; } - `, + `, options: [ { public: '^pub[A-Z]', @@ -90,7 +120,11 @@ class Class { { code: ` class Test { - constructor(public __a: string, protected __b: string, private __c: string = 100) {} + constructor( + public __a: string, + protected __b: string, + private __c: string = 100, + ) {} } `, options: [ @@ -106,14 +140,18 @@ class Test { // Semantically invalid test case, TS has to throw an error. ` class Foo { - constructor(private ...name: string[], private [test]: [string]) {} + constructor(private ...name: string[], private [test]: [string]) {} } - `, + `, }, ], invalid: [ { - code: `class Class { fooBar() {} }`, + code: ` +class Class { + fooBar() {} +} + `, options: [{ public: '^_' }], errors: [ { @@ -123,13 +161,17 @@ class Foo { convention: '/^_/', name: 'fooBar', }, - line: 1, - column: 15, + line: 3, + column: 3, }, ], }, { - code: `class Class { public fooBar() {} }`, + code: ` +class Class { + public fooBar() {} +} + `, options: [{ public: '^_' }], errors: [ { @@ -139,13 +181,17 @@ class Foo { convention: '/^_/', name: 'fooBar', }, - line: 1, - column: 22, + line: 3, + column: 10, }, ], }, { - code: `class Class { protected fooBar() {} }`, + code: ` +class Class { + protected fooBar() {} +} + `, options: [{ protected: '^_' }], errors: [ { @@ -155,13 +201,17 @@ class Foo { convention: '/^_/', name: 'fooBar', }, - line: 1, - column: 25, + line: 3, + column: 13, }, ], }, { - code: `class Class { private fooBar() {} }`, + code: ` +class Class { + private fooBar() {} +} + `, options: [{ private: '^_' }], errors: [ { @@ -171,20 +221,20 @@ class Foo { convention: '/^_/', name: 'fooBar', }, - line: 1, - column: 23, + line: 3, + column: 11, }, ], }, { code: ` class Class { - one() {} - public two() {} - protected three() {} - private four() {} + one() {} + public two() {} + protected three() {} + private four() {} } - `, + `, options: [ { public: '^pub[A-Z]', @@ -201,7 +251,7 @@ class Class { name: 'one', }, line: 3, - column: 5, + column: 3, }, { messageId: 'incorrectName', @@ -211,7 +261,7 @@ class Class { name: 'two', }, line: 4, - column: 12, + column: 10, }, { messageId: 'incorrectName', @@ -221,7 +271,7 @@ class Class { name: 'three', }, line: 5, - column: 15, + column: 13, }, { messageId: 'incorrectName', @@ -231,19 +281,19 @@ class Class { name: 'four', }, line: 6, - column: 13, + column: 11, }, ], }, { code: ` class Class { - one: string; - public two: string; - protected three: string; - private four: string; + one: string; + public two: string; + protected three: string; + private four: string; } - `, + `, options: [ { public: '^pub[A-Z]', @@ -260,7 +310,7 @@ class Class { name: 'one', }, line: 3, - column: 5, + column: 3, }, { messageId: 'incorrectName', @@ -270,7 +320,7 @@ class Class { name: 'two', }, line: 4, - column: 12, + column: 10, }, { messageId: 'incorrectName', @@ -280,7 +330,7 @@ class Class { name: 'three', }, line: 5, - column: 15, + column: 13, }, { messageId: 'incorrectName', @@ -290,19 +340,19 @@ class Class { name: 'four', }, line: 6, - column: 13, + column: 11, }, ], }, { code: ` class Class { - one = true; - public two = true; - protected three = true; - private four = true; + one = true; + public two = true; + protected three = true; + private four = true; } - `, + `, options: [ { public: '^pub[A-Z]', @@ -319,7 +369,7 @@ class Class { name: 'one', }, line: 3, - column: 5, + column: 3, }, { messageId: 'incorrectName', @@ -329,7 +379,7 @@ class Class { name: 'two', }, line: 4, - column: 12, + column: 10, }, { messageId: 'incorrectName', @@ -339,7 +389,7 @@ class Class { name: 'three', }, line: 5, - column: 15, + column: 13, }, { messageId: 'incorrectName', @@ -349,14 +399,14 @@ class Class { name: 'four', }, line: 6, - column: 13, + column: 11, }, ], }, { code: ` class Test { - constructor(public a: string, protected b: string, private c: string = 100) {} + constructor(public a: string, protected b: string, private c: string = 100) {} } `, options: [ @@ -375,7 +425,7 @@ class Test { name: 'a', }, line: 3, - column: 24, + column: 22, }, { messageId: 'incorrectName', @@ -385,7 +435,7 @@ class Test { name: 'b', }, line: 3, - column: 45, + column: 43, }, { messageId: 'incorrectName', @@ -395,7 +445,7 @@ class Test { name: 'c', }, line: 3, - column: 64, + column: 62, }, ], }, From 48de9cd2bac2d7f517f956760aca9db0177b05dc Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:50:11 -0700 Subject: [PATCH 72/92] chore: member-delimiter-style --- .../rules/member-delimiter-style.test.ts | 407 +++++++++--------- 1 file changed, 206 insertions(+), 201 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/member-delimiter-style.test.ts b/packages/eslint-plugin/tests/rules/member-delimiter-style.test.ts index 529f8a7c0bd8..0ace26623133 100644 --- a/packages/eslint-plugin/tests/rules/member-delimiter-style.test.ts +++ b/packages/eslint-plugin/tests/rules/member-delimiter-style.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the delimiter, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/member-delimiter-style'; import { RuleTester } from '../RuleTester'; @@ -12,14 +17,14 @@ interface Foo { name: string; age: number; } - `, + `, { code: ` interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -32,7 +37,7 @@ interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -45,7 +50,7 @@ interface Foo { name: string; age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -58,7 +63,7 @@ interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: true }, @@ -71,7 +76,7 @@ interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'comma' }, @@ -84,7 +89,7 @@ interface Foo { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -97,7 +102,7 @@ interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'none', requireLast: true }, @@ -110,7 +115,7 @@ interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'none', requireLast: false }, @@ -123,7 +128,7 @@ interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { @@ -147,7 +152,7 @@ interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { @@ -165,7 +170,7 @@ interface Foo { name: string; age: number } - `, + `, options: [ { multiline: { @@ -189,7 +194,7 @@ interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { @@ -213,7 +218,7 @@ interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { @@ -231,7 +236,7 @@ interface Foo { name: string, age: number } - `, + `, options: [ { multiline: { @@ -255,7 +260,7 @@ interface Foo { name: string age: number } - `, + `, options: [ { multiline: { @@ -276,7 +281,7 @@ interface Foo { name: string age: number } - `, + `, options: [ { multiline: { @@ -299,14 +304,14 @@ type Foo = { name: string; age: number; } - `, + `, { code: ` type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: true } }], }, { @@ -315,7 +320,7 @@ type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi' } }], }, { @@ -324,7 +329,7 @@ type Foo = { name: string; age: number } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: false } }], }, { @@ -333,7 +338,7 @@ type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: true } }], }, { @@ -342,7 +347,7 @@ type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma' } }], }, { @@ -351,7 +356,7 @@ type Foo = { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], }, { @@ -360,7 +365,7 @@ type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: true } }], }, { @@ -369,7 +374,7 @@ type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], }, { @@ -378,7 +383,7 @@ type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { @@ -399,7 +404,7 @@ type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { @@ -417,7 +422,7 @@ type Foo = { name: string; age: number } - `, + `, options: [ { multiline: { @@ -441,7 +446,7 @@ type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { @@ -465,7 +470,7 @@ type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { @@ -483,7 +488,7 @@ type Foo = { name: string, age: number } - `, + `, options: [ { multiline: { @@ -507,7 +512,7 @@ type Foo = { name: string age: number } - `, + `, options: [ { multiline: { @@ -528,7 +533,7 @@ type Foo = { name: string age: number } - `, + `, options: [ { multiline: { @@ -557,7 +562,7 @@ type Bar = { name: string, age: number, } - `, + `, options: [ { multiline: { @@ -709,7 +714,7 @@ interface Foo { } interface Bar { name: string, age: number } - `, + `, options: [ { multiline: { @@ -728,7 +733,7 @@ interface Foo { } type Bar = { name: string, age: number } - `, + `, options: [ { multiline: { @@ -755,13 +760,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, errors: [ { messageId: 'expectedSemi', @@ -781,13 +786,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi' } }], errors: [ { @@ -808,13 +813,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: true } }], errors: [ { @@ -835,13 +840,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: false } }], errors: [ { @@ -857,13 +862,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: true } }], errors: [ { @@ -879,13 +884,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma' } }], errors: [ { @@ -906,13 +911,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: true } }], errors: [ { @@ -933,13 +938,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -955,13 +960,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma' } }], errors: [ { @@ -982,13 +987,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: true } }], errors: [ { @@ -1009,13 +1014,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -1031,13 +1036,13 @@ interface Foo { name: string age: number; } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -1058,13 +1063,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none' } }], errors: [ { @@ -1085,13 +1090,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: true } }], errors: [ { @@ -1112,13 +1117,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -1134,13 +1139,13 @@ interface Foo { name: string age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -1156,13 +1161,13 @@ interface Foo { name: string, age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none' } }], errors: [ { @@ -1183,13 +1188,13 @@ interface Foo { name: string, age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: true } }], errors: [ { @@ -1210,13 +1215,13 @@ interface Foo { name: string, age: number } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -1232,13 +1237,13 @@ interface Foo { name: string age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -1254,13 +1259,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma' }, @@ -1288,13 +1293,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -1324,13 +1329,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -1355,13 +1360,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -1389,13 +1394,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -1428,13 +1433,13 @@ interface Foo { name: string age: number } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1462,13 +1467,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -1496,13 +1501,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -1535,13 +1540,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1569,13 +1574,13 @@ interface Foo { name: string age: number; } - `, + `, output: ` interface Foo { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1608,13 +1613,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -1642,13 +1647,13 @@ interface Foo { name: string; age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -1678,13 +1683,13 @@ interface Foo { name: string; age: number } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1712,13 +1717,13 @@ interface Foo { name: string age: number; } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1746,13 +1751,13 @@ interface Foo { name: string, age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -1780,13 +1785,13 @@ interface Foo { name: string, age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -1816,13 +1821,13 @@ interface Foo { name: string, age: number } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1850,13 +1855,13 @@ interface Foo { name: string age: number, } - `, + `, output: ` interface Foo { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -1884,13 +1889,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, errors: [ { messageId: 'expectedSemi', @@ -1910,13 +1915,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi' } }], errors: [ { @@ -1937,13 +1942,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: true } }], errors: [ { @@ -1964,13 +1969,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: true } }], errors: [ { @@ -1986,13 +1991,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma' } }], errors: [ { @@ -2013,13 +2018,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: true } }], errors: [ { @@ -2040,13 +2045,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -2062,13 +2067,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma' } }], errors: [ { @@ -2089,13 +2094,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: true } }], errors: [ { @@ -2116,13 +2121,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -2138,13 +2143,13 @@ type Foo = { name: string age: number; } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [{ multiline: { delimiter: 'comma', requireLast: false } }], errors: [ { @@ -2165,13 +2170,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none' } }], errors: [ { @@ -2192,13 +2197,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: true } }], errors: [ { @@ -2219,13 +2224,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -2241,13 +2246,13 @@ type Foo = { name: string age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -2263,13 +2268,13 @@ type Foo = { name: string, age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none' } }], errors: [ { @@ -2290,13 +2295,13 @@ type Foo = { name: string, age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: true } }], errors: [ { @@ -2317,13 +2322,13 @@ type Foo = { name: string, age: number } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -2339,13 +2344,13 @@ type Foo = { name: string age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [{ multiline: { delimiter: 'none', requireLast: false } }], errors: [ { @@ -2361,13 +2366,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma' }, @@ -2395,13 +2400,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -2431,13 +2436,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -2462,13 +2467,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -2496,13 +2501,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -2535,13 +2540,13 @@ type Foo = { name: string age: number } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -2569,13 +2574,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -2603,13 +2608,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string, age: number, } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -2642,13 +2647,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -2676,13 +2681,13 @@ type Foo = { name: string age: number; } - `, + `, output: ` type Foo = { name: string, age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -2715,13 +2720,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi' }, @@ -2749,13 +2754,13 @@ type Foo = { name: string; age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: false }, @@ -2785,13 +2790,13 @@ type Foo = { name: string; age: number } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -2819,13 +2824,13 @@ type Foo = { name: string age: number; } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'semi', requireLast: true }, @@ -2853,13 +2858,13 @@ type Foo = { name: string, age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'comma' }, @@ -2887,13 +2892,13 @@ type Foo = { name: string, age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: false }, @@ -2923,13 +2928,13 @@ type Foo = { name: string, age: number } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: true }, @@ -2957,13 +2962,13 @@ type Foo = { name: string age: number, } - `, + `, output: ` type Foo = { name: string age: number } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: true }, @@ -2996,7 +3001,7 @@ type Bar = { name: string, age: number, } - `, + `, output: ` interface Foo { name: string, @@ -3007,7 +3012,7 @@ type Bar = { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'none', requireLast: true }, @@ -3045,12 +3050,12 @@ type Bar = { interface Foo { [key: string]: any } - `, + `, output: ` interface Foo { [key: string]: any; } - `, + `, errors: [ { messageId: 'expectedSemi', @@ -3064,12 +3069,12 @@ interface Foo { interface Foo { [key: string]: any } - `, + `, output: ` interface Foo { [key: string]: any; } - `, + `, options: [{ singleline: { delimiter: 'comma' } }], errors: [ { @@ -3143,12 +3148,12 @@ interface Foo { type Foo = { [key: string]: any } - `, + `, output: ` type Foo = { [key: string]: any; } - `, + `, errors: [ { messageId: 'expectedSemi', @@ -3162,12 +3167,12 @@ type Foo = { type Foo = { [key: string]: any } - `, + `, output: ` type Foo = { [key: string]: any; } - `, + `, options: [{ singleline: { delimiter: 'semi' } }], errors: [ { @@ -3232,7 +3237,7 @@ interface Foo { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: false } }], errors: [ { @@ -3248,7 +3253,7 @@ interface Foo { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: true }, @@ -3286,7 +3291,7 @@ type Foo = { name: string; age: number; } - `, + `, options: [{ multiline: { delimiter: 'semi', requireLast: false } }], errors: [ { @@ -3302,7 +3307,7 @@ type Foo = { name: string; age: number; } - `, + `, options: [ { multiline: { delimiter: 'comma', requireLast: true }, From c089d914331183fc6e882d87cc6e1466f08c0fb8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:50:52 -0700 Subject: [PATCH 73/92] chore: interface-name-prefix --- .../tests/rules/interface-name-prefix.test.ts | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/interface-name-prefix.test.ts b/packages/eslint-plugin/tests/rules/interface-name-prefix.test.ts index 79e70f50ab80..23c58a4687a0 100644 --- a/packages/eslint-plugin/tests/rules/interface-name-prefix.test.ts +++ b/packages/eslint-plugin/tests/rules/interface-name-prefix.test.ts @@ -30,55 +30,55 @@ ruleTester.run('interface-name-prefix', rule, { valid: [ ` interface Animal { - name: string; + name: string; } - `, + `, { code: ` interface IAnimal { - name: string; + name: string; } - `, + `, options: ['always'], }, { code: ` interface _IAnimal { - name: string; + name: string; } - `, + `, options: [{ prefixWithI: 'always', allowUnderscorePrefix: true }], }, { code: ` interface IIguana { - name: string; + name: string; } - `, + `, options: ['always'], }, { code: ` interface Iguana { - name: string; + name: string; } - `, + `, options: ['never'], }, { code: ` interface Animal { - name: string; + name: string; } - `, + `, options: ['never'], }, { code: ` interface I18n { - name: string; + name: string; } - `, + `, options: ['never'], }, ], @@ -86,9 +86,9 @@ interface I18n { { code: ` interface IAnimal { - name: string; + name: string; } - `, + `, errors: [ { messageId: 'noPrefix', @@ -100,9 +100,9 @@ interface IAnimal { { code: ` interface Animal { - name: string; + name: string; } - `, + `, options: ['always'], errors: [ { @@ -115,9 +115,9 @@ interface Animal { { code: ` interface Animal { - name: string; + name: string; } - `, + `, options: [{ prefixWithI: 'always', allowUnderscorePrefix: true }], errors: [ { @@ -130,9 +130,9 @@ interface Animal { { code: ` interface Iguana { - name: string; + name: string; } - `, + `, options: ['always'], errors: [ { @@ -145,9 +145,9 @@ interface Iguana { { code: ` interface IIguana { - name: string; + name: string; } - `, + `, options: ['never'], errors: [ { @@ -160,9 +160,9 @@ interface IIguana { { code: ` interface IAnimal { - name: string; + name: string; } - `, + `, options: ['never'], errors: [ { @@ -175,9 +175,9 @@ interface IAnimal { { code: ` interface _IAnimal { - name: string; + name: string; } - `, + `, options: ['never'], errors: [ { From 03f3c100ff4874c4d3fbcff61b21ddc0d7cbe84d Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:52:30 -0700 Subject: [PATCH 74/92] chore: generic-type-naming --- .../tests/rules/generic-type-naming.test.ts | 95 ++++++++++++------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/generic-type-naming.test.ts b/packages/eslint-plugin/tests/rules/generic-type-naming.test.ts index 11f7d1618bb2..da26e9675b64 100644 --- a/packages/eslint-plugin/tests/rules/generic-type-naming.test.ts +++ b/packages/eslint-plugin/tests/rules/generic-type-naming.test.ts @@ -7,35 +7,60 @@ const ruleTester = new RuleTester({ ruleTester.run('generic-type-naming', rule, { valid: [ - { code: 'class { }', options: [] }, - { code: 'type ReadOnly = {}', options: [] }, - { code: 'interface SimpleMap { }', options: [] }, - { code: 'function get() {}', options: [] }, - { code: 'interface GenericIdentityFn { (arg: T): T }', options: [] }, - { code: 'class { }', options: ['^x+$'] }, - { code: 'class { }', options: ['^[A-Z]$'] }, - { - code: 'class extends B implements Foo { }', + { + code: 'class {}', + options: [], + }, + { + code: 'type ReadOnly = {};', + options: [], + }, + { + code: 'interface SimpleMap {}', + options: [], + }, + { + code: 'function get() {}', + options: [], + }, + { + code: ` +interface GenericIdentityFn { + (arg: T): T; +} + `, + options: [], + }, + { + code: 'class {}', + options: ['^x+$'], + }, + { + code: 'class {}', + options: ['^[A-Z]$'], + }, + { + code: 'class extends B implements Foo {}', options: ['^[A-Z]$'], }, { code: ` class extends B implements Foo { - test () { - type Foo = Bar - } + test() { + type Foo = Bar; + } } - `, + `, options: ['^[A-Z]$'], }, { - code: 'class CounterContainer extends Container { }', + code: 'class CounterContainer extends Container {}', options: ['^T$'], }, ], invalid: [ { - code: 'class { }', + code: 'class {}', options: [], errors: [ { @@ -49,7 +74,7 @@ class extends B implements Foo { ], }, { - code: 'class { }', + code: 'class {}', options: ['^[A-Z]+$'], errors: [ { @@ -61,7 +86,7 @@ class extends B implements Foo { ], }, { - code: 'interface SimpleMap { }', + code: 'interface SimpleMap {}', options: ['^[A-Z]+$'], errors: [ { @@ -73,7 +98,7 @@ class extends B implements Foo { ], }, { - code: 'type R = {}', + code: 'type R = {};', options: ['^[A-Z]+$'], errors: [ { @@ -97,25 +122,29 @@ class extends B implements Foo { ], }, { - code: 'interface GenericIdentityFn { (arg: x): x }', + code: ` +interface GenericIdentityFn { + (arg: x): x; +} + `, options: ['^[A-Z]+$'], errors: [ { messageId: 'paramNotMatchRule', data: { name: 'x', rule: '^[A-Z]+$' }, - line: 1, - column: 32, + line: 3, + column: 4, }, ], }, { code: ` class extends B implements Foo { - test () { - type Foo = Bar - } + test() { + type Foo = Bar; + } } - `, + `, options: ['^[A-Z][0-9]$'], errors: [ { @@ -128,24 +157,24 @@ class extends B implements Foo { messageId: 'paramNotMatchRule', data: { name: 'Z', rule: '^[A-Z][0-9]$' }, line: 3, - column: 10, + column: 8, }, { messageId: 'paramNotMatchRule', data: { name: 'T', rule: '^[A-Z][0-9]$' }, line: 4, - column: 18, + column: 14, }, ], }, { code: ` abstract class extends B implements Foo { - test () { - type Foo = Bar - } + test() { + type Foo = Bar; + } } - `, + `, options: ['^[A-Z][0-9]$'], errors: [ { @@ -164,13 +193,13 @@ abstract class extends B implements Foo { messageId: 'paramNotMatchRule', data: { name: 'Z', rule: '^[A-Z][0-9]$' }, line: 3, - column: 10, + column: 8, }, { messageId: 'paramNotMatchRule', data: { name: 'T', rule: '^[A-Z][0-9]$' }, line: 4, - column: 18, + column: 14, }, ], }, From 64852916d764d38cb737cd162d3ed69d055f7a82 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 29 Mar 2020 23:55:06 -0700 Subject: [PATCH 75/92] chore: func-call-spacing --- .../eslint-plugin/tests/rules/func-call-spacing.test.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/func-call-spacing.test.ts b/packages/eslint-plugin/tests/rules/func-call-spacing.test.ts index e2250716a075..db884fd3cf12 100644 --- a/packages/eslint-plugin/tests/rules/func-call-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/func-call-spacing.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule, { MessageIds, Options } from '../../src/rules/func-call-spacing'; import { RuleTester } from '../RuleTester'; @@ -180,7 +185,7 @@ ruleTester.run('func-call-spacing', rule, { this.cancelled.add(request) this.decrement(request) (request.reject(new api.Cancel())) - `, + `, output: null, // no change errors: [ { @@ -208,7 +213,7 @@ var a = foo code: ` var a = foo (baz()) - `, + `, output: null, // no change errors: [ { From 6a9f65c89b9791d53dbb6eee66e0d9d153747274 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:01:19 -0700 Subject: [PATCH 76/92] chore: explicit-module-boundary-types --- .../explicit-module-boundary-types.test.ts | 306 ++++++++++-------- 1 file changed, 171 insertions(+), 135 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index 388adb994315..cdff53533b73 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -10,28 +10,28 @@ ruleTester.run('explicit-module-boundary-types', rule, { { code: ` function test(): void { - return; + return; } - `, + `, }, { code: ` export function test(): void { - return; + return; } - `, + `, }, { code: ` export var fn = function(): number { - return 1; + return 1; }; - `, + `, }, { code: ` export var arrowFn = (): string => 'test'; - `, + `, }, { code: ` @@ -46,7 +46,7 @@ class Test { } arrow = (): string => 'arrow'; } - `, + `, }, { code: ` @@ -61,42 +61,42 @@ export class Test { } arrow = (): string => 'arrow'; } - `, + `, }, { code: ` export function test(): void { - nested(); - return; + nested(); + return; - function nested() {} + function nested() {} } - `, + `, }, { code: ` export function test(): string { - const nested = () => 'value'; - return nested(); + const nested = () => 'value'; + return nested(); } - `, + `, }, { code: ` export function test(): string { - class Nested { - public method() { - return 'value'; - } + class Nested { + public method() { + return 'value'; } - return new Nested().method(); + } + return new Nested().method(); } - `, + `, }, { code: ` export var arrowFn: Foo = () => 'test'; - `, + `, options: [ { allowTypedFunctionExpressions: true, @@ -105,8 +105,10 @@ export var arrowFn: Foo = () => 'test'; }, { code: ` -export var funcExpr: Foo = function() { return 'test'; }; - `, +export var funcExpr: Foo = function() { + return 'test'; +}; + `, options: [ { allowTypedFunctionExpressions: true, @@ -114,18 +116,18 @@ export var funcExpr: Foo = function() { return 'test'; }; ], }, { - code: `const x = (() => {}) as Foo`, + code: 'const x = (() => {}) as Foo;', options: [{ allowTypedFunctionExpressions: true }], }, { - code: `const x = (() => {})`, + code: 'const x = (() => {});', options: [{ allowTypedFunctionExpressions: true }], }, { code: ` export const x = { foo: () => {}, -} as Foo +} as Foo; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -133,7 +135,7 @@ export const x = { code: ` export const x = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -141,7 +143,7 @@ export const x = { code: ` export const x: Foo = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -151,7 +153,7 @@ export const x: Foo = { type MethodType = () => void; export class App { - public method: MethodType = () => {} + public method: MethodType = () => {}; } `, options: [{ allowTypedFunctionExpressions: true }], @@ -169,37 +171,45 @@ export const myObj = { { code: ` export default () => (): void => {}; - `, + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export default () => function (): void {}; - `, +export default () => function(): void {}; + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export default () => { return (): void => {} }; - `, +export default () => { + return (): void => {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export default () => { return function (): void {} }; - `, +export default () => { + return function(): void {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export function fn() { return (): void => {} }; - `, +export function fn() { + return (): void => {}; +} + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export function fn() { return function (): void {} }; - `, +export function fn() { + return function(): void {}; +} + `, options: [{ allowHigherOrderFunctions: true }], }, { @@ -207,20 +217,26 @@ export function fn() { return function (): void {} }; export function FunctionDeclaration() { return function FunctionExpression_Within_FunctionDeclaration() { return function FunctionExpression_Within_FunctionExpression() { - return () => { // ArrowFunctionExpression_Within_FunctionExpression - return () => // ArrowFunctionExpression_Within_ArrowFunctionExpression - (): number => 1 // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody - } - } - } + return () => { + // ArrowFunctionExpression_Within_FunctionExpression + return () => + // ArrowFunctionExpression_Within_ArrowFunctionExpression + (): number => 1; // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody + }; + }; + }; } - `, + `, options: [{ allowHigherOrderFunctions: true }], }, { code: ` -export default () => () => { return (): void => { return; } }; - `, +export default () => () => { + return (): void => { + return; + }; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, { @@ -243,9 +259,9 @@ new Accumulator().accumulate(() => 1); }, { code: ` -export const func1 = (value: number) => (({ type: "X", value }) as const); -export const func2 = (value: number) => ({ type: "X", value } as const); -export const func3 = (value: number) => (x as const); +export const func1 = (value: number) => ({ type: 'X', value } as const); +export const func2 = (value: number) => ({ type: 'X', value } as const); +export const func3 = (value: number) => x as const; export const func4 = (value: number) => x as const; `, options: [ @@ -257,7 +273,7 @@ export const func4 = (value: number) => x as const; { code: ` export const func1 = (value: string) => value; -export const func2 = (value: number) => ({ type: "X", value }); +export const func2 = (value: number) => ({ type: 'X', value }); `, options: [ { @@ -315,13 +331,8 @@ export class Test { { code: ` export const Foo: FC = () => ( -
{}} - b={function (e) {}} - c={function foo(e) {}} - > -
-) +
{}} b={function(e) {}} c={function foo(e) {}}>
+); `, parserOptions: { ecmaFeatures: { jsx: true }, @@ -329,13 +340,9 @@ export const Foo: FC = () => ( }, { code: ` -export const Foo: JSX.Element = -
{}} - b={function (e) {}} - c={function foo(e) {}} - > -
+export const Foo: JSX.Element = ( +
{}} b={function(e) {}} c={function foo(e) {}}>
+); `, parserOptions: { ecmaFeatures: { jsx: true }, @@ -345,10 +352,7 @@ export const Foo: JSX.Element = invalid: [ { code: ` -export function test( - a: number, - b: number, -) { +export function test(a: number, b: number) { return; } `, @@ -356,9 +360,9 @@ export function test( { messageId: 'missingReturnType', line: 2, - endLine: 5, + endLine: 2, column: 8, - endColumn: 2, + endColumn: 43, }, ], }, @@ -413,13 +417,13 @@ export var arrowFn = () => 'test'; export class Test { constructor() {} get prop() { - return 1; + return 1; } set prop(value) {} method() { return; } - arrow = (arg) => 'arrow'; + arrow = arg => 'arrow'; private method() { return; } @@ -452,14 +456,14 @@ export class Test { line: 11, endLine: 11, column: 11, - endColumn: 19, + endColumn: 17, }, { messageId: 'missingArgType', line: 11, endLine: 11, column: 11, - endColumn: 27, + endColumn: 25, }, ], }, @@ -467,11 +471,11 @@ export class Test { code: ` export class Foo { public a = () => {}; - public b = function () {}; + public b = function() {}; public c = function test() {}; static d = () => {}; - static e = function () {}; + static e = function() {}; } `, errors: [ @@ -487,7 +491,7 @@ export class Foo { line: 4, endLine: 4, column: 14, - endColumn: 25, + endColumn: 24, }, { messageId: 'missingReturnType', @@ -508,12 +512,12 @@ export class Foo { line: 8, endLine: 8, column: 14, - endColumn: 25, + endColumn: 24, }, ], }, { - code: 'export default () => true ? (() => {}) : ((): void => {});', + code: 'export default () => (true ? () => {} : (): void => {});', errors: [ { messageId: 'missingReturnType', @@ -545,20 +549,24 @@ export class Foo { ], }, { - code: "export var funcExpr = function() { return 'test'; };", + code: ` +export var funcExpr = function() { + return 'test'; +}; + `, options: [{ allowTypedFunctionExpressions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, + line: 2, + endLine: 2, column: 23, endColumn: 33, }, ], }, { - code: 'export const x = (() => {}) as Foo', + code: 'export const x = (() => {}) as Foo;', options: [{ allowTypedFunctionExpressions: false }], errors: [ { @@ -575,7 +583,7 @@ export class Foo { interface Foo {} export const x = { foo: () => {}, -} as Foo +} as Foo; `, options: [{ allowTypedFunctionExpressions: false }], errors: [ @@ -593,7 +601,7 @@ export const x = { interface Foo {} export const x: Foo = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: false }], errors: [ @@ -620,7 +628,7 @@ export const x: Foo = { ], }, { - code: 'export default () => function () {};', + code: 'export default () => function() {};', options: [{ allowHigherOrderFunctions: true }], errors: [ { @@ -628,59 +636,75 @@ export const x: Foo = { line: 1, endLine: 1, column: 22, - endColumn: 33, + endColumn: 32, }, ], }, { - code: 'export default () => { return () => {} };', + code: ` +export default () => { + return () => {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 31, - endColumn: 36, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, { - code: 'export default () => { return function () {} };', + code: ` +export default () => { + return function() {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 31, - endColumn: 42, + line: 3, + endLine: 3, + column: 10, + endColumn: 20, }, ], }, { - code: 'export function fn() { return () => {} };', + code: ` +export function fn() { + return () => {}; +} + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 31, - endColumn: 36, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, { - code: 'export function fn() { return function () {} };', + code: ` +export function fn() { + return function() {}; +} + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 31, - endColumn: 42, + line: 3, + endLine: 3, + column: 10, + endColumn: 20, }, ], }, @@ -689,40 +713,48 @@ export const x: Foo = { export function FunctionDeclaration() { return function FunctionExpression_Within_FunctionDeclaration() { return function FunctionExpression_Within_FunctionExpression() { - return () => { // ArrowFunctionExpression_Within_FunctionExpression - return () => // ArrowFunctionExpression_Within_ArrowFunctionExpression - () => 1 // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody - } - } - } + return () => { + // ArrowFunctionExpression_Within_FunctionExpression + return () => + // ArrowFunctionExpression_Within_ArrowFunctionExpression + () => 1; // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody + }; + }; + }; } - `, + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 7, - endLine: 7, + line: 9, + endLine: 9, column: 11, endColumn: 16, }, ], }, { - code: 'export default () => () => { return () => { return; } };', + code: ` +export default () => () => { + return () => { + return; + }; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 37, - endColumn: 42, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, { - code: 'export default (() => true)()', + code: 'export default (() => true)();', options: [ { allowTypedFunctionExpressions: false, @@ -740,8 +772,8 @@ export function FunctionDeclaration() { }, { code: ` -export const func1 = (value: number) => ({ type: "X", value } as any); -export const func2 = (value: number) => ({ type: "X", value } as Action); +export const func1 = (value: number) => ({ type: 'X', value } as any); +export const func2 = (value: number) => ({ type: 'X', value } as Action); `, options: [ { @@ -767,7 +799,7 @@ export const func2 = (value: number) => ({ type: "X", value } as Action); }, { code: ` -export const func = (value: number) => ({ type: "X", value } as const); +export const func = (value: number) => ({ type: 'X', value } as const); `, options: [ { @@ -834,19 +866,23 @@ export const func2 = (value: number) => value; ], }, { - code: 'export function fn(test): string { return "123" };', + code: ` +export function fn(test): string { + return '123'; +} + `, errors: [ { messageId: 'missingArgType', - line: 1, - endLine: 1, + line: 2, + endLine: 4, column: 8, - endColumn: 50, + endColumn: 2, }, ], }, { - code: 'export const fn = (one: number, two): string => "123";', + code: "export const fn = (one: number, two): string => '123';", errors: [ { messageId: 'missingArgType', From 0e6d87d192eceff83ab087af6c437ad90a5a3f82 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:06:22 -0700 Subject: [PATCH 77/92] chore: explicit-member-accessibility --- .../explicit-member-accessibility.test.ts | 368 ++++++++++-------- 1 file changed, 198 insertions(+), 170 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts b/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts index fe0a09dd4c16..906f1b6cc600 100644 --- a/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/explicit-member-accessibility'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -109,72 +109,72 @@ class Test { filename: 'test.ts', code: ` class Test { - protected name: string - private x: number - public getX () { - return this.x + protected name: string; + private x: number; + public getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - protected name: string - protected foo?: string - public "foo-bar"?: string + protected name: string; + protected foo?: string; + public 'foo-bar'?: string; } - `, + `, }, { filename: 'test.ts', code: ` class Test { - public constructor({x, y}: {x: number; y: number;}) {} + public constructor({ x, y }: { x: number; y: number }) {} } - `, + `, }, { filename: 'test.ts', code: ` class Test { - protected name: string - protected foo?: string - public getX () { - return this.x + protected name: string; + protected foo?: string; + public getX() { + return this.x; } } - `, + `, options: [{ accessibility: 'explicit' }], }, { filename: 'test.ts', code: ` class Test { - protected name: string - protected foo?: string - getX () { - return this.x + protected name: string; + protected foo?: string; + getX() { + return this.x; } } - `, + `, options: [{ accessibility: 'no-public' }], }, { filename: 'test.ts', code: ` class Test { - name: string - foo?: string - getX () { - return this.x + name: string; + foo?: string; + getX() { + return this.x; } get fooName(): string { - return this.foo + ' ' + this.name + return this.foo + ' ' + this.name; } } - `, + `, options: [{ accessibility: 'no-public' }], }, { @@ -182,7 +182,7 @@ class Test { code: ` class Test { private x: number; - constructor (x: number) { + constructor(x: number) { this.x = x; } get internalValue() { @@ -191,7 +191,7 @@ class Test { private set internalValue(value: number) { this.x = value; } - public square (): number { + public square(): number { return this.x * this.x; } } @@ -203,7 +203,7 @@ class Test { code: ` class Test { private x: number; - public constructor (x: number) { + public constructor(x: number) { this.x = x; } public get internalValue() { @@ -212,10 +212,10 @@ class Test { public set internalValue(value: number) { this.x = value; } - public square (): number { + public square(): number { return this.x * this.x; } - half (): number { + half(): number { return this.x / 2; } } @@ -226,7 +226,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(private x: number){} + constructor(private x: number) {} } `, options: [{ accessibility: 'no-public' }], @@ -235,7 +235,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(public x: number){} + constructor(public x: number) {} } `, options: [ @@ -249,7 +249,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(public foo: number){} + constructor(public foo: number) {} } `, options: [{ accessibility: 'no-public' }], @@ -258,8 +258,8 @@ class Test { filename: 'test.ts', code: ` class Test { - public getX () { - return this.x + public getX() { + return this.x; } } `, @@ -269,8 +269,8 @@ class Test { filename: 'test.ts', code: ` class Test { - public static getX () { - return this.x + public static getX() { + return this.x; } } `, @@ -280,8 +280,8 @@ class Test { filename: 'test.ts', code: ` class Test { - get getX () { - return this.x + get getX() { + return this.x; } } `, @@ -291,8 +291,8 @@ class Test { filename: 'test.ts', code: ` class Test { - getX () { - return this.x + getX() { + return this.x; } } `, @@ -300,24 +300,38 @@ class Test { }, { filename: 'test.ts', - code: 'class Test { x = 2 }', + code: ` +class Test { + x = 2; +} + `, options: [{ overrides: { properties: 'off' } }], }, { filename: 'test.ts', - code: 'class Test { private x = 2 }', + code: ` +class Test { + private x = 2; +} + `, options: [{ overrides: { properties: 'explicit' } }], }, { filename: 'test.ts', - code: `class Test { - x = 2 - private x = 2 - }`, + code: ` +class Test { + x = 2; + private x = 2; +} + `, options: [{ overrides: { properties: 'no-public' } }], }, { - code: 'class Test { constructor(private { x }: any[]) { }}', + code: ` +class Test { + constructor(private { x }: any[]) {} +} + `, options: [{ accessibility: 'no-public' }], }, ], @@ -412,12 +426,12 @@ class Test { filename: 'test.ts', code: ` class Test { - x: number - public getX () { - return this.x + x: number; + public getX() { + return this.x; } } - `, + `, errors: [ { messageId: 'missingAccessibility', @@ -431,23 +445,23 @@ class Test { ], output: ` class Test { - x: number - public getX () { - return this.x + x: number; + public getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - private x: number - getX () { - return this.x + private x: number; + getX() { + return this.x; } } - `, + `, errors: [ { messageId: 'missingAccessibility', @@ -461,23 +475,23 @@ class Test { ], output: ` class Test { - private x: number - getX () { - return this.x + private x: number; + getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - x?: number - getX? () { - return this.x + x?: number; + getX?() { + return this.x; } } - `, + `, errors: [ { messageId: 'missingAccessibility', @@ -500,24 +514,24 @@ class Test { ], output: ` class Test { - x?: number - getX? () { - return this.x + x?: number; + getX?() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - protected name: string - protected foo?: string - public getX () { - return this.x + protected name: string; + protected foo?: string; + public getX() { + return this.x; } } - `, + `, options: [{ accessibility: 'no-public' }], errors: [ { @@ -532,25 +546,25 @@ class Test { ], output: ` class Test { - protected name: string - protected foo?: string - getX () { - return this.x + protected name: string; + protected foo?: string; + getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - protected name: string - public foo?: string - getX () { - return this.x + protected name: string; + public foo?: string; + getX() { + return this.x; } } - `, + `, options: [{ accessibility: 'no-public' }], errors: [ { @@ -565,24 +579,24 @@ class Test { ], output: ` class Test { - protected name: string - foo?: string - getX () { - return this.x + protected name: string; + foo?: string; + getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { - public x: number - public getX () { - return this.x + public x: number; + public getX() { + return this.x; } } - `, + `, errors: [ { messageId: 'unwantedPublicAccessibility', @@ -598,19 +612,19 @@ class Test { options: [{ accessibility: 'no-public' }], output: ` class Test { - x: number - getX () { - return this.x + x: number; + getX() { + return this.x; } } - `, + `, }, { filename: 'test.ts', code: ` class Test { private x: number; - constructor (x: number) { + constructor(x: number) { this.x = x; } get internalValue() { @@ -637,7 +651,7 @@ class Test { output: ` class Test { private x: number; - constructor (x: number) { + constructor(x: number) { this.x = x; } get internalValue() { @@ -654,7 +668,7 @@ class Test { code: ` class Test { private x: number; - constructor (x: number) { + constructor(x: number) { this.x = x; } get internalValue() { @@ -685,7 +699,7 @@ class Test { output: ` class Test { private x: number; - constructor (x: number) { + constructor(x: number) { this.x = x; } get internalValue() { @@ -701,7 +715,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(public x: number){} + constructor(public x: number) {} public foo(): string { return 'foo'; } @@ -721,7 +735,7 @@ class Test { ], output: ` class Test { - constructor(public x: number){} + constructor(public x: number) {} public foo(): string { return 'foo'; } @@ -732,7 +746,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(public x: number){} + constructor(public x: number) {} } `, errors: [ @@ -744,7 +758,7 @@ class Test { ], output: ` class Test { - constructor(public x: number){} + constructor(public x: number) {} } `, }, @@ -752,7 +766,7 @@ class Test { filename: 'test.ts', code: ` class Test { - constructor(public readonly x: number){} + constructor(public readonly x: number) {} } `, options: [ @@ -770,13 +784,17 @@ class Test { ], output: ` class Test { - constructor(readonly x: number){} + constructor(readonly x: number) {} } `, }, { filename: 'test.ts', - code: 'class Test { x = 2 }', + code: ` +class Test { + x = 2; +} + `, options: [ { accessibility: 'off', @@ -786,18 +804,24 @@ class Test { errors: [ { messageId: 'missingAccessibility', - line: 1, - column: 14, + line: 3, + column: 3, }, ], - output: 'class Test { x = 2 }', + output: ` +class Test { + x = 2; +} + `, }, { filename: 'test.ts', - code: `class Test { - public x = 2 - private x = 2 - }`, + code: ` +class Test { + public x = 2; + private x = 2; +} + `, options: [ { accessibility: 'off', @@ -807,30 +831,40 @@ class Test { errors: [ { messageId: 'unwantedPublicAccessibility', - line: 2, - column: 9, + line: 3, + column: 3, }, ], - output: `class Test { - x = 2 - private x = 2 - }`, + output: ` +class Test { + x = 2; + private x = 2; +} + `, }, { - code: 'class Test { constructor(public ...x: any[]) { }}', + code: ` +class Test { + constructor(public ...x: any[]) {} +} + `, options: [{ accessibility: 'explicit' }], errors: [ { messageId: 'missingAccessibility', - line: 1, - column: 14, + line: 3, + column: 3, }, ], - output: 'class Test { constructor(public ...x: any[]) { }}', + output: ` +class Test { + constructor(public ...x: any[]) {} +} + `, }, { filename: 'test.ts', - code: ` + code: noFormat` class Test { @public public /*public*/constructor(private foo: string) {} @@ -848,7 +882,7 @@ class Test { column: 3, }, ], - output: ` + output: noFormat` class Test { @public /*public*/constructor(private foo: string) {} @@ -914,7 +948,7 @@ class Test { filename: 'test.ts', code: ` class Test { - public foo = ""; + public foo = ''; } `, options: [ @@ -931,16 +965,16 @@ class Test { ], output: ` class Test { - foo = ""; + foo = ''; } `, }, { filename: 'test.ts', - code: ` + code: noFormat` class Test { - contructor(public/* Hi there */ readonly foo) + contructor(public/* Hi there */ readonly foo); } `, options: [ @@ -958,7 +992,7 @@ class Test { ], output: ` class Test { - contructor(/* Hi there */ readonly foo) + contructor(/* Hi there */ readonly foo); } `, }, @@ -966,7 +1000,7 @@ class Test { filename: 'test.ts', code: ` class Test { - contructor(public readonly foo: string) + contructor(public readonly foo: string); } `, options: [ @@ -983,33 +1017,17 @@ class Test { ], output: ` class Test { - contructor(readonly foo: string) + contructor(readonly foo: string); } `, }, { filename: 'test.ts', - code: - 'class EnsureWhiteSPaceSpan { public constructor() {}}', - options: [ - { - accessibility: 'no-public', - overrides: { parameterProperties: 'no-public' }, - }, - ], - errors: [ - { - messageId: 'unwantedPublicAccessibility', - line: 1, - column: 30, - }, - ], - output: 'class EnsureWhiteSPaceSpan { constructor() {}}', - }, - { - filename: 'test.ts', - code: - 'class EnsureWhiteSPaceSpan { public /* */ constructor() {}}', + code: ` +class EnsureWhiteSPaceSpan { + public constructor() {} +} + `, options: [ { accessibility: 'no-public', @@ -1019,17 +1037,23 @@ class Test { errors: [ { messageId: 'unwantedPublicAccessibility', - line: 1, - column: 30, + line: 3, + column: 3, }, ], - output: - 'class EnsureWhiteSPaceSpan { /* */ constructor() {}}', + output: ` +class EnsureWhiteSPaceSpan { + constructor() {} +} + `, }, { filename: 'test.ts', - code: - 'class EnsureWhiteSPaceSpan { public /* */ constructor() {}}', + code: ` +class EnsureWhiteSPaceSpan { + public /* */ constructor() {} +} + `, options: [ { accessibility: 'no-public', @@ -1039,11 +1063,15 @@ class Test { errors: [ { messageId: 'unwantedPublicAccessibility', - line: 1, - column: 30, + line: 3, + column: 3, }, ], - output: 'class EnsureWhiteSPaceSpan { /* */ constructor() {}}', + output: ` +class EnsureWhiteSPaceSpan { + /* */ constructor() {} +} + `, }, ], }); From df671bc3840b2d0db323b4e7d67e40f86ebfa899 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:10:03 -0700 Subject: [PATCH 78/92] chore: explicit-function-return-type --- .../explicit-function-return-type.test.ts | 303 ++++++++++-------- 1 file changed, 172 insertions(+), 131 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index fb353eec1a37..46ab49441858 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -11,23 +11,23 @@ ruleTester.run('explicit-function-return-type', rule, { filename: 'test.ts', code: ` function test(): void { - return; + return; } - `, + `, }, { filename: 'test.ts', code: ` var fn = function(): number { - return 1; + return 1; }; - `, + `, }, { filename: 'test.ts', code: ` var arrowFn = (): string => 'test'; - `, + `, }, { filename: 'test.ts', @@ -43,11 +43,11 @@ class Test { } arrow = (): string => 'arrow'; } - `, + `, }, { filename: 'test.ts', - code: `fn(() => {});`, + code: 'fn(() => {});', options: [ { allowExpressions: true, @@ -56,7 +56,7 @@ class Test { }, { filename: 'test.ts', - code: `fn(function() {});`, + code: 'fn(function() {});', options: [ { allowExpressions: true, @@ -65,7 +65,7 @@ class Test { }, { filename: 'test.ts', - code: `[function() {}, () => {}]`, + code: '[function() {}, () => {}];', options: [ { allowExpressions: true, @@ -74,7 +74,7 @@ class Test { }, { filename: 'test.ts', - code: `(function() {});`, + code: '(function() {});', options: [ { allowExpressions: true, @@ -83,7 +83,7 @@ class Test { }, { filename: 'test.ts', - code: `(() => {})();`, + code: '(() => {})();', options: [ { allowExpressions: true, @@ -92,7 +92,7 @@ class Test { }, { filename: 'test.ts', - code: `export default (): void => {}`, + code: 'export default (): void => {};', options: [ { allowExpressions: true, @@ -103,7 +103,7 @@ class Test { filename: 'test.ts', code: ` var arrowFn: Foo = () => 'test'; - `, + `, options: [ { allowTypedFunctionExpressions: true, @@ -113,8 +113,10 @@ var arrowFn: Foo = () => 'test'; { filename: 'test.ts', code: ` -var funcExpr: Foo = function() { return 'test'; }; - `, +var funcExpr: Foo = function() { + return 'test'; +}; + `, options: [ { allowTypedFunctionExpressions: true, @@ -123,12 +125,12 @@ var funcExpr: Foo = function() { return 'test'; }; }, { filename: 'test.ts', - code: `const x = (() => {}) as Foo`, + code: 'const x = (() => {}) as Foo;', options: [{ allowTypedFunctionExpressions: true }], }, { filename: 'test.ts', - code: `const x = (() => {})`, + code: 'const x = (() => {});', options: [{ allowTypedFunctionExpressions: true }], }, { @@ -136,7 +138,7 @@ var funcExpr: Foo = function() { return 'test'; }; code: ` const x = { foo: () => {}, -} as Foo +} as Foo; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -145,7 +147,7 @@ const x = { code: ` const x = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -154,7 +156,7 @@ const x = { code: ` const x: Foo = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: true }], }, @@ -165,7 +167,7 @@ const x: Foo = { type MethodType = () => void; class App { - private method: MethodType = () => {} + private method: MethodType = () => {}; } `, options: [{ allowTypedFunctionExpressions: true }], @@ -185,42 +187,50 @@ const myObj = { filename: 'test.ts', code: ` () => (): void => {}; - `, + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -() => function (): void {}; - `, +() => function(): void {}; + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -() => { return (): void => {} }; - `, +() => { + return (): void => {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -() => { return function (): void {} }; - `, +() => { + return function(): void {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -function fn() { return (): void => {} }; - `, +function fn() { + return (): void => {}; +} + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -function fn() { return function (): void {} }; - `, +function fn() { + return function(): void {}; +} + `, options: [{ allowHigherOrderFunctions: true }], }, { @@ -229,33 +239,39 @@ function fn() { return function (): void {} }; function FunctionDeclaration() { return function FunctionExpression_Within_FunctionDeclaration() { return function FunctionExpression_Within_FunctionExpression() { - return () => { // ArrowFunctionExpression_Within_FunctionExpression - return () => // ArrowFunctionExpression_Within_ArrowFunctionExpression - (): number => 1 // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody - } - } - } + return () => { + // ArrowFunctionExpression_Within_FunctionExpression + return () => + // ArrowFunctionExpression_Within_ArrowFunctionExpression + (): number => 1; // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody + }; + }; + }; } - `, + `, options: [{ allowHigherOrderFunctions: true }], }, { filename: 'test.ts', code: ` -() => () => { return (): void => { return; } }; - `, +() => () => { + return (): void => { + return; + }; +}; + `, options: [{ allowHigherOrderFunctions: true }], }, // https://github.com/typescript-eslint/typescript-eslint/issues/679 { filename: 'test.ts', code: ` -declare function foo(arg: () => void): void -foo(() => 1) -foo(() => {}) -foo(() => null) -foo(() => true) -foo(() => '') +declare function foo(arg: () => void): void; +foo(() => 1); +foo(() => {}); +foo(() => null); +foo(() => true); +foo(() => ''); `, options: [ { @@ -266,12 +282,12 @@ foo(() => '') { filename: 'test.ts', code: ` -declare function foo(arg: () => void): void -foo?.(() => 1) -foo?.bar(() => {}) -foo?.bar?.(() => null) -foo.bar?.(() => true) -foo?.(() => '') +declare function foo(arg: () => void): void; +foo?.(() => 1); +foo?.bar(() => {}); +foo?.bar?.(() => null); +foo.bar?.(() => true); +foo?.(() => ''); `, options: [ { @@ -301,22 +317,22 @@ new Accumulator().accumulate(() => 1); { filename: 'test.ts', code: ` -declare function foo(arg: { meth: () => number }): void +declare function foo(arg: { meth: () => number }): void; foo({ meth() { return 1; }, -}) +}); foo({ - meth: function () { + meth: function() { return 1; }, -}) +}); foo({ meth: () => { return 1; }, -}) +}); `, options: [ { @@ -327,9 +343,9 @@ foo({ { filename: 'test.ts', code: ` -const func = (value: number) => (({ type: "X", value }) as const); -const func = (value: number) => ({ type: "X", value } as const); -const func = (value: number) => (x as const); +const func = (value: number) => ({ type: 'X', value } as const); +const func = (value: number) => ({ type: 'X', value } as const); +const func = (value: number) => x as const; const func = (value: number) => x as const; `, options: [ @@ -355,10 +371,7 @@ new Foo(1, () => {}); { filename: 'test.ts', code: ` -function test( - a: number, - b: number, -) { +function test(a: number, b: number) { return; } `, @@ -366,9 +379,9 @@ function test( { messageId: 'missingReturnType', line: 2, - endLine: 5, + endLine: 2, column: 1, - endColumn: 2, + endColumn: 36, }, ], }, @@ -427,7 +440,7 @@ var arrowFn = () => 'test'; class Test { constructor() {} get prop() { - return 1; + return 1; } set prop() {} method() { @@ -532,7 +545,7 @@ function test() { }, { filename: 'test.ts', - code: 'export default function() {};', + code: 'export default function() {}', options: [{ allowExpressions: true }], errors: [ { @@ -549,11 +562,11 @@ function test() { code: ` class Foo { public a = () => {}; - public b = function () {}; + public b = function() {}; public c = function test() {}; static d = () => {}; - static e = function () {}; + static e = function() {}; } `, options: [{ allowExpressions: true }], @@ -570,7 +583,7 @@ class Foo { line: 4, endLine: 4, column: 14, - endColumn: 25, + endColumn: 24, }, { messageId: 'missingReturnType', @@ -591,7 +604,7 @@ class Foo { line: 8, endLine: 8, column: 14, - endColumn: 25, + endColumn: 24, }, ], }, @@ -611,13 +624,17 @@ class Foo { }, { filename: 'test.ts', - code: "var funcExpr = function() { return 'test'; };", + code: ` +var funcExpr = function() { + return 'test'; +}; + `, options: [{ allowTypedFunctionExpressions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, + line: 2, + endLine: 2, column: 16, endColumn: 26, }, @@ -626,7 +643,7 @@ class Foo { { filename: 'test.ts', - code: 'const x = (() => {}) as Foo', + code: 'const x = (() => {}) as Foo;', options: [{ allowTypedFunctionExpressions: false }], errors: [ { @@ -644,7 +661,7 @@ class Foo { interface Foo {} const x = { foo: () => {}, -} as Foo +} as Foo; `, options: [{ allowTypedFunctionExpressions: false }], errors: [ @@ -663,7 +680,7 @@ const x = { interface Foo {} const x: Foo = { foo: () => {}, -} +}; `, options: [{ allowTypedFunctionExpressions: false }], errors: [ @@ -692,7 +709,7 @@ const x: Foo = { }, { filename: 'test.ts', - code: '() => function () {};', + code: '() => function() {};', options: [{ allowHigherOrderFunctions: true }], errors: [ { @@ -700,63 +717,79 @@ const x: Foo = { line: 1, endLine: 1, column: 7, - endColumn: 18, + endColumn: 17, }, ], }, { filename: 'test.ts', - code: '() => { return () => {} };', + code: ` +() => { + return () => {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 16, - endColumn: 21, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, { filename: 'test.ts', - code: '() => { return function () {} };', + code: ` +() => { + return function() {}; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 16, - endColumn: 27, + line: 3, + endLine: 3, + column: 10, + endColumn: 20, }, ], }, { filename: 'test.ts', - code: 'function fn() { return () => {} };', + code: ` +function fn() { + return () => {}; +} + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 24, - endColumn: 29, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, { filename: 'test.ts', - code: 'function fn() { return function () {} };', + code: ` +function fn() { + return function() {}; +} + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 24, - endColumn: 35, + line: 3, + endLine: 3, + column: 10, + endColumn: 20, }, ], }, @@ -766,20 +799,22 @@ const x: Foo = { function FunctionDeclaration() { return function FunctionExpression_Within_FunctionDeclaration() { return function FunctionExpression_Within_FunctionExpression() { - return () => { // ArrowFunctionExpression_Within_FunctionExpression - return () => // ArrowFunctionExpression_Within_ArrowFunctionExpression - () => 1 // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody - } - } - } + return () => { + // ArrowFunctionExpression_Within_FunctionExpression + return () => + // ArrowFunctionExpression_Within_ArrowFunctionExpression + () => 1; // ArrowFunctionExpression_Within_ArrowFunctionExpression_WithNoBody + }; + }; + }; } - `, + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 7, - endLine: 7, + line: 9, + endLine: 9, column: 11, endColumn: 16, }, @@ -787,15 +822,21 @@ function FunctionDeclaration() { }, { filename: 'test.ts', - code: '() => () => { return () => { return; } };', + code: ` +() => () => { + return () => { + return; + }; +}; + `, options: [{ allowHigherOrderFunctions: true }], errors: [ { messageId: 'missingReturnType', - line: 1, - endLine: 1, - column: 22, - endColumn: 27, + line: 3, + endLine: 3, + column: 10, + endColumn: 15, }, ], }, @@ -803,12 +844,12 @@ function FunctionDeclaration() { { filename: 'test.ts', code: ` -declare function foo(arg: () => void): void -foo(() => 1) -foo(() => {}) -foo(() => null) -foo(() => true) -foo(() => '') +declare function foo(arg: () => void): void; +foo(() => 1); +foo(() => {}); +foo(() => null); +foo(() => true); +foo(() => ''); `, options: [ { @@ -883,7 +924,7 @@ new Accumulator().accumulate(() => 1); }, { filename: 'test.ts', - code: '(() => true)()', + code: '(() => true)();', options: [ { allowTypedFunctionExpressions: false, @@ -902,22 +943,22 @@ new Accumulator().accumulate(() => 1); { filename: 'test.ts', code: ` -declare function foo(arg: { meth: () => number }): void +declare function foo(arg: { meth: () => number }): void; foo({ meth() { return 1; }, -}) +}); foo({ - meth: function () { + meth: function() { return 1; }, -}) +}); foo({ meth: () => { return 1; }, -}) +}); `, options: [ { @@ -937,7 +978,7 @@ foo({ line: 9, endLine: 9, column: 9, - endColumn: 20, + endColumn: 19, }, { messageId: 'missingReturnType', @@ -951,8 +992,8 @@ foo({ { filename: 'test.ts', code: ` -const func = (value: number) => ({ type: "X", value } as any); -const func = (value: number) => ({ type: "X", value } as Action); +const func = (value: number) => ({ type: 'X', value } as any); +const func = (value: number) => ({ type: 'X', value } as Action); `, options: [ { @@ -979,7 +1020,7 @@ const func = (value: number) => ({ type: "X", value } as Action); { filename: 'test.ts', code: ` -const func = (value: number) => ({ type: "X", value } as const); +const func = (value: number) => ({ type: 'X', value } as const); `, options: [ { From f075c71ff59d615c59ee4630d49cc7b36b7a63a8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:10:38 -0700 Subject: [PATCH 79/92] chore: default-param-last --- .../tests/rules/default-param-last.test.ts | 96 +++++++++---------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/default-param-last.test.ts b/packages/eslint-plugin/tests/rules/default-param-last.test.ts index d965df3c77fa..76e23bbe8a78 100644 --- a/packages/eslint-plugin/tests/rules/default-param-last.test.ts +++ b/packages/eslint-plugin/tests/rules/default-param-last.test.ts @@ -19,29 +19,29 @@ ruleTester.run('default-param-last', rule, { 'function foo(a: number, b?: number, c = 1) {}', 'function foo(a: number, b = 1, ...c) {}', - 'const foo = function () {}', - 'const foo = function (a: number) {}', - 'const foo = function (a = 1) {}', - 'const foo = function (a?: number) {}', - 'const foo = function (a: number, b: number) {}', - 'const foo = function (a: number, b: number, c?: number) {}', - 'const foo = function (a: number, b = 1) {}', - 'const foo = function (a: number, b = 1, c = 1) {}', - 'const foo = function (a: number, b = 1, c?: number) {}', - 'const foo = function (a: number, b?: number, c = 1) {}', - 'const foo = function (a: number, b = 1, ...c) {}', + 'const foo = function() {};', + 'const foo = function(a: number) {};', + 'const foo = function(a = 1) {};', + 'const foo = function(a?: number) {};', + 'const foo = function(a: number, b: number) {};', + 'const foo = function(a: number, b: number, c?: number) {};', + 'const foo = function(a: number, b = 1) {};', + 'const foo = function(a: number, b = 1, c = 1) {};', + 'const foo = function(a: number, b = 1, c?: number) {};', + 'const foo = function(a: number, b?: number, c = 1) {};', + 'const foo = function(a: number, b = 1, ...c) {};', - 'const foo = () => {}', - 'const foo = (a: number) => {}', - 'const foo = (a = 1) => {}', - 'const foo = (a?: number) => {}', - 'const foo = (a: number, b: number) => {}', - 'const foo = (a: number, b: number, c?: number) => {}', - 'const foo = (a: number, b = 1) => {}', - 'const foo = (a: number, b = 1, c = 1) => {}', - 'const foo = (a: number, b = 1, c?: number) => {}', - 'const foo = (a: number, b?: number, c = 1) => {}', - 'const foo = (a: number, b = 1, ...c) => {}', + 'const foo = () => {};', + 'const foo = (a: number) => {};', + 'const foo = (a = 1) => {};', + 'const foo = (a?: number) => {};', + 'const foo = (a: number, b: number) => {};', + 'const foo = (a: number, b: number, c?: number) => {};', + 'const foo = (a: number, b = 1) => {};', + 'const foo = (a: number, b = 1, c = 1) => {};', + 'const foo = (a: number, b = 1, c?: number) => {};', + 'const foo = (a: number, b?: number, c = 1) => {};', + 'const foo = (a: number, b = 1, ...c) => {};', ` class Foo { constructor(a: number, b: number, c: number) {} @@ -251,7 +251,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b: number) {}', + code: 'const foo = function(a = 1, b: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -262,7 +262,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b = 2, c: number) {}', + code: 'const foo = function(a = 1, b = 2, c: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -279,7 +279,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b: number, c = 2, d: number) {}', + code: 'const foo = function(a = 1, b: number, c = 2, d: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -296,7 +296,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b: number, c = 2) {}', + code: 'const foo = function(a = 1, b: number, c = 2) {};', errors: [ { messageId: 'shouldBeLast', @@ -307,7 +307,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b: number, ...c) {}', + code: 'const foo = function(a = 1, b: number, ...c) {};', errors: [ { messageId: 'shouldBeLast', @@ -318,7 +318,7 @@ class Foo { ], }, { - code: 'const foo = function(a?: number, b: number) {}', + code: 'const foo = function(a?: number, b: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -329,7 +329,7 @@ class Foo { ], }, { - code: 'const foo = function(a: number, b?: number, c: number) {}', + code: 'const foo = function(a: number, b?: number, c: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -340,7 +340,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, b?: number, c: number) {}', + code: 'const foo = function(a = 1, b?: number, c: number) {};', errors: [ { messageId: 'shouldBeLast', @@ -357,7 +357,7 @@ class Foo { ], }, { - code: 'const foo = function(a = 1, { b }) {}', + code: 'const foo = function(a = 1, { b }) {};', errors: [ { messageId: 'shouldBeLast', @@ -368,7 +368,7 @@ class Foo { ], }, { - code: 'const foo = function({ a } = {}, b) {}', + code: 'const foo = function({ a } = {}, b) {};', errors: [ { messageId: 'shouldBeLast', @@ -379,7 +379,7 @@ class Foo { ], }, { - code: 'const foo = function({ a, b } = { a: 1, b: 2 }, c) {}', + code: 'const foo = function({ a, b } = { a: 1, b: 2 }, c) {};', errors: [ { messageId: 'shouldBeLast', @@ -390,7 +390,7 @@ class Foo { ], }, { - code: 'const foo = function([a] = [], b) {}', + code: 'const foo = function([a] = [], b) {};', errors: [ { messageId: 'shouldBeLast', @@ -401,7 +401,7 @@ class Foo { ], }, { - code: 'const foo = function([a, b] = [1, 2], c) {}', + code: 'const foo = function([a, b] = [1, 2], c) {};', errors: [ { messageId: 'shouldBeLast', @@ -412,7 +412,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b: number) => {}', + code: 'const foo = (a = 1, b: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -423,7 +423,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b = 2, c: number) => {}', + code: 'const foo = (a = 1, b = 2, c: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -440,7 +440,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b: number, c = 2, d: number) => {}', + code: 'const foo = (a = 1, b: number, c = 2, d: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -457,7 +457,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b: number, c = 2) => {}', + code: 'const foo = (a = 1, b: number, c = 2) => {};', errors: [ { messageId: 'shouldBeLast', @@ -468,7 +468,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b: number, ...c) => {}', + code: 'const foo = (a = 1, b: number, ...c) => {};', errors: [ { messageId: 'shouldBeLast', @@ -479,7 +479,7 @@ class Foo { ], }, { - code: 'const foo = (a?: number, b: number) => {}', + code: 'const foo = (a?: number, b: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -490,7 +490,7 @@ class Foo { ], }, { - code: 'const foo = (a: number, b?: number, c: number) => {}', + code: 'const foo = (a: number, b?: number, c: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -501,7 +501,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, b?: number, c: number) => {}', + code: 'const foo = (a = 1, b?: number, c: number) => {};', errors: [ { messageId: 'shouldBeLast', @@ -518,7 +518,7 @@ class Foo { ], }, { - code: 'const foo = (a = 1, { b }) => {}', + code: 'const foo = (a = 1, { b }) => {};', errors: [ { messageId: 'shouldBeLast', @@ -529,7 +529,7 @@ class Foo { ], }, { - code: 'const foo = ({ a } = {}, b) => {}', + code: 'const foo = ({ a } = {}, b) => {};', errors: [ { messageId: 'shouldBeLast', @@ -540,7 +540,7 @@ class Foo { ], }, { - code: 'const foo = ({ a, b } = { a: 1, b: 2 }, c) => {}', + code: 'const foo = ({ a, b } = { a: 1, b: 2 }, c) => {};', errors: [ { messageId: 'shouldBeLast', @@ -551,7 +551,7 @@ class Foo { ], }, { - code: 'const foo = ([a] = [], b) => {}', + code: 'const foo = ([a] = [], b) => {};', errors: [ { messageId: 'shouldBeLast', @@ -562,7 +562,7 @@ class Foo { ], }, { - code: 'const foo = ([a, b] = [1, 2], c) => {}', + code: 'const foo = ([a, b] = [1, 2], c) => {};', errors: [ { messageId: 'shouldBeLast', From fbad51c3a2a982d1080e234d09da2274708ece95 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:15:29 -0700 Subject: [PATCH 80/92] chore: consistent-type-definitions --- .../rules/consistent-type-definitions.test.ts | 86 ++++++++++--------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts index b777a1fc68f7..78e5d3ceb018 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/consistent-type-definitions'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -8,58 +8,62 @@ const ruleTester = new RuleTester({ ruleTester.run('consistent-type-definitions', rule, { valid: [ { - code: `var foo = { };`, + code: 'var foo = {};', options: ['interface'], }, { - code: `interface A {}`, + code: 'interface A {}', options: ['interface'], }, { - code: `interface A extends B { x: number; }`, + code: ` +interface A extends B { + x: number; +} + `, options: ['interface'], }, { - code: `type U = string;`, + code: 'type U = string;', options: ['interface'], }, { - code: `type V = { x: number; } | { y: string; };`, + code: 'type V = { x: number } | { y: string };', options: ['interface'], }, { code: ` type Record = { - [K in T]: U; -} -`, + [K in T]: U; +}; + `, options: ['interface'], }, { - code: `type T = { x: number; }`, + code: 'type T = { x: number };', options: ['type'], }, { - code: `type A = { x: number; } & B & C;`, + code: 'type A = { x: number } & B & C;', options: ['type'], }, { - code: `type A = { x: number; } & B & C;`, + code: 'type A = { x: number } & B & C;', options: ['type'], }, { code: ` export type W = { - x: T, + x: T; }; -`, + `, options: ['type'], }, ], invalid: [ { - code: `type T = { x: number; };`, - output: `interface T { x: number; }`, + code: noFormat`type T = { x: number; };`, + output: noFormat`interface T { x: number; }`, options: ['interface'], errors: [ { @@ -70,8 +74,8 @@ export type W = { ], }, { - code: `type T={ x: number; };`, - output: `interface T { x: number; }`, + code: noFormat`type T={ x: number; };`, + output: noFormat`interface T { x: number; }`, options: ['interface'], errors: [ { @@ -82,8 +86,8 @@ export type W = { ], }, { - code: `type T= { x: number; };`, - output: `interface T { x: number; }`, + code: noFormat`type T= { x: number; };`, + output: noFormat`interface T { x: number; }`, options: ['interface'], errors: [ { @@ -96,14 +100,14 @@ export type W = { { code: ` export type W = { - x: T, + x: T; }; -`, + `, output: ` export interface W { - x: T, + x: T; } -`, + `, options: ['interface'], errors: [ { @@ -114,8 +118,8 @@ export interface W { ], }, { - code: `interface T { x: number; }`, - output: `type T = { x: number; }`, + code: noFormat`interface T { x: number; }`, + output: noFormat`type T = { x: number; }`, options: ['type'], errors: [ { @@ -126,8 +130,8 @@ export interface W { ], }, { - code: `interface T{ x: number; }`, - output: `type T = { x: number; }`, + code: noFormat`interface T{ x: number; }`, + output: noFormat`type T = { x: number; }`, options: ['type'], errors: [ { @@ -138,8 +142,8 @@ export interface W { ], }, { - code: `interface T { x: number; }`, - output: `type T = { x: number; }`, + code: noFormat`interface T { x: number; }`, + output: noFormat`type T = { x: number; }`, options: ['type'], errors: [ { @@ -150,8 +154,8 @@ export interface W { ], }, { - code: `interface A extends B, C { x: number; };`, - output: `type A = { x: number; } & B & C;`, + code: noFormat`interface A extends B, C { x: number; };`, + output: noFormat`type A = { x: number; } & B & C;`, options: ['type'], errors: [ { @@ -162,8 +166,8 @@ export interface W { ], }, { - code: `interface A extends B, C { x: number; };`, - output: `type A = { x: number; } & B & C;`, + code: noFormat`interface A extends B, C { x: number; };`, + output: noFormat`type A = { x: number; } & B & C;`, options: ['type'], errors: [ { @@ -176,14 +180,14 @@ export interface W { { code: ` export interface W { - x: T, -}; -`, - output: ` + x: T; +} + `, + output: noFormat` export type W = { - x: T, -}; -`, + x: T; +} + `, options: ['type'], errors: [ { From cd150aa4a3523e6fa9dfa7592ec27e0be920fb42 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:16:04 -0700 Subject: [PATCH 81/92] chore: comma-spacing --- .../tests/rules/comma-spacing.test.ts | 347 +++++++++--------- 1 file changed, 176 insertions(+), 171 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts index 12a6e0219f8c..7147815b7595 100644 --- a/packages/eslint-plugin/tests/rules/comma-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/comma-spacing.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/comma-spacing'; import { RuleTester } from '../RuleTester'; @@ -7,280 +12,280 @@ const ruleTester = new RuleTester({ ruleTester.run('comma-spacing', rule, { valid: [ - `foo(1, true/* comment */, 'text');`, - `foo(1, true /* comment */, 'text');`, - `foo(1, true/* comment *//* comment */, 'text');`, - `foo(1, true/* comment */ /* comment */, 'text');`, - `foo(1, true, /* comment */ 'text');`, - `foo(1, // comment\n true, /* comment */ 'text');`, - { - code: `foo(1, // comment\n true,/* comment */ 'text');`, + "foo(1, true/* comment */, 'text');", + "foo(1, true /* comment */, 'text');", + "foo(1, true/* comment *//* comment */, 'text');", + "foo(1, true/* comment */ /* comment */, 'text');", + "foo(1, true, /* comment */ 'text');", + "foo(1, // comment\n true, /* comment */ 'text');", + { + code: "foo(1, // comment\n true,/* comment */ 'text');", options: [{ before: false, after: false }], }, - `const a = 1, b = 2;`, - `const foo = [, ];`, - `const foo = [1, ];`, - `const foo = [, 2];`, - `const foo = [1, 2];`, - `const foo = [, , ];`, - `const foo = [1, , ];`, - `const foo = [, 2, ];`, - `const foo = [, , 3];`, - `const foo = [1, 2, ];`, - `const foo = [, 2, 3];`, - `const foo = [1, , 3];`, - `const foo = [1, 2, 3];`, - `const foo = {'foo':'foo', 'baz':'baz'};`, - `const foo = {'foo':'foo', 'baz':\n'baz'};`, - `const foo = {'foo':\n'foo', 'baz':\n'baz'};`, - `function foo(a, b){}`, - `function foo(a, b = 1){}`, - `function foo(a = 1, b, c){}`, - `const foo = (a, b) => {}`, - `const foo = (a=1, b) => {}`, - `const foo = a => a + 2`, - `a, b`, - `const a = (1 + 2, 2)`, - `a(b, c)`, - `new A(b, c)`, - `foo((a), b)`, - `const b = ((1 + 2), 2)`, - `parseInt((a + b), 10)`, - `go.boom((a + b), 10)`, - `go.boom((a + b), 10, (4))`, - `const x = [ (a + c), (b + b) ]`, - `[' , ']`, + 'const a = 1, b = 2;', + 'const foo = [, ];', + 'const foo = [1, ];', + 'const foo = [, 2];', + 'const foo = [1, 2];', + 'const foo = [, , ];', + 'const foo = [1, , ];', + 'const foo = [, 2, ];', + 'const foo = [, , 3];', + 'const foo = [1, 2, ];', + 'const foo = [, 2, 3];', + 'const foo = [1, , 3];', + 'const foo = [1, 2, 3];', + "const foo = {'foo':'foo', 'baz':'baz'};", + "const foo = {'foo':'foo', 'baz':\n'baz'};", + "const foo = {'foo':\n'foo', 'baz':\n'baz'};", + 'function foo(a, b){}', + 'function foo(a, b = 1){}', + 'function foo(a = 1, b, c){}', + 'const foo = (a, b) => {}', + 'const foo = (a=1, b) => {}', + 'const foo = a => a + 2', + 'a, b', + 'const a = (1 + 2, 2)', + 'a(b, c)', + 'new A(b, c)', + 'foo((a), b)', + 'const b = ((1 + 2), 2)', + 'parseInt((a + b), 10)', + 'go.boom((a + b), 10)', + 'go.boom((a + b), 10, (4))', + 'const x = [ (a + c), (b + b) ]', + "[' , ']", '[` , `]', '`${[1, 2]}`', - `fn(a, b,)`, - `const fn = (a, b,) => {}`, - `const fn = function (a, b,) {}`, - `foo(/,/, 'a')`, - `const x = ',,,,,';`, - `const code = 'var foo = 1, bar = 3;'`, - `['apples', \n 'oranges'];`, - `{x: 'var x,y,z'}`, - { - code: `const foo = {'foo':\n'bar' ,'baz':\n'qur'};`, + 'fn(a, b,)', + 'const fn = (a, b,) => {}', + 'const fn = function (a, b,) {}', + "foo(/,/, 'a')", + "const x = ',,,,,';", + "const code = 'var foo = 1, bar = 3;'", + "['apples', \n 'oranges'];", + "{x: 'var x,y,z'}", + { + code: "const foo = {'foo':\n'bar' ,'baz':\n'qur'};", options: [{ before: true, after: false }], }, { - code: `const a = 1 ,b = 2;`, + code: 'const a = 1 ,b = 2;', options: [{ before: true, after: false }], }, { - code: `function foo(a ,b){}`, + code: 'function foo(a ,b){}', options: [{ before: true, after: false }], }, { - code: `const arr = [,];`, + code: 'const arr = [,];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 ,];`, + code: 'const arr = [1 ,];', options: [{ before: true, after: false }], }, { - code: `const arr = [ ,2];`, + code: 'const arr = [ ,2];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 ,2];`, + code: 'const arr = [1 ,2];', options: [{ before: true, after: false }], }, { - code: `const arr = [,,];`, + code: 'const arr = [,,];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 , ,];`, + code: 'const arr = [1 , ,];', options: [{ before: true, after: false }], }, { - code: `const arr = [ ,2 ,];`, + code: 'const arr = [ ,2 ,];', options: [{ before: true, after: false }], }, { - code: `const arr = [ , ,3];`, + code: 'const arr = [ , ,3];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 ,2 ,];`, + code: 'const arr = [1 ,2 ,];', options: [{ before: true, after: false }], }, { - code: `const arr = [ ,2 ,3];`, + code: 'const arr = [ ,2 ,3];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 , ,3];`, + code: 'const arr = [1 , ,3];', options: [{ before: true, after: false }], }, { - code: `const arr = [1 ,2 ,3];`, + code: 'const arr = [1 ,2 ,3];', options: [{ before: true, after: false }], }, { - code: `const obj = {'foo':'bar' , 'baz':'qur'};`, + code: "const obj = {'foo':'bar' , 'baz':'qur'};", options: [{ before: true, after: true }], }, { - code: `const a = 1 , b = 2;`, + code: 'const a = 1 , b = 2;', options: [{ before: true, after: true }], }, { - code: `const arr = [, ];`, + code: 'const arr = [, ];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , ];`, + code: 'const arr = [1 , ];', options: [{ before: true, after: true }], }, { - code: `const arr = [ , 2];`, + code: 'const arr = [ , 2];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , 2];`, + code: 'const arr = [1 , 2];', options: [{ before: true, after: true }], }, { - code: `const arr = [, , ];`, + code: 'const arr = [, , ];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , , ];`, + code: 'const arr = [1 , , ];', options: [{ before: true, after: true }], }, { - code: `const arr = [ , 2 , ];`, + code: 'const arr = [ , 2 , ];', options: [{ before: true, after: true }], }, { - code: `const arr = [ , , 3];`, + code: 'const arr = [ , , 3];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , 2 , ];`, + code: 'const arr = [1 , 2 , ];', options: [{ before: true, after: true }], }, { - code: `const arr = [, 2 , 3];`, + code: 'const arr = [, 2 , 3];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , , 3];`, + code: 'const arr = [1 , , 3];', options: [{ before: true, after: true }], }, { - code: `const arr = [1 , 2 , 3];`, + code: 'const arr = [1 , 2 , 3];', options: [{ before: true, after: true }], }, { - code: `a , b`, + code: 'a , b', options: [{ before: true, after: true }], }, { - code: `const arr = [,];`, + code: 'const arr = [,];', options: [{ before: false, after: false }], }, { - code: `const arr = [ ,];`, + code: 'const arr = [ ,];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,];`, + code: 'const arr = [1,];', options: [{ before: false, after: false }], }, { - code: `const arr = [,2];`, + code: 'const arr = [,2];', options: [{ before: false, after: false }], }, { - code: `const arr = [ ,2];`, + code: 'const arr = [ ,2];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,2];`, + code: 'const arr = [1,2];', options: [{ before: false, after: false }], }, { - code: `const arr = [,,];`, + code: 'const arr = [,,];', options: [{ before: false, after: false }], }, { - code: `const arr = [ ,,];`, + code: 'const arr = [ ,,];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,,];`, + code: 'const arr = [1,,];', options: [{ before: false, after: false }], }, { - code: `const arr = [,2,];`, + code: 'const arr = [,2,];', options: [{ before: false, after: false }], }, { - code: `const arr = [ ,2,];`, + code: 'const arr = [ ,2,];', options: [{ before: false, after: false }], }, { - code: `const arr = [,,3];`, + code: 'const arr = [,,3];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,2,];`, + code: 'const arr = [1,2,];', options: [{ before: false, after: false }], }, { - code: `const arr = [,2,3];`, + code: 'const arr = [,2,3];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,,3];`, + code: 'const arr = [1,,3];', options: [{ before: false, after: false }], }, { - code: `const arr = [1,2,3];`, + code: 'const arr = [1,2,3];', options: [{ before: false, after: false }], }, { - code: `const a = (1 + 2,2)`, + code: 'const a = (1 + 2,2)', options: [{ before: false, after: false }], }, 'const a; console.log(`${a}`, "a");', - `const [a, b] = [1, 2];`, - `const [a, b, ] = [1, 2];`, - `const [a, , b] = [1, 2, 3];`, - `const [ , b] = a;`, - `const [, b] = a;`, + 'const [a, b] = [1, 2];', + 'const [a, b, ] = [1, 2];', + 'const [a, , b] = [1, 2, 3];', + 'const [ , b] = a;', + 'const [, b] = a;', { - code: `
,`, + code: ',', parserOptions: { ecmaFeatures: { jsx: true }, }, }, { - code: ` , `, + code: ' , ', parserOptions: { ecmaFeatures: { jsx: true }, }, }, { - code: `Hello, world`, + code: 'Hello, world', options: [{ before: true, after: false }], parserOptions: { ecmaFeatures: { jsx: true } }, }, - `const Foo = (foo: T) => {}`, - `function foo() {}`, - `class Foo {}`, - `interface Foo{}`, + 'const Foo = (foo: T) => {}', + 'function foo() {}', + 'class Foo {}', + 'interface Foo{}', ], invalid: [ { - code: `a(b,c)`, - output: `a(b , c)`, + code: 'a(b,c)', + output: 'a(b , c)', options: [{ before: true, after: true }], errors: [ { @@ -298,8 +303,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `new A(b,c)`, - output: `new A(b , c)`, + code: 'new A(b,c)', + output: 'new A(b , c)', options: [{ before: true, after: true }], errors: [ { @@ -317,8 +322,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const a = 1 ,b = 2;`, - output: `const a = 1, b = 2;`, + code: 'const a = 1 ,b = 2;', + output: 'const a = 1, b = 2;', errors: [ { messageId: 'unexpected', @@ -335,8 +340,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1 , 2];`, - output: `const arr = [1, 2];`, + code: 'const arr = [1 , 2];', + output: 'const arr = [1, 2];', errors: [ { messageId: 'unexpected', @@ -359,8 +364,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1 , ];`, - output: `const arr = [1 ,];`, + code: 'const arr = [1 , ];', + output: 'const arr = [1 ,];', options: [{ before: true, after: false }], errors: [ { @@ -372,8 +377,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1 ,2];`, - output: `const arr = [1, 2];`, + code: 'const arr = [1 ,2];', + output: 'const arr = [1, 2];', errors: [ { messageId: 'unexpected', @@ -390,8 +395,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [(1) , 2];`, - output: `const arr = [(1), 2];`, + code: 'const arr = [(1) , 2];', + output: 'const arr = [(1), 2];', errors: [ { messageId: 'unexpected', @@ -402,8 +407,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1, 2];`, - output: `const arr = [1 ,2];`, + code: 'const arr = [1, 2];', + output: 'const arr = [1 ,2];', options: [{ before: true, after: false }], errors: [ { @@ -421,8 +426,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1\n , 2];`, - output: `const arr = [1\n ,2];`, + code: 'const arr = [1\n , 2];', + output: 'const arr = [1\n ,2];', options: [{ before: false, after: false }], errors: [ { @@ -434,8 +439,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1,\n 2];`, - output: `const arr = [1 ,\n 2];`, + code: 'const arr = [1,\n 2];', + output: 'const arr = [1 ,\n 2];', options: [{ before: true, after: false }], errors: [ { @@ -447,8 +452,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const obj = {'foo':\n'bar', 'baz':\n'qur'};`, - output: `const obj = {'foo':\n'bar' ,'baz':\n'qur'};`, + code: "const obj = {'foo':\n'bar', 'baz':\n'qur'};", + output: "const obj = {'foo':\n'bar' ,'baz':\n'qur'};", options: [{ before: true, after: false }], errors: [ { @@ -466,8 +471,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const obj = {a: 1\n ,b: 2};`, - output: `const obj = {a: 1\n , b: 2};`, + code: 'const obj = {a: 1\n ,b: 2};', + output: 'const obj = {a: 1\n , b: 2};', options: [{ before: false, after: true }], errors: [ { @@ -479,8 +484,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const obj = {a: 1 ,\n b: 2};`, - output: `const obj = {a: 1,\n b: 2};`, + code: 'const obj = {a: 1 ,\n b: 2};', + output: 'const obj = {a: 1,\n b: 2};', options: [{ before: false, after: false }], errors: [ { @@ -492,8 +497,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1 ,2];`, - output: `const arr = [1 , 2];`, + code: 'const arr = [1 ,2];', + output: 'const arr = [1 , 2];', options: [{ before: true, after: true }], errors: [ { @@ -505,8 +510,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1,2];`, - output: `const arr = [1 , 2];`, + code: 'const arr = [1,2];', + output: 'const arr = [1 , 2];', options: [{ before: true, after: true }], errors: [ { @@ -524,8 +529,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const obj = {'foo':\n'bar','baz':\n'qur'};`, - output: `const obj = {'foo':\n'bar' , 'baz':\n'qur'};`, + code: "const obj = {'foo':\n'bar','baz':\n'qur'};", + output: "const obj = {'foo':\n'bar' , 'baz':\n'qur'};", options: [{ before: true, after: true }], errors: [ { @@ -543,8 +548,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const arr = [1 , 2];`, - output: `const arr = [1,2];`, + code: 'const arr = [1 , 2];', + output: 'const arr = [1,2];', options: [{ before: false, after: false }], errors: [ { @@ -562,8 +567,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `a ,b`, - output: `a, b`, + code: 'a ,b', + output: 'a, b', options: [{ before: false, after: true }], errors: [ { @@ -581,8 +586,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function foo(a,b){}`, - output: `function foo(a , b){}`, + code: 'function foo(a,b){}', + output: 'function foo(a , b){}', options: [{ before: true, after: true }], errors: [ { @@ -600,8 +605,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const foo = (a,b) => {}`, - output: `const foo = (a , b) => {}`, + code: 'const foo = (a,b) => {}', + output: 'const foo = (a , b) => {}', options: [{ before: true, after: true }], errors: [ { @@ -619,8 +624,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `const foo = (a = 1,b) => {}`, - output: `const foo = (a = 1 , b) => {}`, + code: 'const foo = (a = 1,b) => {}', + output: 'const foo = (a = 1 , b) => {}', options: [{ before: true, after: true }], errors: [ { @@ -638,8 +643,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function foo(a = 1 ,b = 2) {}`, - output: `function foo(a = 1, b = 2) {}`, + code: 'function foo(a = 1 ,b = 2) {}', + output: 'function foo(a = 1, b = 2) {}', options: [{ before: false, after: true }], errors: [ { @@ -657,8 +662,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `{foo(1 ,2)}`, - output: `{foo(1, 2)}`, + code: '{foo(1 ,2)}', + output: '{foo(1, 2)}', parserOptions: { ecmaFeatures: { jsx: true }, }, @@ -678,8 +683,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `foo(1, true/* comment */ , 'foo');`, - output: `foo(1, true/* comment */, 'foo');`, + code: "foo(1, true/* comment */ , 'foo');", + output: "foo(1, true/* comment */, 'foo');", errors: [ { messageId: 'unexpected', @@ -690,8 +695,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `foo(1, true,/* comment */ 'foo');`, - output: `foo(1, true, /* comment */ 'foo');`, + code: "foo(1, true,/* comment */ 'foo');", + output: "foo(1, true, /* comment */ 'foo');", errors: [ { messageId: 'missing', @@ -702,8 +707,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `foo(404,// comment\n true, 'hello');`, - output: `foo(404, // comment\n true, 'hello');`, + code: "foo(404,// comment\n true, 'hello');", + output: "foo(404, // comment\n true, 'hello');", errors: [ { messageId: 'missing', @@ -714,8 +719,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function Foo() {}`, - output: `function Foo() {}`, + code: 'function Foo() {}', + output: 'function Foo() {}', errors: [ { messageId: 'missing', @@ -726,8 +731,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function Foo() {}`, - output: `function Foo() {}`, + code: 'function Foo() {}', + output: 'function Foo() {}', errors: [ { messageId: 'unexpected', @@ -738,8 +743,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function Foo() {}`, - output: `function Foo() {}`, + code: 'function Foo() {}', + output: 'function Foo() {}', errors: [ { messageId: 'unexpected', @@ -756,8 +761,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function Foo() {}`, - output: `function Foo() {}`, + code: 'function Foo() {}', + output: 'function Foo() {}', options: [{ before: false, after: false }], errors: [ { @@ -769,8 +774,8 @@ ruleTester.run('comma-spacing', rule, { ], }, { - code: `function Foo() {}`, - output: `function Foo() {}`, + code: 'function Foo() {}', + output: 'function Foo() {}', options: [{ before: true, after: false }], errors: [ { From 2c424d50de3d64d7dde394684e51f7f324dd3ad2 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:16:31 -0700 Subject: [PATCH 82/92] chore: class-name-casing --- .../tests/rules/class-name-casing.test.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/class-name-casing.test.ts b/packages/eslint-plugin/tests/rules/class-name-casing.test.ts index 11f6dc8b8889..1977a36415da 100644 --- a/packages/eslint-plugin/tests/rules/class-name-casing.test.ts +++ b/packages/eslint-plugin/tests/rules/class-name-casing.test.ts @@ -35,12 +35,19 @@ ruleTester.run('class-name-casing', rule, { 'class ÈClassNameWithUnicode {}', 'class ClassNameWithæUnicode {}', // Following test cases are valid, but no one is going to write code like this - 'var { bar } = class { static bar() { return 2 } }', - `var [ bar ] = class { - static [Symbol.iterator]() { - return { next: () => ({ value: 1, done: false}) } - } - } + ` +var { bar } = class { + static bar() { + return 2; + } +}; + `, + ` +var [bar] = class { + static [Symbol.iterator]() { + return { next: () => ({ value: 1, done: false }) }; + } +}; `, ], @@ -116,7 +123,7 @@ ruleTester.run('class-name-casing', rule, { ], }, { - code: 'var bar = class invalidName {}', + code: 'var bar = class invalidName {};', errors: [ { messageId: 'notPascalCased', From 026eb1963eb029d49236ffb4ff30b8880bb11c90 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:25:30 -0700 Subject: [PATCH 83/92] chore: class-literal-property-style --- .../class-literal-property-style.test.ts | 431 ++++++++++++------ 1 file changed, 297 insertions(+), 134 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts index f0c5f2afc055..19d5fb183b54 100644 --- a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts +++ b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/class-literal-property-style'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -7,18 +7,50 @@ const ruleTester = new RuleTester({ ruleTester.run('class-literal-property-style', rule, { valid: [ - 'class Mx { declare readonly p1 = 1; }', - 'class Mx { readonly p1 = "hello world"; }', - 'class Mx { p1 = "hello world"; }', - 'class Mx { static p1 = "hello world"; }', - 'class Mx { p1: string; }', - 'class Mx { get p1(); }', - 'class Mx { get p1() {} }', - 'abstract class Mx { abstract get p1(): string }', + ` +class Mx { + declare readonly p1 = 1; +} + `, + ` +class Mx { + readonly p1 = 'hello world'; +} + `, + ` +class Mx { + p1 = 'hello world'; +} + `, + ` +class Mx { + static p1 = 'hello world'; +} + `, + ` +class Mx { + p1: string; +} + `, + ` +class Mx { + get p1(); +} + `, + ` +class Mx { + get p1() {} +} + `, + ` +abstract class Mx { + abstract get p1(): string; +} + `, ` class Mx { get mySetting() { - if(this._aValue) { + if (this._aValue) { return 'on'; } @@ -29,14 +61,14 @@ ruleTester.run('class-literal-property-style', rule, { ` class Mx { get mySetting() { - return \`build-\${process.env.build}\` + return \`build-\${process.env.build}\`; } } `, ` class Mx { getMySetting() { - if(this._aValue) { + if (this._aValue) { return 'on'; } @@ -64,31 +96,63 @@ ruleTester.run('class-literal-property-style', rule, { options: ['fields'], }, { - code: 'class Mx { declare public readonly foo = 1; }', + code: ` +class Mx { + public declare readonly foo = 1; +} + `, options: ['getters'], }, { - code: 'class Mx { get p1() { return "hello world"; } }', + code: ` +class Mx { + get p1() { + return 'hello world'; + } +} + `, options: ['getters'], }, { - code: 'class Mx { p1 = "hello world"; }', + code: ` +class Mx { + p1 = 'hello world'; +} + `, options: ['getters'], }, { - code: 'class Mx { p1: string; }', + code: ` +class Mx { + p1: string; +} + `, options: ['getters'], }, { - code: 'class Mx { readonly p1 = [1, 2, 3]; }', + code: ` +class Mx { + readonly p1 = [1, 2, 3]; +} + `, options: ['getters'], }, { - code: 'class Mx { static p1: string; }', + code: ` +class Mx { + static p1: string; +} + `, options: ['getters'], }, { - code: 'class Mx { static get p1() { return "hello world"; } }', + code: ` +class Mx { + static get p1() { + return 'hello world'; + } +} + `, options: ['getters'], }, { @@ -116,259 +180,358 @@ ruleTester.run('class-literal-property-style', rule, { ], invalid: [ { - code: 'class Mx { get p1() { return "hello world"; } }', - output: 'class Mx { readonly p1 = "hello world"; }', + code: ` +class Mx { + get p1() { + return 'hello world'; + } +} + `, + output: ` +class Mx { + readonly p1 = 'hello world'; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 16, - line: 1, + column: 7, + line: 3, }, ], }, { - code: 'class Mx { get p1() { return `hello world`; } }', - output: 'class Mx { readonly p1 = `hello world`; }', + code: ` +class Mx { + get p1() { + return \`hello world\`; + } +} + `, + output: ` +class Mx { + readonly p1 = \`hello world\`; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 16, - line: 1, + column: 7, + line: 3, }, ], }, { - code: 'class Mx { static get p1() { return "hello world"; } }', - output: 'class Mx { static readonly p1 = "hello world"; }', + code: ` +class Mx { + static get p1() { + return 'hello world'; + } +} + `, + output: ` +class Mx { + static readonly p1 = 'hello world'; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 23, - line: 1, + column: 14, + line: 3, }, ], }, { - code: - 'class Mx { public static readonly static private public protected get foo() { return 1; } }', - output: 'class Mx { public static readonly foo = 1; }', + code: ` +class Mx { + public static get foo() { + return 1; + } +} + `, + output: ` +class Mx { + public static readonly foo = 1; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 71, - line: 1, + column: 21, + line: 3, }, ], }, { code: ` - class Mx { - public get [myValue]() { - return 'a literal value'; - } - } +class Mx { + public get [myValue]() { + return 'a literal value'; + } +} `, output: ` - class Mx { - public readonly [myValue] = 'a literal value'; - } +class Mx { + public readonly [myValue] = 'a literal value'; +} `, errors: [ { messageId: 'preferFieldStyle', - column: 23, + column: 15, line: 3, }, ], }, { code: ` - class Mx { - public get [myValue]() { - return 12345n; - } - } +class Mx { + public get [myValue]() { + return 12345n; + } +} `, output: ` - class Mx { - public readonly [myValue] = 12345n; - } +class Mx { + public readonly [myValue] = 12345n; +} `, errors: [ { messageId: 'preferFieldStyle', - column: 23, + column: 15, line: 3, }, ], }, { code: ` - class Mx { - public readonly [myValue] = 'a literal value'; - } +class Mx { + public readonly [myValue] = 'a literal value'; +} `, - output: ` - class Mx { - public get [myValue]() { return 'a literal value'; } - } + output: noFormat` +class Mx { + public get [myValue]() { return 'a literal value'; } +} `, errors: [ { messageId: 'preferGetterStyle', - column: 28, + column: 20, line: 3, }, ], options: ['getters'], }, { - code: 'class Mx { readonly p1 = "hello world"; }', - output: 'class Mx { get p1() { return "hello world"; } }', + code: ` +class Mx { + readonly p1 = 'hello world'; +} + `, + output: noFormat` +class Mx { + get p1() { return 'hello world'; } +} + `, errors: [ { messageId: 'preferGetterStyle', - column: 21, - line: 1, + column: 12, + line: 3, }, ], options: ['getters'], }, { - code: 'class Mx { readonly p1 = `hello world`; }', - output: 'class Mx { get p1() { return `hello world`; } }', + code: ` +class Mx { + readonly p1 = \`hello world\`; +} + `, + output: noFormat` +class Mx { + get p1() { return \`hello world\`; } +} + `, errors: [ { messageId: 'preferGetterStyle', - column: 21, - line: 1, + column: 12, + line: 3, }, ], options: ['getters'], }, { - code: 'class Mx { static readonly p1 = "hello world"; }', - output: 'class Mx { static get p1() { return "hello world"; } }', + code: ` +class Mx { + static readonly p1 = 'hello world'; +} + `, + output: noFormat` +class Mx { + static get p1() { return 'hello world'; } +} + `, errors: [ { messageId: 'preferGetterStyle', - column: 28, - line: 1, + column: 19, + line: 3, }, ], options: ['getters'], }, { - code: 'class Mx { protected get p1() { return "hello world"; } }', - output: 'class Mx { protected readonly p1 = "hello world"; }', + code: ` +class Mx { + protected get p1() { + return 'hello world'; + } +} + `, + output: ` +class Mx { + protected readonly p1 = 'hello world'; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 26, - line: 1, + column: 17, + line: 3, }, ], options: ['fields'], }, { - code: 'class Mx { protected readonly p1 = "hello world"; }', - output: 'class Mx { protected get p1() { return "hello world"; } }', + code: ` +class Mx { + protected readonly p1 = 'hello world'; +} + `, + output: noFormat` +class Mx { + protected get p1() { return 'hello world'; } +} + `, errors: [ { messageId: 'preferGetterStyle', - column: 31, - line: 1, + column: 22, + line: 3, }, ], options: ['getters'], }, { - code: 'class Mx { public static get p1() { return "hello world"; } }', - output: 'class Mx { public static readonly p1 = "hello world"; }', + code: ` +class Mx { + public static get p1() { + return 'hello world'; + } +} + `, + output: ` +class Mx { + public static readonly p1 = 'hello world'; +} + `, errors: [ { messageId: 'preferFieldStyle', - column: 30, - line: 1, + column: 21, + line: 3, }, ], }, { - code: 'class Mx { public static readonly p1 = "hello world"; }', - output: 'class Mx { public static get p1() { return "hello world"; } }', + code: ` +class Mx { + public static readonly p1 = 'hello world'; +} + `, + output: noFormat` +class Mx { + public static get p1() { return 'hello world'; } +} + `, errors: [ { messageId: 'preferGetterStyle', - column: 35, - line: 1, + column: 26, + line: 3, }, ], options: ['getters'], }, { code: ` - class Mx { - public get myValue() { - return gql\` - { - user(id: 5) { - firstName - lastName - } - } - \`; - } +class Mx { + public get myValue() { + return gql\` + { + user(id: 5) { + firstName + lastName } + } + \`; + } +} `, - output: ` - class Mx { - public readonly myValue = gql\` - { - user(id: 5) { - firstName - lastName - } - } - \`; + output: noFormat` +class Mx { + public readonly myValue = gql\` + { + user(id: 5) { + firstName + lastName } + } + \`; +} `, errors: [ { messageId: 'preferFieldStyle', - column: 22, + column: 14, line: 3, }, ], }, { code: ` - class Mx { - public readonly myValue = gql\` - { - user(id: 5) { - firstName - lastName - } - } - \`; - } +class Mx { + public readonly myValue = gql\` + { + user(id: 5) { + firstName + lastName + } + } + \`; +} `, - output: ` - class Mx { - public get myValue() { return gql\` - { - user(id: 5) { - firstName - lastName - } - } - \`; } - } + output: noFormat` +class Mx { + public get myValue() { return gql\` + { + user(id: 5) { + firstName + lastName + } + } + \`; } +} `, errors: [ { messageId: 'preferGetterStyle', - column: 27, + column: 19, line: 3, }, ], From bc46b7444cb985fe361b16ac86b72b6f55efc61e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:27:28 -0700 Subject: [PATCH 84/92] chore: camelcase --- .../tests/rules/camelcase.test.ts | 200 +++++++++++++----- 1 file changed, 152 insertions(+), 48 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/camelcase.test.ts b/packages/eslint-plugin/tests/rules/camelcase.test.ts index 617cdf3e40f7..1a35ee87cc82 100644 --- a/packages/eslint-plugin/tests/rules/camelcase.test.ts +++ b/packages/eslint-plugin/tests/rules/camelcase.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/camelcase'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -8,75 +8,147 @@ const ruleTester = new RuleTester({ ruleTester.run('camelcase', rule, { valid: [ { - code: 'interface Foo { b_ar: number }', + code: ` +interface Foo { + b_ar: number; +} + `, options: [{ properties: 'never' }], }, { - code: 'interface Foo { bar: number }', + code: ` +interface Foo { + bar: number; +} + `, options: [{ properties: 'always' }], }, { - code: 'class Foo { b_ar: number; }', + code: ` +class Foo { + b_ar: number; +} + `, options: [{ properties: 'never' }], }, { - code: 'class Foo { bar: number; }', + code: ` +class Foo { + bar: number; +} + `, options: [{ properties: 'always' }], }, { - code: 'class Foo { b_ar: number = 0; }', + code: ` +class Foo { + b_ar: number = 0; +} + `, options: [{ properties: 'never' }], }, { - code: 'class Foo { bar: number = 0; }', + code: ` +class Foo { + bar: number = 0; +} + `, options: [{ properties: 'always' }], }, { - code: 'class Foo { constructor(private b_ar: number) {} }', + code: ` +class Foo { + constructor(private b_ar: number) {} +} + `, options: [{ properties: 'never' }], }, { - code: 'class Foo { constructor(private bar: number) {} }', + code: ` +class Foo { + constructor(private bar: number) {} +} + `, options: [{ properties: 'always' }], }, { - code: 'class Foo { constructor(private b_ar: number = 0) {} }', + code: ` +class Foo { + constructor(private b_ar: number = 0) {} +} + `, options: [{ properties: 'never' }], }, { - code: 'class Foo { constructor(private bar: number = 0) {} }', + code: ` +class Foo { + constructor(private bar: number = 0) {} +} + `, options: [{ properties: 'always' }], }, { - code: 'abstract class Foo { b_ar: number; }', + code: ` +abstract class Foo { + b_ar: number; +} + `, options: [{ properties: 'never' }], }, { - code: 'abstract class Foo { bar: number; }', + code: ` +abstract class Foo { + bar: number; +} + `, options: [{ properties: 'always' }], }, { - code: 'abstract class Foo { b_ar: number = 0; }', + code: ` +abstract class Foo { + b_ar: number = 0; +} + `, options: [{ properties: 'never' }], }, { - code: 'abstract class Foo { bar: number = 0; }', + code: ` +abstract class Foo { + bar: number = 0; +} + `, options: [{ properties: 'always' }], }, { - code: 'abstract class Foo { abstract b_ar: number; }', + code: ` +abstract class Foo { + abstract b_ar: number; +} + `, options: [{ properties: 'never' }], }, { - code: 'abstract class Foo { abstract bar: number; }', + code: ` +abstract class Foo { + abstract bar: number; +} + `, options: [{ properties: 'always' }], }, { - code: 'abstract class Foo { abstract b_ar: number = 0; }', + code: ` +abstract class Foo { + abstract b_ar: number = 0; +} + `, options: [{ properties: 'never' }], }, { - code: 'abstract class Foo { abstract bar: number = 0; }', + code: ` +abstract class Foo { + abstract bar: number = 0; +} + `, options: [{ properties: 'always' }], }, { @@ -141,19 +213,19 @@ class Foo { }, { code: ` -type Foo = {} +type Foo = {}; `, options: [{ genericType: 'always' }], }, { code: ` -type Foo = {} +type Foo = {}; `, options: [{ genericType: 'always' }], }, { code: ` -type Foo = {} +type Foo = {}; `, options: [{ genericType: 'never' }], }, @@ -183,7 +255,7 @@ class Foo { code: 'const foo = foo.bar?.foo_bar_baz;', }, { - code: 'const foo = (foo?.bar?.baz)?.foo_bar_baz;', + code: noFormat`const foo = (foo?.bar?.baz)?.foo_bar_baz;`, }, { code: 'const foo = foo_bar?.foo;', @@ -193,7 +265,11 @@ class Foo { invalid: [ { - code: 'interface Foo { b_ar: number }', + code: ` +interface Foo { + b_ar: number; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -201,13 +277,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 17, + line: 3, + column: 3, }, ], }, { - code: 'class Foo { b_ar: number; }', + code: ` +class Foo { + b_ar: number; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -215,13 +295,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 13, + line: 3, + column: 3, }, ], }, { - code: 'class Foo { constructor(private b_ar: number) {} }', + code: ` +class Foo { + constructor(private b_ar: number) {} +} + `, options: [{ properties: 'always' }], errors: [ { @@ -229,13 +313,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 33, + line: 3, + column: 23, }, ], }, { - code: 'class Foo { constructor(private b_ar: number = 0) {} }', + code: ` +class Foo { + constructor(private b_ar: number = 0) {} +} + `, options: [{ properties: 'always' }], errors: [ { @@ -243,13 +331,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 33, + line: 3, + column: 23, }, ], }, { - code: 'abstract class Foo { b_ar: number; }', + code: ` +abstract class Foo { + b_ar: number; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -257,13 +349,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 22, + line: 3, + column: 3, }, ], }, { - code: 'abstract class Foo { b_ar: number = 0; }', + code: ` +abstract class Foo { + b_ar: number = 0; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -271,13 +367,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 22, + line: 3, + column: 3, }, ], }, { - code: 'abstract class Foo { abstract b_ar: number; }', + code: ` +abstract class Foo { + abstract b_ar: number; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -285,13 +385,17 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 31, + line: 3, + column: 12, }, ], }, { - code: 'abstract class Foo { abstract b_ar: number = 0; }', + code: ` +abstract class Foo { + abstract b_ar: number = 0; +} + `, options: [{ properties: 'always' }], errors: [ { @@ -299,8 +403,8 @@ class Foo { data: { name: 'b_ar', }, - line: 1, - column: 31, + line: 3, + column: 12, }, ], }, @@ -319,7 +423,7 @@ class Foo { ], }, { - code: 'const foo = (foo_test?.bar)?.baz;', + code: noFormat`const foo = (foo_test?.bar)?.baz;`, options: [{ properties: 'always' }], errors: [ { From e18cb6a331906b7c2862c52d10eb8579b7ab0f1b Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:28:20 -0700 Subject: [PATCH 85/92] chore: brace-style --- .../tests/rules/brace-style.test.ts | 352 +++++++++--------- 1 file changed, 184 insertions(+), 168 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/brace-style.test.ts b/packages/eslint-plugin/tests/rules/brace-style.test.ts index 21d10998eac1..28b9413795db 100644 --- a/packages/eslint-plugin/tests/rules/brace-style.test.ts +++ b/packages/eslint-plugin/tests/rules/brace-style.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the position of braces, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import rule from '../../src/rules/brace-style'; import { RuleTester } from '../RuleTester'; @@ -211,67 +216,67 @@ catch (e) options: ['allman'], }, { - code: `function foo () { return; }`, + code: 'function foo () { return; }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `function foo () { a(); b(); return; }`, + code: 'function foo () { a(); b(); return; }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `function a(b,c,d) { }`, + code: 'function a(b,c,d) { }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `!function foo () { return; }`, + code: '!function foo () { return; }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `!function a(b,c,d) { }`, + code: '!function a(b,c,d) { }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `if (foo) { bar(); }`, + code: 'if (foo) { bar(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `if (a) { b(); } else { c(); }`, + code: 'if (a) { b(); } else { c(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `while (foo) { bar(); }`, + code: 'while (foo) { bar(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `for (;;) { bar(); }`, + code: 'for (;;) { bar(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `with (foo) { bar(); }`, + code: 'with (foo) { bar(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `switch (foo) { case 'bar': break; }`, + code: "switch (foo) { case 'bar': break; }", options: ['1tbs', { allowSingleLine: true }], }, { - code: `try { bar(); } catch (e) { baz(); }`, + code: 'try { bar(); } catch (e) { baz(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `do { bar(); } while (true)`, + code: 'do { bar(); } while (true)', options: ['1tbs', { allowSingleLine: true }], }, { - code: `for (foo in bar) { baz(); }`, + code: 'for (foo in bar) { baz(); }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `if (a && b && c) { }`, + code: 'if (a && b && c) { }', options: ['1tbs', { allowSingleLine: true }], }, { - code: `switch(0) {}`, + code: 'switch(0) {}', options: ['1tbs', { allowSingleLine: true }], }, { @@ -289,7 +294,7 @@ catch (e) { baz(); } options: ['stroustrup', { allowSingleLine: true }], }, { - code: `var foo = () => { return; }`, + code: 'var foo = () => { return; }', options: ['stroustrup', { allowSingleLine: true }], parserOptions: { ecmaVersion: 6 }, }, @@ -308,7 +313,7 @@ catch (e) { baz(); } options: ['allman', { allowSingleLine: true }], }, { - code: `var foo = () => { return; }`, + code: 'var foo = () => { return; }', options: ['allman', { allowSingleLine: true }], parserOptions: { ecmaVersion: 6 }, }, @@ -345,7 +350,7 @@ switch(x) options: ['allman'], }, { - code: `switch(x) {}`, + code: 'switch(x) {}', options: ['allman', { allowSingleLine: true }], }, { @@ -388,25 +393,25 @@ Foo options: ['allman'], }, { - code: `class Foo {}`, + code: 'class Foo {}', options: ['1tbs', { allowSingleLine: true }], }, { - code: `class Foo {}`, + code: 'class Foo {}', options: ['allman', { allowSingleLine: true }], }, { - code: `(class {})`, + code: '(class {})', options: ['1tbs', { allowSingleLine: true }], }, { - code: `(class {})`, + code: '(class {})', options: ['allman', { allowSingleLine: true }], }, // https://github.com/eslint/eslint/issues/7908 { - code: `{}`, + code: '{}', }, { code: ` @@ -568,7 +573,7 @@ enum Foo { options: ['stroustrup'], }, { - code: `enum Foo { A, B }`, + code: 'enum Foo { A, B }', options: ['1tbs', { allowSingleLine: true }], }, ], @@ -591,8 +596,8 @@ if (f) { errors: [{ messageId: 'nextLineClose' }], }, { - code: `var foo = () => { return; }`, - output: `var foo = () => {\n return; \n}`, + code: 'var foo = () => { return; }', + output: 'var foo = () => {\n return; \n}', parserOptions: { ecmaVersion: 6 }, errors: [ { messageId: 'blockSameLine' }, @@ -600,31 +605,31 @@ if (f) { ], }, { - code: `function foo() { return; }`, - output: `function foo() {\n return; \n}`, + code: 'function foo() { return; }', + output: 'function foo() {\n return; \n}', errors: [ { messageId: 'blockSameLine' }, { messageId: 'singleLineClose' }, ], }, { - code: `function foo() \n { \n return; }`, - output: `function foo() { \n return; \n}`, + code: 'function foo() \n { \n return; }', + output: 'function foo() { \n return; \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `!function foo() \n { \n return; }`, - output: `!function foo() { \n return; \n}`, + code: '!function foo() \n { \n return; }', + output: '!function foo() { \n return; \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `if (foo) \n { \n bar(); }`, - output: `if (foo) { \n bar(); \n}`, + code: 'if (foo) \n { \n bar(); }', + output: 'if (foo) { \n bar(); \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `if (a) { \nb();\n } else \n { c(); }`, - output: `if (a) { \nb();\n } else {\n c(); \n}`, + code: 'if (a) { \nb();\n } else \n { c(); }', + output: 'if (a) { \nb();\n } else {\n c(); \n}', errors: [ { messageId: 'nextLineOpen' }, { messageId: 'blockSameLine' }, @@ -632,104 +637,108 @@ if (f) { ], }, { - code: `while (foo) \n { \n bar(); }`, - output: `while (foo) { \n bar(); \n}`, + code: 'while (foo) \n { \n bar(); }', + output: 'while (foo) { \n bar(); \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `for (;;) \n { \n bar(); }`, - output: `for (;;) { \n bar(); \n}`, + code: 'for (;;) \n { \n bar(); }', + output: 'for (;;) { \n bar(); \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `with (foo) \n { \n bar(); }`, - output: `with (foo) { \n bar(); \n}`, + code: 'with (foo) \n { \n bar(); }', + output: 'with (foo) { \n bar(); \n}', errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `switch (foo) \n { \n case 'bar': break; }`, - output: `switch (foo) { \n case 'bar': break; \n}`, + code: "switch (foo) \n { \n case 'bar': break; }", + output: "switch (foo) { \n case 'bar': break; \n}", errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `switch (foo) \n { }`, - output: `switch (foo) { }`, + code: 'switch (foo) \n { }', + output: 'switch (foo) { }', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try \n { \n bar(); \n } catch (e) {}`, - output: `try { \n bar(); \n } catch (e) {}`, + code: 'try \n { \n bar(); \n } catch (e) {}', + output: 'try { \n bar(); \n } catch (e) {}', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try { \n bar(); \n } catch (e) \n {}`, - output: `try { \n bar(); \n } catch (e) {}`, + code: 'try { \n bar(); \n } catch (e) \n {}', + output: 'try { \n bar(); \n } catch (e) {}', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `do \n { \n bar(); \n} while (true)`, - output: `do { \n bar(); \n} while (true)`, + code: 'do \n { \n bar(); \n} while (true)', + output: 'do { \n bar(); \n} while (true)', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `for (foo in bar) \n { \n baz(); \n }`, - output: `for (foo in bar) { \n baz(); \n }`, + code: 'for (foo in bar) \n { \n baz(); \n }', + output: 'for (foo in bar) { \n baz(); \n }', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `for (foo of bar) \n { \n baz(); \n }`, - output: `for (foo of bar) { \n baz(); \n }`, + code: 'for (foo of bar) \n { \n baz(); \n }', + output: 'for (foo of bar) { \n baz(); \n }', parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try { \n bar(); \n }\ncatch (e) {\n}`, - output: `try { \n bar(); \n } catch (e) {\n}`, + code: 'try { \n bar(); \n }\ncatch (e) {\n}', + output: 'try { \n bar(); \n } catch (e) {\n}', errors: [{ messageId: 'nextLineClose' }], }, { - code: `try { \n bar(); \n } catch (e) {\n}\n finally {\n}`, - output: `try { \n bar(); \n } catch (e) {\n} finally {\n}`, + code: 'try { \n bar(); \n } catch (e) {\n}\n finally {\n}', + output: 'try { \n bar(); \n } catch (e) {\n} finally {\n}', errors: [{ messageId: 'nextLineClose' }], }, { - code: `if (a) { \nb();\n } \n else { \nc();\n }`, - output: `if (a) { \nb();\n } else { \nc();\n }`, + code: 'if (a) { \nb();\n } \n else { \nc();\n }', + output: 'if (a) { \nb();\n } else { \nc();\n }', errors: [{ messageId: 'nextLineClose' }], }, { - code: `try { \n bar(); \n }\ncatch (e) {\n} finally {\n}`, - output: `try { \n bar(); \n }\ncatch (e) {\n}\n finally {\n}`, + code: 'try { \n bar(); \n }\ncatch (e) {\n} finally {\n}', + output: 'try { \n bar(); \n }\ncatch (e) {\n}\n finally {\n}', options: ['stroustrup'], errors: [{ messageId: 'sameLineClose' }], }, { - code: `try { \n bar(); \n } catch (e) {\n}\n finally {\n}`, - output: `try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}`, + code: 'try { \n bar(); \n } catch (e) {\n}\n finally {\n}', + output: 'try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}', options: ['stroustrup'], errors: [{ messageId: 'sameLineClose' }], }, { - code: `if (a) { \nb();\n } else { \nc();\n }`, - output: `if (a) { \nb();\n }\n else { \nc();\n }`, + code: 'if (a) { \nb();\n } else { \nc();\n }', + output: 'if (a) { \nb();\n }\n else { \nc();\n }', options: ['stroustrup'], errors: [{ messageId: 'sameLineClose' }], }, { - code: `if (foo) {\nbaz();\n} else if (bar) {\nbaz();\n}\nelse {\nqux();\n}`, - output: `if (foo) {\nbaz();\n}\n else if (bar) {\nbaz();\n}\nelse {\nqux();\n}`, + code: + 'if (foo) {\nbaz();\n} else if (bar) {\nbaz();\n}\nelse {\nqux();\n}', + output: + 'if (foo) {\nbaz();\n}\n else if (bar) {\nbaz();\n}\nelse {\nqux();\n}', options: ['stroustrup'], errors: [{ messageId: 'sameLineClose' }], }, { - code: `if (foo) {\npoop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}`, - output: `if (foo) {\npoop();\n} \nelse if (bar) {\nbaz();\n}\n else if (thing) {\nboom();\n}\nelse {\nqux();\n}`, + code: + 'if (foo) {\npoop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}', + output: + 'if (foo) {\npoop();\n} \nelse if (bar) {\nbaz();\n}\n else if (thing) {\nboom();\n}\nelse {\nqux();\n}', options: ['stroustrup'], errors: [{ messageId: 'sameLineClose' }], }, { - code: `try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}`, - output: `try \n{ \n bar(); \n }\n catch (e) \n{\n}\n finally \n{\n}`, + code: 'try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}', + output: 'try \n{ \n bar(); \n }\n catch (e) \n{\n}\n finally \n{\n}', options: ['allman'], errors: [ { messageId: 'sameLineOpen', line: 1 }, @@ -738,8 +747,8 @@ if (f) { ], }, { - code: `switch(x) { case 1: \nbar(); }\n `, - output: `switch(x) \n{\n case 1: \nbar(); \n}\n `, + code: 'switch(x) { case 1: \nbar(); }\n ', + output: 'switch(x) \n{\n case 1: \nbar(); \n}\n ', options: ['allman'], errors: [ { messageId: 'sameLineOpen', line: 1 }, @@ -748,8 +757,8 @@ if (f) { ], }, { - code: `if (a) { \nb();\n } else { \nc();\n }`, - output: `if (a) \n{ \nb();\n }\n else \n{ \nc();\n }`, + code: 'if (a) { \nb();\n } else { \nc();\n }', + output: 'if (a) \n{ \nb();\n }\n else \n{ \nc();\n }', options: ['allman'], errors: [ { messageId: 'sameLineOpen' }, @@ -758,8 +767,10 @@ if (f) { ], }, { - code: `if (foo) {\nbaz();\n} else if (bar) {\nbaz();\n}\nelse {\nqux();\n}`, - output: `if (foo) \n{\nbaz();\n}\n else if (bar) \n{\nbaz();\n}\nelse \n{\nqux();\n}`, + code: + 'if (foo) {\nbaz();\n} else if (bar) {\nbaz();\n}\nelse {\nqux();\n}', + output: + 'if (foo) \n{\nbaz();\n}\n else if (bar) \n{\nbaz();\n}\nelse \n{\nqux();\n}', options: ['allman'], errors: [ { messageId: 'sameLineOpen' }, @@ -769,8 +780,10 @@ if (f) { ], }, { - code: `if (foo)\n{ poop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}`, - output: `if (foo)\n{\n poop();\n} \nelse if (bar) \n{\nbaz();\n}\n else if (thing) \n{\nboom();\n}\nelse \n{\nqux();\n}`, + code: + 'if (foo)\n{ poop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}', + output: + 'if (foo)\n{\n poop();\n} \nelse if (bar) \n{\nbaz();\n}\n else if (thing) \n{\nboom();\n}\nelse \n{\nqux();\n}', options: ['allman'], errors: [ { messageId: 'blockSameLine' }, @@ -781,159 +794,162 @@ if (f) { ], }, { - code: `if (foo)\n{\n bar(); }`, - output: `if (foo)\n{\n bar(); \n}`, + code: 'if (foo)\n{\n bar(); }', + output: 'if (foo)\n{\n bar(); \n}', options: ['allman'], errors: [{ messageId: 'singleLineClose' }], }, { - code: `try\n{\n somethingRisky();\n} catch (e)\n{\n handleError()\n}`, - output: `try\n{\n somethingRisky();\n}\n catch (e)\n{\n handleError()\n}`, + code: 'try\n{\n somethingRisky();\n} catch (e)\n{\n handleError()\n}', + output: + 'try\n{\n somethingRisky();\n}\n catch (e)\n{\n handleError()\n}', options: ['allman'], errors: [{ messageId: 'sameLineClose' }], }, // allowSingleLine: true { - code: `function foo() { return; \n}`, - output: `function foo() {\n return; \n}`, + code: 'function foo() { return; \n}', + output: 'function foo() {\n return; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'blockSameLine' }], }, { - code: `function foo() { a(); b(); return; \n}`, - output: `function foo() {\n a(); b(); return; \n}`, + code: 'function foo() { a(); b(); return; \n}', + output: 'function foo() {\n a(); b(); return; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'blockSameLine' }], }, { - code: `function foo() { \n return; }`, - output: `function foo() { \n return; \n}`, + code: 'function foo() { \n return; }', + output: 'function foo() { \n return; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'singleLineClose' }], }, { - code: `function foo() {\na();\nb();\nreturn; }`, - output: `function foo() {\na();\nb();\nreturn; \n}`, + code: 'function foo() {\na();\nb();\nreturn; }', + output: 'function foo() {\na();\nb();\nreturn; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'singleLineClose' }], }, { - code: `!function foo() { \n return; }`, - output: `!function foo() { \n return; \n}`, + code: '!function foo() { \n return; }', + output: '!function foo() { \n return; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'singleLineClose' }], }, { - code: `if (a) { b();\n } else { c(); }`, - output: `if (a) {\n b();\n } else { c(); }`, + code: 'if (a) { b();\n } else { c(); }', + output: 'if (a) {\n b();\n } else { c(); }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'blockSameLine' }], }, { - code: `if (a) { b(); }\nelse { c(); }`, - output: `if (a) { b(); } else { c(); }`, + code: 'if (a) { b(); }\nelse { c(); }', + output: 'if (a) { b(); } else { c(); }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineClose' }], }, { - code: `while (foo) { \n bar(); }`, - output: `while (foo) { \n bar(); \n}`, + code: 'while (foo) { \n bar(); }', + output: 'while (foo) { \n bar(); \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'singleLineClose' }], }, { - code: `for (;;) { bar(); \n }`, - output: `for (;;) {\n bar(); \n }`, + code: 'for (;;) { bar(); \n }', + output: 'for (;;) {\n bar(); \n }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'blockSameLine' }], }, { - code: `with (foo) { bar(); \n }`, - output: `with (foo) {\n bar(); \n }`, + code: 'with (foo) { bar(); \n }', + output: 'with (foo) {\n bar(); \n }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'blockSameLine' }], }, { - code: `switch (foo) \n { \n case \`bar\`: break; }`, - output: `switch (foo) { \n case \`bar\`: break; \n}`, + code: 'switch (foo) \n { \n case `bar`: break; }', + output: 'switch (foo) { \n case `bar`: break; \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }, { messageId: 'singleLineClose' }], }, { - code: `switch (foo) \n { }`, - output: `switch (foo) { }`, + code: 'switch (foo) \n { }', + output: 'switch (foo) { }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try { bar(); }\ncatch (e) { baz(); }`, - output: `try { bar(); } catch (e) { baz(); }`, + code: 'try { bar(); }\ncatch (e) { baz(); }', + output: 'try { bar(); } catch (e) { baz(); }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineClose' }], }, { - code: `try \n { \n bar(); \n } catch (e) {}`, - output: `try { \n bar(); \n } catch (e) {}`, + code: 'try \n { \n bar(); \n } catch (e) {}', + output: 'try { \n bar(); \n } catch (e) {}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try { \n bar(); \n } catch (e) \n {}`, - output: `try { \n bar(); \n } catch (e) {}`, + code: 'try { \n bar(); \n } catch (e) \n {}', + output: 'try { \n bar(); \n } catch (e) {}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }], }, { - code: `do \n { \n bar(); \n} while (true)`, - output: `do { \n bar(); \n} while (true)`, + code: 'do \n { \n bar(); \n} while (true)', + output: 'do { \n bar(); \n} while (true)', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }], }, { - code: `for (foo in bar) \n { \n baz(); \n }`, - output: `for (foo in bar) { \n baz(); \n }`, + code: 'for (foo in bar) \n { \n baz(); \n }', + output: 'for (foo in bar) { \n baz(); \n }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineOpen' }], }, { - code: `try { \n bar(); \n }\ncatch (e) {\n}`, - output: `try { \n bar(); \n } catch (e) {\n}`, + code: 'try { \n bar(); \n }\ncatch (e) {\n}', + output: 'try { \n bar(); \n } catch (e) {\n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineClose' }], }, { - code: `try { \n bar(); \n } catch (e) {\n}\n finally {\n}`, - output: `try { \n bar(); \n } catch (e) {\n} finally {\n}`, + code: 'try { \n bar(); \n } catch (e) {\n}\n finally {\n}', + output: 'try { \n bar(); \n } catch (e) {\n} finally {\n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineClose' }], }, { - code: `if (a) { \nb();\n } \n else { \nc();\n }`, - output: `if (a) { \nb();\n } else { \nc();\n }`, + code: 'if (a) { \nb();\n } \n else { \nc();\n }', + output: 'if (a) { \nb();\n } else { \nc();\n }', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'nextLineClose' }], }, { - code: `try { \n bar(); \n }\ncatch (e) {\n} finally {\n}`, - output: `try { \n bar(); \n }\ncatch (e) {\n}\n finally {\n}`, + code: 'try { \n bar(); \n }\ncatch (e) {\n} finally {\n}', + output: 'try { \n bar(); \n }\ncatch (e) {\n}\n finally {\n}', options: ['stroustrup', { allowSingleLine: true }], errors: [{ messageId: 'sameLineClose' }], }, { - code: `try { \n bar(); \n } catch (e) {\n}\n finally {\n}`, - output: `try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}`, + code: 'try { \n bar(); \n } catch (e) {\n}\n finally {\n}', + output: 'try { \n bar(); \n }\n catch (e) {\n}\n finally {\n}', options: ['stroustrup', { allowSingleLine: true }], errors: [{ messageId: 'sameLineClose' }], }, { - code: `if (a) { \nb();\n } else { \nc();\n }`, - output: `if (a) { \nb();\n }\n else { \nc();\n }`, + code: 'if (a) { \nb();\n } else { \nc();\n }', + output: 'if (a) { \nb();\n }\n else { \nc();\n }', options: ['stroustrup', { allowSingleLine: true }], errors: [{ messageId: 'sameLineClose' }], }, { - code: `if (foo)\n{ poop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}`, - output: `if (foo)\n{\n poop();\n} \nelse if (bar) \n{\nbaz();\n}\n else if (thing) \n{\nboom();\n}\nelse \n{\nqux();\n}`, + code: + 'if (foo)\n{ poop();\n} \nelse if (bar) {\nbaz();\n} else if (thing) {\nboom();\n}\nelse {\nqux();\n}', + output: + 'if (foo)\n{\n poop();\n} \nelse if (bar) \n{\nbaz();\n}\n else if (thing) \n{\nboom();\n}\nelse \n{\nqux();\n}', options: ['allman', { allowSingleLine: true }], errors: [ { messageId: 'blockSameLine' }, @@ -945,25 +961,25 @@ if (f) { }, // Comment interferes with fix { - code: `if (foo) // comment \n{\nbar();\n}`, + code: 'if (foo) // comment \n{\nbar();\n}', output: null, errors: [{ messageId: 'nextLineOpen' }], }, // https://github.com/eslint/eslint/issues/7493 { - code: `if (foo) {\n bar\n.baz }`, - output: `if (foo) {\n bar\n.baz \n}`, + code: 'if (foo) {\n bar\n.baz }', + output: 'if (foo) {\n bar\n.baz \n}', errors: [{ messageId: 'singleLineClose' }], }, { - code: `if (foo)\n{\n bar\n.baz }`, - output: `if (foo)\n{\n bar\n.baz \n}`, + code: 'if (foo)\n{\n bar\n.baz }', + output: 'if (foo)\n{\n bar\n.baz \n}', options: ['allman'], errors: [{ messageId: 'singleLineClose' }], }, { - code: `if (foo) { bar\n.baz }`, - output: `if (foo) {\n bar\n.baz \n}`, + code: 'if (foo) { bar\n.baz }', + output: 'if (foo) {\n bar\n.baz \n}', options: ['1tbs', { allowSingleLine: true }], errors: [ { messageId: 'blockSameLine' }, @@ -971,8 +987,8 @@ if (f) { ], }, { - code: `if (foo) { bar\n.baz }`, - output: `if (foo) \n{\n bar\n.baz \n}`, + code: 'if (foo) { bar\n.baz }', + output: 'if (foo) \n{\n bar\n.baz \n}', options: ['allman', { allowSingleLine: true }], errors: [ { messageId: 'sameLineOpen' }, @@ -981,46 +997,46 @@ if (f) { ], }, { - code: `switch (x) {\n case 1: foo() }`, - output: `switch (x) {\n case 1: foo() \n}`, + code: 'switch (x) {\n case 1: foo() }', + output: 'switch (x) {\n case 1: foo() \n}', options: ['1tbs', { allowSingleLine: true }], errors: [{ messageId: 'singleLineClose' }], }, { - code: `class Foo\n{\n}`, - output: `class Foo {\n}`, + code: 'class Foo\n{\n}', + output: 'class Foo {\n}', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `(class\n{\n})`, - output: `(class {\n})`, + code: '(class\n{\n})', + output: '(class {\n})', errors: [{ messageId: 'nextLineOpen' }], }, { - code: `class Foo{\n}`, - output: `class Foo\n{\n}`, + code: 'class Foo{\n}', + output: 'class Foo\n{\n}', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, { - code: `(class {\n})`, - output: `(class \n{\n})`, + code: '(class {\n})', + output: '(class \n{\n})', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, { - code: `class Foo {\nbar() {\n}}`, - output: `class Foo {\nbar() {\n}\n}`, + code: 'class Foo {\nbar() {\n}}', + output: 'class Foo {\nbar() {\n}\n}', errors: [{ messageId: 'singleLineClose' }], }, { - code: `(class Foo {\nbar() {\n}})`, - output: `(class Foo {\nbar() {\n}\n})`, + code: '(class Foo {\nbar() {\n}})', + output: '(class Foo {\nbar() {\n}\n})', errors: [{ messageId: 'singleLineClose' }], }, { - code: `class\nFoo{}`, - output: `class\nFoo\n{}`, + code: 'class\nFoo{}', + output: 'class\nFoo\n{}', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, @@ -1070,8 +1086,8 @@ interface Foo { errors: [{ messageId: 'nextLineOpen' }], }, { - code: `interface Foo { \n }`, - output: `interface Foo \n{ \n }`, + code: 'interface Foo { \n }', + output: 'interface Foo \n{ \n }', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, @@ -1101,8 +1117,8 @@ module "Foo" { errors: [{ messageId: 'nextLineOpen' }], }, { - code: `module "Foo" { \n }`, - output: `module "Foo" \n{ \n }`, + code: 'module "Foo" { \n }', + output: 'module "Foo" \n{ \n }', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, @@ -1132,8 +1148,8 @@ namespace Foo { errors: [{ messageId: 'nextLineOpen' }], }, { - code: `namespace Foo { \n }`, - output: `namespace Foo \n{ \n }`, + code: 'namespace Foo { \n }', + output: 'namespace Foo \n{ \n }', options: ['allman'], errors: [{ messageId: 'sameLineOpen' }], }, @@ -1163,8 +1179,8 @@ enum Foo { errors: [{ messageId: 'nextLineOpen' }], }, { - code: `enum Foo { A }`, - output: `enum Foo \n{\n A \n}`, + code: 'enum Foo { A }', + output: 'enum Foo \n{\n A \n}', options: ['allman'], errors: [ { messageId: 'sameLineOpen' }, From 5970e3a36a3567b88ddd692d891ffbb2f2bda753 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:31:10 -0700 Subject: [PATCH 86/92] chore: ban-types --- .../tests/rules/ban-types.test.ts | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/ban-types.test.ts b/packages/eslint-plugin/tests/rules/ban-types.test.ts index 0edeb74f8d1c..772fa7716f8f 100644 --- a/packages/eslint-plugin/tests/rules/ban-types.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-types.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/ban-types'; -import { RuleTester } from '../RuleTester'; +import { RuleTester, noFormat } from '../RuleTester'; import { InferOptionsTypeFromRule } from '../../src/util'; const ruleTester = new RuleTester({ @@ -47,7 +47,7 @@ ruleTester.run('ban-types', rule, { valid: [ 'let f = Object();', // Should not fail if there is no options set 'let f: {} = {};', - 'let f: { x: number, y: number } = { x: 1, y: 1 };', + 'let f: { x: number; y: number } = { x: 1, y: 1 };', { code: 'let f = Object();', options, @@ -65,11 +65,11 @@ ruleTester.run('ban-types', rule, { options, }, { - code: 'let a: _.NS.Bad', + code: 'let a: _.NS.Bad;', options, }, { - code: 'let a: NS.Bad._', + code: 'let a: NS.Bad._;', options, }, // Replace default options instead of merging with extendDefaults: false @@ -88,11 +88,11 @@ ruleTester.run('ban-types', rule, { ], }, { - code: 'let a: undefined', + code: 'let a: undefined;', options: options2, }, { - code: 'let a: null', + code: 'let a: null;', options: options3, }, ], @@ -166,8 +166,8 @@ ruleTester.run('ban-types', rule, { ], }, { - code: 'let b: {c: String};', - output: 'let b: {c: string};', + code: 'let b: { c: String };', + output: 'let b: { c: string };', errors: [ { messageId: 'bannedTypeMessage', @@ -177,7 +177,7 @@ ruleTester.run('ban-types', rule, { customMessage: ' Use string instead.', }, line: 1, - column: 12, + column: 13, }, ], options, @@ -231,22 +231,22 @@ ruleTester.run('ban-types', rule, { { code: ` class Foo extends Bar implements Baz { - constructor (foo: String | Object) {} + constructor(foo: String | Object) {} - exit() : Array { - const foo: String = 1 as String + exit(): Array { + const foo: String = 1 as String; } } - `, + `, output: ` class Foo extends Bar implements Baz { - constructor (foo: string | Object) {} + constructor(foo: string | Object) {} - exit() : Array { - const foo: string = 1 as string + exit(): Array { + const foo: string = 1 as string; } } - `, + `, errors: [ { messageId: 'bannedTypeMessage', @@ -285,7 +285,7 @@ class Foo extends Bar implements Baz { customMessage: ' Use string instead.', }, line: 3, - column: 21, + column: 20, }, { messageId: 'bannedTypeMessage', @@ -294,13 +294,13 @@ class Foo extends Bar implements Baz { customMessage: " Use '{}' instead.", }, line: 3, - column: 30, + column: 29, }, { messageId: 'bannedTypeMessage', data: { name: 'Array', customMessage: '' }, line: 5, - column: 12, + column: 11, }, { messageId: 'bannedTypeMessage', @@ -310,7 +310,7 @@ class Foo extends Bar implements Baz { customMessage: ' Use string instead.', }, line: 5, - column: 18, + column: 17, }, { messageId: 'bannedTypeMessage', @@ -383,8 +383,8 @@ let b: Foo; options, }, { - code: `let foo: {} = {};`, - output: `let foo: object = {};`, + code: 'let foo: {} = {};', + output: 'let foo: object = {};', options: [ { types: { @@ -408,7 +408,7 @@ let b: Foo; ], }, { - code: ` + code: noFormat` let foo: {} = {}; let bar: { } = {}; `, @@ -473,8 +473,8 @@ let bar: object = {}; ], }, { - code: 'let a: Foo< F >;', - output: 'let a: Foo< T >;', + code: noFormat`let a: Foo< F >;`, + output: noFormat`let a: Foo< T >;`, errors: [ { messageId: 'bannedTypeMessage', From 33c2ed89a2b5f0ccd48a7d9b05a4e920aac2efc3 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:31:33 -0700 Subject: [PATCH 87/92] chore: ban-ts-ignore --- .../tests/rules/ban-ts-ignore.test.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/ban-ts-ignore.test.ts b/packages/eslint-plugin/tests/rules/ban-ts-ignore.test.ts index d9132df2b0d4..e1471950f464 100644 --- a/packages/eslint-plugin/tests/rules/ban-ts-ignore.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-ts-ignore.test.ts @@ -7,12 +7,14 @@ const ruleTester = new RuleTester({ ruleTester.run('ban-ts-ignore', rule, { valid: [ - `// just a comment containing @ts-ignore somewhere`, - `/* @ts-ignore */`, - `/** @ts-ignore */`, - `/* + '// just a comment containing @ts-ignore somewhere', + '/* @ts-ignore */', + '/** @ts-ignore */', + ` +/* // @ts-ignore in a block -*/`, +*/ + `, ], invalid: [ { @@ -49,9 +51,9 @@ ruleTester.run('ban-ts-ignore', rule, { code: ` if (false) { // @ts-ignore: Unreachable code error - console.log("hello"); + console.log('hello'); } - `, + `, errors: [ { messageId: 'tsIgnoreComment', From 85fcb6c090a34a715d7039ac89a4b3d43512cc7a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:31:59 -0700 Subject: [PATCH 88/92] chore: ban-ts-comment --- .../tests/rules/ban-ts-comment.test.ts | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts b/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts index c2226f7041a7..fcbb788a3082 100644 --- a/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts @@ -5,14 +5,16 @@ const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', }); -ruleTester.run('ts-ignore', rule, { +ruleTester.run('ban-ts-comment', rule, { valid: [ - `// just a comment containing @ts-ignore somewhere`, - `/* @ts-ignore */`, - `/** @ts-ignore */`, - `/* + '// just a comment containing @ts-ignore somewhere', + '/* @ts-ignore */', + '/** @ts-ignore */', + ` +/* // @ts-ignore in a block -*/`, +*/ + `, { code: '// @ts-ignore', options: [{ 'ts-ignore': false }], @@ -68,9 +70,9 @@ ruleTester.run('ts-ignore', rule, { code: ` if (false) { // @ts-ignore: Unreachable code error - console.log("hello"); + console.log('hello'); } - `, + `, errors: [ { data: { directive: 'ignore' }, @@ -85,12 +87,14 @@ if (false) { ruleTester.run('ts-nocheck', rule, { valid: [ - `// just a comment containing @ts-nocheck somewhere`, - `/* @ts-nocheck */`, - `/** @ts-nocheck */`, - `/* + '// just a comment containing @ts-nocheck somewhere', + '/* @ts-nocheck */', + '/** @ts-nocheck */', + ` +/* // @ts-nocheck in a block -*/`, +*/ + `, { code: '// @ts-nocheck', options: [{ 'ts-nocheck': false }], @@ -146,9 +150,9 @@ ruleTester.run('ts-nocheck', rule, { code: ` if (false) { // @ts-nocheck: Unreachable code error - console.log("hello"); + console.log('hello'); } - `, + `, errors: [ { data: { directive: 'nocheck' }, @@ -163,12 +167,14 @@ if (false) { ruleTester.run('ts-check', rule, { valid: [ - `// just a comment containing @ts-check somewhere`, - `/* @ts-check */`, - `/** @ts-check */`, - `/* + '// just a comment containing @ts-check somewhere', + '/* @ts-check */', + '/** @ts-check */', + ` +/* // @ts-check in a block -*/`, +*/ + `, { code: '// @ts-check', options: [{ 'ts-check': false }], @@ -216,9 +222,9 @@ ruleTester.run('ts-check', rule, { code: ` if (false) { // @ts-check: Unreachable code error - console.log("hello"); + console.log('hello'); } - `, + `, options: [{ 'ts-check': true }], errors: [ { From f94b4344406434ee068173035d916a28f320c2b1 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:32:39 -0700 Subject: [PATCH 89/92] chore: await-thenable --- .../tests/rules/await-thenable.test.ts | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/await-thenable.test.ts b/packages/eslint-plugin/tests/rules/await-thenable.test.ts index 5ee0f7d03410..52fc1ca23061 100644 --- a/packages/eslint-plugin/tests/rules/await-thenable.test.ts +++ b/packages/eslint-plugin/tests/rules/await-thenable.test.ts @@ -13,51 +13,51 @@ const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', }); -ruleTester.run('await-promise', rule, { +ruleTester.run('await-thenable', rule, { valid: [ ` async function test() { - await Promise.resolve("value"); - await Promise.reject(new Error("message")); + await Promise.resolve('value'); + await Promise.reject(new Error('message')); } -`, + `, ` async function test() { await (async () => true)(); } -`, + `, ` async function test() { function returnsPromise() { - return Promise.resolve("value"); + return Promise.resolve('value'); } await returnsPromise(); } -`, + `, ` async function test() { async function returnsPromiseAsync() {} await returnsPromiseAsync(); } -`, + `, ` async function test() { let anyValue: any; await anyValue; } -`, + `, ` async function test() { let unknownValue: unknown; await unknownValue; } -`, + `, ` async function test() { const numberPromise: Promise; await numberPromise; } -`, + `, ` async function test() { class Foo extends Promise {} @@ -68,7 +68,7 @@ async function test() { const bar: Bar = Bar.resolve(2); await bar; } -`, + `, ` async function test() { await (Math.random() > 0.5 ? numberPromise : 0); @@ -78,15 +78,17 @@ async function test() { const intersectionPromise: Promise & number; await intersectionPromise; } -`, + `, ` async function test() { - class Thenable { then(callback: () => {}) { } }; + class Thenable { + then(callback: () => {}) {} + } const thenable = new Thenable(); await thenable; } -`, + `, ` // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/promise-polyfill/index.d.ts // Type definitions for promise-polyfill 6.0 @@ -96,7 +98,7 @@ async function test() { // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped interface PromisePolyfillConstructor extends PromiseConstructor { - _immediateFn?: (handler: (() => void) | string) => void; + _immediateFn?: (handler: (() => void) | string) => void; } declare const PromisePolyfill: PromisePolyfillConstructor; @@ -106,7 +108,7 @@ async function test() { await promise; } -`, + `, ` // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/bluebird/index.d.ts // Type definitions for bluebird 3.5 @@ -151,14 +153,21 @@ type CatchFilter = ((error: E) => boolean) | (object & E); type IterableItem = R extends Iterable ? U : never; type IterableOrNever = Extract>; type Resolvable = R | PromiseLike; -type IterateFunction = (item: T, index: number, arrayLength: number) => Resolvable; +type IterateFunction = ( + item: T, + index: number, + arrayLength: number, +) => Resolvable; declare class Bluebird implements PromiseLike { - then(onFulfill?: (value: R) => Resolvable, onReject?: (error: any) => Resolvable): Bluebird; // For simpler signature help. + then( + onFulfill?: (value: R) => Resolvable, + onReject?: (error: any) => Resolvable, + ): Bluebird; // For simpler signature help. then( - onfulfilled?: ((value: R) => Resolvable) | null, - onrejected?: ((reason: any) => Resolvable) | null - ): Bluebird; + onfulfilled?: ((value: R) => Resolvable) | null, + onrejected?: ((reason: any) => Resolvable) | null, + ): Bluebird; } declare const bluebird: Bluebird; @@ -166,7 +175,7 @@ declare const bluebird: Bluebird; async function test() { await bluebird; } -`, + `, ], invalid: [ @@ -174,14 +183,14 @@ async function test() { code: ` async function test() { await 0; - await "value"; + await 'value'; - await (Math.random() > 0.5 ? "" : 0); + await (Math.random() > 0.5 ? '' : 0); class NonPromise extends Array {} await new NonPromise(); } -`, + `, errors: [ { line: 3, @@ -204,7 +213,9 @@ async function test() { { code: ` async function test() { - class IncorrectThenable { then() { } }; + class IncorrectThenable { + then() {} + } const thenable = new IncorrectThenable(); await thenable; @@ -212,7 +223,7 @@ async function test() { `, errors: [ { - line: 6, + line: 8, messageId, }, ], From 407694ae486a9ff6394e3699ba72c087c3fd887d Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:33:08 -0700 Subject: [PATCH 90/92] chore: adjacent-overload-signatures --- .../adjacent-overload-signatures.test.ts | 237 +++++++++--------- 1 file changed, 122 insertions(+), 115 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts b/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts index 4cb7eef0dbcf..8e844bb96bef 100644 --- a/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts +++ b/packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts @@ -19,209 +19,216 @@ export { error }; { code: ` import { connect } from 'react-redux'; -export interface ErrorMessageModel { message: string; } -function mapStateToProps() { } -function mapDispatchToProps() { } +export interface ErrorMessageModel { + message: string; +} +function mapStateToProps() {} +function mapDispatchToProps() {} export default connect(mapStateToProps, mapDispatchToProps)(ErrorMessage); - `, + `, parserOptions: { sourceType: 'module' }, }, ` -export const foo = "a", bar = "b"; +export const foo = 'a', + bar = 'b'; export interface Foo {} export class Foo {} - `, + `, ` export interface Foo {} -export const foo = "a", bar = "b"; +export const foo = 'a', + bar = 'b'; export class Foo {} - `, + `, ` -const foo = "a", bar = "b"; +const foo = 'a', + bar = 'b'; interface Foo {} class Foo {} - `, + `, ` interface Foo {} -const foo = "a", bar = "b"; +const foo = 'a', + bar = 'b'; class Foo {} - `, + `, ` export class Foo {} export class Bar {} export type FooBar = Foo | Bar; - `, + `, ` export interface Foo {} export class Foo {} export class Bar {} export type FooBar = Foo | Bar; - `, + `, ` export function foo(s: string); export function foo(n: number); export function foo(sn: string | number) {} export function bar(): void {} export function baz(): void {} - `, + `, ` function foo(s: string); function foo(n: number); function foo(sn: string | number) {} function bar(): void {} function baz(): void {} - `, + `, ` declare function foo(s: string); declare function foo(n: number); declare function foo(sn: string | number); declare function bar(): void; declare function baz(): void; - `, + `, ` -declare module "Foo" { - export function foo(s: string): void; - export function foo(n: number): void; - export function foo(sn: string | number): void; - export function bar(): void; - export function baz(): void; -} - `, +declare module 'Foo' { + export function foo(s: string): void; + export function foo(n: number): void; + export function foo(sn: string | number): void; + export function bar(): void; + export function baz(): void; +} + `, ` declare namespace Foo { - export function foo(s: string): void; - export function foo(n: number): void; - export function foo(sn: string | number): void; - export function bar(): void; - export function baz(): void; + export function foo(s: string): void; + export function foo(n: number): void; + export function foo(sn: string | number): void; + export function bar(): void; + export function baz(): void; } - `, + `, ` type Foo = { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; -} - `, + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; +}; + `, ` type Foo = { - foo(s: string): void; - ["foo"](n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; -} - `, + foo(s: string): void; + ['foo'](n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; +}; + `, ` interface Foo { - (s: string): void; - (n: number): void; - (sn: string | number): void; - foo(n: number): void; - bar(): void; - baz(): void; -} - `, + (s: string): void; + (n: number): void; + (sn: string | number): void; + foo(n: number): void; + bar(): void; + baz(): void; +} + `, ` interface Foo { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; } - `, + `, ` interface Foo { - foo(s: string): void; - ["foo"](n: number): void; - foo(sn: string | number): void; - bar(): void; - baz(): void; + foo(s: string): void; + ['foo'](n: number): void; + foo(sn: string | number): void; + bar(): void; + baz(): void; } - `, + `, ` interface Foo { - foo(): void; - bar: { - baz(s: string): void; - baz(n: number): void; - baz(sn: string | number): void; - } + foo(): void; + bar: { + baz(s: string): void; + baz(n: number): void; + baz(sn: string | number): void; + }; } - `, + `, ` interface Foo { - new(s: string); - new(n: number); - new(sn: string | number); - foo(): void; + new (s: string); + new (n: number); + new (sn: string | number); + foo(): void; } - `, + `, ` class Foo { - constructor(s: string); - constructor(n: number); - constructor(sn: string | number) {} - bar(): void {} - baz(): void {} + constructor(s: string); + constructor(n: number); + constructor(sn: string | number) {} + bar(): void {} + baz(): void {} } - `, + `, ` class Foo { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void {} - bar(): void {} - baz(): void {} + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void {} + bar(): void {} + baz(): void {} } - `, + `, ` class Foo { - foo(s: string): void; - ["foo"](n: number): void; - foo(sn: string | number): void {} - bar(): void {} - baz(): void {} + foo(s: string): void; + ['foo'](n: number): void; + foo(sn: string | number): void {} + bar(): void {} + baz(): void {} } - `, + `, ` class Foo { - name: string; - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void {} - bar(): void {} - baz(): void {} -} - `, + name: string; + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void {} + bar(): void {} + baz(): void {} +} + `, ` class Foo { - name: string; - static foo(s: string): void; - static foo(n: number): void; - static foo(sn: string | number): void {} - bar(): void {} - baz(): void {} -} - `, + name: string; + static foo(s: string): void; + static foo(n: number): void; + static foo(sn: string | number): void {} + bar(): void {} + baz(): void {} +} + `, ` class Test { static test() {} untest() {} test() {} } - `, + `, // examples from https://github.com/nzakas/eslint-plugin-typescript/issues/138 - 'export default function(foo : T) {}', - 'export default function named(foo : T) {}', + 'export default function(foo: T) {}', + 'export default function named(foo: T) {}', ` interface Foo { [Symbol.toStringTag](): void; [Symbol.iterator](): void; -}`, +} + `, ], invalid: [ { From b622e8c821d23ccde69ede65dda0cad4010d72cd Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 30 Mar 2020 00:36:06 -0700 Subject: [PATCH 91/92] chore: indent --- .../tests/rules/indent/indent-eslint.test.ts | 5 + .../tests/rules/indent/indent.test.ts | 108 ++++++++++-------- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts b/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts index d726a5e2c95f..a9dfda6b127b 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent-eslint.test.ts @@ -4,6 +4,11 @@ // NOTE - this test suite is intentionally kept in a separate file to our // custom tests. This is to keep a clear boundary between the two. +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import { AST_TOKEN_TYPES, AST_NODE_TYPES, diff --git a/packages/eslint-plugin/tests/rules/indent/indent.test.ts b/packages/eslint-plugin/tests/rules/indent/indent.test.ts index fe88d3de17a0..6cec699c053e 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent.test.ts @@ -1,3 +1,8 @@ +/* eslint-disable eslint-comments/no-use */ +// this rule tests the spacing, which prettier will want to fix and break the tests +/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ +/* eslint-enable eslint-comments/no-use */ + import { AST_NODE_TYPES, TSESLint, @@ -704,7 +709,7 @@ export default class App extends Vue return this.$store.state.errorHandler.error } } - `, + `, // https://github.com/eslint/typescript-eslint-parser/issues/474 ` /** @@ -713,7 +718,7 @@ export default class App extends Vue * @returns {string} */ function foo(name: string, age: number): string {} - `, + `, ` const firebaseApp = firebase.apps.length ? firebase.app() @@ -725,7 +730,7 @@ const firebaseApp = firebase.apps.length storageBucket: __FIREBASE_STORAGE_BUCKET__, messagingSenderId: __FIREBASE_MESSAGING_SENDER_ID__, }) - `, + `, // https://github.com/bradzacher/eslint-plugin-typescript/issues/271 { code: ` @@ -734,7 +739,7 @@ const foo = { b: 2 }, bar = 1; - `, + `, options: [4, { VariableDeclarator: { const: 3 } }], }, { @@ -744,7 +749,7 @@ const foo : Foo = { b: 2 }, bar = 1; - `, + `, options: [4, { VariableDeclarator: { const: 3 } }], }, { @@ -756,7 +761,7 @@ const name: string = ' Typescript ' greeting: string = (" Hello " + name) .toUpperCase() .trim(); - `, + `, options: [2, { VariableDeclarator: { const: 3 } }], }, { @@ -768,15 +773,15 @@ const div: JQuery = $('
') button: JQuery = $('