use of org.mozilla.javascript.ast.ParseProblem in project pmd by pmd.
the class EcmascriptParser method parse.
public EcmascriptNode<AstRoot> parse(final Reader reader) {
try {
final List<ParseProblem> parseProblems = new ArrayList<>();
final String sourceCode = IOUtils.toString(reader);
final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
EcmascriptNode<AstRoot> tree = treeBuilder.build(astRoot);
suppressMap = new HashMap<>();
if (astRoot.getComments() != null) {
for (Comment comment : astRoot.getComments()) {
int nopmd = comment.getValue().indexOf(suppressMarker);
if (nopmd > -1) {
String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
EcmascriptNode<Comment> node = treeBuilder.build(comment);
suppressMap.put(node.getBeginLine(), suppression);
}
}
}
return tree;
} catch (IOException e) {
throw new ParseException(e);
}
}
use of org.mozilla.javascript.ast.ParseProblem in project pmd by pmd.
the class EcmascriptTreeBuilder method handleParseProblems.
private void handleParseProblems(EcmascriptNode<? extends AstNode> node) {
if (node instanceof TrailingCommaNode) {
TrailingCommaNode trailingCommaNode = (TrailingCommaNode) node;
int nodeStart = node.getNode().getAbsolutePosition();
int nodeEnd = nodeStart + node.getNode().getLength() - 1;
for (ParseProblem parseProblem : parseProblems) {
// The node overlaps the comma (i.e. end of the problem)?
int problemStart = parseProblem.getFileOffset();
int commaPosition = problemStart + parseProblem.getLength() - 1;
if (nodeStart <= commaPosition && commaPosition <= nodeEnd) {
if ("Trailing comma is not legal in an ECMA-262 object initializer".equals(parseProblem.getMessage())) {
// Report on the shortest code block containing the
// problem (i.e. inner most code in nested structures).
EcmascriptNode<?> currentNode = (EcmascriptNode<?>) parseProblemToNode.get(parseProblem);
if (currentNode == null || node.getNode().getLength() < currentNode.getNode().getLength()) {
parseProblemToNode.put(parseProblem, trailingCommaNode);
}
}
}
}
}
}
Aggregations