Cleanup javascript builder

This commit is contained in:
Clément Fournier
2020-04-22 07:34:24 +02:00
parent 8f2bda4934
commit 959b70fabd
2 changed files with 14 additions and 7 deletions

View File

@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.ecmascript.ast;
import org.mozilla.javascript.ast.AstNode;
import net.sourceforge.pmd.lang.ast.AstVisitor;
import net.sourceforge.pmd.lang.ast.SourceCodePositioner;
import net.sourceforge.pmd.lang.ast.impl.AbstractNode;
import net.sourceforge.pmd.util.document.FileLocation;
import net.sourceforge.pmd.util.document.TextDocument;
@ -18,6 +17,7 @@ abstract class AbstractEcmascriptNode<T extends AstNode> extends AbstractNode<Ab
protected final T node;
private String image;
private TextDocument textDocument;
private int absPos;
AbstractEcmascriptNode(T node) {
this.node = node;
@ -38,13 +38,16 @@ abstract class AbstractEcmascriptNode<T extends AstNode> extends AbstractNode<Ab
}
/* package private */
void calculateLineNumbers(TextDocument positioner) {
int calculateAbsolutePos(TextDocument positioner, int parentRelPos) {
this.textDocument = positioner;
int absPos = parentRelPos + node.getPosition();
this.absPos = absPos;
return absPos;
}
@Override
public FileLocation getReportLocation() {
return textDocument.toLocation(TextRegion.fromOffsetLength(node.getAbsolutePosition(), node.getLength()));
return textDocument.toLocation(TextRegion.fromOffsetLength(absPos, node.getLength()));
}
@Override

View File

@ -171,7 +171,7 @@ final class EcmascriptTreeBuilder implements NodeVisitor {
public <T extends AstNode> EcmascriptNode<T> build(T astNode) {
EcmascriptNode<T> node = buildInternal(astNode);
calculateLineNumbers(node);
calculateLineNumbers(node, astNode.getAbsolutePosition());
// Set all the trailing comma nodes
for (AbstractEcmascriptNode<?> trailingCommaNode : parseProblemToNode.values()) {
@ -231,7 +231,7 @@ final class EcmascriptTreeBuilder implements NodeVisitor {
if (trailingCommaLocalizedMessage.equals(parseProblem.getMessage())) {
// Report on the shortest code block containing the
// problem (i.e. inner most code in nested structures).
AbstractEcmascriptNode<?> currentNode = (AbstractEcmascriptNode<?>) parseProblemToNode.get(parseProblem);
AbstractEcmascriptNode<?> currentNode = parseProblemToNode.get(parseProblem);
if (currentNode == null || node.node.getLength() < currentNode.node.getLength()) {
parseProblemToNode.put(parseProblem, node);
}
@ -241,7 +241,11 @@ final class EcmascriptTreeBuilder implements NodeVisitor {
}
}
private void calculateLineNumbers(EcmascriptNode<?> node) {
node.descendantsOrSelf().forEach(n -> ((AbstractEcmascriptNode<?>) n).calculateLineNumbers(textDocument));
private void calculateLineNumbers(EcmascriptNode<?> node, int parentAbsPos) {
int absPos = ((AbstractEcmascriptNode<?>) node).calculateAbsolutePos(textDocument, parentAbsPos);
for (EcmascriptNode<?> child : node.children()) {
((AbstractEcmascriptNode<?>) child).calculateAbsolutePos(textDocument, absPos);
}
}
}