diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/SyntaxNodeAnalysisTaskRunner.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/SyntaxNodeAnalysisTaskRunner.java index 31fd0518d801..1120c06c7fb9 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/SyntaxNodeAnalysisTaskRunner.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/SyntaxNodeAnalysisTaskRunner.java @@ -59,6 +59,12 @@ private void runTasks(ModuleContext moduleContext, List reportedDiag runTasks(documentContext.syntaxTree(), moduleContext.moduleId(), srcDocumentId, reportedDiagnostics); } + + for (DocumentId testSrcDocumentId : moduleContext.testSrcDocumentIds()) { + DocumentContext testSrcDocumentContext = moduleContext.documentContext(testSrcDocumentId); + runTasks(testSrcDocumentContext.syntaxTree(), moduleContext.moduleId(), + testSrcDocumentId, reportedDiagnostics); + } } private void runTasks(SyntaxTree syntaxTree, diff --git a/project-api/project-api-test/build.gradle b/project-api/project-api-test/build.gradle index d9fe5a21af06..7bd6210b0e86 100644 --- a/project-api/project-api-test/build.gradle +++ b/project-api/project-api-test/build.gradle @@ -57,6 +57,7 @@ dependencies { compilerPluginJar project(':project-api-test-artifact:init-function-codegen-compiler-plugin') compilerPluginJar project(':project-api-test-artifact:init-function-code-modify-compiler-plugin') compilerPluginJar project(':compiler-plugins:package-semantic-analyzer') + compilerPluginJar project(':project-api-test-artifact:init-function-diagnostic-compiler-plugin') testRuntime project(":ballerina-lang-test") balRt project(':ballerina-rt') diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java index ac0a0176307a..1b85d8fe9fa3 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java @@ -397,6 +397,28 @@ public void testCodeAnalyzerCompilerPluginForTestSources() { "INFO [tests/test.bal:(6:1,6:9)] testYee"); } + @Test + public void testCodeAnalyzerCompilerPluginForTestSources() { + Package currentPackage = loadPackage("package_plugin_diagnostic_user_1"); + // Check whether there are any diagnostics + DiagnosticResult diagnosticResult = currentPackage.getCompilation().diagnosticResult(); + diagnosticResult.diagnostics().forEach(OUT::println); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 5, + "Unexpected number of compilation diagnostics"); + + Iterator diagnosticIterator = diagnosticResult.diagnostics().iterator(); + Assert.assertEquals(diagnosticIterator.next().toString(), + "INFO [main.bal:(3:8,3:16)] main"); + Assert.assertEquals(diagnosticIterator.next().toString(), + "INFO [main.bal:(6:1,6:9)] foo"); + Assert.assertEquals(diagnosticIterator.next().toString(), + "INFO [main.bal:(9:1,9:9)] bar"); + Assert.assertEquals(diagnosticIterator.next().toString(), + "INFO [tests/test.bal:(3:1,3:9)] testFoo"); + Assert.assertEquals(diagnosticIterator.next().toString(), + "INFO [tests/test.bal:(6:1,6:9)] testYee"); + } + public void assertDiagnostics(Package currentPackage) { // Check whether there are any diagnostics DiagnosticResult diagnosticResult = currentPackage.getCompilation().diagnosticResult(); diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/Ballerina.toml new file mode 100644 index 000000000000..e597093fb142 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "pramjs" +name = "package_comp_plugin_diagnostic_init_function" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/CompilerPlugin.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/CompilerPlugin.toml new file mode 100644 index 000000000000..9f6ca5103310 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/CompilerPlugin.toml @@ -0,0 +1,8 @@ +[plugin] +class = "io.samjs.plugins.init.functiondiagnostic.DiagnosticFunctionPlugin" + +[[dependency]] +path = "../../../../../build/compiler-plugin-jars/init-function-diagnostic-compiler-plugin-1.0.0.jar" + +[[dependency]] +path = "../../../../../build/compiler-plugin-jars/diagnostic-utils-lib-1.0.0.jar" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/main.bal new file mode 100644 index 000000000000..41a101b1c439 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_comp_plugin_diagnostic_init_function/main.bal @@ -0,0 +1,3 @@ +public function doSomething() { + // Do nothing +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/Ballerina.toml new file mode 100644 index 000000000000..9b67e3c35815 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "pramjs" +name = "package_plugin_diagnostic_user_1" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/main.bal new file mode 100644 index 000000000000..9dec1f3801bf --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/main.bal @@ -0,0 +1,10 @@ +import pramjs/package_comp_plugin_diagnostic_init_function as _; + +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/tests/test.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/tests/test.bal new file mode 100644 index 000000000000..e4cf6cf1b7cc --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_diagnostic_user_1/tests/test.bal @@ -0,0 +1,7 @@ +import pramjs/package_comp_plugin_diagnostic_init_function as _; + +function testFoo() { +} + +function testYee() { +} diff --git a/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/build.gradle b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/build.gradle new file mode 100644 index 000000000000..037b8158d0f2 --- /dev/null +++ b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/build.gradle @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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. + * + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Bal File Diagnostic' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') + implementation project(':project-api-test-artifact:diagnostic-utils-lib') +} + +ext.moduleName = 'compiler.plugin.test.init.balfilediagnostic' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} + + diff --git a/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/io/samjs/plugins/init/functiondiagnostic/DiagnosticFunctionPlugin.java b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/io/samjs/plugins/init/functiondiagnostic/DiagnosticFunctionPlugin.java new file mode 100644 index 000000000000..ce83490bc48e --- /dev/null +++ b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/io/samjs/plugins/init/functiondiagnostic/DiagnosticFunctionPlugin.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * 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. + */ +package io.samjs.plugins.init.functiondiagnostic; + +import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.CodeAnalysisContext; +import io.ballerina.projects.plugins.CodeAnalyzer; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.tools.diagnostics.Diagnostic; +import io.ballerina.tools.diagnostics.DiagnosticSeverity; +import io.samjs.jarlibrary.diagnosticutils.DiagnosticUtils; + +/** + * A sample {@code CompilerPlugin} that adding diagnostic for each function definition. + * + * @since 2201.0.4 + */ +public class DiagnosticFunctionPlugin extends CompilerPlugin { + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeAnalyzer(new BalFileNodeAnalyzer()); + } + + /** + * A sample {@code CodeAnalyzer} that adding diagnostic for each function definition. + * + * @since 2201.0.4 + */ + public static class BalFileNodeAnalyzer extends CodeAnalyzer { + + @Override + public void init(CodeAnalysisContext analysisContext) { + analysisContext.addSyntaxNodeAnalysisTask(syntaxNodeAnalysisContext -> { + FunctionDefinitionNode funcDefNode = (FunctionDefinitionNode) syntaxNodeAnalysisContext.node(); + // Report a test diagnostic + Diagnostic diagnostic = DiagnosticUtils.createDiagnostic("CODEGEN_PLUGIN_FUNCTION", + funcDefNode.functionName().text(), + funcDefNode.functionKeyword().location(), DiagnosticSeverity.INFO); + syntaxNodeAnalysisContext.reportDiagnostic(diagnostic); + }, SyntaxKind.FUNCTION_DEFINITION); + } + } +} diff --git a/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/module-info.java b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/module-info.java new file mode 100644 index 000000000000..6e53c42f939b --- /dev/null +++ b/project-api/test-artifacts/init-function-diagnostic-compiler-plugin/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module compiler.plugin.test.init.functiondiagnostic { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + requires compiler.plugin.test.diagnostic.utils.lib; + + exports io.samjs.plugins.init.functiondiagnostic; +} diff --git a/settings.gradle b/settings.gradle index bf1925d9e8c7..13b8e2778a8b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -120,6 +120,7 @@ include(':project-api-test-artifact:logging-file-appender-plugin') include(':project-api-test-artifact:init-function-codegen-compiler-plugin') include(':project-api-test-artifact:init-function-code-modify-compiler-plugin') include(':project-api-test-artifact:simple-code-gen-plugin-with-resource-gen') +include(':project-api-test-artifact:init-function-diagnostic-compiler-plugin') include(':identifier-util') //include(':ballerina-libs') @@ -143,6 +144,7 @@ project(':project-api-test-artifact:logging-file-appender-plugin').projectDir = project(':project-api-test-artifact:init-function-codegen-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-codegen-compiler-plugin') project(':project-api-test-artifact:init-function-code-modify-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-code-modify-compiler-plugin') project(':project-api-test-artifact:simple-code-gen-plugin-with-resource-gen').projectDir = file('project-api/test-artifacts/simple-code-gen-plugin-with-resource-gen') +project(':project-api-test-artifact:init-function-diagnostic-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-diagnostic-compiler-plugin') project(':ballerina-lang:internal').projectDir = file('langlib/lang.__internal') project(':ballerina-lang:annotations').projectDir = file('langlib/lang.annotations') project(':ballerina-lang:jballerina.java').projectDir = file('langlib/jballerina.java')