summaryrefslogtreecommitdiff
path: root/linter
diff options
context:
space:
mode:
authorpants <niklasjo2004@gmail.com>2026-09-21 14:16:22 -0700
committerpants <niklasjo2004@gmail.com>2026-09-21 14:16:22 -0700
commit4a0d047e83141d3ebe48a2bf4ea58e1192a01333 (patch)
tree7125d1be1df434ca98a1d08fb4a30ebd28730d19 /linter
Diffstat (limited to 'linter')
-rw-r--r--linter/BlankPrintlnCheck.java19
-rw-r--r--linter/BreakCheck.java23
-rw-r--r--linter/Check.java11
-rw-r--r--linter/Error.java43
-rw-r--r--linter/Linter.java39
-rw-r--r--linter/LinterMain.java22
-rw-r--r--linter/LongLineCheck.java21
-rw-r--r--linter/TestFile.java10
-rw-r--r--linter/TestFile.txt89
9 files changed, 277 insertions, 0 deletions
diff --git a/linter/BlankPrintlnCheck.java b/linter/BlankPrintlnCheck.java
new file mode 100644
index 0000000..dfb4f22
--- /dev/null
+++ b/linter/BlankPrintlnCheck.java
@@ -0,0 +1,19 @@
+// Nik Johnson
+// 2-24-2024
+// TA: Andy Ruan
+
+import java.util.*;
+
+// checks lines for the characters "System.out.println("")"
+public class BlankPrintlnCheck implements Check {
+
+ // if the line being checked has a blank println statement, return a new error (code 3)
+ // takes line and line number
+ // returns error inside optional
+ public Optional<Error> lint(String line, int lineNumber) {
+ if (line.contains("System.out.println(\"\")")) {
+ return Optional.of(new Error(3, lineNumber, "Line contains blank print statement"));
+ }
+ return Optional.empty();
+ }
+}
diff --git a/linter/BreakCheck.java b/linter/BreakCheck.java
new file mode 100644
index 0000000..0007fe3
--- /dev/null
+++ b/linter/BreakCheck.java
@@ -0,0 +1,23 @@
+// Nik Johnson
+// 2-24-2024
+// TA: Andy Ruan
+
+import java.util.*;
+
+// checks Strings for the characters "break" that come BEFORE the characters "//"
+public class BreakCheck implements Check {
+
+ // if the line being checked has "break" in it
+ // after removing all single line comments, return a new error (code 2)
+ // takes line and line number
+ // returns error inside optional
+ public Optional<Error> lint(String line, int lineNumber) {
+ String input = line;
+ String output = input.replaceAll("//.*", "");
+
+ if (output.contains("break")) {
+ return Optional.of(new Error(2, lineNumber, "we cant use break in this class bro"));
+ }
+ return Optional.empty();
+ }
+}
diff --git a/linter/Check.java b/linter/Check.java
new file mode 100644
index 0000000..2b3bca5
--- /dev/null
+++ b/linter/Check.java
@@ -0,0 +1,11 @@
+import java.util.*;
+
+// A common interface for linters that can check a single line of code
+// Every Check will check for its own type of error on a single line of code.
+public interface Check {
+ // Checks for this Check's error condition on this line with this line number.
+ // If an error exists on this line, returns an Optional with an Error present
+ // indicating an error occurred. If no errors are present, returns an empty Optional.
+ public Optional<Error> lint(String line, int lineNumber);
+}
+
diff --git a/linter/Error.java b/linter/Error.java
new file mode 100644
index 0000000..da4a051
--- /dev/null
+++ b/linter/Error.java
@@ -0,0 +1,43 @@
+// Nik Johnson
+// 2-24-2024
+// TA: Andy Ruan
+
+/*
+Defining an object called Error, which has the fields:
+code: the errors code, represented by an int
+lineNumber: the line where the error occurred
+message: a description of the error
+*/
+public class Error {
+
+ private int code;
+ private int lineNumber;
+ private String message;
+
+ // construct an error with its fields
+ public Error(int code, int lineNumber, String message) {
+ this.code = code;
+ this.lineNumber = lineNumber;
+ this.message = message;
+ }
+
+ // format an error and its information nicely
+ public String toString() {
+ return ("(Line: " + lineNumber + ") " + "has error code " + code + "\n" + message);
+ }
+
+ // return line where the error occurs
+ public int getLineNumber() {
+ return this.lineNumber;
+ }
+
+ // return the error code (a number)
+ public int getCode() {
+ return this.code;
+ }
+
+ // return the errors description
+ public String getMessage() {
+ return this.message;
+ }
+}
diff --git a/linter/Linter.java b/linter/Linter.java
new file mode 100644
index 0000000..e30a8c3
--- /dev/null
+++ b/linter/Linter.java
@@ -0,0 +1,39 @@
+// 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;
+ }
+}
diff --git a/linter/LinterMain.java b/linter/LinterMain.java
new file mode 100644
index 0000000..97fb2b8
--- /dev/null
+++ b/linter/LinterMain.java
@@ -0,0 +1,22 @@
+// Nik Johnson
+// 2-24-2024
+// TA: Andy Ruan
+
+import java.util.*;
+import java.io.*;
+
+public class LinterMain {
+ public static final String FILE_NAME = "TestFile.txt";
+
+ public static void main(String[] args) throws FileNotFoundException {
+ List<Check> checks = new ArrayList<>();
+ checks.add(new LongLineCheck());
+ checks.add(new BreakCheck());
+ checks.add(new BlankPrintlnCheck());
+ Linter linter = new Linter(checks);
+ List<Error> errors = linter.lint(FILE_NAME);
+ for (Error e : errors) {
+ System.out.println(e);
+ }
+ }
+}
diff --git a/linter/LongLineCheck.java b/linter/LongLineCheck.java
new file mode 100644
index 0000000..d5d4368
--- /dev/null
+++ b/linter/LongLineCheck.java
@@ -0,0 +1,21 @@
+// Nik Johnson
+// 2-24-2024
+// TA: Andy Ruan
+
+import java.util.*;
+
+// long line checker
+public class LongLineCheck implements Check {
+
+ // if the line being checked is 100 characters or longer, return a new error (code 1)
+ // takes line and line number
+ // returns error inside optional
+ public Optional<Error> lint(String line, int lineNumber) {
+ if (line.length() >= 100) {
+ return Optional.of(new Error(1, lineNumber,
+ "Line length exceeded maximum length of 100 characters"));
+ }
+
+ return Optional.empty();
+ }
+}
diff --git a/linter/TestFile.java b/linter/TestFile.java
new file mode 100644
index 0000000..ebdff55
--- /dev/null
+++ b/linter/TestFile.java
@@ -0,0 +1,10 @@
+public class TestFile {
+ public static void main(String[] args) {
+ System.out.println("This is a really really really long line that should fail the long line check");
+ while (true) {
+ System.out.println("");
+ break;
+ }
+ // break
+ }
+}
diff --git a/linter/TestFile.txt b/linter/TestFile.txt
new file mode 100644
index 0000000..76a8979
--- /dev/null
+++ b/linter/TestFile.txt
@@ -0,0 +1,89 @@
+package com.example;
+
+public class Example {
+ public static void main(String[] args) {
+ int x = 5;
+ while (x < 10) {
+ System.out.println("Hello, world!");
+ break;
+ }
+ for (int i = 0; i < 10; i++) {
+ if (i % 2 == 0) {
+ continue;
+ } else {
+ System.out.println(i);
+ }
+ }
+ try {
+ throw new Exception("Test");
+ } catch (Exception e) {
+ System.out.println("Caught an exception: " + e.getMessage());
+ } finally {
+ System.out.println("Finally block executed.");
+ }
+ for (int j = 0; j < 100; j++) {
+ if (j % 2 == 0) {
+ break;
+ } else {
+ System.out.println(j);
+ }
+ }
+ while (x > 5) {
+ System.out.println("Hello, world!");
+ break;
+ }
+ if (x % 2 == 0) {
+ continue;
+ } else {
+ System.out.println(x);
+ }
+ for (int k = 0; k < 1000; k++) {
+ if (k % 2 == 0) {
+ break;
+ } else {
+ System.out.println(k);
+ }
+ }
+ while (x > 5) {
+ System.out.println("Hello, world!");
+ break;
+ }
+ if (x % 2 == 0) {
+ continue;
+ } else {
+ System.out.println(x);
+ }
+ for (int l = 0; l < 10000; l++) {
+ if (l % 2 == 0) {
+ break;
+ } else {
+ System.out.println(l);
+ }
+ }
+ while (x > 5) {
+ System.out.println("Hello, world!");
+ break;
+ }
+ if (x % 2 == 0) {
+ continue;
+ } else {
+ System.out.println(x);
+ }
+ for (int m = 0; m < 100000; m++) {
+ if (m % 2 == 0) {
+ break;
+ } else {
+ System.out.println(m);
+ }
+ }
+ while (x > 5) {
+ System.out.println("Hello, world!");
+ break;
+ }
+ if (x % 2 == 0) {
+ continue;
+ } else {
+ System.out.println(x);
+ }
+ }
+}