From 9a883635a4b0cbf759d1ae791398933d6fb0d614 Mon Sep 17 00:00:00 2001 From: Denys Kniazevych Date: Sat, 23 Feb 2019 16:40:51 +0100 Subject: [PATCH] fix(eslint-plugin): support BigInt in restrict-plus-operands rule (#309) (#310) --- .../docs/rules/restrict-plus-operands.md | 2 ++ .../src/rules/restrict-plus-operands.ts | 12 ++++++++++- .../rules/restrict-plus-operands.test.ts | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md index a1436daf769..1efffa83e69 100644 --- a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md +++ b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md @@ -4,12 +4,14 @@ Examples of **correct** code: ```ts var foo = parseInt('5.5', 10) + 10; +var foo = 1n + 1n; ``` Examples of **incorrect** code: ```ts var foo = '5.5' + 5; +var foo = 1n + 1; ``` ## Options diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 3b50170b59c..6b63f88a4f7 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -18,6 +18,7 @@ export default util.createRule({ "Operands of '+' operation must either be both strings or both numbers.", notStrings: "Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.", + notBigInts: "Operands of '+' operation must be both bigints.", }, schema: [], }, @@ -27,7 +28,7 @@ export default util.createRule({ const typeChecker = service.program.getTypeChecker(); - type BaseLiteral = 'string' | 'number' | 'invalid'; + type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid'; /** * Helper function to get base type of node @@ -41,6 +42,10 @@ export default util.createRule({ if (type.isStringLiteral()) { return 'string'; } + // is BigIntLiteral + if (type.flags & ts.TypeFlags.BigIntLiteral) { + return 'bigint'; + } if (type.isUnion()) { const types = type.types.map(getBaseTypeOfLiteralType); @@ -81,6 +86,11 @@ export default util.createRule({ node, messageId: 'notStrings', }); + } else if (leftType === 'bigint' || rightType === 'bigint') { + context.report({ + node, + messageId: 'notBigInts', + }); } else { context.report({ node, 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 3d44ba022b8..4cff2c7c5c1 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -22,6 +22,7 @@ ruleTester.run('restrict-plus-operands', rule, { `var foo = "5.5" + "10";`, `var foo = parseInt("5.5", 10) + 10;`, `var foo = parseFloat("5.5", 10) + 10;`, + `var foo = 1n + 1n;`, ` function test () : number { return 2; } var foo = test("5.5", 10) + 10; @@ -262,5 +263,25 @@ var foo = pair + pair; }, ], }, + { + code: `var foo = 1n + 1`, + errors: [ + { + messageId: 'notBigInts', + line: 1, + column: 11, + }, + ], + }, + { + code: `var foo = 1 + 1n`, + errors: [ + { + messageId: 'notBigInts', + line: 1, + column: 11, + }, + ], + }, ], });