Search in sources :

Example 11 with Node

use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.

the class PageCountLinesTest method testCountLinesHtmlFile.

@Test
public void testCountLinesHtmlFile() {
    List<Node> nodeList = lexer.parse(readFile("checks/AvoidHtmlCommentCheck/document.html"));
    HtmlSourceCode htmlSourceCode = createHtmlSourceCode("test/document.html");
    scanner.scan(nodeList, htmlSourceCode);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(8);
    assertThat(htmlSourceCode.getDetailedLinesOfCode()).containsOnly(1, 2, 3, 4, 6, 7, 8, 9);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(1);
}
Also used : Node(org.sonar.plugins.html.node.Node) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) Test(org.junit.Test)

Example 12 with Node

use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.

the class PageCountLines method count.

private void count(List<Node> nodeList) {
    for (int i = 0; i < nodeList.size(); i++) {
        Node node = nodeList.get(i);
        Node previousNode = i > 0 ? nodeList.get(i - 1) : null;
        Node nextNode = i < nodeList.size() - 1 ? nodeList.get(i + 1) : null;
        handleToken(node, previousNode, nextNode);
    }
    addMeasures();
}
Also used : Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode)

Example 13 with Node

use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.

the class ElementTokenizer method parseNestedTag.

/**
 * Parse a nested tag with PageLexer.
 * The nested tag is added as an attribute to its parent element.
 */
private static void parseNestedTag(CodeReader codeReader, TagNode element) {
    PageLexer nestedPageLexer = new PageLexer();
    List<Node> nodeList = nestedPageLexer.nestedParse(codeReader);
    // add the nested tags as attribute.
    for (Node node : nodeList) {
        element.getAttributes().add(new Attribute(node.getCode()));
    }
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute) Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode)

Example 14 with Node

use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.

the class VueLexer method parse.

@Override
public List<Node> parse(Reader reader) {
    List<Node> nodes = super.parse(reader);
    boolean firstTemplateTag = true;
    List<Node> templateNodes = new LinkedList<>();
    Deque<Object> templateLevels = new LinkedList<>();
    for (Node node : nodes) {
        if (node.getNodeType() == NodeType.TAG) {
            TagNode tagNode = (TagNode) node;
            if (tagNode.equalsElementName(TEMPLATE)) {
                if (tagNode.isEndElement()) {
                    if (!templateLevels.isEmpty()) {
                        templateLevels.pop();
                        if (templateLevels.isEmpty()) {
                            break;
                        }
                    }
                } else if (!tagNode.hasEnd()) {
                    templateLevels.push(TEMPLATE_LEVEL);
                }
            }
        }
        if (!templateLevels.isEmpty()) {
            if (firstTemplateTag) {
                firstTemplateTag = false;
            } else {
                templateNodes.add(node);
            }
        }
    }
    return templateNodes;
}
Also used : Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode) LinkedList(java.util.LinkedList) TagNode(org.sonar.plugins.html.node.TagNode)

Example 15 with Node

use of org.sonar.plugins.html.node.Node in project sonar-web by SonarSource.

the class PageCountLinesTest method testCountLines.

@Test
public void testCountLines() {
    List<Node> nodeList = lexer.parse(readFile("src/main/webapp/user-properties.jsp"));
    assertThat(nodeList.size()).isGreaterThan(100);
    HtmlSourceCode htmlSourceCode = createHtmlSourceCode("test/user-properties.jsp");
    scanner.scan(nodeList, htmlSourceCode);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(224);
    assertThat(htmlSourceCode.getDetailedLinesOfCode().size()).isEqualTo(224);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(14);
}
Also used : Node(org.sonar.plugins.html.node.Node) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) Test(org.junit.Test)

Aggregations

Node (org.sonar.plugins.html.node.Node)39 Test (org.junit.Test)28 TagNode (org.sonar.plugins.html.node.TagNode)24 StringReader (java.io.StringReader)23 TextNode (org.sonar.plugins.html.node.TextNode)21 CommentNode (org.sonar.plugins.html.node.CommentNode)19 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)19 FileReader (java.io.FileReader)4 Attribute (org.sonar.plugins.html.node.Attribute)3 HtmlSourceCode (org.sonar.plugins.html.visitor.HtmlSourceCode)3 ArrayList (java.util.ArrayList)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 CodeReader (org.sonar.channel.CodeReader)2 File (java.io.File)1 Reader (java.io.Reader)1 ArrayDeque (java.util.ArrayDeque)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1