Skip to content

Commit

Permalink
Merge pull request #19 from prakanth97/fix_compilerPlugin
Browse files Browse the repository at this point in the history
Fix document lookup in compiler plugin
  • Loading branch information
prakanth97 authored May 6, 2024
2 parents 70a02bb + 28198d4 commit 0e66df4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diagnostic> 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<Diagnostic> 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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "xmldata_test"
name = "sample_10"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -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;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ballerina/data.xmldata;

type Data record {|
string A;
@xmldata:Name {
value: "A"
}
string B;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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.plugins.AnalysisTask;
import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext;
import io.ballerina.tools.diagnostics.Diagnostic;
Expand All @@ -72,13 +71,11 @@
public class XmldataRecordFieldValidator implements AnalysisTask<SyntaxNodeAnalysisContext> {

private SemanticModel semanticModel;
private Document srcFile;
private final HashMap<Location, DiagnosticInfo> allDiagnosticInfo = new HashMap<>();

@Override
public void perform(SyntaxNodeAnalysisContext ctx) {
semanticModel = ctx.semanticModel();
srcFile = ctx.currentPackage().getDefaultModule().document(ctx.documentId());
List<Diagnostic> diagnostics = semanticModel.diagnostics();
boolean erroneousCompilation = diagnostics.stream()
.anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR));
Expand Down Expand Up @@ -260,15 +257,16 @@ private void detectNameAnnotUsageWarning(RecordFieldSymbol fieldSymbol, SyntaxNo
return;
}

Optional<Symbol> 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;
Expand Down

0 comments on commit 0e66df4

Please sign in to comment.