Search in sources :

Example 1 with PageLexer

use of org.sonar.plugins.html.lex.PageLexer in project sonar-web by SonarSource.

the class HeaderCheckTest method scanWithWrongInputFile.

public static void scanWithWrongInputFile(File file, DefaultNodeVisitor visitor) {
    HtmlAstScanner walker = new HtmlAstScanner(Collections.emptyList());
    walker.addVisitor(visitor);
    FileReader reader;
    try {
        reader = new FileReader(file);
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to read file");
    }
    HtmlSourceCode result = new HtmlSourceCode(new TestInputFileBuilder("key", /* wrong path */
    ".").setLanguage(HtmlConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()).build());
    walker.scan(new PageLexer().parse(reader), // won't be able to resolve the file
    result);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) PageLexer(org.sonar.plugins.html.lex.PageLexer) FileReader(java.io.FileReader) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.html.visitor.HtmlAstScanner) ExpectedException(org.junit.rules.ExpectedException)

Example 2 with PageLexer

use of org.sonar.plugins.html.lex.PageLexer in project sonar-web by SonarSource.

the class TestHelper method scan.

public static HtmlSourceCode scan(File file, DefaultNodeVisitor visitor) {
    FileReader fileReader;
    try {
        fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    }
    HtmlSourceCode result = new HtmlSourceCode(new TestInputFileBuilder("key", file.getPath()).setLanguage(HtmlConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()).setCharset(StandardCharsets.UTF_8).build());
    HtmlAstScanner walker = new HtmlAstScanner(Arrays.asList(new PageCountLines(), new ComplexityVisitor()));
    walker.addVisitor(visitor);
    walker.scan(new PageLexer().parse(fileReader), result);
    return result;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) PageLexer(org.sonar.plugins.html.lex.PageLexer) ComplexityVisitor(org.sonar.plugins.html.analyzers.ComplexityVisitor) FileNotFoundException(java.io.FileNotFoundException) PageCountLines(org.sonar.plugins.html.analyzers.PageCountLines) FileReader(java.io.FileReader) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.html.visitor.HtmlAstScanner)

Example 3 with PageLexer

use of org.sonar.plugins.html.lex.PageLexer in project sonar-web by SonarSource.

the class PageCountLinesTest method setUp.

@Before
public void setUp() {
    lexer = new PageLexer();
    scanner = new HtmlAstScanner(Collections.emptyList());
    scanner.addVisitor(new PageCountLines());
}
Also used : PageLexer(org.sonar.plugins.html.lex.PageLexer) HtmlAstScanner(org.sonar.plugins.html.visitor.HtmlAstScanner) Before(org.junit.Before)

Example 4 with PageLexer

use of org.sonar.plugins.html.lex.PageLexer in project sonar-web by SonarSource.

the class HtmlSensor method execute.

@Override
public void execute(SensorContext sensorContext) {
    FileSystem fileSystem = sensorContext.fileSystem();
    // configure page scanner and the visitors
    final HtmlAstScanner scanner = setupScanner(sensorContext);
    FilePredicates predicates = fileSystem.predicates();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.and(predicates.hasType(InputFile.Type.MAIN), predicates.or(predicates.hasLanguages(HtmlConstants.LANGUAGE_KEY, HtmlConstants.JSP_LANGUAGE_KEY), predicates.or(Stream.of(OTHER_FILE_SUFFIXES).map(predicates::hasExtension).toArray(FilePredicate[]::new)))));
    for (InputFile inputFile : inputFiles) {
        if (sensorContext.isCancelled()) {
            return;
        }
        HtmlSourceCode sourceCode = new HtmlSourceCode(inputFile);
        try (Reader reader = new InputStreamReader(inputFile.inputStream(), inputFile.charset())) {
            PageLexer lexer = inputFile.filename().endsWith(".vue") ? new VueLexer() : new PageLexer();
            scanner.scan(lexer.parse(reader), sourceCode);
            saveMetrics(sensorContext, sourceCode);
            saveLineLevelMeasures(inputFile, sourceCode);
        } catch (Exception e) {
            LOG.error("Cannot analyze file " + inputFile, e);
            sensorContext.newAnalysisError().onFile(inputFile).message(e.getMessage()).save();
        }
    }
}
Also used : VueLexer(org.sonar.plugins.html.lex.VueLexer) PageLexer(org.sonar.plugins.html.lex.PageLexer) InputStreamReader(java.io.InputStreamReader) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) HtmlAstScanner(org.sonar.plugins.html.visitor.HtmlAstScanner) InputFile(org.sonar.api.batch.fs.InputFile)

Example 5 with PageLexer

use of org.sonar.plugins.html.lex.PageLexer in project sonar-web by SonarSource.

the class NoSonarScannerTest method scanNoSonar.

@Test
public void scanNoSonar() throws Exception {
    List<Node> nodeList;
    try (Reader reader = new StringReader(CONTENT)) {
        nodeList = new PageLexer().parse(reader);
    }
    InputFile inputFile = new TestInputFileBuilder("key", "dummy.jsp").setContents(CONTENT).setCharset(StandardCharsets.UTF_8).build();
    NoSonarFilter noSonarFilter = spy(new NoSonarFilter());
    HtmlAstScanner pageScanner = new HtmlAstScanner(Collections.emptyList());
    pageScanner.addVisitor(new NoSonarScanner(noSonarFilter));
    pageScanner.scan(nodeList, new HtmlSourceCode(inputFile));
    verify(noSonarFilter, times(1)).noSonarInFile(eq(inputFile), argThat(new IsOnlyIgnoringLine2()));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) PageLexer(org.sonar.plugins.html.lex.PageLexer) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) Node(org.sonar.plugins.html.node.Node) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Aggregations

PageLexer (org.sonar.plugins.html.lex.PageLexer)5 InputFile (org.sonar.api.batch.fs.InputFile)4 HtmlAstScanner (org.sonar.plugins.html.visitor.HtmlAstScanner)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 HtmlSourceCode (org.sonar.plugins.html.visitor.HtmlSourceCode)3 File (java.io.File)2 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1 FilePredicates (org.sonar.api.batch.fs.FilePredicates)1 FileSystem (org.sonar.api.batch.fs.FileSystem)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1 ComplexityVisitor (org.sonar.plugins.html.analyzers.ComplexityVisitor)1 PageCountLines (org.sonar.plugins.html.analyzers.PageCountLines)1 VueLexer (org.sonar.plugins.html.lex.VueLexer)1