use of org.sonar.plugins.html.visitor.HtmlAstScanner 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);
}
use of org.sonar.plugins.html.visitor.HtmlAstScanner 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;
}
use of org.sonar.plugins.html.visitor.HtmlAstScanner 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());
}
use of org.sonar.plugins.html.visitor.HtmlAstScanner 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();
}
}
}
use of org.sonar.plugins.html.visitor.HtmlAstScanner in project sonar-web by SonarSource.
the class HtmlSensor method setupScanner.
/**
* Create PageScanner with Visitors.
*/
private HtmlAstScanner setupScanner(SensorContext context) {
List<DefaultNodeVisitor> visitors = new ArrayList<>();
if (context.runtime().getProduct() != SonarProduct.SONARLINT) {
visitors.add(new HtmlTokensVisitor(context));
}
visitors.add(new PageCountLines());
visitors.add(new ComplexityVisitor());
visitors.add(new NoSonarScanner(noSonarFilter));
HtmlAstScanner scanner = new HtmlAstScanner(visitors);
for (Object check : checks.all()) {
((AbstractPageCheck) check).setRuleKey(checks.ruleKey(check));
scanner.addVisitor((AbstractPageCheck) check);
}
return scanner;
}
Aggregations