This repository has been archived on 2026-03-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CSE-122/linter/Linter.java
2026-03-18 00:39:22 -07:00

40 lines
1.1 KiB
Java

// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
import java.io.*;
// file-based linting object, linting based on checks defined in a list
public class Linter {
private List<Check> checks;
// constructor, takes list of checks to perform
public Linter(List<Check> checks) {
this.checks = checks;
}
// compile list of errors based on output of check classes
// takes filename to perform checks on
// return list of errors present in that file
public List<Error> lint(String fileName) throws FileNotFoundException {
File toBeChecked = new File(fileName);
Scanner fileScanner = new Scanner(toBeChecked);
List<Error> errors = new ArrayList<>();
int lineNumber = 1;
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
for (Check check : checks) {
Optional<Error> error = check.lint(line, lineNumber);
if (error.isPresent()) {
errors.add(error.get());
}
}
lineNumber++;
}
fileScanner.close();
return errors;
}
}