Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit e17ea57

Browse files
vedadeeptaadidahiya
authored andcommitted
fix: add bigint support to restrict-plus-operand rule (#4814)
1 parent a3a0b32 commit e17ea57

File tree

7 files changed

+75
-18
lines changed

7 files changed

+75
-18
lines changed

src/rules/restrictPlusOperandsRule.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Rule extends Lint.Rules.TypedRule {
3636
/* tslint:enable:object-literal-sort-keys */
3737

3838
public static INVALID_TYPES_ERROR =
39-
"Operands of '+' operation must either be both strings or both numbers";
39+
"Operands of '+' operation must either be both strings or both numbers or both bigints";
4040
public static SUGGEST_TEMPLATE_LITERALS = ". Consider using template literals.";
4141

4242
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) {
8585
return typeString;
8686
}
8787

88-
function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid" {
88+
function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "bigint" | "invalid" {
8989
if (
9090
isTypeFlagSet(type, ts.TypeFlags.StringLiteral) ||
9191
isTypeFlagSet(type, ts.TypeFlags.String)
@@ -96,6 +96,11 @@ function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid
9696
isTypeFlagSet(type, ts.TypeFlags.Number)
9797
) {
9898
return "number";
99+
} else if (
100+
isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral) ||
101+
isTypeFlagSet(type, ts.TypeFlags.BigInt)
102+
) {
103+
return "bigint";
99104
} else if (isUnionType(type) && !isTypeFlagSet(type, ts.TypeFlags.Enum)) {
100105
const types = type.types.map(getBaseTypeOfLiteralType);
101106
return allSame(types) ? types[0] : "invalid";

test/rules/restrict-plus-operands/test.ts.lint renamed to test/rules/restrict-plus-operands/default/test.ts.lint

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,38 @@ var pair: NumberStringPair = {
1717

1818
// bad
1919
var bad1 = 5 + "10";
20-
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + "10". Consider using template literals.]
20+
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + "10". Consider using template literals.]
2121
var bad2 = [] + 5;
22-
~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + 5]
22+
~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + 5]
2323
var bad3 = [] + {};
24-
~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + {}]
24+
~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + {}]
2525
var bad4 = [] + [];
26-
~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + []]
26+
~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + []]
2727
var bad4 = 5 + [];
28-
~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + []]
28+
~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + []]
2929
var bad5 = "5" + {};
30-
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found "5" + {}. Consider using template literals.]
30+
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found "5" + {}. Consider using template literals.]
3131
var bad6 = 5.5 + "5";
32-
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5.5 + "5". Consider using template literals.]
32+
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5.5 + "5". Consider using template literals.]
3333
var bad7 = "5.5" + 5;
34-
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found "5.5" + 5. Consider using template literals.]
34+
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found "5.5" + 5. Consider using template literals.]
3535
var bad8 = x + y;
36-
~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + string. Consider using template literals.]
36+
~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + string. Consider using template literals.]
3737
var bad9 = y + x;
38-
~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found string + number. Consider using template literals.]
38+
~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found string + number. Consider using template literals.]
3939
var bad10 = x + {};
40-
~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + {}]
40+
~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + {}]
4141
var bad11 = [] + y;
42-
~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found [] + string. Consider using template literals.]
42+
~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found [] + string. Consider using template literals.]
4343
var bad12 = pair.first + "10";
44-
~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found number + "10". Consider using template literals.]
44+
~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + "10". Consider using template literals.]
4545
var bad13 = 5 + pair.second;
46-
~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found 5 + string. Consider using template literals.]
46+
~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found 5 + string. Consider using template literals.]
4747
var bad14 = pair + pair;
48-
~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found NumberStringPair + NumberStringPair]
48+
~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found NumberStringPair + NumberStringPair]
4949
var anyTyped: any = 5;
5050
var bad15 = anyTyped + 12;
51-
~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, but found any + 12]
51+
~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found any + 12]
5252

5353
// good
5454
var good1 = 5 + 10;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[typescript]: >= 3.2.0
2+
type MyNumber = number;
3+
type MyString = string;
4+
type MyBigInt = bigint;
5+
interface NumberStringBigint {
6+
first: MyNumber,
7+
second: MyString,
8+
third: MyBigInt
9+
}
10+
11+
var x = 5;
12+
var y = "10";
13+
var bigintVar = BigInt(200);
14+
var anyVar: any = 200;
15+
var pair: NumberStringBigint = {
16+
first: 5, first: 5,
17+
second: "10" second: "10",
18+
third: BigInt(100),
19+
};
20+
21+
const bigIntPassA = BigInt(1) + BigInt(2);
22+
const bigIntPassB = BigInt(1) + 100n;
23+
const bigIntFailA = BigInt(1) + 2;
24+
~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + 2]
25+
const bigIntFailB = BigInt(1) + "failureString";
26+
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + "failureString". Consider using template literals.]
27+
28+
const bigIntFailC = bigintVar + x;
29+
~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + number]
30+
const bigIntFailD = y + bigintVar;
31+
~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found string + bigint. Consider using template literals.]
32+
const bigIntFailE = bigintVar + anyVar;
33+
~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + any]
34+
35+
const bigIntFailF = pair.first + pair.third;
36+
~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found number + bigint]
37+
const bigIntFailG = pair.third + pair.second;
38+
~~~~~~~~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + string. Consider using template literals.]
39+
const bigIntFailH = bigintVar + [];
40+
~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + []]
41+
const bigIntFailI = bigintVar + {};
42+
~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers or both bigints, but found bigint + {}]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"restrict-plus-operands": true
4+
}
5+
}

0 commit comments

Comments
 (0)