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

Commit

Permalink
fix: add bigint support to restrict-plus-operand rule (#4814)
Browse files Browse the repository at this point in the history
  • Loading branch information
vedadeepta authored and adidahiya committed Jul 30, 2019
1 parent a3a0b32 commit e17ea57
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
9 changes: 7 additions & 2 deletions src/rules/restrictPlusOperandsRule.ts
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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)
Expand All @@ -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";
Expand Down
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions 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 + {}]
5 changes: 5 additions & 0 deletions test/rules/restrict-plus-operands/esnext-bigint/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "esnext"
}
}
5 changes: 5 additions & 0 deletions test/rules/restrict-plus-operands/esnext-bigint/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"restrict-plus-operands": true
}
}

0 comments on commit e17ea57

Please sign in to comment.