Skip to content

Commit

Permalink
Merge pull request #1 from azinneera/sync-2201.0.3-stage-new
Browse files Browse the repository at this point in the history
Revert "Revert "Add missing test compiler plugin diagnostics""
  • Loading branch information
IMS94 authored Apr 18, 2022
2 parents 60580e0 + 95c7237 commit e80b62d
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ private void runTasks(ModuleContext moduleContext, List<Diagnostic> 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,
Expand Down
1 change: 1 addition & 0 deletions project-api/project-api-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diagnostic> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "pramjs"
name = "package_comp_plugin_diagnostic_init_function"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public function doSomething() {
// Do nothing
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "pramjs"
name = "package_plugin_diagnostic_user_1"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pramjs/package_comp_plugin_diagnostic_init_function as _;

public function main() {
}

function foo() {
}

function bar() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pramjs/package_comp_plugin_diagnostic_init_function as _;

function testFoo() {
}

function testYee() {
}
Original file line number Diff line number Diff line change
@@ -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()
}
}


Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit e80b62d

Please sign in to comment.