From 2e46a75998fe1a6882fd63ae9ebc8fc4e6c5984c Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 8 Oct 2024 08:47:45 +0530 Subject: [PATCH 1/9] Add static rule for invalid range operator --- .../io/ballerina/scan/internal/CoreRule.java | 5 +- .../scan/internal/StaticCodeAnalyzer.java | 74 +++++++++++++++++++ .../io/ballerina/scan/utils/Constants.java | 8 ++ .../scan/internal/StaticCodeAnalyzerTest.java | 53 ++++++++++--- .../core-rules/invalid_range_operator.bal | 65 ++++++++++++++++ 5 files changed, 195 insertions(+), 10 deletions(-) create mode 100644 scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 211049e3..471bd708 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -20,6 +20,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; +import io.ballerina.scan.utils.Constants; import java.util.ArrayList; import java.util.List; @@ -30,7 +31,9 @@ * @since 0.1.0 * */ enum CoreRule { - AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)); + AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)), + INVALID_RANGE_OPERATOR(RuleFactory.createRule(10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index d7cf2c0e..d78fbab1 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -18,14 +18,24 @@ package io.ballerina.scan.internal; +import io.ballerina.compiler.syntax.tree.BasicLiteralNode; +import io.ballerina.compiler.syntax.tree.BinaryExpressionNode; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; +import io.ballerina.compiler.syntax.tree.ExpressionNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; +import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.syntax.tree.NodeVisitor; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; +import io.ballerina.compiler.syntax.tree.Token; +import io.ballerina.compiler.syntax.tree.UnaryExpressionNode; import io.ballerina.projects.Document; import io.ballerina.scan.ScannerContext; +import static io.ballerina.compiler.syntax.tree.SyntaxKind.DOUBLE_DOT_LT_TOKEN; +import static io.ballerina.compiler.syntax.tree.SyntaxKind.ELLIPSIS_TOKEN; + /** * {@code StaticCodeAnalyzer} contains the logic to perform core static code analysis on Ballerina documents. * @@ -58,4 +68,68 @@ public void visit(CheckExpressionNode checkExpressionNode) { CoreRule.AVOID_CHECKPANIC.rule()); } } + + public void visit(BinaryExpressionNode binaryExpressionNode) { + if (binaryExpressionNode.operator().kind().equals(ELLIPSIS_TOKEN) + || binaryExpressionNode.operator().kind().equals(DOUBLE_DOT_LT_TOKEN)) { + validateRangeExpressionOperator(scannerContext, document, binaryExpressionNode.lhsExpr(), + binaryExpressionNode.rhsExpr(), binaryExpressionNode.operator(), binaryExpressionNode.location()); + } + } + + private void validateRangeExpressionOperator(ScannerContext scannerContext, Document document, + Node n1, Node n2, Token operator, NodeLocation location) { + String lhsExpr = null; + String rhsExpr = null; + boolean isMinusOperatorPresentInLhs = false; + boolean isMinusOperatorPresentInRhs = false; + + if (n1 instanceof UnaryExpressionNode unaryN1) { + if (unaryN1.unaryOperator().kind() == SyntaxKind.MINUS_TOKEN) { + isMinusOperatorPresentInLhs = true; + } + n1 = unaryN1.expression(); + } + if (n2 instanceof UnaryExpressionNode unaryN2) { + if (unaryN2.unaryOperator().kind() == SyntaxKind.MINUS_TOKEN) { + isMinusOperatorPresentInRhs = true; + } + n2 = unaryN2.expression(); + } + + if (n1 instanceof BasicLiteralNode lhsExprNode) { + lhsExpr = lhsExprNode.literalToken().text(); + if (isMinusOperatorPresentInLhs) { + lhsExpr = "-" + lhsExpr; + } + } + + if (n2 instanceof BasicLiteralNode rhsExprNode) { + rhsExpr = rhsExprNode.literalToken().text(); + if (isMinusOperatorPresentInRhs) { + rhsExpr = "-" + rhsExpr; + } + } + + if (lhsExpr != null && rhsExpr != null) { + // According to the spec, these literal tokens are integers. So no need to cast or check. + try { + int lhs = Integer.parseInt(lhsExpr); + int rhs = Integer.parseInt(rhsExpr); + if (operator.kind() == DOUBLE_DOT_LT_TOKEN) { + if (lhs >= rhs) { + scannerContext.getReporter().reportIssue(document, location, + CoreRule.INVALID_RANGE_OPERATOR.rule()); + } + } else if (operator.kind() == ELLIPSIS_TOKEN) { + if (lhs > rhs) { + scannerContext.getReporter().reportIssue(document, location, + CoreRule.INVALID_RANGE_OPERATOR.rule()); + } + } + } catch (NumberFormatException e) { + // ignore + } + } + } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java index 372de85d..9ff52e11 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java @@ -62,6 +62,14 @@ public class Constants { static final String RULE_DESCRIPTION_COLUMN = "Rule Description"; static final String[] RULE_PRIORITY_LIST = {"ballerina", "ballerina/", "ballerinax/", "wso2/"}; + public static class RuleDescription { + public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; + public static final String INVALID_RANGE_OPERATOR = "Invalid range operator"; + + private RuleDescription() { + } + } + private Constants() { } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 242b11e2..fac7b5e8 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -27,6 +27,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; +import io.ballerina.scan.utils.Constants; import io.ballerina.tools.text.LineRange; import org.testng.Assert; import org.testng.annotations.Test; @@ -57,18 +58,52 @@ void testCheckpanicAnalyzer() { staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); Assert.assertEquals(issues.size(), 1); - Issue issue = issues.get(0); + assertIssue(issues.get(0), documentName, 20, 17, 20, 39, "ballerina:1", 1, + Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); + } + + @Test(description = "test checkpanic analyzer") + void testRangeOperatorAnalyzer() { + String documentName = "invalid_range_operator.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_OPERATOR.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 9); + assertIssue(issues.get(0), documentName, 9, 21, 9, 27, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 13, 21, 13, 27, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 17, 21, 17, 28, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 41, 21, 41, 27, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 45, 21, 45, 27, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 49, 21, 49, 28, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 53, 21, 53, 26, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 57, 21, 57, 28, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 61, 21, 61, 28, "ballerina:10", 10, + Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + } + + void assertIssue(Issue issue, String documentName, int startLine, int startOffset, int endLine, int endOffset, + String ruleId, int numericId, String description, RuleKind ruleKind) { Assert.assertEquals(issue.source(), Source.BUILT_IN); LineRange location = issue.location().lineRange(); Assert.assertEquals(location.fileName(), documentName); - Assert.assertEquals(location.startLine().line(), 20); - Assert.assertEquals(location.startLine().offset(), 17); - Assert.assertEquals(location.endLine().line(), 20); - Assert.assertEquals(location.endLine().offset(), 39); + Assert.assertEquals(location.startLine().line(), startLine); + Assert.assertEquals(location.startLine().offset(), startOffset); + Assert.assertEquals(location.endLine().line(), endLine); + Assert.assertEquals(location.endLine().offset(), endOffset); Rule rule = issue.rule(); - Assert.assertEquals(rule.id(), "ballerina:1"); - Assert.assertEquals(rule.numericId(), 1); - Assert.assertEquals(rule.description(), "Avoid checkpanic"); - Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); + Assert.assertEquals(rule.id(), ruleId); + Assert.assertEquals(rule.numericId(), numericId); + Assert.assertEquals(rule.description(), description); + Assert.assertEquals(rule.kind(), ruleKind); } } diff --git a/scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal b/scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal new file mode 100644 index 00000000..b291e5af --- /dev/null +++ b/scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal @@ -0,0 +1,65 @@ +function testRangeOperator() { + foreach int i in 0...10 { + // code + } + + foreach int i in -5...-2 { + // code + } + + foreach int i in 0...-1 { // warning + // code + } + + foreach int i in 10...9 { // warning + // code + } + + foreach int i in -2...-3 { // warning + // code + } + + foreach int i in 0...0 { + // code + } + + foreach int i in 10...10 { + // code + } + + foreach int i in -2...-2 { + // code + } + + foreach int i in 0..<10 { + // code + } + + foreach int i in -5..<-2 { + // code + } + + foreach int i in 0..<-1 { // warning + // code + } + + foreach int i in 10..<9 { // warning + // code + } + + foreach int i in -2..<-3 { // warning + // code + } + + foreach int i in 0..<0 { // warning + // code + } + + foreach int i in 10..<10 { // warning + // code + } + + foreach int i in -2..<-2 { // warning + // code + } +} From 470d92aed296f2f9f5ef78db6b1e21611a8af26b Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 20 Feb 2025 12:59:43 +0530 Subject: [PATCH 2/9] Add invalid range rule to the list --- .../io/ballerina/scan/internal/CoreRule.java | 4 +- .../scan/internal/StaticCodeAnalyzer.java | 13 ++-- .../io/ballerina/scan/utils/Constants.java | 8 --- .../ballerina/scan/internal/CoreRuleTest.java | 2 +- .../ballerina/scan/internal/Rule006Test.java | 66 +++++++++++++++++++ .../scan/internal/StaticCodeAnalyzerTest.java | 30 --------- .../unix/list-rules-output.txt | 1 + .../unix/print-rules-to-console.txt | 3 +- .../windows/list-rules-output.txt | 1 + .../windows/print-rules-to-console.txt | 3 +- ...l => rule006_invalid_range_expression.bal} | 0 11 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java rename scan-command/src/test/resources/test-resources/core-rules/{invalid_range_operator.bal => rule006_invalid_range_expression.bal} (100%) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index d802009a..52062e91 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -20,7 +20,6 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.Constants; import java.util.ArrayList; import java.util.List; @@ -34,8 +33,7 @@ enum CoreRule { AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)), UNUSED_FUNCTION_PARAMETER(RuleFactory.createRule(2, "Unused function parameter", RuleKind.CODE_SMELL)), - INVALID_RANGE_OPERATOR(RuleFactory.createRule(10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL)); + INVALID_RANGE_OPERATOR(RuleFactory.createRule(10, "Invalid range operator", RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index b9219afb..da58ca4d 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -18,15 +18,10 @@ package io.ballerina.scan.internal; -import io.ballerina.compiler.syntax.tree.BasicLiteralNode; -import io.ballerina.compiler.syntax.tree.BinaryExpressionNode; -import io.ballerina.compiler.syntax.tree.CheckExpressionNode; -import io.ballerina.compiler.syntax.tree.ExpressionNode; -import io.ballerina.compiler.syntax.tree.ModulePartNode; -import io.ballerina.compiler.syntax.tree.Node; -import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.api.symbols.Symbol; +import io.ballerina.compiler.syntax.tree.BasicLiteralNode; +import io.ballerina.compiler.syntax.tree.BinaryExpressionNode; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; import io.ballerina.compiler.syntax.tree.ExplicitAnonymousFunctionExpressionNode; import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; @@ -36,6 +31,7 @@ import io.ballerina.compiler.syntax.tree.IncludedRecordParameterNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.syntax.tree.NodeVisitor; import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; @@ -45,9 +41,10 @@ import io.ballerina.projects.Document; import io.ballerina.scan.ScannerContext; +import java.util.Optional; + import static io.ballerina.compiler.syntax.tree.SyntaxKind.DOUBLE_DOT_LT_TOKEN; import static io.ballerina.compiler.syntax.tree.SyntaxKind.ELLIPSIS_TOKEN; -import java.util.Optional; /** * {@code StaticCodeAnalyzer} contains the logic to perform core static code analysis on Ballerina documents. diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java index 9ff52e11..372de85d 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java @@ -62,14 +62,6 @@ public class Constants { static final String RULE_DESCRIPTION_COLUMN = "Rule Description"; static final String[] RULE_PRIORITY_LIST = {"ballerina", "ballerina/", "ballerinax/", "wso2/"}; - public static class RuleDescription { - public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; - public static final String INVALID_RANGE_OPERATOR = "Invalid range operator"; - - private RuleDescription() { - } - } - private Constants() { } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java index 1dd3c15f..25acd149 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java @@ -34,7 +34,7 @@ public class CoreRuleTest { @Test(description = "test all rules") void testAllRules() { - Assert.assertEquals(CoreRule.rules().size(), 2); + Assert.assertEquals(CoreRule.rules().size(), 3); } @Test(description = "test checkpanic rule") diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java new file mode 100644 index 00000000..3d5cba01 --- /dev/null +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.scan.internal; + +import io.ballerina.projects.Document; +import io.ballerina.scan.Issue; +import io.ballerina.scan.RuleKind; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +/** + * Unused function parameters analyzer tests. + * + * @since 0.1.0 + */ +public class Rule006Test extends StaticCodeAnalyzerTest { + private static final String INVALID_RANGE_OPERATOR = "Invalid range operator"; + + @Test(description = "test invalid range operator") + void testRangeOperatorAnalyzer() { + String documentName = "rule006_invalid_range_expression.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_OPERATOR.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, + scannerContext, document.module().getCompilation().getSemanticModel()); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 9); + assertIssue(issues.get(0), documentName, 9, 21, 9, 27, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 13, 21, 13, 27, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 17, 21, 17, 28, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 41, 21, 41, 27, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 45, 21, 45, 27, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 49, 21, 49, 28, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 53, 21, 53, 26, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 57, 21, 57, 28, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 61, 21, 61, 28, "ballerina:6", 10, + INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + } +} diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 7c53b418..32faf327 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -27,7 +27,6 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; -import io.ballerina.scan.utils.Constants; import io.ballerina.tools.text.LineRange; import org.testng.Assert; @@ -47,35 +46,6 @@ Document loadDocument(String documentName) { return defaultModule.document(defaultModule.documentIds().iterator().next()); } - @Test(description = "test checkpanic analyzer") - void testRangeOperatorAnalyzer() { - String documentName = "invalid_range_operator.bal"; - Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_OPERATOR.rule())); - StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext); - staticCodeAnalyzer.analyze(); - List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 9); - assertIssue(issues.get(0), documentName, 9, 21, 9, 27, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 13, 21, 13, 27, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 17, 21, 17, 28, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 41, 21, 41, 27, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 45, 21, 45, 27, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 49, 21, 49, 28, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 53, 21, 53, 26, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 57, 21, 57, 28, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 61, 21, 61, 28, "ballerina:10", 10, - Constants.RuleDescription.INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - } - void assertIssue(Issue issue, String documentName, int startLine, int startOffset, int endLine, int endOffset, String ruleId, int numericId, String description, RuleKind ruleKind) { Assert.assertEquals(issue.source(), Source.BUILT_IN); diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index 5ad2e669..b643a52b 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -2,6 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:10 | CODE_SMELL | Invalid range operator ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index 431cad6f..b643a52b 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -2,6 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:10 | CODE_SMELL | Invalid range operator ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 @@ -11,4 +12,4 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj ballerinax/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 exampleOrg/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 exampleOrg/example_module_static_code_analyzer:2 | BUG | rule 2 - exampleOrg/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 \ No newline at end of file + exampleOrg/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index e384b4ea..f8764c42 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -2,6 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:10 | CODE_SMELL | Invalid range operator ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index cab063a4..f8764c42 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -2,6 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:10 | CODE_SMELL | Invalid range operator ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 @@ -11,4 +12,4 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj ballerinax/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 exampleOrg/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 exampleOrg/example_module_static_code_analyzer:2 | BUG | rule 2 - exampleOrg/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 \ No newline at end of file + exampleOrg/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal b/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal similarity index 100% rename from scan-command/src/test/resources/test-resources/core-rules/invalid_range_operator.bal rename to scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal From e78366c19c75ae444b43469c321faffbede27a69 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 25 Feb 2025 14:41:02 +0530 Subject: [PATCH 3/9] Update range expresions rule description --- .../io/ballerina/scan/internal/CoreRule.java | 2 +- .../scan/internal/StaticCodeAnalyzer.java | 93 +++++++++---------- .../ballerina/scan/internal/Rule006Test.java | 52 ++++++----- .../unix/list-rules-output.txt | 2 +- .../unix/print-rules-to-console.txt | 2 +- .../windows/list-rules-output.txt | 2 +- .../windows/print-rules-to-console.txt | 2 +- .../rule006_invalid_range_expression.bal | 24 +++++ scan-command/src/test/resources/testng.xml | 1 + 9 files changed, 103 insertions(+), 77 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 52062e91..b3a3eda9 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -33,7 +33,7 @@ enum CoreRule { AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)), UNUSED_FUNCTION_PARAMETER(RuleFactory.createRule(2, "Unused function parameter", RuleKind.CODE_SMELL)), - INVALID_RANGE_OPERATOR(RuleFactory.createRule(10, "Invalid range operator", RuleKind.CODE_SMELL)); + INVALID_RANGE_EXPRESSION(RuleFactory.createRule(10, "Invalid range expression", RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index da58ca4d..0d326dd9 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -81,66 +81,40 @@ public void visit(CheckExpressionNode checkExpressionNode) { } public void visit(BinaryExpressionNode binaryExpressionNode) { - if (binaryExpressionNode.operator().kind().equals(ELLIPSIS_TOKEN) - || binaryExpressionNode.operator().kind().equals(DOUBLE_DOT_LT_TOKEN)) { + SyntaxKind binaryOperatorKind = binaryExpressionNode.operator().kind(); + if (binaryOperatorKind.equals(ELLIPSIS_TOKEN) + || binaryOperatorKind.equals(DOUBLE_DOT_LT_TOKEN)) { validateRangeExpressionOperator(scannerContext, document, binaryExpressionNode.lhsExpr(), binaryExpressionNode.rhsExpr(), binaryExpressionNode.operator(), binaryExpressionNode.location()); } } private void validateRangeExpressionOperator(ScannerContext scannerContext, Document document, - Node n1, Node n2, Token operator, NodeLocation location) { - String lhsExpr = null; - String rhsExpr = null; - boolean isMinusOperatorPresentInLhs = false; - boolean isMinusOperatorPresentInRhs = false; - - if (n1 instanceof UnaryExpressionNode unaryN1) { - if (unaryN1.unaryOperator().kind() == SyntaxKind.MINUS_TOKEN) { - isMinusOperatorPresentInLhs = true; - } - n1 = unaryN1.expression(); - } - if (n2 instanceof UnaryExpressionNode unaryN2) { - if (unaryN2.unaryOperator().kind() == SyntaxKind.MINUS_TOKEN) { - isMinusOperatorPresentInRhs = true; - } - n2 = unaryN2.expression(); - } - - if (n1 instanceof BasicLiteralNode lhsExprNode) { - lhsExpr = lhsExprNode.literalToken().text(); - if (isMinusOperatorPresentInLhs) { - lhsExpr = "-" + lhsExpr; - } - } + Node lhsNode, Node rhsNode, Token operator, NodeLocation location) { + Optional lhsExpr = getExprStringValueFromRangeExprNode(lhsNode); + Optional rhsExpr = getExprStringValueFromRangeExprNode(rhsNode); - if (n2 instanceof BasicLiteralNode rhsExprNode) { - rhsExpr = rhsExprNode.literalToken().text(); - if (isMinusOperatorPresentInRhs) { - rhsExpr = "-" + rhsExpr; - } + if (lhsExpr.isEmpty() || rhsExpr.isEmpty()) { + return; } - if (lhsExpr != null && rhsExpr != null) { - // According to the spec, these literal tokens are integers. So no need to cast or check. - try { - int lhs = Integer.parseInt(lhsExpr); - int rhs = Integer.parseInt(rhsExpr); - if (operator.kind() == DOUBLE_DOT_LT_TOKEN) { - if (lhs >= rhs) { - scannerContext.getReporter().reportIssue(document, location, - CoreRule.INVALID_RANGE_OPERATOR.rule()); - } - } else if (operator.kind() == ELLIPSIS_TOKEN) { - if (lhs > rhs) { - scannerContext.getReporter().reportIssue(document, location, - CoreRule.INVALID_RANGE_OPERATOR.rule()); - } + // According to the spec, these literal tokens are integers. So no need to cast or check. + try { + int lhsValue = Integer.parseInt(lhsExpr.get()); + int rhsValue = Integer.parseInt(rhsExpr.get()); + if (operator.kind() == DOUBLE_DOT_LT_TOKEN) { + if (lhsValue >= rhsValue) { + scannerContext.getReporter().reportIssue(document, location, + CoreRule.INVALID_RANGE_EXPRESSION.rule()); + } + } else if (operator.kind() == ELLIPSIS_TOKEN) { + if (lhsValue > rhsValue) { + scannerContext.getReporter().reportIssue(document, location, + CoreRule.INVALID_RANGE_EXPRESSION.rule()); } - } catch (NumberFormatException e) { - // ignore } + } catch (NumberFormatException e) { + // ignore } } @@ -199,4 +173,25 @@ private boolean isUnusedNode(Node node) { Optional symbol = semanticModel.symbol(node); return symbol.filter(value -> semanticModel.references(value).size() == 1).isPresent(); } + + private Optional getExprStringValueFromRangeExprNode(Node rangeExprNode) { + boolean isMinusOperatorPresent = false; + + if (rangeExprNode instanceof UnaryExpressionNode unaryNode) { + if (unaryNode.unaryOperator().kind() == SyntaxKind.MINUS_TOKEN) { + isMinusOperatorPresent = true; + } + rangeExprNode = unaryNode.expression(); + } + + if (rangeExprNode instanceof BasicLiteralNode literalNode) { + String expr = literalNode.literalToken().text(); + if (isMinusOperatorPresent) { + expr = "-" + expr; + } + return Optional.of(expr); + } + + return Optional.empty(); + } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java index 3d5cba01..4dc2d7f5 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java @@ -27,40 +27,46 @@ import java.util.List; /** - * Unused function parameters analyzer tests. + * Invalid range expression analyzer tests. * * @since 0.1.0 */ public class Rule006Test extends StaticCodeAnalyzerTest { - private static final String INVALID_RANGE_OPERATOR = "Invalid range operator"; + private static final String INVALID_RANGE_EXPRESSION = "Invalid range expression"; - @Test(description = "test invalid range operator") + @Test(description = "test invalid range expresion") void testRangeOperatorAnalyzer() { String documentName = "rule006_invalid_range_expression.bal"; Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_OPERATOR.rule())); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_EXPRESSION.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext, document.module().getCompilation().getSemanticModel()); staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 9); - assertIssue(issues.get(0), documentName, 9, 21, 9, 27, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 13, 21, 13, 27, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 17, 21, 17, 28, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 41, 21, 41, 27, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 45, 21, 45, 27, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 49, 21, 49, 28, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 53, 21, 53, 26, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 57, 21, 57, 28, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 61, 21, 61, 28, "ballerina:6", 10, - INVALID_RANGE_OPERATOR, RuleKind.CODE_SMELL); + Assert.assertEquals(issues.size(), 12); + assertIssue(issues.get(0), documentName, 25, 21, 25, 27, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 29, 21, 29, 27, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 33, 21, 33, 28, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 57, 21, 57, 27, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 61, 21, 61, 27, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 65, 21, 65, 28, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 69, 21, 69, 26, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 73, 21, 73, 28, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 77, 21, 77, 28, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(9), documentName, 81, 22, 81, 29, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(10), documentName, 83, 22, 83, 29, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); + assertIssue(issues.get(11), documentName, 87, 14, 87, 22, "ballerina:10", 10, + INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index b643a52b..7469d4b0 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range operator + ballerina:10 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index b643a52b..7469d4b0 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range operator + ballerina:10 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index f8764c42..92a90b60 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range operator + ballerina:10 | CODE_SMELL | Invalid range exprssion ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index f8764c42..5a9ad308 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range operator + ballerina:10 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal b/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal index b291e5af..ad539b74 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal @@ -1,3 +1,19 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + function testRangeOperator() { foreach int i in 0...10 { // code @@ -62,4 +78,12 @@ function testRangeOperator() { foreach int i in -2..<-2 { // warning // code } + + _ = from int i in 10..<10 select [1, 2, 3]; // warning + + _ = from int i in -2..<-2 select 1; // warning + + _ = from int i in -2..<2 select 1; + + var val = 10 ... 3; // warning } diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index cbdaee06..f117eccc 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -31,6 +31,7 @@ under the License. + From 42c9b5b01e2d7580bd37732337df19ba542b2549 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 25 Feb 2025 15:08:14 +0530 Subject: [PATCH 4/9] Update windows list rules output --- .../resources/command-outputs/windows/list-rules-output.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index 92a90b60..5a9ad308 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range exprssion + ballerina:10 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 From 332badf4f7b9f13c4cf2607b8606cbebdc895312 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Sat, 15 Mar 2025 23:58:51 +0530 Subject: [PATCH 5/9] Update rule id --- .../io/ballerina/scan/internal/CoreRule.java | 2 +- .../{Rule006Test.java => Rule012Test.java} | 28 +++++++++---------- .../unix/print-rules-to-console.txt | 2 +- .../windows/list-rules-output.txt | 2 +- .../windows/print-rules-to-console.txt | 2 +- ...l => rule012_invalid_range_expression.bal} | 0 scan-command/src/test/resources/testng.xml | 2 +- 7 files changed, 19 insertions(+), 19 deletions(-) rename scan-command/src/test/java/io/ballerina/scan/internal/{Rule006Test.java => Rule012Test.java} (89%) rename scan-command/src/test/resources/test-resources/core-rules/{rule006_invalid_range_expression.bal => rule012_invalid_range_expression.bal} (100%) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index b3a3eda9..d2a338fb 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -33,7 +33,7 @@ enum CoreRule { AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)), UNUSED_FUNCTION_PARAMETER(RuleFactory.createRule(2, "Unused function parameter", RuleKind.CODE_SMELL)), - INVALID_RANGE_EXPRESSION(RuleFactory.createRule(10, "Invalid range expression", RuleKind.CODE_SMELL)); + INVALID_RANGE_EXPRESSION(RuleFactory.createRule(12, "Invalid range expression", RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule012Test.java similarity index 89% rename from scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java rename to scan-command/src/test/java/io/ballerina/scan/internal/Rule012Test.java index 4dc2d7f5..3d3ed4d3 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/Rule006Test.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule012Test.java @@ -31,12 +31,12 @@ * * @since 0.1.0 */ -public class Rule006Test extends StaticCodeAnalyzerTest { +public class Rule012Test extends StaticCodeAnalyzerTest { private static final String INVALID_RANGE_EXPRESSION = "Invalid range expression"; @Test(description = "test invalid range expresion") void testRangeOperatorAnalyzer() { - String documentName = "rule006_invalid_range_expression.bal"; + String documentName = "rule012_invalid_range_expression.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.INVALID_RANGE_EXPRESSION.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, @@ -44,29 +44,29 @@ void testRangeOperatorAnalyzer() { staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); Assert.assertEquals(issues.size(), 12); - assertIssue(issues.get(0), documentName, 25, 21, 25, 27, "ballerina:10", 10, + assertIssue(issues.get(0), documentName, 25, 21, 25, 27, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 29, 21, 29, 27, "ballerina:10", 10, + assertIssue(issues.get(1), documentName, 29, 21, 29, 27, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 33, 21, 33, 28, "ballerina:10", 10, + assertIssue(issues.get(2), documentName, 33, 21, 33, 28, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 57, 21, 57, 27, "ballerina:10", 10, + assertIssue(issues.get(3), documentName, 57, 21, 57, 27, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 61, 21, 61, 27, "ballerina:10", 10, + assertIssue(issues.get(4), documentName, 61, 21, 61, 27, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 65, 21, 65, 28, "ballerina:10", 10, + assertIssue(issues.get(5), documentName, 65, 21, 65, 28, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 69, 21, 69, 26, "ballerina:10", 10, + assertIssue(issues.get(6), documentName, 69, 21, 69, 26, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 73, 21, 73, 28, "ballerina:10", 10, + assertIssue(issues.get(7), documentName, 73, 21, 73, 28, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 77, 21, 77, 28, "ballerina:10", 10, + assertIssue(issues.get(8), documentName, 77, 21, 77, 28, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(9), documentName, 81, 22, 81, 29, "ballerina:10", 10, + assertIssue(issues.get(9), documentName, 81, 22, 81, 29, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(10), documentName, 83, 22, 83, 29, "ballerina:10", 10, + assertIssue(issues.get(10), documentName, 83, 22, 83, 29, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 87, 14, 87, 22, "ballerina:10", 10, + assertIssue(issues.get(11), documentName, 87, 14, 87, 22, "ballerina:12", 12, INVALID_RANGE_EXPRESSION, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index 7469d4b0..e5d588ec 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range expression + ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index 5a9ad308..87990995 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range expression + ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index 5a9ad308..87990995 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range expression + ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 diff --git a/scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal b/scan-command/src/test/resources/test-resources/core-rules/rule012_invalid_range_expression.bal similarity index 100% rename from scan-command/src/test/resources/test-resources/core-rules/rule006_invalid_range_expression.bal rename to scan-command/src/test/resources/test-resources/core-rules/rule012_invalid_range_expression.bal diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index f117eccc..de26732b 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -31,7 +31,7 @@ under the License. - + From 50650969037844d1c282edea07ab75e911db037c Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Sat, 15 Mar 2025 23:59:17 +0530 Subject: [PATCH 6/9] Update the error log --- .../test/resources/command-outputs/unix/list-rules-output.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index 7469d4b0..e5d588ec 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -2,7 +2,7 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:10 | CODE_SMELL | Invalid range expression + ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 From b7e0a0e3ac221820e361161d89fcec5e849e700d Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Sun, 16 Mar 2025 00:06:28 +0530 Subject: [PATCH 7/9] Update rule logs --- .../test/resources/command-outputs/unix/list-rules-output.txt | 2 +- .../resources/command-outputs/unix/print-rules-to-console.txt | 2 +- .../resources/command-outputs/windows/list-rules-output.txt | 2 +- .../command-outputs/windows/print-rules-to-console.txt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index e5d588ec..3c6c4b58 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -2,8 +2,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter + ballerina:12 | CODE_SMELL | Invalid range expression ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index e5d588ec..3c6c4b58 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -2,8 +2,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter + ballerina:12 | CODE_SMELL | Invalid range expression ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index 87990995..2683ed5c 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -2,8 +2,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter + ballerina:12 | CODE_SMELL | Invalid range expression ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index 87990995..2683ed5c 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -2,8 +2,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-proj RuleID | Rule Kind | Rule Description --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:12 | CODE_SMELL | Invalid range expression ballerina:2 | CODE_SMELL | Unused function parameter + ballerina:12 | CODE_SMELL | Invalid range expression ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 From c0c7b0198e20039906611e7769ef13d3a0f375f8 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Sun, 16 Mar 2025 19:07:31 +0530 Subject: [PATCH 8/9] Refactor static code analyzer --- .../scan/internal/StaticCodeAnalyzer.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index 7ce86953..58a602e1 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -23,11 +23,11 @@ import io.ballerina.compiler.api.symbols.ObjectTypeSymbol; import io.ballerina.compiler.api.symbols.Qualifier; import io.ballerina.compiler.api.symbols.Symbol; -import io.ballerina.compiler.syntax.tree.BasicLiteralNode; import io.ballerina.compiler.api.symbols.SymbolKind; import io.ballerina.compiler.api.symbols.TypeDefinitionSymbol; import io.ballerina.compiler.api.symbols.TypeSymbol; import io.ballerina.compiler.syntax.tree.AssignmentStatementNode; +import io.ballerina.compiler.syntax.tree.BasicLiteralNode; import io.ballerina.compiler.syntax.tree.BinaryExpressionNode; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; import io.ballerina.compiler.syntax.tree.ClassDefinitionNode; @@ -40,16 +40,16 @@ import io.ballerina.compiler.syntax.tree.IncludedRecordParameterNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; import io.ballerina.compiler.syntax.tree.Node; -import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.syntax.tree.NodeList; +import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.syntax.tree.NodeVisitor; import io.ballerina.compiler.syntax.tree.ObjectFieldNode; import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; import io.ballerina.compiler.syntax.tree.Token; -import io.ballerina.compiler.syntax.tree.UnaryExpressionNode; import io.ballerina.compiler.syntax.tree.TypeDefinitionNode; +import io.ballerina.compiler.syntax.tree.UnaryExpressionNode; import io.ballerina.projects.Document; import io.ballerina.scan.ScannerContext; import io.ballerina.scan.utils.Constants; @@ -59,8 +59,8 @@ import static io.ballerina.compiler.syntax.tree.SyntaxKind.DOUBLE_DOT_LT_TOKEN; import static io.ballerina.compiler.syntax.tree.SyntaxKind.ELLIPSIS_TOKEN; -import static io.ballerina.compiler.syntax.tree.SyntaxKind.PRIVATE_KEYWORD; import static io.ballerina.compiler.syntax.tree.SyntaxKind.ISOLATED_KEYWORD; +import static io.ballerina.compiler.syntax.tree.SyntaxKind.PRIVATE_KEYWORD; import static io.ballerina.compiler.syntax.tree.SyntaxKind.PUBLIC_KEYWORD; import static io.ballerina.scan.utils.Constants.INIT_FUNCTION; import static io.ballerina.scan.utils.Constants.MAIN_FUNCTION; @@ -104,12 +104,19 @@ public void visit(CheckExpressionNode checkExpressionNode) { } public void visit(BinaryExpressionNode binaryExpressionNode) { + reportIssuesWithTrivialOperations(binaryExpressionNode); + filterSameReferenceIssueBasedOnOperandType(binaryExpressionNode.operator()).ifPresent(rule -> { + checkUsageOfSameOperandInBinaryExpr(binaryExpressionNode.lhsExpr(), + binaryExpressionNode.rhsExpr(), rule, binaryExpressionNode); + }); + SyntaxKind binaryOperatorKind = binaryExpressionNode.operator().kind(); if (binaryOperatorKind.equals(ELLIPSIS_TOKEN) || binaryOperatorKind.equals(DOUBLE_DOT_LT_TOKEN)) { validateRangeExpressionOperator(scannerContext, document, binaryExpressionNode.lhsExpr(), binaryExpressionNode.rhsExpr(), binaryExpressionNode.operator(), binaryExpressionNode.location()); } + this.visitSyntaxNode(binaryExpressionNode); } private void validateRangeExpressionOperator(ScannerContext scannerContext, Document document, @@ -155,13 +162,6 @@ public void visit(ObjectFieldNode objectFieldNode) { }); this.visitSyntaxNode(objectFieldNode); } - public void visit(BinaryExpressionNode binaryExpressionNode) { - reportIssuesWithTrivialOperations(binaryExpressionNode); - filterSameReferenceIssueBasedOnOperandType(binaryExpressionNode.operator()).ifPresent(rule -> { - checkUsageOfSameOperandInBinaryExpr(binaryExpressionNode.lhsExpr(), - binaryExpressionNode.rhsExpr(), rule, binaryExpressionNode); - }); - } @Override public void visit(AssignmentStatementNode assignmentStatementNode) { From ee2592f9df7d1b265504dbc8ba5fa15968fc1667 Mon Sep 17 00:00:00 2001 From: gayaldassanayake Date: Sun, 16 Mar 2025 20:37:51 +0530 Subject: [PATCH 9/9] Fix ballerina plugin version to 12.0 --- .github/workflows/central-publish.yml | 2 +- .github/workflows/full_build.yml | 4 ++-- .github/workflows/publish-release.yml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/central-publish.yml b/.github/workflows/central-publish.yml index 5d9f96c5..58ffc528 100644 --- a/.github/workflows/central-publish.yml +++ b/.github/workflows/central-publish.yml @@ -30,7 +30,7 @@ jobs: - name: Set Up Ballerina uses: ballerina-platform/setup-ballerina@v1.1.0 with: - version: latest + version: 2201.12.0 - name: Build with Gradle env: diff --git a/.github/workflows/full_build.yml b/.github/workflows/full_build.yml index ffcd020b..92e3dfb8 100644 --- a/.github/workflows/full_build.yml +++ b/.github/workflows/full_build.yml @@ -26,7 +26,7 @@ jobs: - name: Set Up Ballerina uses: ballerina-platform/setup-ballerina@v1.1.1 with: - version: latest + version: 2201.12.0 - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -66,7 +66,7 @@ jobs: - name: Set Up Ballerina uses: ballerina-platform/setup-ballerina@v1.1.1 with: - version: latest + version: 2201.12.0 - name: Build with Gradle env: diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index 8b60e959..cacc4da6 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -22,7 +22,7 @@ jobs: - name: Set Up Ballerina uses: ballerina-platform/setup-ballerina@v1.1.0 with: - version: latest + version: 2201.12.0 - name: Set version env variable run: echo "VERSION=$((grep -w "version" | cut -d= -f2) < gradle.properties | rev | cut --complement -d- -f1 | rev)" >> $GITHUB_ENV