From e17ea57b59b14c517b5c6ac0fb1505941a4628df Mon Sep 17 00:00:00 2001 From: Veda Date: Wed, 31 Jul 2019 00:06:54 +0530 Subject: [PATCH] fix: add bigint support to restrict-plus-operand rule (#4814) --- src/rules/restrictPlusOperandsRule.ts | 9 +++- .../{ => default}/test.ts.lint | 32 +++++++------- .../{ => default}/tsconfig.json | 0 .../{ => default}/tslint.json | 0 .../esnext-bigint/test.ts.lint | 42 +++++++++++++++++++ .../esnext-bigint/tsconfig.json | 5 +++ .../esnext-bigint/tslint.json | 5 +++ 7 files changed, 75 insertions(+), 18 deletions(-) rename test/rules/restrict-plus-operands/{ => default}/test.ts.lint (59%) rename test/rules/restrict-plus-operands/{ => default}/tsconfig.json (100%) rename test/rules/restrict-plus-operands/{ => default}/tslint.json (100%) create mode 100644 test/rules/restrict-plus-operands/esnext-bigint/test.ts.lint create mode 100644 test/rules/restrict-plus-operands/esnext-bigint/tsconfig.json create mode 100644 test/rules/restrict-plus-operands/esnext-bigint/tslint.json diff --git a/src/rules/restrictPlusOperandsRule.ts b/src/rules/restrictPlusOperandsRule.ts index 5b34e6a5cce..a4cc39764a2 100644 --- a/src/rules/restrictPlusOperandsRule.ts +++ b/src/rules/restrictPlusOperandsRule.ts @@ -36,7 +36,7 @@ export class Rule extends Lint.Rules.TypedRule { /* tslint:enable:object-literal-sort-keys */ public static INVALID_TYPES_ERROR = - "Operands of '+' operation must either be both strings or both numbers"; + "Operands of '+' operation must either be both strings or both numbers or both bigints"; public static SUGGEST_TEMPLATE_LITERALS = ". Consider using template literals."; public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { @@ -85,7 +85,7 @@ function getTypeString(tc: ts.TypeChecker, node: ts.Node, type: ts.Type) { return typeString; } -function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid" { +function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "bigint" | "invalid" { if ( isTypeFlagSet(type, ts.TypeFlags.StringLiteral) || isTypeFlagSet(type, ts.TypeFlags.String) @@ -96,6 +96,11 @@ function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid isTypeFlagSet(type, ts.TypeFlags.Number) ) { return "number"; + } else if ( + isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral) || + isTypeFlagSet(type, ts.TypeFlags.BigInt) + ) { + return "bigint"; } else if (isUnionType(type) && !isTypeFlagSet(type, ts.TypeFlags.Enum)) { const types = type.types.map(getBaseTypeOfLiteralType); return allSame(types) ? types[0] : "invalid"; diff --git a/test/rules/restrict-plus-operands/test.ts.lint b/test/rules/restrict-plus-operands/default/test.ts.lint similarity index 59% rename from test/rules/restrict-plus-operands/test.ts.lint rename to test/rules/restrict-plus-operands/default/test.ts.lint index 4899d42ced7..f1c604d3efe 100644 --- a/test/rules/restrict-plus-operands/test.ts.lint +++ b/test/rules/restrict-plus-operands/default/test.ts.lint @@ -17,38 +17,38 @@ var pair: NumberStringPair = { // bad var bad1 = 5 + "10"; - ~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + "10". Consider using template literals.] + ~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + "10". Consider using template literals.] var bad2 = [] + 5; - ~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + 5] + ~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + 5] var bad3 = [] + {}; - ~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + {}] + ~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + {}] var bad4 = [] + []; - ~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + []] + ~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + []] var bad4 = 5 + []; - ~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + []] + ~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + []] var bad5 = "5" + {}; - ~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found "5" + {}. Consider using template literals.] + ~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found "5" + {}. Consider using template literals.] var bad6 = 5.5 + "5"; - ~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5.5 + "5". Consider using template literals.] + ~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5.5 + "5". Consider using template literals.] var bad7 = "5.5" + 5; - ~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found "5.5" + 5. Consider using template literals.] + ~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found "5.5" + 5. Consider using template literals.] var bad8 = x + y; - ~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + string. Consider using template literals.] + ~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + string. Consider using template literals.] var bad9 = y + x; - ~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found string + number. Consider using template literals.] + ~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found string + number. Consider using template literals.] var bad10 = x + {}; - ~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + {}] + ~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + {}] var bad11 = [] + y; - ~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + string. Consider using template literals.] + ~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + string. Consider using template literals.] var bad12 = pair.first + "10"; - ~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + "10". Consider using template literals.] + ~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + "10". Consider using template literals.] var bad13 = 5 + pair.second; - ~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + string. Consider using template literals.] + ~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + string. Consider using template literals.] var bad14 = pair + pair; - ~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found NumberStringPair + NumberStringPair] + ~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found NumberStringPair + NumberStringPair] var anyTyped: any = 5; var bad15 = anyTyped + 12; - ~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found any + 12] + ~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found any + 12] // good var good1 = 5 + 10; diff --git a/test/rules/restrict-plus-operands/tsconfig.json b/test/rules/restrict-plus-operands/default/tsconfig.json similarity index 100% rename from test/rules/restrict-plus-operands/tsconfig.json rename to test/rules/restrict-plus-operands/default/tsconfig.json diff --git a/test/rules/restrict-plus-operands/tslint.json b/test/rules/restrict-plus-operands/default/tslint.json similarity index 100% rename from test/rules/restrict-plus-operands/tslint.json rename to test/rules/restrict-plus-operands/default/tslint.json diff --git a/test/rules/restrict-plus-operands/esnext-bigint/test.ts.lint b/test/rules/restrict-plus-operands/esnext-bigint/test.ts.lint new file mode 100644 index 00000000000..b8ddc80558c --- /dev/null +++ b/test/rules/restrict-plus-operands/esnext-bigint/test.ts.lint @@ -0,0 +1,42 @@ +[typescript]: >= 3.2.0 +type MyNumber = number; +type MyString = string; +type MyBigInt = bigint; +interface NumberStringBigint { + first: MyNumber, + second: MyString, + third: MyBigInt +} + +var x = 5; +var y = "10"; +var bigintVar = BigInt(200); +var anyVar: any = 200; +var pair: NumberStringBigint = { + first: 5, first: 5, + second: "10" second: "10", + third: BigInt(100), +}; + +const bigIntPassA = BigInt(1) + BigInt(2); +const bigIntPassB = BigInt(1) + 100n; +const bigIntFailA = BigInt(1) + 2; + ~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + 2] +const bigIntFailB = BigInt(1) + "failureString"; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + "failureString". Consider using template literals.] + +const bigIntFailC = bigintVar + x; + ~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + number] +const bigIntFailD = y + bigintVar; + ~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found string + bigint. Consider using template literals.] +const bigIntFailE = bigintVar + anyVar; + ~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + any] + +const bigIntFailF = pair.first + pair.third; + ~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + bigint] +const bigIntFailG = pair.third + pair.second; + ~~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + string. Consider using template literals.] +const bigIntFailH = bigintVar + []; + ~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + []] +const bigIntFailI = bigintVar + {}; + ~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + {}] diff --git a/test/rules/restrict-plus-operands/esnext-bigint/tsconfig.json b/test/rules/restrict-plus-operands/esnext-bigint/tsconfig.json new file mode 100644 index 00000000000..0d58d115f70 --- /dev/null +++ b/test/rules/restrict-plus-operands/esnext-bigint/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "esnext" + } +} diff --git a/test/rules/restrict-plus-operands/esnext-bigint/tslint.json b/test/rules/restrict-plus-operands/esnext-bigint/tslint.json new file mode 100644 index 00000000000..e5801e9ba40 --- /dev/null +++ b/test/rules/restrict-plus-operands/esnext-bigint/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "restrict-plus-operands": true + } +}