From d36c08f25d39b6a00646e5559410de4c8071fca6 Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Tue, 25 Jul 2023 02:15:48 +0300 Subject: [PATCH] [TS Linter] disable binary bit operations on non-integer literals Signed-off-by: Igor Rossinski --- linter/src/TypeScriptLinter.ts | 5 +++- linter/test/binary_wrong_types.ts | 7 +++++ linter/test/binary_wrong_types.ts.relax.json | 30 +++++++++++++++++++ linter/test/binary_wrong_types.ts.strict.json | 30 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index f255eedab..fcf56b373 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -915,7 +915,10 @@ export class TypeScriptLinter { tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.GreaterThanGreaterThanToken || tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken ) { - if (!(Utils.isNumberType(leftOperandType) && Utils.isNumberType(rightOperandType))) + if (!(Utils.isNumberType(leftOperandType) && Utils.isNumberType(rightOperandType)) || + ( tsLhsExpr.kind === ts.SyntaxKind.NumericLiteral && !Utils.isIntegerConstantValue(tsLhsExpr as ts.NumericLiteral)) || + ( tsRhsExpr.kind === ts.SyntaxKind.NumericLiteral && !Utils.isIntegerConstantValue(tsRhsExpr as ts.NumericLiteral)) + ) this.incrementCounters(node, FaultID.BitOpWithWrongType); } else if (tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.CommaToken) { // CommaOpertor is allowed in 'for' statement initalizer and incrementor diff --git a/linter/test/binary_wrong_types.ts b/linter/test/binary_wrong_types.ts index 976ed7a1f..e62f9d37c 100644 --- a/linter/test/binary_wrong_types.ts +++ b/linter/test/binary_wrong_types.ts @@ -97,3 +97,10 @@ const a000 = ((k = 10), 2 + 7); function foo(n: number, m: number): number { return (n/m) + ((n%m) ? 1 : 0); } + +const b40 = 7 & 5.5; +const b41 = 5.5 & b40; +const b42 = 2 | 5.5; +const b43 = 5.5 | b42; +const b44 = 4 ^ 5.5; +const b45 = 5.5 ^ b44; diff --git a/linter/test/binary_wrong_types.ts.relax.json b/linter/test/binary_wrong_types.ts.relax.json index c79d40409..f5197b0ef 100644 --- a/linter/test/binary_wrong_types.ts.relax.json +++ b/linter/test/binary_wrong_types.ts.relax.json @@ -138,6 +138,36 @@ "line": 95, "column": 15, "problem": "CommaOperator" + }, + { + "line": 101, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 102, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 103, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 104, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 105, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 106, + "column": 13, + "problem": "BitOpWithWrongType" } ] } \ No newline at end of file diff --git a/linter/test/binary_wrong_types.ts.strict.json b/linter/test/binary_wrong_types.ts.strict.json index c79d40409..f5197b0ef 100644 --- a/linter/test/binary_wrong_types.ts.strict.json +++ b/linter/test/binary_wrong_types.ts.strict.json @@ -138,6 +138,36 @@ "line": 95, "column": 15, "problem": "CommaOperator" + }, + { + "line": 101, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 102, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 103, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 104, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 105, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 106, + "column": 13, + "problem": "BitOpWithWrongType" } ] } \ No newline at end of file -- Gitee