-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add core analyzer with analysis report generation
- Loading branch information
Showing
31 changed files
with
1,467 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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; | ||
|
||
import io.ballerina.projects.Document; | ||
import io.ballerina.tools.diagnostics.Location; | ||
|
||
/** | ||
* {@code Reporter} represents the reporter used for reporting {@link Issue} instances during static code analysis. | ||
* | ||
* @since 0.1.0 | ||
* */ | ||
public interface Reporter { | ||
/** | ||
* Reports an issue identified during static code analysis with the use of the numeric identifier of the rule. | ||
* | ||
* @param reportedDocument the Ballerina document for which the issue is reported | ||
* @param location location of reported issue | ||
* @param ruleId numeric identifier of the violated static code analysis rule | ||
* */ | ||
void reportIssue(Document reportedDocument, Location location, int ruleId); | ||
|
||
/** | ||
* Reports an issue identified during static code analysis with the use of the rule. | ||
* | ||
* @param reportedDocument the Ballerina document for which the issue is reported | ||
* @param location location of reported issue | ||
* @param rule static code analysis rule | ||
* */ | ||
void reportIssue(Document reportedDocument, Location location, Rule rule); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ScanCommand/src/main/java/io/ballerina/scan/ScannerContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* {@code ScannerContext} represents a context that exposes properties required by scanner plugins from the scan tool. | ||
* | ||
* @since 0.1.0 | ||
* */ | ||
public interface ScannerContext { | ||
/** | ||
* Returns the {@link Reporter} to be used to report identified issues. | ||
* | ||
* @return reporter that needs to be used to report issues identified. | ||
* */ | ||
Reporter getReporter(); | ||
} |
30 changes: 30 additions & 0 deletions
30
ScanCommand/src/main/java/io/ballerina/scan/exceptions/RuleNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.exceptions; | ||
|
||
/** | ||
* Represents the exception thrown for an unidentified static code analysis rule during reporting of an analysis issue. | ||
* | ||
* @since 0.1.0 | ||
* */ | ||
public class RuleNotFoundException extends RuntimeException { | ||
public RuleNotFoundException(int ruleId) { | ||
super(String.format("Rule not found: Invalid rule numeric identifier '%d'.", ruleId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ScanCommand/src/main/java/io/ballerina/scan/internal/IssueImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.scan.Issue; | ||
import io.ballerina.scan.Rule; | ||
import io.ballerina.scan.Source; | ||
import io.ballerina.tools.diagnostics.Location; | ||
import io.ballerina.tools.text.LineRange; | ||
import io.ballerina.tools.text.TextRange; | ||
import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation; | ||
|
||
/** | ||
* Represents the implementation of the {@link Issue} interface. | ||
* | ||
* @since 0.1.0 | ||
* */ | ||
public class IssueImpl implements Issue { | ||
private final BLangDiagnosticLocation location; | ||
private final RuleImpl rule; | ||
private final Source source; | ||
private final String fileName; | ||
private final String filePath; | ||
|
||
IssueImpl(Location location, | ||
Rule rule, | ||
Source source, | ||
String fileName, | ||
String filePath) { | ||
LineRange lineRange = location.lineRange(); | ||
TextRange textRange = location.textRange(); | ||
this.location = new BLangDiagnosticLocation(lineRange.fileName(), lineRange.startLine().line(), | ||
lineRange.endLine().line(), lineRange.startLine().offset(), lineRange.endLine().offset(), | ||
textRange.startOffset(), textRange.length()); | ||
this.rule = (RuleImpl) rule; | ||
this.source = source; | ||
this.fileName = fileName; | ||
this.filePath = filePath; | ||
} | ||
|
||
@Override | ||
public Location location() { | ||
return location; | ||
} | ||
|
||
@Override | ||
public Rule rule() { | ||
return rule; | ||
} | ||
|
||
@Override | ||
public Source source() { | ||
return source; | ||
} | ||
|
||
public String filePath() { | ||
return filePath; | ||
} | ||
|
||
public String fileName() { | ||
return fileName; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
ScanCommand/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.projects.DocumentId; | ||
import io.ballerina.projects.Module; | ||
import io.ballerina.projects.Project; | ||
import io.ballerina.scan.Issue; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import static io.ballerina.scan.internal.InbuiltRules.INBUILT_RULES; | ||
|
||
/** | ||
* Represents the project analyzer used for analyzing projects. | ||
* | ||
* @since 0.1.0 | ||
* */ | ||
class ProjectAnalyzer { | ||
List<Issue> analyze(Project project) { | ||
ScannerContextImpl scannerContext = new ScannerContextImpl(INBUILT_RULES); | ||
project.currentPackage().moduleIds().forEach(moduleId -> { | ||
Module module = project.currentPackage().module(moduleId); | ||
module.documentIds().forEach(analyzeDocument(module, scannerContext)); | ||
module.testDocumentIds().forEach(analyzeDocument(module, scannerContext)); | ||
}); | ||
return scannerContext.getReporter().getIssues(); | ||
} | ||
|
||
private Consumer<DocumentId> analyzeDocument(Module module, ScannerContextImpl scannerContext) { | ||
return documentId -> { | ||
Document document = module.document(documentId); | ||
StaticCodeAnalyzer analyzer = new StaticCodeAnalyzer(document, scannerContext); | ||
analyzer.analyze(); | ||
}; | ||
} | ||
} |
Oops, something went wrong.