From bd842e06cbb9408b3dc950375923ea06f8f1d123 Mon Sep 17 00:00:00 2001 From: "sunghyun.cho" Date: Mon, 28 Sep 2020 16:30:09 +0900 Subject: [PATCH 1/4] fix: test case , check intersection type --- .../src/rules/restrict-plus-operands.ts | 14 + .../rules/restrict-plus-operands.test.ts | 865 +++++++++--------- 2 files changed, 449 insertions(+), 430 deletions(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 0917484ea02..1c20437f102 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -54,6 +54,8 @@ export default util.createRule({ * Helper function to get base type of node */ function getBaseTypeOfLiteralType(type: ts.Type): BaseLiteral { + // eslint-disable-next-line no-debugger + debugger; if (type.isNumberLiteral()) { return 'number'; } @@ -70,6 +72,18 @@ export default util.createRule({ return types.every(value => value === types[0]) ? types[0] : 'invalid'; } + if (type.isIntersection()) { + const types = type.types.map(getBaseTypeOfLiteralType); + // eslint-disable-next-line no-console + console.log({ types }); + /** + * unknown & string === string + string & string === string + 'string literal' & string === 'string literal' + */ + return types.some(value => value === 'string') ? 'string' : 'invalid'; + } + const stringType = typeChecker.typeToString(type); if ( 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 43682806f59..31310c13cd9 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -13,439 +13,444 @@ 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 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; + // `, + // ` + // function test(s: string, n: number): number { + // return 2; + // } + // var foo = test('5.5', 10) + 10; + // `, + // ` + // var x = 5; + // var z = 8.2; + // var foo = x + z; + // `, + // ` + // 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 foo = pair.first + 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' as string) + pair.second; + // `, + // ` + // const foo = + // 'hello' + + // (someBoolean ? 'a' : 'b') + + // (() => (someBoolean ? 'c' : 'd'))() + + // 'e'; + // `, + // 'const balls = true;', + // 'balls === true;', + // // https://github.com/typescript-eslint/typescript-eslint/issues/230 + // ` + // function foo(a: T) { + // return a + ''; + // } + // `, + // ` + // function foo(a: T) { + // return a + ''; + // } + // `, + // ` + // function foo(a: T) { + // return a + 1; + // } + // `, + // ` + // function foo(a: T) { + // return a + 1; + // } + // `, ` - var foo = 1n; - foo + 2n; +declare const a: {} & string; +declare const b: string; +const x = a + b; `, - ` -function test(s: string, n: number): number { - return 2; -} -var foo = test('5.5', 10) + 10; - `, - ` -var x = 5; -var z = 8.2; -var foo = x + z; - `, - ` -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 foo = pair.first + 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' as string) + pair.second; - `, - ` - const foo = - 'hello' + - (someBoolean ? 'a' : 'b') + - (() => (someBoolean ? 'c' : 'd'))() + - 'e'; - `, - 'const balls = true;', - 'balls === true;', - // https://github.com/typescript-eslint/typescript-eslint/issues/230 - ` -function foo(a: T) { - return a + ''; -} - `, - ` -function foo(a: T) { - return a + ''; -} - `, - ` -function foo(a: T) { - return a + 1; -} - `, - ` -function foo(a: T) { - return a + 1; -} - `, - { - code: ` -let foo: number = 0; -foo += 1; - `, - options: [ - { - checkCompoundAssignments: false, - }, - ], - }, - { - code: ` -let foo: number = 0; -foo += 'string'; - `, - options: [ - { - checkCompoundAssignments: false, - }, - ], - }, + // { + // code: ` + // let foo: number = 0; + // foo += 1; + // `, + // options: [ + // { + // checkCompoundAssignments: false, + // }, + // ], + // }, + // { + // code: ` + // let foo: number = 0; + // foo += 'string'; + // `, + // options: [ + // { + // checkCompoundAssignments: false, + // }, + // ], + // }, ], invalid: [ - { - code: "var foo = '1' + 1;", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: 'var foo = [] + {};', - errors: [ - { - messageId: 'notNumbers', - line: 1, - column: 11, - }, - ], - }, - { - code: "var foo = 5 + '10';", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: 'var foo = [] + 5;', - errors: [ - { - messageId: 'notNumbers', - line: 1, - column: 11, - }, - ], - }, - { - code: 'var foo = [] + [];', - errors: [ - { - messageId: 'notNumbers', - line: 1, - column: 11, - }, - ], - }, - { - code: 'var foo = 5 + [];', - errors: [ - { - messageId: 'notNumbers', - line: 1, - column: 11, - }, - ], - }, - { - code: "var foo = '5' + {};", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: "var foo = 5.5 + '5';", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: "var foo = '5.5' + 5;", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: ` -var x = 5; -var y = '10'; -var foo = x + y; - `, - errors: [ - { - messageId: 'notStrings', - line: 4, - column: 11, - }, - ], - }, - { - code: ` -var x = 5; -var y = '10'; -var foo = y + x; - `, - errors: [ - { - messageId: 'notStrings', - line: 4, - column: 11, - }, - ], - }, - { - code: ` -var x = 5; -var foo = x + {}; - `, - errors: [ - { - messageId: 'notNumbers', - line: 3, - column: 11, - }, - ], - }, - { - code: ` -var y = '10'; -var foo = [] + y; - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 11, - }, - ], - }, - { - code: ` -var pair: { first: number; second: string } = { first: 5, second: '10' }; -var foo = pair.first + '10'; - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 11, - }, - ], - }, - { - code: ` -var pair: { first: number; second: string } = { first: 5, second: '10' }; -var foo = 5 + pair.second; - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 11, - }, - ], - }, - { - code: "var foo = parseInt('5.5', 10) + '10';", - errors: [ - { - messageId: 'notStrings', - line: 1, - column: 11, - }, - ], - }, - { - code: ` -var pair = { first: 5, second: '10' }; -var foo = pair + pair; - `, - errors: [ - { - messageId: 'notNumbers', - line: 3, - column: 11, - }, - ], - }, - { - code: 'var foo = 1n + 1;', - errors: [ - { - messageId: 'notBigInts', - line: 1, - column: 11, - }, - ], - }, - { - code: 'var foo = 1 + 1n;', - errors: [ - { - messageId: 'notBigInts', - line: 1, - column: 11, - }, - ], - }, - { - code: ` - var foo = 1n; - foo + 1; - `, - errors: [ - { - messageId: 'notBigInts', - line: 3, - column: 9, - }, - ], - }, - { - code: ` - var foo = 1; - foo + 1n; - `, - errors: [ - { - messageId: 'notBigInts', - line: 3, - column: 9, - }, - ], - }, - // https://github.com/typescript-eslint/typescript-eslint/issues/230 - { - code: ` -function foo(a: T) { - return a + 1; -} - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 10, - }, - ], - }, - { - code: ` -function foo(a: T) { - return a + 1; -} - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 10, - }, - ], - }, - { - code: ` -function foo(a: T) { - return a + ''; -} - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 10, - }, - ], - }, - { - code: ` -function foo(a: T) { - return a + ''; -} - `, - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 10, - }, - ], - }, - { - code: ` -let foo: string | undefined; -foo += 'some data'; - `, - options: [ - { - checkCompoundAssignments: true, - }, - ], - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 1, - }, - ], - }, - { - code: ` -let foo = ''; -foo += 0; - `, - options: [ - { - checkCompoundAssignments: true, - }, - ], - errors: [ - { - messageId: 'notStrings', - line: 3, - column: 1, - }, - ], - }, + // { + // code: "var foo = '1' + 1;", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = [] + {};', + // errors: [ + // { + // messageId: 'notNumbers', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: "var foo = 5 + '10';", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = [] + 5;', + // errors: [ + // { + // messageId: 'notNumbers', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = [] + [];', + // errors: [ + // { + // messageId: 'notNumbers', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = 5 + [];', + // errors: [ + // { + // messageId: 'notNumbers', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: "var foo = '5' + {};", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: "var foo = 5.5 + '5';", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: "var foo = '5.5' + 5;", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var x = 5; + // var y = '10'; + // var foo = x + y; + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 4, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var x = 5; + // var y = '10'; + // var foo = y + x; + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 4, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var x = 5; + // var foo = x + {}; + // `, + // errors: [ + // { + // messageId: 'notNumbers', + // line: 3, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var y = '10'; + // var foo = [] + y; + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var pair: { first: number; second: string } = { first: 5, second: '10' }; + // var foo = pair.first + '10'; + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var pair: { first: number; second: string } = { first: 5, second: '10' }; + // var foo = 5 + pair.second; + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 11, + // }, + // ], + // }, + // { + // code: "var foo = parseInt('5.5', 10) + '10';", + // errors: [ + // { + // messageId: 'notStrings', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var pair = { first: 5, second: '10' }; + // var foo = pair + pair; + // `, + // errors: [ + // { + // messageId: 'notNumbers', + // line: 3, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = 1n + 1;', + // errors: [ + // { + // messageId: 'notBigInts', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: 'var foo = 1 + 1n;', + // errors: [ + // { + // messageId: 'notBigInts', + // line: 1, + // column: 11, + // }, + // ], + // }, + // { + // code: ` + // var foo = 1n; + // foo + 1; + // `, + // errors: [ + // { + // messageId: 'notBigInts', + // line: 3, + // column: 9, + // }, + // ], + // }, + // { + // code: ` + // var foo = 1; + // foo + 1n; + // `, + // errors: [ + // { + // messageId: 'notBigInts', + // line: 3, + // column: 9, + // }, + // ], + // }, + // // https://github.com/typescript-eslint/typescript-eslint/issues/230 + // { + // code: ` + // function foo(a: T) { + // return a + 1; + // } + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 10, + // }, + // ], + // }, + // { + // code: ` + // function foo(a: T) { + // return a + 1; + // } + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 10, + // }, + // ], + // }, + // { + // code: ` + // function foo(a: T) { + // return a + ''; + // } + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 10, + // }, + // ], + // }, + // { + // code: ` + // function foo(a: T) { + // return a + ''; + // } + // `, + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 10, + // }, + // ], + // }, + // { + // code: ` + // let foo: string | undefined; + // foo += 'some data'; + // `, + // options: [ + // { + // checkCompoundAssignments: true, + // }, + // ], + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 1, + // }, + // ], + // }, + // { + // code: ` + // let foo = ''; + // foo += 0; + // `, + // options: [ + // { + // checkCompoundAssignments: true, + // }, + // ], + // errors: [ + // { + // messageId: 'notStrings', + // line: 3, + // column: 1, + // }, + // ], + // }, ], }); From ca5a50ef7d8e9f001ae61881115aaaeadaeac22e Mon Sep 17 00:00:00 2001 From: sunghyunjo Date: Fri, 2 Oct 2020 23:49:58 +0900 Subject: [PATCH 2/4] chore: test case of intersection type --- .../src/rules/restrict-plus-operands.ts | 9 - .../rules/restrict-plus-operands.test.ts | 959 ++++++++++-------- 2 files changed, 527 insertions(+), 441 deletions(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 1c20437f102..0edf8a37d20 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -54,8 +54,6 @@ export default util.createRule({ * Helper function to get base type of node */ function getBaseTypeOfLiteralType(type: ts.Type): BaseLiteral { - // eslint-disable-next-line no-debugger - debugger; if (type.isNumberLiteral()) { return 'number'; } @@ -74,13 +72,6 @@ export default util.createRule({ if (type.isIntersection()) { const types = type.types.map(getBaseTypeOfLiteralType); - // eslint-disable-next-line no-console - console.log({ types }); - /** - * unknown & string === string - string & string === string - 'string literal' & string === 'string literal' - */ return types.some(value => value === 'string') ? 'string' : 'invalid'; } 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 31310c13cd9..be224812727 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -13,444 +13,539 @@ 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; - // `, - // ` - // function test(s: string, n: number): number { - // return 2; - // } - // var foo = test('5.5', 10) + 10; - // `, - // ` - // var x = 5; - // var z = 8.2; - // var foo = x + z; - // `, - // ` - // 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 foo = pair.first + 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' as string) + pair.second; - // `, - // ` - // const foo = - // 'hello' + - // (someBoolean ? 'a' : 'b') + - // (() => (someBoolean ? 'c' : 'd'))() + - // 'e'; - // `, - // 'const balls = true;', - // 'balls === true;', - // // https://github.com/typescript-eslint/typescript-eslint/issues/230 - // ` - // function foo(a: T) { - // return a + ''; - // } - // `, - // ` - // function foo(a: T) { - // return a + ''; - // } - // `, - // ` - // function foo(a: T) { - // return a + 1; - // } - // `, - // ` - // function foo(a: T) { - // return a + 1; - // } - // `, + '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 x = 5; +var z = 8.2; +var foo = x + z; + `, + ` +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 foo = pair.first + 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' as string) + pair.second; + `, + ` + const foo = + 'hello' + + (someBoolean ? 'a' : 'b') + + (() => (someBoolean ? 'c' : 'd'))() + + 'e'; + `, + 'const balls = true;', + 'balls === true;', + // https://github.com/typescript-eslint/typescript-eslint/issues/230 + ` +function foo(a: T) { + return a + ''; +} + `, + ` +function foo(a: T) { + return a + ''; +} + `, + ` +function foo(a: T) { + return a + 1; +} + `, + ` +function foo(a: T) { + return a + 1; +} + `, ` declare const a: {} & string; declare const b: string; const x = a + b; `, - // { - // code: ` - // let foo: number = 0; - // foo += 1; - // `, - // options: [ - // { - // checkCompoundAssignments: false, - // }, - // ], - // }, - // { - // code: ` - // let foo: number = 0; - // foo += 'string'; - // `, - // options: [ - // { - // checkCompoundAssignments: false, - // }, - // ], - // }, + ` +declare const a: unknown & string; +declare const b: string; +const x = a + b; + `, + ` +declare const a: string & string; +declare const b: string; +const x = a + b; + `, + ` +declare const a: 'string literal' & string; +declare const b: string; +const x = a + b; + `, + { + code: ` +let foo: number = 0; +foo += 1; + `, + options: [ + { + checkCompoundAssignments: false, + }, + ], + }, + { + code: ` +let foo: number = 0; +foo += 'string'; + `, + options: [ + { + checkCompoundAssignments: false, + }, + ], + }, ], invalid: [ - // { - // code: "var foo = '1' + 1;", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = [] + {};', - // errors: [ - // { - // messageId: 'notNumbers', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: "var foo = 5 + '10';", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = [] + 5;', - // errors: [ - // { - // messageId: 'notNumbers', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = [] + [];', - // errors: [ - // { - // messageId: 'notNumbers', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = 5 + [];', - // errors: [ - // { - // messageId: 'notNumbers', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: "var foo = '5' + {};", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: "var foo = 5.5 + '5';", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: "var foo = '5.5' + 5;", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var x = 5; - // var y = '10'; - // var foo = x + y; - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 4, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var x = 5; - // var y = '10'; - // var foo = y + x; - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 4, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var x = 5; - // var foo = x + {}; - // `, - // errors: [ - // { - // messageId: 'notNumbers', - // line: 3, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var y = '10'; - // var foo = [] + y; - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var pair: { first: number; second: string } = { first: 5, second: '10' }; - // var foo = pair.first + '10'; - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var pair: { first: number; second: string } = { first: 5, second: '10' }; - // var foo = 5 + pair.second; - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 11, - // }, - // ], - // }, - // { - // code: "var foo = parseInt('5.5', 10) + '10';", - // errors: [ - // { - // messageId: 'notStrings', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var pair = { first: 5, second: '10' }; - // var foo = pair + pair; - // `, - // errors: [ - // { - // messageId: 'notNumbers', - // line: 3, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = 1n + 1;', - // errors: [ - // { - // messageId: 'notBigInts', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: 'var foo = 1 + 1n;', - // errors: [ - // { - // messageId: 'notBigInts', - // line: 1, - // column: 11, - // }, - // ], - // }, - // { - // code: ` - // var foo = 1n; - // foo + 1; - // `, - // errors: [ - // { - // messageId: 'notBigInts', - // line: 3, - // column: 9, - // }, - // ], - // }, - // { - // code: ` - // var foo = 1; - // foo + 1n; - // `, - // errors: [ - // { - // messageId: 'notBigInts', - // line: 3, - // column: 9, - // }, - // ], - // }, - // // https://github.com/typescript-eslint/typescript-eslint/issues/230 - // { - // code: ` - // function foo(a: T) { - // return a + 1; - // } - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 10, - // }, - // ], - // }, - // { - // code: ` - // function foo(a: T) { - // return a + 1; - // } - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 10, - // }, - // ], - // }, - // { - // code: ` - // function foo(a: T) { - // return a + ''; - // } - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 10, - // }, - // ], - // }, - // { - // code: ` - // function foo(a: T) { - // return a + ''; - // } - // `, - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 10, - // }, - // ], - // }, - // { - // code: ` - // let foo: string | undefined; - // foo += 'some data'; - // `, - // options: [ - // { - // checkCompoundAssignments: true, - // }, - // ], - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 1, - // }, - // ], - // }, - // { - // code: ` - // let foo = ''; - // foo += 0; - // `, - // options: [ - // { - // checkCompoundAssignments: true, - // }, - // ], - // errors: [ - // { - // messageId: 'notStrings', - // line: 3, - // column: 1, - // }, - // ], - // }, + { + code: "var foo = '1' + 1;", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: 'var foo = [] + {};', + errors: [ + { + messageId: 'notNumbers', + line: 1, + column: 11, + }, + ], + }, + { + code: "var foo = 5 + '10';", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: 'var foo = [] + 5;', + errors: [ + { + messageId: 'notNumbers', + line: 1, + column: 11, + }, + ], + }, + { + code: 'var foo = [] + [];', + errors: [ + { + messageId: 'notNumbers', + line: 1, + column: 11, + }, + ], + }, + { + code: 'var foo = 5 + [];', + errors: [ + { + messageId: 'notNumbers', + line: 1, + column: 11, + }, + ], + }, + { + code: "var foo = '5' + {};", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: "var foo = 5.5 + '5';", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: "var foo = '5.5' + 5;", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: ` +var x = 5; +var y = '10'; +var foo = x + y; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 11, + }, + ], + }, + { + code: ` +var x = 5; +var y = '10'; +var foo = y + x; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 11, + }, + ], + }, + { + code: ` +var x = 5; +var foo = x + {}; + `, + errors: [ + { + messageId: 'notNumbers', + line: 3, + column: 11, + }, + ], + }, + { + code: ` +var y = '10'; +var foo = [] + y; + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 11, + }, + ], + }, + { + code: ` +var pair: { first: number; second: string } = { first: 5, second: '10' }; +var foo = pair.first + '10'; + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 11, + }, + ], + }, + { + code: ` +var pair: { first: number; second: string } = { first: 5, second: '10' }; +var foo = 5 + pair.second; + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 11, + }, + ], + }, + { + code: "var foo = parseInt('5.5', 10) + '10';", + errors: [ + { + messageId: 'notStrings', + line: 1, + column: 11, + }, + ], + }, + { + code: ` +var pair = { first: 5, second: '10' }; +var foo = pair + pair; + `, + errors: [ + { + messageId: 'notNumbers', + line: 3, + column: 11, + }, + ], + }, + { + code: 'var foo = 1n + 1;', + errors: [ + { + messageId: 'notBigInts', + line: 1, + column: 11, + }, + ], + }, + { + code: 'var foo = 1 + 1n;', + errors: [ + { + messageId: 'notBigInts', + line: 1, + column: 11, + }, + ], + }, + { + code: ` + var foo = 1n; + foo + 1; + `, + errors: [ + { + messageId: 'notBigInts', + line: 3, + column: 9, + }, + ], + }, + { + code: ` + var foo = 1; + foo + 1n; + `, + errors: [ + { + messageId: 'notBigInts', + line: 3, + column: 9, + }, + ], + }, + // https://github.com/typescript-eslint/typescript-eslint/issues/230 + { + code: ` +function foo(a: T) { + return a + 1; +} + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 10, + }, + ], + }, + { + code: ` +function foo(a: T) { + return a + 1; +} + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 10, + }, + ], + }, + { + code: ` +function foo(a: T) { + return a + ''; +} + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 10, + }, + ], + }, + { + code: ` +function foo(a: T) { + return a + ''; +} + `, + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 10, + }, + ], + }, + { + code: ` + declare const a: boolean & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` + declare const a: number & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` + declare const a: symbol & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` + declare const a: object & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` + declare const a: never & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` + declare const a: any & string; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, + { + code: ` +let foo: string | undefined; +foo += 'some data'; + `, + options: [ + { + checkCompoundAssignments: true, + }, + ], + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 1, + }, + ], + }, + { + code: ` +let foo = ''; +foo += 0; + `, + options: [ + { + checkCompoundAssignments: true, + }, + ], + errors: [ + { + messageId: 'notStrings', + line: 3, + column: 1, + }, + ], + }, ], }); From 5520ffd13eba479e7e40ebaf01e899eccae52386 Mon Sep 17 00:00:00 2001 From: sunghyunjo Date: Fri, 2 Oct 2020 23:51:48 +0900 Subject: [PATCH 3/4] chore: add original test --- .../eslint-plugin/tests/rules/restrict-plus-operands.test.ts | 4 ++++ 1 file changed, 4 insertions(+) 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 be224812727..0604fd19909 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -24,6 +24,10 @@ ruleTester.run('restrict-plus-operands', rule, { 'var foo = 1n + 1n;', 'var foo = BigInt(1) + 1n;', ` + var foo = 1n; + foo + 2n; + `, + ` function test(s: string, n: number): number { return 2; } From ee2014a9b4c50bc4bb3b5110cd8ab50964425101 Mon Sep 17 00:00:00 2001 From: sunghyunjo Date: Sat, 3 Oct 2020 22:42:52 +0900 Subject: [PATCH 4/4] chore: add tc that doesn't contain a string --- .../tests/rules/restrict-plus-operands.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 0604fd19909..daa02fa46d9 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -515,6 +515,20 @@ function foo(a: T) { }, ], }, + { + code: ` + declare const a: { a: 1 } & { b: 2 }; + declare const b: string; + const x = a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 4, + column: 19, + }, + ], + }, { code: ` let foo: string | undefined;