From 18df40b9c2c460c38b7c6ec01d420ed579e5d84f Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Mon, 6 May 2024 18:35:17 +0530 Subject: [PATCH 1/3] Fix document lookup in compiler plugin --- .../compiler/XmldataRecordFieldValidator.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java index 761e7a3..c970452 100644 --- a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java @@ -47,6 +47,7 @@ import io.ballerina.compiler.syntax.tree.VariableDeclarationNode; import io.ballerina.lib.data.xmldata.compiler.objects.QualifiedName; import io.ballerina.projects.Document; +import io.ballerina.projects.Module; import io.ballerina.projects.plugins.AnalysisTask; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; import io.ballerina.tools.diagnostics.Diagnostic; @@ -76,7 +77,16 @@ public class XmldataRecordFieldValidator implements AnalysisTask diagnostics = semanticModel.diagnostics(); boolean erroneousCompilation = diagnostics.stream() .anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)); From 120e34ac5c857f5f480607524e57509e6c4d4d06 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Mon, 6 May 2024 19:16:19 +0530 Subject: [PATCH 2/3] Refactor typesymbol lookup --- .../compiler/XmldataRecordFieldValidator.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java index c970452..d2ad798 100644 --- a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java @@ -46,8 +46,6 @@ import io.ballerina.compiler.syntax.tree.TypeDefinitionNode; import io.ballerina.compiler.syntax.tree.VariableDeclarationNode; import io.ballerina.lib.data.xmldata.compiler.objects.QualifiedName; -import io.ballerina.projects.Document; -import io.ballerina.projects.Module; import io.ballerina.projects.plugins.AnalysisTask; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; import io.ballerina.tools.diagnostics.Diagnostic; @@ -71,22 +69,11 @@ public class XmldataRecordFieldValidator implements AnalysisTask { private SemanticModel semanticModel; - private Document srcFile; private final HashMap allDiagnosticInfo = new HashMap<>(); @Override public void perform(SyntaxNodeAnalysisContext ctx) { semanticModel = ctx.semanticModel(); - for (Module module : ctx.currentPackage().modules()) { - if (module.moduleId().equals(ctx.documentId().moduleId())) { - srcFile = module.document(ctx.documentId()); - break; - } - } - if (srcFile == null) { - return; - } - List diagnostics = semanticModel.diagnostics(); boolean erroneousCompilation = diagnostics.stream() .anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)); @@ -268,15 +255,16 @@ private void detectNameAnnotUsageWarning(RecordFieldSymbol fieldSymbol, SyntaxNo return; } - Optional symbol = semanticModel.symbol(srcFile, location.get().lineRange().startLine()); - if (symbol.isEmpty()) { + TypeSymbol typeSymbol = fieldSymbol.typeDescriptor(); + if (typeSymbol.typeKind() != TypeDescKind.TYPE_REFERENCE) { return; } - - if (symbol.get().kind() != SymbolKind.TYPE_DEFINITION) { + TypeReferenceTypeSymbol typeReferenceTypeSymbol = (TypeReferenceTypeSymbol) typeSymbol; + Symbol symbol = typeReferenceTypeSymbol.definition(); + if (symbol == null || symbol.kind() != SymbolKind.TYPE_DEFINITION) { return; } - TypeDefinitionSymbol typeDefinitionSymbol = (TypeDefinitionSymbol) symbol.get(); + TypeDefinitionSymbol typeDefinitionSymbol = (TypeDefinitionSymbol) symbol; typeDefinitionSymbol.annotations().forEach(annotationSymbol -> { if (!isAnnotFromXmldata(annotationSymbol)) { return; From f501c650eaa25b82205c18889d8a926be3aa4ad0 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Mon, 6 May 2024 20:02:37 +0530 Subject: [PATCH 3/3] Add compiler plugin test for sub module in project --- .../xmldata/compiler/CompilerPluginTest.java | 19 +++++++++++++++++++ .../sample_package_10/Ballerina.toml | 4 ++++ .../sample_package_10/main.bal | 15 +++++++++++++++ .../sample_package_10/modules/util/util.bal | 9 +++++++++ 4 files changed, 47 insertions(+) create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/Ballerina.toml create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/main.bal create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/modules/util/util.bal diff --git a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java index 69699c9..956519d 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java @@ -178,4 +178,23 @@ public void testUnionTypeNegative() { Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(), "invalid field: duplicate field found"); } + + @Test + public void testCompilerPluginWithAProjectWithSubModule() { + DiagnosticResult diagnosticResult = + CompilerPluginTestUtils.loadPackage("sample_package_10").getCompilation().diagnosticResult(); + List errorDiagnosticsList = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) + .collect(Collectors.toList()); + Assert.assertEquals(errorDiagnosticsList.size(), 1); + Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + "invalid field: duplicate field found"); + + List warningDiagnosticsList = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.WARNING)) + .collect(Collectors.toList()); + Assert.assertEquals(warningDiagnosticsList.size(), 1); + Assert.assertEquals(warningDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + "invalid annotation attachment: child record does not allow name annotation"); + } } diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/Ballerina.toml b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/Ballerina.toml new file mode 100644 index 0000000..11869cd --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "xmldata_test" +name = "sample_10" +version = "0.1.0" diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/main.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/main.bal new file mode 100644 index 0000000..393821c --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/main.bal @@ -0,0 +1,15 @@ +import ballerina/data.xmldata; + +@xmldata:Name { + value: "Data" +} +type Data3 record {| + Data4 a; +|}; + +@xmldata:Name { + value: "A" +} +type Data4 record {| + string b; +|}; diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/modules/util/util.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/modules/util/util.bal new file mode 100644 index 0000000..989563e --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_10/modules/util/util.bal @@ -0,0 +1,9 @@ +import ballerina/data.xmldata; + +type Data record {| + string A; + @xmldata:Name { + value: "A" + } + string B; +|};