• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / #3788

pending completion
#3788

push

github actions

web-flow
Merge pull request #4387 from adangel/pmd7-language-versions

137 of 137 new or added lines in 32 files covered. (100.0%)

67152 of 127777 relevant lines covered (52.55%)

0.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParser.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.ecmascript.ast;
6

7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11

12
import org.mozilla.javascript.CompilerEnvirons;
13
import org.mozilla.javascript.Context;
14
import org.mozilla.javascript.Parser;
15
import org.mozilla.javascript.ast.AstRoot;
16
import org.mozilla.javascript.ast.Comment;
17
import org.mozilla.javascript.ast.ErrorCollector;
18
import org.mozilla.javascript.ast.ParseProblem;
19

20
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
21
import net.sourceforge.pmd.lang.LanguageVersion;
22
import net.sourceforge.pmd.lang.ast.AstInfo;
23
import net.sourceforge.pmd.lang.ast.FileAnalysisException;
24
import net.sourceforge.pmd.lang.ast.ParseException;
25
import net.sourceforge.pmd.lang.ast.RootNode;
26

27
public final class EcmascriptParser implements net.sourceforge.pmd.lang.ast.Parser {
28
    private final LanguagePropertyBundle properties;
29

30
    public EcmascriptParser(LanguagePropertyBundle properties) {
1✔
31
        this.properties = properties;
1✔
32
    }
1✔
33

34
    private AstRoot parseEcmascript(final String sourceCode, final LanguageVersion version, final List<ParseProblem> parseProblems) throws ParseException {
35
        final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
1✔
36
        compilerEnvirons.setRecordingComments(true);
1✔
37
        compilerEnvirons.setRecordingLocalJsDocComments(true);
1✔
38
        compilerEnvirons.setLanguageVersion(determineRhinoLanguageVersion(version));
1✔
39
        // Scope's don't appear to get set right without this
40
        compilerEnvirons.setIdeMode(true);
1✔
41
        compilerEnvirons.setWarnTrailingComma(true);
1✔
42
        // see bug #1150 "EmptyExpression" for valid statements!
43
        compilerEnvirons.setReservedKeywordAsIdentifier(true);
1✔
44

45
        // TODO We should do something with Rhino errors...
46
        final ErrorCollector errorCollector = new ErrorCollector();
1✔
47
        final Parser parser = new Parser(compilerEnvirons, errorCollector);
1✔
48
        // TODO Fix hardcode
49
        final String sourceURI = "unknown";
1✔
50
        final int beginLineno = 1;
1✔
51
        AstRoot astRoot = parser.parse(sourceCode, sourceURI, beginLineno);
1✔
52
        parseProblems.addAll(errorCollector.getErrors());
1✔
53
        return astRoot;
1✔
54
    }
55

56
    private static int determineRhinoLanguageVersion(LanguageVersion version) {
57
        switch (version.getVersion()) {
1✔
58
        case "3": return Context.VERSION_1_5;
×
59
        case "5": return Context.VERSION_1_8;
×
60
        default: return Context.VERSION_ES6;
1✔
61
        }
62
    }
63

64
    @Override
65
    public RootNode parse(ParserTask task) throws FileAnalysisException {
66
        final LanguageVersion version = task.getLanguageVersion();
1✔
67
        final List<ParseProblem> parseProblems = new ArrayList<>();
1✔
68
        final AstRoot astRoot = parseEcmascript(task.getSourceText(), version, parseProblems);
1✔
69
        final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(parseProblems);
1✔
70
        ASTAstRoot tree = (ASTAstRoot) treeBuilder.build(astRoot);
1✔
71

72
        String suppressMarker = properties.getSuppressMarker();
1✔
73
        Map<Integer, String> suppressMap = new HashMap<>();
1✔
74
        if (astRoot.getComments() != null) {
1✔
75
            for (Comment comment : astRoot.getComments()) {
1✔
76
                int nopmd = comment.getValue().indexOf(suppressMarker);
1✔
77
                if (nopmd > -1) {
1✔
78
                    String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
1✔
79
                    suppressMap.put(comment.getLineno(), suppression);
1✔
80
                }
81
            }
1✔
82
        }
83
        tree.setAstInfo(new AstInfo<>(task, tree).withSuppressMap(suppressMap));
1✔
84
        return tree;
1✔
85
    }
86

87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc