Skip to content

Commit

Permalink
MethodReturnMatchesDocCheck points to the actual type-doc part
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Jul 9, 2023
1 parent 226394e commit 12a84a1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changes
- Add type hierarchy provider to magik-language-server.
- Add inlay hint provider to magik-language-server.
- Add code actions for @parameter and @return type-doc parts.
- MethodReturnMatchesDocCheck points to the actual type-doc part.
- Various small fixes.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public String toString() {
return String.format(
"%s@%s(%s, %s)",
this.getClass().getName(), Integer.toHexString(this.hashCode()),
this.getPakkage(), this.getString());
this.currentPackage, this.string);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package nl.ramsolutions.sw.magik.typedchecks.checks;

import com.sonar.sslr.api.AstNode;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.Map;
import nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper;
import nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper;
import nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner;
import nl.ramsolutions.sw.magik.analysis.typing.TypeReader;
import nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType;
import nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult;
import nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResultString;
import nl.ramsolutions.sw.magik.analysis.typing.types.TypeString;
import nl.ramsolutions.sw.magik.api.TypeDocGrammar;
import nl.ramsolutions.sw.magik.parser.TypeDocParser;
import nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck;
import nl.ramsolutions.sw.magik.utils.StreamUtils;

/**
* Check to check if the @return types from method doc matches the reasoned return types.
Expand All @@ -27,30 +29,45 @@ protected void walkPostMethodDefinition(final AstNode node) {
return;
}

final ExpressionResult docExpressionResult = this.extractMethodDocResult(node);
final ExpressionResult reasonedReturnResult = this.extractReasonedResult(node);
if (!docExpressionResult.equals(reasonedReturnResult)) {
final String docTypesStr = docExpressionResult.getTypeNames(", ");
final String reasonedTypesStr = reasonedReturnResult.getTypeNames(", ");
final String message = String.format(MESSAGE, docTypesStr, reasonedTypesStr);
this.addIssue(node, message);
}
final ExpressionResultString methodResultString = this.extractReasonedResult(node);
final Map<AstNode, TypeString> typeDocNodes = this.extractMethodDocResult(node);
final ITypeKeeper typeKeeper = this.getTypeKeeper();
final TypeReader typeReader = new TypeReader(typeKeeper);
StreamUtils.zip(methodResultString.stream(), typeDocNodes.entrySet().stream())
.forEach(entry -> {
if (entry.getKey() == null
|| entry.getValue() == null) {
// Only bother use with type-doc returns.
return;
}

final TypeString methodReturnTypeString = entry.getKey();
final AbstractType methodReturnType = typeReader.parseTypeString(methodReturnTypeString);

final Map.Entry<AstNode, TypeString> typeDocEntry = entry.getValue();
final TypeString docReturnTypeString = typeDocEntry.getValue();
final AbstractType docReturnType = typeReader.parseTypeString(docReturnTypeString);

if (!methodReturnType.equals(docReturnType)) {
final String message = String.format(MESSAGE, docReturnTypeString, methodReturnTypeString);
final AstNode returnTypeNode = typeDocEntry.getKey();
final AstNode typeValueNode = returnTypeNode.getFirstChild(TypeDocGrammar.TYPE_VALUE);
this.addIssue(typeValueNode, message);
}
});
}

private ExpressionResult extractReasonedResult(final AstNode node) {
private ExpressionResultString extractReasonedResult(final AstNode node) {
final LocalTypeReasoner reasoner = this.getReasoner();
ExpressionResult result = reasoner.getNodeTypeSilent(node);
return Objects.requireNonNullElse(result, new ExpressionResult());
final ExpressionResult result = reasoner.getNodeType(node);
return result.stream()
.map(AbstractType::getTypeString)
.collect(ExpressionResultString.COLLECTOR);
}

private ExpressionResult extractMethodDocResult(final AstNode node) {
private Map<AstNode, TypeString> extractMethodDocResult(final AstNode node) {
final TypeDocParser docParser = new TypeDocParser(node);
final ITypeKeeper typeKeeper = this.getTypeKeeper();
final TypeReader typeParser = new TypeReader(typeKeeper);
final List<AbstractType> docReturnTypes = docParser.getReturnTypes().stream()
.map(typeParser::parseTypeString)
.collect(Collectors.toList());
return new ExpressionResult(docReturnTypes);
return docParser.getReturnTypeNodes();
}

}

0 comments on commit 12a84a1

Please sign in to comment.