Search in sources :

Example 1 with ParseProblem

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);
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseProblem(org.mozilla.javascript.ast.ParseProblem) ParseException(net.sourceforge.pmd.lang.ast.ParseException) AstRoot(org.mozilla.javascript.ast.AstRoot)

Example 2 with ParseProblem

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);
                    }
                }
            }
        }
    }
}
Also used : ParseProblem(org.mozilla.javascript.ast.ParseProblem)

Aggregations

ParseProblem (org.mozilla.javascript.ast.ParseProblem)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ParseException (net.sourceforge.pmd.lang.ast.ParseException)1 AstRoot (org.mozilla.javascript.ast.AstRoot)1 Comment (org.mozilla.javascript.ast.Comment)1